/** * Changes name of a method, propagates the change in the executable references of the model. */ public static void changeMethodName(final CtMethod<?> method, String newName) { final List<CtExecutableReference<?>> references = Query.getElements(method.getFactory(), new TypeFilter<CtExecutableReference<?>>(CtExecutableReference.class) { @Override public boolean matches(CtExecutableReference<?> reference) { return reference.getDeclaration() == method; } }); method.setSimpleName(newName); for (CtExecutableReference<?> reference : references) { reference.setSimpleName(newName); } }
/** See doc in {@link CtMethod#copyMethod()} */ public static CtMethod<?> copyMethod(final CtMethod<?> method) { CtMethod<?> clone = method.clone(); StringBuilder tentativeTypeName = new StringBuilder(method.getSimpleName() + "Copy"); CtType parent = method.getParent(CtType.class); while (!parent.getMethodsByName(tentativeTypeName.toString()).isEmpty()) { tentativeTypeName.append("X"); } final String cloneMethodName = tentativeTypeName.toString(); clone.setSimpleName(cloneMethodName); parent.addMethod(clone); new CtScanner() { @Override public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) { CtExecutable<T> declaration = reference.getDeclaration(); if (declaration == null) { return; } if (declaration == method) { reference.setSimpleName(cloneMethodName); } if (reference.getDeclaration() != clone) { throw new SpoonException("post condition broken " + reference); } super.visitCtExecutableReference(reference); } }.scan(clone); return clone; }
private CtMethod valuesMethod() { if (valuesMethod == null) { valuesMethod = getFactory().Core().createMethod(); valuesMethod.setParent(this); valuesMethod.addModifier(ModifierKind.PUBLIC); valuesMethod.addModifier(ModifierKind.STATIC); valuesMethod.setSimpleName("values"); valuesMethod.setImplicit(true); valuesMethod.setType(factory.Type().createArrayReference(getReference())); } return valuesMethod; }
method.setSimpleName(name); if (parameters != null) { method.setParameters(parameters);
@Override public void visitMethod(RtMethod method, Annotation parent) { final CtMethod<Object> ctMethod = factory.Core().createMethod(); ctMethod.setSimpleName(method.getName()); /** * java 8 static interface methods are marked as abstract but has body */ if (Modifier.isAbstract(method.getModifiers()) == false) { ctMethod.setBody(factory.Core().createBlock()); } setModifier(ctMethod, method.getModifiers(), method.getDeclaringClass()); ctMethod.setDefaultMethod(method.isDefault()); enter(new ExecutableRuntimeBuilderContext(method.getMethod(), ctMethod)); super.visitMethod(method, parent); exit(); contexts.peek().addMethod(ctMethod); }
private CtMethod valueOfMethod() { if (valueOfMethod == null) { valueOfMethod = getFactory().Core().createMethod(); valueOfMethod.setParent(this); valueOfMethod.addModifier(ModifierKind.PUBLIC); valueOfMethod.addModifier(ModifierKind.STATIC); valueOfMethod.setSimpleName("valueOf"); valueOfMethod.setImplicit(true); valueOfMethod.addThrownType( getFactory().Type().createReference(IllegalArgumentException.class)); valueOfMethod.setType(getReference()); factory.Method().createParameter(valueOfMethod, factory.Type().STRING, "name"); } return valueOfMethod; }
adaptedMethod.setParent(classTypingContext.getAdaptationScope()); adaptedMethod.setModifiers(method.getModifiers()); adaptedMethod.setSimpleName(method.getSimpleName());
@Override public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) { CtMethod<Object> m = factory.Core().createMethod(); m.setSimpleName(CharOperation.charToString(methodDeclaration.selector)); if (methodDeclaration.binding != null) { m.setExtendedModifiers(getModifiers(methodDeclaration.binding.modifiers, true, true)); } for (CtExtendedModifier extendedModifier : getModifiers(methodDeclaration.modifiers, false, true)) { m.addModifier(extendedModifier.getKind()); // avoid to keep implicit AND explicit modifier of the same kind. } m.setDefaultMethod(methodDeclaration.isDefaultMethod()); context.enter(m, methodDeclaration); // Create block if (!methodDeclaration.isAbstract() && (methodDeclaration.modifiers & ClassFileConstants.AccNative) == 0) { context.enter(getFactory().Core().createBlock(), methodDeclaration); context.exit(methodDeclaration); } // We consider the receiver as a standard argument (i.e. as a parameter) Receiver receiver = methodDeclaration.receiver; if (receiver != null) { receiver.traverse(this, methodDeclaration.scope); } return true; }
/** * Clones a method. * * @param method Method to be cloned * @param suffix Suffix for the cloned method's name * @return The cloned method */ public static CtMethod cloneMethod(CtMethod<?> method, String suffix) { CtMethod cloned_method = method.clone(); //rename the clone cloned_method.setSimpleName(method.getSimpleName() + (suffix.isEmpty() ? "" : suffix + cloneNumber)); cloneNumber++; CtAnnotation toRemove = cloned_method.getAnnotations().stream() .filter(annotation -> annotation.toString().contains("Override")) .findFirst().orElse(null); if (toRemove != null) { cloned_method.removeAnnotation(toRemove); } return cloned_method; }
static CtMethod<?> createTestWithLog(CtMethod test, final String filter, List<CtLocalVariable<?>> ctVariableReads) { CtMethod clone = CloneHelper.cloneTestMethodNoAmp(test); clone.setSimpleName(test.getSimpleName() + "_withlog"); final List<CtStatement> allStatement = clone.getElements(new TypeFilter<>(CtStatement.class)); allStatement.stream() .filter(statement -> isStmtToLog(filter, statement) || ctVariableReads.contains(statement)) .forEach(statement -> addLogStmt(statement, test.getSimpleName() + "__" + indexOfByRef(allStatement, statement)) ); return clone; }
.map(CtMethod::clone) .map(ctMethod -> { ctMethod.setSimpleName(ctMethod.getSimpleName() + i); return ctMethod; })
/** * Adds surrounding try/catch/fail in a failing test. * * @param test Failing test method to amplify * @param failure Test's failure description * @return New amplified test */ @SuppressWarnings("unchecked") public CtMethod<?> surroundWithTryCatchFail(CtMethod<?> test, Failure failure) { CtMethod cloneMethodTest = CloneHelper.cloneTestMethodForAmp(test, ""); cloneMethodTest.setSimpleName(test.getSimpleName()); // TestTimedOutException means infinite loop // AssertionError means that some assertion remained in the test: TODO if (UNSUPPORTED_EXECEPTION.contains(failure.fullQualifiedNameOfException)) { return null; } cloneMethodTest = TestFramework.get().generateExpectedExceptionsBlock(cloneMethodTest, failure, this.numberOfFail); Counter.updateAssertionOf(cloneMethodTest, 1); return cloneMethodTest; }
.forEach(index -> { final CtMethod<?> clone = ctMethod.clone(); clone.setSimpleName(clone.getSimpleName() + "_" + index); testClass.addMethod(clone); addedMethod.add(clone);
private static <T> void processOverridden(CtClass<?> mergeInto, CtClass<?> toMerge, final CtMethod<T> methodToMerge) { List<CtInvocation<T>> superInvocations = mergeInto.getElements( new Filter<CtInvocation<T>>() { @Override public boolean matches(CtInvocation<T> invocation) { if (!(invocation.getTarget() instanceof CtSuperAccess)) return false; CtExecutable<?> m = invocation.getExecutable().getDeclaration(); return m != null && MethodNode.overrides((CtMethod<?>) m, methodToMerge); } }); methodToMerge.setSimpleName(classPrefixedName(toMerge, methodToMerge)); methodToMerge.setVisibility(PRIVATE); removeAnnotation(methodToMerge, Override.class); for (CtInvocation<T> superInvocation : superInvocations) { superInvocation.setTarget(null); superInvocation.setExecutable(methodToMerge.getReference()); } add(mergeInto, methodToMerge, mergeInto::addMethod); }
method.setSimpleName(name); if (parameters != null) { method.setParameters(parameters);
private CtMethod<Void> initAfterClassMethod(Factory factory) { final CtMethod<Void> afterClassMethod = factory.createMethod(); afterClassMethod.setType(factory.Type().VOID_PRIMITIVE); afterClassMethod.addModifier(ModifierKind.PUBLIC); afterClassMethod.addModifier(ModifierKind.STATIC); afterClassMethod.setSimpleName("afterClass"); final CtAnnotation annotation = factory.createAnnotation(); annotation.setAnnotationType(factory.Annotation().create(this.getFullQualifiedNameOfAnnotationAfterClass()).getReference()); afterClassMethod.addAnnotation(annotation); afterClassMethod.setBody(factory.createBlock()); return afterClassMethod; } }
@Override public CtMethod<?> generateExpectedExceptionsBlock(CtMethod<?> test, Failure failure, int numberOfFail) { final Factory factory = InputConfiguration.get().getFactory(); final String[] split = failure.fullQualifiedNameOfException.split("\\."); final String simpleNameOfException = split[split.length - 1]; CtTry tryBlock = factory.Core().createTry(); tryBlock.setBody(test.getBody()); String snippet = this.qualifiedNameOfAssertClass + ".fail(\"" + test.getSimpleName() + " should have thrown " + simpleNameOfException + "\")"; tryBlock.getBody().addStatement(factory.Code().createCodeSnippetStatement(snippet)); DSpotUtils.addComment(tryBlock, "AssertGenerator generate try/catch block with fail statement", CtComment.CommentType.INLINE); CtCatch ctCatch = factory.Core().createCatch(); CtTypeReference exceptionType = factory.Type().createReference(failure.fullQualifiedNameOfException); ctCatch.setParameter(factory.Code().createCatchVariable(exceptionType, this.getCorrectExpectedNameOfException(test))); ctCatch.setBody(factory.Core().createBlock()); List<CtCatch> catchers = new ArrayList<>(1); catchers.add(ctCatch); addAssertionOnException(test, ctCatch, failure); tryBlock.setCatchers(catchers); CtBlock body = factory.Core().createBlock(); body.addStatement(tryBlock); test.setBody(body); test.setSimpleName(test.getSimpleName() + "_failAssert" + (numberOfFail)); return test; }
@Override public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) { CtMethod<Object> m = factory.Core().createMethod(); m.setSimpleName(new String(methodDeclaration.selector)); m.setType(references.getTypeReference(methodDeclaration.returnType.resolvedType)); m.setModifiers(getModifiers(methodDeclaration.modifiers));
test.setSimpleName(test.getSimpleName() + "_failAssert" + (numberOfFail));