Search in sources :

Example 36 with CtBlock

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

the class PositionTest method testPositionStatement.

@Test
public void testPositionStatement() throws Exception {
    final Factory build = build(FooStatement.class);
    final CtType<FooStatement> foo = build.Type().get(FooStatement.class);
    String classContent = getClassContent(foo);
    CtMethod<?> method1 = foo.getMethodsByName("m").get(0);
    CtBlock<?> body = method1.getBody();
    SourcePosition positionBody = body.getPosition();
    assertEquals(7, positionBody.getLine());
    assertEquals(23, positionBody.getEndLine());
    assertEquals("{\n" + "\t\tint field2 = m2(parm1);\n" + "\t\tthis.field = m2(parm1);\n" + "\t\tif(parm1 > 2 && true) {\n" + "\t\t\tswitch (parm1) {\n" + "\t\t\tcase 1:\n" + "\t\t\t\treturn;\n" + "\t\t\tdefault:\n" + "\t\t\t\tparm1++;\n" + "\t\t\t}\n" + "\t\t\tint count = 0;\n" + "\t\t\tfor (int i =0; i< parm1; i++) {\n" + "\t\t\t\tcount ++;\n" + "\t\t\t}\n" + "\t\t}\n" + "\t\treturn;\n" + "\t}", contentAtPosition(classContent, positionBody));
    SourcePosition positionLocalVariable = body.getStatement(0).getPosition();
    assertEquals(8, positionLocalVariable.getLine());
    assertEquals(8, positionLocalVariable.getEndLine());
    assertEquals("int field2 = m2(parm1);", contentAtPosition(classContent, positionLocalVariable));
    SourcePosition positionFieldWrite = body.getStatement(1).getPosition();
    assertEquals(9, positionFieldWrite.getLine());
    assertEquals(9, positionFieldWrite.getEndLine());
    assertEquals("this.field = m2(parm1);", contentAtPosition(classContent, positionFieldWrite));
    CtIf ctIf = body.getStatement(2);
    SourcePosition positionIf = ctIf.getPosition();
    assertEquals(10, positionIf.getLine());
    assertEquals(21, positionIf.getEndLine());
    assertEquals("if(parm1 > 2 && true) {\n" + "\t\t\tswitch (parm1) {\n" + "\t\t\tcase 1:\n" + "\t\t\t\treturn;\n" + "\t\t\tdefault:\n" + "\t\t\t\tparm1++;\n" + "\t\t\t}\n" + "\t\t\tint count = 0;\n" + "\t\t\tfor (int i =0; i< parm1; i++) {\n" + "\t\t\t\tcount ++;\n" + "\t\t\t}\n" + "\t\t}", contentAtPosition(classContent, positionIf));
    SourcePosition positionSwitch = ((CtBlock) ctIf.getThenStatement()).getStatement(0).getPosition();
    assertEquals(11, positionSwitch.getLine());
    assertEquals(16, positionSwitch.getEndLine());
    assertEquals("switch (parm1) {\n" + "\t\t\tcase 1:\n" + "\t\t\t\treturn;\n" + "\t\t\tdefault:\n" + "\t\t\t\tparm1++;\n" + "\t\t\t}", contentAtPosition(classContent, positionSwitch));
    positionLocalVariable = ((CtBlock) ctIf.getThenStatement()).getStatement(1).getPosition();
    assertEquals(17, positionLocalVariable.getLine());
    assertEquals(17, positionLocalVariable.getEndLine());
    assertEquals("int count = 0;", contentAtPosition(classContent, positionLocalVariable));
    SourcePosition positionFor = ((CtBlock) ctIf.getThenStatement()).getStatement(2).getPosition();
    assertEquals(18, positionFor.getLine());
    assertEquals(20, positionFor.getEndLine());
    assertEquals("for (int i =0; i< parm1; i++) {\n" + "\t\t\t\tcount ++;\n" + "\t\t\t}", contentAtPosition(classContent, positionFor));
    SourcePosition positionReturn = method1.getBody().getStatement(3).getPosition();
    assertEquals(22, positionReturn.getLine());
    assertEquals(22, positionReturn.getEndLine());
    assertEquals("return;", contentAtPosition(classContent, positionReturn));
}
Also used : CtBlock(spoon.reflect.code.CtBlock) FooStatement(spoon.test.position.testclasses.FooStatement) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) SourcePosition(spoon.reflect.cu.SourcePosition) Factory(spoon.reflect.factory.Factory) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 37 with CtBlock

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

the class LoopTest method testForeachShouldHaveAlwaysABlockInItsBody.

@Test
public void testForeachShouldHaveAlwaysABlockInItsBody() throws Exception {
    final CtClass<Join> aType = build(Join.class, Condition.class).Class().get(Join.class);
    final CtConstructor<Join> joinCtConstructor = aType.getConstructors().stream().findFirst().get();
    final CtLoop ctLoop = joinCtConstructor.getBody().getElements(new TypeFilter<>(CtLoop.class)).get(0);
    assertTrue(ctLoop.getBody() instanceof CtBlock);
    // 
    String expected = // 
    "for (spoon.test.loop.testclasses.Condition<? super T> condition : conditions)" + nl + "    this.conditions.add(spoon.test.loop.testclasses.Join.notNull(condition));" + nl;
    assertEquals(expected, ctLoop.toString());
}
Also used : CtBlock(spoon.reflect.code.CtBlock) Join(spoon.test.loop.testclasses.Join) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLoop(spoon.reflect.code.CtLoop) Test(org.junit.Test)

Aggregations

CtBlock (spoon.reflect.code.CtBlock)37 Test (org.junit.Test)24 CtStatement (spoon.reflect.code.CtStatement)17 CtIf (spoon.reflect.code.CtIf)15 Factory (spoon.reflect.factory.Factory)15 Launcher (spoon.Launcher)13 CtMethod (spoon.reflect.declaration.CtMethod)10 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtElement (spoon.reflect.declaration.CtElement)7 CtInvocation (spoon.reflect.code.CtInvocation)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)5 ArrayList (java.util.ArrayList)4 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)4 CtClass (spoon.reflect.declaration.CtClass)4 File (java.io.File)3 List (java.util.List)3 CtAssignment (spoon.reflect.code.CtAssignment)3 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)3 SourcePosition (spoon.reflect.cu.SourcePosition)3