Search in sources :

Example 1 with CtFor

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

the class CommentTest method testBlockComment.

@Test
public void testBlockComment() {
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(BlockComment.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(51, 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(4, type.getComments().size());
    assertEquals(createFakeBlockComment(f, "comment class"), type.getComments().get(1));
    CtField<?> field = type.getField("field");
    assertEquals(2, field.getComments().size());
    assertEquals(createFakeBlockComment(f, "Comment Field"), field.getComments().get(0));
    assertEquals("/* Comment Field */" + newLine + "/* comment in field */" + newLine + "private int field = 10;", field.toString());
    CtAnonymousExecutable ctAnonymousExecutable = type.getAnonymousExecutables().get(0);
    assertEquals(1, ctAnonymousExecutable.getComments().size());
    assertEquals(createFakeBlockComment(f, "comment static block"), ctAnonymousExecutable.getComments().get(0));
    assertEquals(createFakeBlockComment(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(createFakeBlockComment(f, "comment constructor"), constructor.getComments().get(0));
    // index 0 is the implicit super call
    assertEquals(createFakeBlockComment(f, "Comment in constructor"), constructor.getBody().getStatement(1));
    assertEquals("/* comment constructor */" + newLine + "public BlockComment() {" + newLine + "    /* Comment in constructor */" + newLine + "}", constructor.toString());
    CtMethod<Object> m = type.getMethod("m");
    assertEquals(1, m.getComments().size());
    assertEquals(createFakeBlockComment(f, "comment method"), m.getComments().get(0));
    assertEquals(createFakeBlockComment(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(createFakeBlockComment(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(createFakeBlockComment(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(createFakeBlockComment(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(createFakeBlockComment(f, "comment constructor call"), ctConstructorCall.getComments().get(0));
    assertEquals("/* comment constructor call */" + newLine + "new spoon.test.comment.testclasses.BlockComment()", ctConstructorCall.toString());
    CtInvocation ctInvocation = m1.getBody().getStatement(4);
    assertEquals(createFakeBlockComment(f, "comment invocation"), ctInvocation.getComments().get(0));
    assertEquals("/* comment invocation */" + newLine + "this.m()", ctInvocation.toString());
    CtLocalVariable ctLocalVariable = m1.getBody().getStatement(5);
    assertEquals(createFakeBlockComment(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(createFakeBlockComment(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(createFakeBlockComment(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(createFakeBlockComment(f, "comment try"), ctTry.getComments().get(0));
    assertEquals("/* comment try */" + newLine + "try {" + newLine + "    /* comment in try */" + newLine + "    i++;" + newLine + "} catch (java.lang.Exception e) {" + newLine + "    /* comment in catch */" + newLine + "}", ctTry.toString());
    CtSynchronized ctSynchronized = m1.getBody().getStatement(9);
    assertEquals(createFakeBlockComment(f, "comment synchronized"), ctSynchronized.getComments().get(0));
    assertEquals("/* comment synchronized */" + newLine + "synchronized(this) {" + newLine + "    /* comment in synchronized */" + newLine + "}", ctSynchronized.toString());
    CtReturn ctReturn = m1.getBody().getStatement(10);
    assertEquals(createFakeBlockComment(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 : 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) CtSynchronized(spoon.reflect.code.CtSynchronized) CtInvocation(spoon.reflect.code.CtInvocation) CtReturn(spoon.reflect.code.CtReturn) CtDo(spoon.reflect.code.CtDo) CtFor(spoon.reflect.code.CtFor) BlockComment(spoon.test.comment.testclasses.BlockComment) 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)

Example 2 with CtFor

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

the class ControlTest method testModelBuildingFor.

@Test
public void testModelBuildingFor() throws Exception {
    CtType<?> type = build("spoon.test.control.testclasses", "Fors");
    assertEquals("Fors", type.getSimpleName());
    List<CtFor> fors = type.getElements(new TypeFilter<CtFor>(CtFor.class));
    assertEquals(4, fors.size());
    CtMethod<?> normalFor = type.getElements(new NamedElementFilter<>(CtMethod.class, "normalFor")).get(0);
    CtFor firstFor = (CtFor) normalFor.getBody().getStatements().get(0);
    assertEquals("int i = 0", firstFor.getForInit().get(0).toString());
    assertEquals("i < 2", firstFor.getExpression().toString());
    assertEquals("i++", firstFor.getForUpdate().get(0).toString());
    CtMethod<?> empty1 = type.getElements(new NamedElementFilter<>(CtMethod.class, "empty1")).get(0);
    CtFor empty1For = (CtFor) empty1.getBody().getStatements().get(1);
    assertEquals("i = 0", empty1For.getForInit().get(0).toString());
    // TODO: is it good to return null??
    // I'm not sure I want to specify this
    // I would prefer to add a fake null object that is printed as empty in
    // the output
    assertNull(empty1For.getExpression());
    assertEquals("i++", empty1For.getForUpdate().get(0).toString());
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtFor(spoon.reflect.code.CtFor) Test(org.junit.Test)

Example 3 with CtFor

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

the class AnnotationLoopTest method testAnnotationDeclaredInForInit.

@Test
public void testAnnotationDeclaredInForInit() throws Exception {
    final CtType<Pozole> aPozole = ModelUtils.buildClass(Pozole.class);
    final CtFor aLoop = aPozole.getMethod("cook").getElements(new TypeFilter<>(CtFor.class)).get(0);
    assertEquals(3, aLoop.getForInit().size());
    assertEquals(SuppressWarnings.class, aLoop.getForInit().get(0).getAnnotations().get(0).getAnnotationType().getActualClass());
    assertEquals(SuppressWarnings.class, aLoop.getForInit().get(1).getAnnotations().get(0).getAnnotationType().getActualClass());
    assertEquals(SuppressWarnings.class, aLoop.getForInit().get(2).getAnnotations().get(0).getAnnotationType().getActualClass());
    assertEquals("u", ((CtLocalVariable) aLoop.getForInit().get(0)).getSimpleName());
    assertEquals("p", ((CtLocalVariable) aLoop.getForInit().get(1)).getSimpleName());
    assertEquals("e", ((CtLocalVariable) aLoop.getForInit().get(2)).getSimpleName());
    assertEquals(aPozole.getFactory().Type().STRING, ((CtLocalVariable) aLoop.getForInit().get(0)).getType());
    assertEquals(aPozole.getFactory().Type().STRING, ((CtLocalVariable) aLoop.getForInit().get(1)).getType());
    assertEquals(aPozole.getFactory().Type().STRING, ((CtLocalVariable) aLoop.getForInit().get(2)).getType());
    final String nl = System.lineSeparator();
    final String expected = "for (@java.lang.SuppressWarnings(\"rawtypes\")" + nl + "java.lang.String u = \"\", p = \"\", e = \"\"; u != e; u = p , p = \"\") {" + nl + "}";
    assertEquals(expected, aLoop.toString());
}
Also used : Pozole(spoon.test.annotation.testclasses.Pozole) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtFor(spoon.reflect.code.CtFor) Test(org.junit.Test)

Example 4 with CtFor

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

the class AccessibleVariablesFinder method getVariable.

private List<CtVariable> getVariable(final CtElement parent) {
    final List<CtVariable> variables = new ArrayList<>();
    if (parent == null) {
        return variables;
    }
    class VariableScanner extends CtInheritanceScanner {

        @Override
        public void visitCtStatementList(CtStatementList e) {
            for (int i = 0; i < e.getStatements().size(); i++) {
                CtStatement ctStatement = e.getStatements().get(i);
                if (ctStatement.getPosition() == null) {
                }
                if (ctStatement.getPosition() != null && ctStatement.getPosition().getSourceStart() > expression.getPosition().getSourceEnd()) {
                    break;
                }
                if (ctStatement instanceof CtVariable) {
                    variables.add((CtVariable) ctStatement);
                }
            }
            super.visitCtStatementList(e);
        }

        @Override
        public <T> void scanCtType(CtType<T> type) {
            List<CtField<?>> fields = type.getFields();
            for (int i = 0; i < fields.size(); i++) {
                CtField<?> ctField = fields.get(i);
                if (ctField.hasModifier(ModifierKind.PUBLIC) || ctField.hasModifier(ModifierKind.PROTECTED)) {
                    variables.add(ctField);
                } else if (ctField.hasModifier(ModifierKind.PRIVATE)) {
                    if (expression.hasParent(type)) {
                        variables.add(ctField);
                    }
                } else if (expression.getParent(CtPackage.class).equals(type.getParent(CtPackage.class))) {
                    // default visibility
                    variables.add(ctField);
                }
            }
            CtTypeReference<?> superclass = type.getSuperclass();
            if (superclass != null) {
                variables.addAll(getVariable(superclass.getTypeDeclaration()));
            }
            Set<CtTypeReference<?>> superInterfaces = type.getSuperInterfaces();
            for (Iterator<CtTypeReference<?>> iterator = superInterfaces.iterator(); iterator.hasNext(); ) {
                CtTypeReference<?> typeReference = iterator.next();
                variables.addAll(getVariable(typeReference.getTypeDeclaration()));
            }
            super.scanCtType(type);
        }

        @Override
        public void visitCtTryWithResource(CtTryWithResource e) {
            variables.addAll(e.getResources());
            super.visitCtTryWithResource(e);
        }

        @Override
        public void scanCtExecutable(CtExecutable e) {
            variables.addAll(e.getParameters());
            super.scanCtExecutable(e);
        }

        @Override
        public void visitCtFor(CtFor e) {
            for (CtStatement ctStatement : e.getForInit()) {
                this.scan(ctStatement);
            }
            super.visitCtFor(e);
        }

        @Override
        public void visitCtForEach(CtForEach e) {
            variables.add(e.getVariable());
            super.visitCtForEach(e);
        }

        @Override
        public void visitCtMethod(CtMethod e) {
            this.scan(e.getBody());
            super.visitCtMethod(e);
        }

        @Override
        public void visitCtLocalVariable(CtLocalVariable e) {
            variables.add(e);
            super.visitCtLocalVariable(e);
        }

        @Override
        public void visitCtCatch(CtCatch e) {
            variables.add(e.getParameter());
            super.visitCtCatch(e);
        }
    }
    new VariableScanner().scan(parent);
    return variables;
}
Also used : ArrayList(java.util.ArrayList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtForEach(spoon.reflect.code.CtForEach) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtVariable(spoon.reflect.declaration.CtVariable) CtStatementList(spoon.reflect.code.CtStatementList) CtPackage(spoon.reflect.declaration.CtPackage) CtTryWithResource(spoon.reflect.code.CtTryWithResource) CtCatch(spoon.reflect.code.CtCatch) CtFor(spoon.reflect.code.CtFor) CtMethod(spoon.reflect.declaration.CtMethod)

Example 5 with CtFor

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

the class CtBodyHolderTest method testForWithBlock.

@Test
public void testForWithBlock() throws Exception {
    Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
    CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
    assertEquals(2, cwbClass.getMethods().size());
    CtMethod<?> method = cwbClass.getMethod("method2");
    CtBlock<?> methodBody = method.getBody();
    assertTrue(methodBody.getStatement(2) instanceof CtFor);
    CtFor forStmnt = (CtFor) methodBody.getStatement(2);
    checkCtBody(forStmnt, "for_block", 0);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ClassWithBodies(spoon.test.ctBodyHolder.testclasses.ClassWithBodies) Factory(spoon.reflect.factory.Factory) CtFor(spoon.reflect.code.CtFor) Test(org.junit.Test)

Aggregations

CtFor (spoon.reflect.code.CtFor)11 Test (org.junit.Test)9 CtClass (spoon.reflect.declaration.CtClass)4 CtMethod (spoon.reflect.declaration.CtMethod)4 Factory (spoon.reflect.factory.Factory)4 CtDo (spoon.reflect.code.CtDo)3 CtIf (spoon.reflect.code.CtIf)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtSwitch (spoon.reflect.code.CtSwitch)3 CtComment (spoon.reflect.code.CtComment)2 CtConstructorCall (spoon.reflect.code.CtConstructorCall)2 CtForEach (spoon.reflect.code.CtForEach)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtReturn (spoon.reflect.code.CtReturn)2 CtStatement (spoon.reflect.code.CtStatement)2 CtSynchronized (spoon.reflect.code.CtSynchronized)2 CtTry (spoon.reflect.code.CtTry)2 CtAnonymousExecutable (spoon.reflect.declaration.CtAnonymousExecutable)2 CtConstructor (spoon.reflect.declaration.CtConstructor)2 CtParameter (spoon.reflect.declaration.CtParameter)2