Search in sources :

Example 6 with CtReturn

use of spoon.reflect.code.CtReturn in project spoon by INRIA.

the class TypeReferenceTest method testIgnoreEnclosingClassInActualTypes.

@Test
public void testIgnoreEnclosingClassInActualTypes() throws Exception {
    final CtType<Panini> aPanini = buildClass(Panini.class);
    final CtStatement ctReturn = aPanini.getMethod("entryIterator").getBody().getStatement(0);
    assertTrue(ctReturn instanceof CtReturn);
    final CtExpression ctConstructorCall = ((CtReturn) ctReturn).getReturnedExpression();
    assertTrue(ctConstructorCall instanceof CtConstructorCall);
    assertEquals("spoon.test.reference.testclasses.Panini<K, V>.Itr<java.util.Map.Entry<K, V>>", ctConstructorCall.getType().toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtReturn(spoon.reflect.code.CtReturn) CtExpression(spoon.reflect.code.CtExpression) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Panini(spoon.test.reference.testclasses.Panini) Test(org.junit.Test)

Example 7 with CtReturn

use of spoon.reflect.code.CtReturn in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method visitCtMethod.

@Override
public <T> void visitCtMethod(CtMethod<T> m) {
    elementPrinterHelper.writeComment(m);
    elementPrinterHelper.visitCtNamedElement(m, sourceCompilationUnit);
    elementPrinterHelper.writeModifiers(m);
    if (m.isDefaultMethod()) {
        printer.writeKeyword("default").writeSpace();
    }
    elementPrinterHelper.writeFormalTypeParameters(m);
    if (m.getFormalCtTypeParameters().size() > 0) {
        printer.writeSpace();
    }
    try (Writable _context = context.modify().ignoreGenerics(false)) {
        scan(m.getType());
    }
    printer.writeSpace();
    printer.writeIdentifier(m.getSimpleName());
    elementPrinterHelper.writeExecutableParameters(m);
    elementPrinterHelper.writeThrowsClause(m);
    if (m.getBody() != null) {
        printer.writeSpace();
        scan(m.getBody());
        if (m.getBody().getPosition() != null) {
            if (m.getBody().getPosition().getCompilationUnit() == sourceCompilationUnit) {
                if (m.getBody().getStatements().isEmpty() || !(m.getBody().getStatements().get(m.getBody().getStatements().size() - 1) instanceof CtReturn)) {
                    getPrinterHelper().putLineNumberMapping(m.getBody().getPosition().getEndLine());
                }
            } else {
                getPrinterHelper().undefineLine();
            }
        } else {
            getPrinterHelper().undefineLine();
        }
    } else {
        printer.writeSeparator(";");
    }
}
Also used : CtReturn(spoon.reflect.code.CtReturn) Writable(spoon.reflect.visitor.PrintingContext.Writable)

Example 8 with CtReturn

use of spoon.reflect.code.CtReturn in project spoon by INRIA.

the class CUFilterTest method testSingleExcludeWithoutFilter.

@Test
public void testSingleExcludeWithoutFilter() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/same-package/A.java");
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    assertEquals(1, model.getAllTypes().size());
    // make sure `B` is not available in `model.getAllTypes`
    assertEquals("A", model.getAllTypes().iterator().next().getSimpleName());
    // make sure declaration of `B` is unknown in `model`
    final CtReturn ctReturn = model.getAllTypes().iterator().next().getMethod("createB").getBody().getStatement(0);
    final CtConstructorCall ctConstructorCall = (CtConstructorCall) ctReturn.getReturnedExpression();
    assertEquals("spoon.test.same.B", ctConstructorCall.getType().getQualifiedName());
}
Also used : CtReturn(spoon.reflect.code.CtReturn) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 9 with CtReturn

use of spoon.reflect.code.CtReturn in project spoon by INRIA.

the class AnnotationTest method testUsageOfTypeAnnotationInCast.

@Test
public void testUsageOfTypeAnnotationInCast() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
    final CtReturn<?> returns = ctClass.getElements(new AbstractFilter<CtReturn<?>>(CtReturn.class) {

        @Override
        public boolean matches(CtReturn<?> element) {
            return !element.getReturnedExpression().getTypeCasts().isEmpty();
        }
    }).get(0);
    final CtExpression<?> returnedExpression = returns.getReturnedExpression();
    final List<CtAnnotation<? extends Annotation>> typeAnnotations = returnedExpression.getTypeCasts().get(0).getAnnotations();
    assertEquals("Cast with a type annotation must have it in its model", 1, typeAnnotations.size());
    assertEquals("Type annotation in the cast must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Cast with an type annotation must be well printed", "((java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String) (s))", returnedExpression.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtReturn(spoon.reflect.code.CtReturn) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Test(org.junit.Test)

Example 10 with CtReturn

use of spoon.reflect.code.CtReturn in project spoon by INRIA.

the class CommentTest method testInLineComment.

@Test
public void testInLineComment() {
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(InlineComment.class);
    String strType = type.toString();
    List<CtComment> comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
    // verify that the number of comment present in the AST is correct
    assertEquals(64, comments.size());
    // verify that all comments present in the AST is printed
    for (CtComment comment : comments) {
        if (comment.getCommentType() == CtComment.CommentType.FILE) {
            // the header of the file is not printed with the toString
            continue;
        }
        assertNotNull(comment.getParent());
        assertTrue(comment.toString() + ":" + comment.getParent() + " is not printed", strType.contains(comment.toString()));
    }
    assertEquals(3, type.getComments().size());
    assertEquals(CtComment.CommentType.FILE, type.getComments().get(0).getCommentType());
    assertEquals(createFakeComment(f, "comment class"), type.getComments().get(1));
    CtField<?> field = type.getField("field");
    assertEquals(3, field.getComments().size());
    assertEquals(createFakeComment(f, "Comment Field"), field.getComments().get(0));
    assertEquals("// Comment Field" + newLine + "// comment field 2" + newLine + "// comment in field" + newLine + "private int field = 10;", field.toString());
    CtAnonymousExecutable ctAnonymousExecutable = type.getAnonymousExecutables().get(0);
    assertEquals(1, ctAnonymousExecutable.getComments().size());
    assertEquals(createFakeComment(f, "comment static block"), ctAnonymousExecutable.getComments().get(0));
    assertEquals(createFakeComment(f, "comment inside static"), ctAnonymousExecutable.getBody().getStatement(0));
    assertEquals("// comment static block" + newLine + "static {" + newLine + "    // comment inside static" + newLine + "}", ctAnonymousExecutable.toString());
    CtConstructor constructor = type.getConstructor();
    assertEquals(1, constructor.getComments().size());
    assertEquals(createFakeComment(f, "comment constructor"), constructor.getComments().get(0));
    // index 0 is the implicit super call
    assertEquals(createFakeComment(f, "Comment in constructor"), constructor.getBody().getStatement(1));
    assertEquals("// comment constructor" + newLine + "public InlineComment() {" + newLine + "    // Comment in constructor" + newLine + "}", constructor.toString());
    CtMethod<Object> m = type.getMethod("m");
    assertEquals(1, m.getComments().size());
    assertEquals(createFakeComment(f, "comment method"), m.getComments().get(0));
    assertEquals(createFakeComment(f, "comment empty method block"), m.getBody().getStatement(0));
    assertEquals("// comment method" + newLine + "public void m() {" + newLine + "    // comment empty method block" + newLine + "}", m.toString());
    CtMethod<Object> m1 = type.getMethod("m1");
    CtSwitch ctSwitch = m1.getBody().getStatement(0);
    assertEquals(createFakeComment(f, "comment switch"), ctSwitch.getComments().get(0));
    assertEquals("// comment switch" + newLine + "switch (1) {" + newLine + "    // before first case" + newLine + "    case 0 :" + newLine + "        // comment case 0: empty case" + newLine + "    case 1 :" + newLine + "        // comment case 1" + newLine + "        int i = 0;" + newLine + "    default :" + newLine + "        // comment default" + newLine + "}", ctSwitch.toString());
    CtFor ctFor = m1.getBody().getStatement(1);
    assertEquals(createFakeComment(f, "comment for"), ctFor.getComments().get(0));
    assertEquals("// comment for" + newLine + "for (int i = 0; i < 10; i++) {" + newLine + "    // comment for block" + newLine + "}", ctFor.toString());
    CtIf ctIf = m1.getBody().getStatement(2);
    assertEquals(createFakeComment(f, "comment if"), ctIf.getComments().get(0));
    assertEquals("// comment if" + newLine + "if ((1 % 2) == 0) {" + newLine + "    // comment unary operator" + newLine + "    (field)++;" + newLine + "}", ctIf.toString());
    CtConstructorCall ctConstructorCall = m1.getBody().getStatement(3);
    assertEquals(createFakeComment(f, "comment constructor call"), ctConstructorCall.getComments().get(0));
    assertEquals("// comment constructor call" + newLine + "new spoon.test.comment.testclasses.InlineComment()", ctConstructorCall.toString());
    CtInvocation ctInvocation = m1.getBody().getStatement(4);
    assertEquals(createFakeComment(f, "comment invocation"), ctInvocation.getComments().get(0));
    assertEquals("// comment invocation" + newLine + "this.m()", ctInvocation.toString());
    CtLocalVariable ctLocalVariable = m1.getBody().getStatement(5);
    assertEquals(createFakeComment(f, "comment local variable"), ctLocalVariable.getComments().get(0));
    assertEquals("// comment local variable" + newLine + "int i = 0", ctLocalVariable.toString());
    CtLocalVariable ctLocalVariable2 = m1.getBody().getStatement(6);
    assertEquals(createFakeComment(f, "comment multi assignments"), ctLocalVariable2.getComments().get(0));
    assertEquals("// comment multi assignments" + newLine + "int j = 2", ctLocalVariable2.toString());
    CtDo ctDo = m1.getBody().getStatement(7);
    assertEquals(createFakeComment(f, "comment dowhile"), ctDo.getComments().get(0));
    assertEquals("// comment dowhile" + newLine + "do {" + newLine + "    // comment in do while" + newLine + "    i++;" + newLine + "    // comment end do while" + newLine + "} while (i < 10 )", ctDo.toString());
    CtTry ctTry = m1.getBody().getStatement(8);
    assertEquals(createFakeComment(f, "comment try"), ctTry.getComments().get(0));
    assertEquals("// comment try" + newLine + "try {" + newLine + "    // comment in try" + newLine + "    i++;" + newLine + "}// between" + newLine + "// try/catch" + newLine + " catch (java.lang.Exception e) {" + newLine + "    // comment in catch" + newLine + "}", ctTry.toString());
    CtSynchronized ctSynchronized = m1.getBody().getStatement(9);
    assertEquals(createFakeComment(f, "comment synchronized"), ctSynchronized.getComments().get(0));
    assertEquals("// comment synchronized" + newLine + "synchronized(this) {" + newLine + "    // comment in synchronized" + newLine + "}", ctSynchronized.toString());
    CtLocalVariable ctLocalVariable1 = m1.getBody().getStatement(10);
    CtConditional ctConditional = (CtConditional) ctLocalVariable1.getDefaultExpression();
    assertEquals(createFakeComment(f, "comment after condition CtConditional"), ctConditional.getCondition().getComments().get(0));
    assertEquals(createFakeComment(f, "comment before then CtConditional"), ctConditional.getThenExpression().getComments().get(0));
    assertEquals(createFakeComment(f, "comment after then CtConditional"), ctConditional.getThenExpression().getComments().get(1));
    assertEquals(createFakeComment(f, "comment before else CtConditional"), ctConditional.getElseExpression().getComments().get(0));
    assertEquals(createFakeComment(f, "comment after else CtConditional"), ctLocalVariable1.getComments().get(0));
    assertEquals("java.lang.Double dou = (i == 1// comment after condition CtConditional" + newLine + ") ? // comment before then CtConditional" + newLine + "null// comment after then CtConditional" + newLine + " : // comment before else CtConditional" + newLine + "new java.lang.Double((j / ((double) (i - 1))))", ctLocalVariable1.toString());
    CtNewArray ctNewArray = (CtNewArray) ((CtLocalVariable) m1.getBody().getStatement(11)).getDefaultExpression();
    assertEquals(createFakeComment(f, "last comment at the end of array"), ctNewArray.getComments().get(0));
    CtElement arrayValue = (CtElement) ctNewArray.getElements().get(0);
    assertEquals(createFakeComment(f, "comment before array value"), arrayValue.getComments().get(0));
    assertEquals(createFakeComment(f, "comment after array value"), arrayValue.getComments().get(1));
    CtLocalVariable ctLocalVariableString = m1.getBody().getStatement(12);
    assertEquals(createFakeComment(f, "comment multi line string"), ((CtBinaryOperator) ((CtBinaryOperator) ctLocalVariableString.getDefaultExpression()).getRightHandOperand()).getLeftHandOperand().getComments().get(0));
    assertEquals("\"\" + (\"\"// comment multi line string" + newLine + " + \"\")", ctLocalVariableString.getDefaultExpression().toString());
    ctLocalVariable1 = m1.getBody().getStatement(13);
    ctConditional = (CtConditional) ctLocalVariable1.getDefaultExpression();
    assertEquals("boolean c = (i == 1) ? // comment before then boolean CtConditional" + newLine + "i == 1// comment after then boolean CtConditional" + newLine + " : i == 2", ctLocalVariable1.toString());
    CtReturn ctReturn = m1.getBody().getStatement(14);
    assertEquals(createFakeComment(f, "comment return"), ctReturn.getComments().get(0));
    assertEquals("// comment return" + newLine + "return", ctReturn.toString());
    CtMethod m2 = type.getMethodsByName("m2").get(0);
    assertEquals(6, m2.getComments().size());
    CtParameter ctParameter = (CtParameter) m2.getParameters().get(0);
    assertEquals(4, ctParameter.getComments().size());
    assertEquals("// comment before type" + newLine + "// comment after parameter" + newLine + "// comment before throws" + newLine + "// comment before exception 1" + newLine + "// comment before exception 2" + newLine + "// comment before block" + newLine + "public void m2(// comment before name" + newLine + "// comment before parameters" + newLine + "// comment before type parameter" + newLine + "// comment before name parameter" + newLine + "int i) throws java.lang.Error, java.lang.Exception {" + newLine + "}", m2.toString());
}
Also used : CtConditional(spoon.reflect.code.CtConditional) CtComment(spoon.reflect.code.CtComment) CtSwitch(spoon.reflect.code.CtSwitch) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtTry(spoon.reflect.code.CtTry) CtNewArray(spoon.reflect.code.CtNewArray) CtSynchronized(spoon.reflect.code.CtSynchronized) InlineComment(spoon.test.comment.testclasses.InlineComment) CtInvocation(spoon.reflect.code.CtInvocation) CtReturn(spoon.reflect.code.CtReturn) CtDo(spoon.reflect.code.CtDo) CtFor(spoon.reflect.code.CtFor) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) CtElement(spoon.reflect.declaration.CtElement) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) CtConstructor(spoon.reflect.declaration.CtConstructor) CtClass(spoon.reflect.declaration.CtClass) CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtReturn (spoon.reflect.code.CtReturn)12 Test (org.junit.Test)9 Launcher (spoon.Launcher)5 CtConstructorCall (spoon.reflect.code.CtConstructorCall)5 CtClass (spoon.reflect.declaration.CtClass)5 Factory (spoon.reflect.factory.Factory)5 CtMethod (spoon.reflect.declaration.CtMethod)4 CtInvocation (spoon.reflect.code.CtInvocation)3 CtStatement (spoon.reflect.code.CtStatement)3 CtModel (spoon.reflect.CtModel)2 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)2 CtBlock (spoon.reflect.code.CtBlock)2 CtComment (spoon.reflect.code.CtComment)2 CtDo (spoon.reflect.code.CtDo)2 CtExpression (spoon.reflect.code.CtExpression)2 CtFor (spoon.reflect.code.CtFor)2 CtIf (spoon.reflect.code.CtIf)2 CtLiteral (spoon.reflect.code.CtLiteral)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtSwitch (spoon.reflect.code.CtSwitch)2