/** * Creates a method. * * @param target * the class where the method is inserted * @param modifiers * the modifiers * @param returnType * the method's return type * @param name * the method's name * @param parameters * the parameters * @param thrownTypes * the thrown types * @param body * the method's body */ public <R, B extends R> CtMethod<R> create(CtClass<?> target, Set<ModifierKind> modifiers, CtTypeReference<R> returnType, String name, List<CtParameter<?>> parameters, Set<CtTypeReference<? extends Throwable>> thrownTypes, CtBlock<B> body) { CtMethod<R> method = create(target, modifiers, returnType, name, parameters, thrownTypes); method.setBody(body); return method; }
/** * Generates a method from a template method by substituting all the * template parameters by their values. * * @param targetType * the target type where to insert the generated method * @param template * the template instance that holds the source template method * and that defines the parameter values * @param sourceMethod * the source template method * @return the generated method */ public static <T> CtMethod<T> insertMethod(CtType<?> targetType, Template<?> template, CtMethod<T> sourceMethod) { CtMethod<T> newMethod = substitute(targetType, template, sourceMethod); if (targetType instanceof CtInterface) { newMethod.setBody(null); } targetType.addMethod(newMethod); return newMethod; }
@Override public <T> void visitCtMethod(CtMethod<T> e) { if (e.getBody() == null && child instanceof CtBlock) { e.setBody((CtBlock) child); return; } else if (child instanceof CtStatement) { visitCtBlock(e.getBody()); return; } else if (child instanceof CtTypeAccess && hasChildEqualsToType(e)) { e.setType(((CtTypeAccess) child).getAccessedType()); substituteAnnotation(e); return; } super.visitCtMethod(e); }
@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); }
public <T> void visitCtMethod(final spoon.reflect.declaration.CtMethod<T> m) { spoon.reflect.declaration.CtMethod<T> aCtMethod = m.getFactory().Core().createMethod(); this.builder.copy(m, aCtMethod); aCtMethod.setAnnotations(this.cloneHelper.clone(m.getAnnotations())); aCtMethod.setFormalCtTypeParameters(this.cloneHelper.clone(m.getFormalCtTypeParameters())); aCtMethod.setType(this.cloneHelper.clone(m.getType())); aCtMethod.setParameters(this.cloneHelper.clone(m.getParameters())); aCtMethod.setThrownTypes(this.cloneHelper.clone(m.getThrownTypes())); aCtMethod.setBody(this.cloneHelper.clone(m.getBody())); aCtMethod.setComments(this.cloneHelper.clone(m.getComments())); this.cloneHelper.tailor(m, aCtMethod); this.other = aCtMethod; }
/** * Creates a method. * * @param target * the class where the method is inserted * @param modifiers * the modifiers * @param returnType * the method's return type * @param name * the method's name * @param parameters * the parameters * @param thrownTypes * the thrown types * @param body * the method's body */ public <R, B extends R> CtMethod<R> create(CtClass<?> target, Set<ModifierKind> modifiers, CtTypeReference<R> returnType, String name, List<CtParameter<?>> parameters, Set<CtTypeReference<? extends Throwable>> thrownTypes, CtBlock<B> body) { CtMethod<R> method = create(target, modifiers, returnType, name, parameters, thrownTypes); method.setBody(body); return method; }
/** * Generates a method from a template method by substituting all the * template parameters by their values. * * @param targetType * the target type where to insert the generated method * @param template * the template instance that holds the source template method * and that defines the parameter values * @param sourceMethod * the source template method * @return the generated method */ public static <T> CtMethod<T> insertMethod(CtType<?> targetType, Template<?> template, CtMethod<T> sourceMethod) { CtMethod<T> newMethod = substitute(targetType, template, sourceMethod); if (targetType instanceof CtInterface) { newMethod.setBody(null); } targetType.addMethod(newMethod); // newMethod.setParent(targetType); return newMethod; }
protected <T> CtMethod<T> createSimpleMethod(CtTypeReference<T> returnType, String name) { CtMethod<T> method = f().Method().create( declaringType, EnumSet.of(PUBLIC), returnType, name, emptyList(), emptySet()); method.setParent(declaringType); CtBlock<T> body = f().Core().createBlock(); method.setBody(body); body.setParent(method); return method; }
@Override public Optional<CtMethod<Void>> getCloseMethod() { if (closeMethod != null && !closeMethod.hasModifier(ABSTRACT)) { initCloseMethodStatements(); return of(closeMethod); } assert initField != null; closeMethodGenerated = true; if (closeMethod == null) { closeMethod = createSimpleMethod(f().Type().VOID_PRIMITIVE, closeMethodName()); cxt.bindClose(closeMethod, this); } else { closeMethod.setBody(f().Core().createBlock()); closeMethod.removeModifier(ABSTRACT); } closeMethod.getBody().addStatement(net.openhft.sg.SpoonUtils.reassignDefault(initField)); initCloseMethodStatements(); return of(closeMethod); }
private void generateFinalAccessors() { List<CtField<?>> finalFields = root.getMergedClass() .getElements((CtField<?> field) -> field.hasModifier(FINAL)); finalFields.forEach(f -> { CtType<?> declaringType = f.getDeclaringType(); if (declaringType.getMethodsByName(f.getSimpleName()).isEmpty()) { Factory factory = f.getFactory(); CtMethod access = factory.Method().create( declaringType, EnumSet.of(PUBLIC), f.getType(), f.getSimpleName(), emptyList(), emptySet()); access.setParent(declaringType); access.setBody((CtBlock) factory.Core().createBlock()); CtReturn fReturn = factory.Core().createReturn(); fReturn.setReturnedExpression( factory.Code().createVariableRead(f.getReference(), false)); access.getBody().addStatement(fReturn); access.setPosition(new LinedSourcePosition( ((LinedSourcePosition) f.getPosition()).delegate, ((LinedSourcePosition) f.getPosition()).line)); } }); }
megaMethod.setBody(methodBodyBlock); methodBodyBlock.addStatement(tryMethodMain);
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; } }
megaMethod.setBody(methodBodyBlock); methodBodyBlock.addStatement(tryMethodMain);
private <T> CtMethod<T> fieldAccess(CtField<T> field) { @SuppressWarnings("unchecked") Map<CtField<T>, CtMethod<T>> fields = (Map<CtField<T>, CtMethod<T>>) (Map) this.fields; return fields.compute(field, (f, proxy) -> { if (proxy == null) { proxy = createSimpleMethod(f.getType(), f.getSimpleName()); } else if (proxy.hasModifier(ABSTRACT)) { proxy.setBody(f().Core().createBlock()); proxy.removeModifier(ABSTRACT); } else { return proxy; } addGuardingPrologue(proxy); CtReturn<T> ret = f().Core().createReturn(); ret.setReturnedExpression(f().Code().createVariableRead(f.getReference(), false)); proxy.getBody().addStatement(ret); cxt.bindAccessMethod(proxy, StageModel.this); return proxy; }); }
static void addTryCatchFailAssertion(Launcher spoon, CtMethod<?> testCaseToBeFix, Failure failure) { final Factory factory = spoon.getFactory(); final CtTry aTry = factory.createTry(); final CtCatch aCatch = factory.createCatch(); aTry.addCatcher(aCatch); String fullQualifiedNameException = failure.fullQualifiedNameOfException; CtTypeReference reference = testCaseToBeFix.getFactory().Type().createReference(fullQualifiedNameException); final String[] splittedNameException = fullQualifiedNameException.split("\\."); String exceptionName = splittedNameException[splittedNameException.length - 1]; if (isAnonymous.test(failure.fullQualifiedNameOfException)) { reference = testCaseToBeFix.getFactory().Type().createReference(fullQualifiedNameException).getSuperclass(); } final CtCatchVariable<? extends Throwable> catchVariable = testCaseToBeFix.getFactory().createCatchVariable( reference, PREFIX_NAME_EXPECTED_EXCEPTION + exceptionName ); aCatch.setParameter(catchVariable); aCatch.setBody(factory.createCodeSnippetStatement( "org.junit.Assert.assertTrue(true)" )); aTry.setBody(testCaseToBeFix.getBody().getStatement(0)); for (int i = 1; i < testCaseToBeFix.getBody().getStatements().size(); i++) { aTry.getBody().addStatement(testCaseToBeFix.getBody().getStatement(i)); } aTry.getBody().addStatement(factory .createCodeSnippetStatement("org.junit.Assert." + NAME_FAIL_METHOD + "(\"" + PREFIX_MESSAGE_EXPECTED_EXCEPTION + exceptionName + "\")") ); testCaseToBeFix.setBody(aTry); }
megaMethod.setBody(methodBodyBlock); methodBodyBlock.addStatement(tryMethodMain);
megaMethod.setBody(methodBodyBlock); methodBodyBlock.addStatement(tryMethodMain);
@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; }
cxt.bindStageInit(stageInitMethod, this); } else { stageInitMethod.setBody(f().Core().createBlock()); stageInitMethod.removeModifier(ABSTRACT);
body.addStatement(invocation); test.setBody(body); test.setSimpleName(test.getSimpleName() + "_failAssert" + (numberOfFail));