Search in sources :

Example 16 with SourcePosition

use of spoon.reflect.cu.SourcePosition 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)

Aggregations

SourcePosition (spoon.reflect.cu.SourcePosition)16 Test (org.junit.Test)8 BodyHolderSourcePosition (spoon.reflect.cu.position.BodyHolderSourcePosition)6 DeclarationSourcePosition (spoon.reflect.cu.position.DeclarationSourcePosition)6 Factory (spoon.reflect.factory.Factory)5 CtBlock (spoon.reflect.code.CtBlock)4 File (java.io.File)3 Launcher (spoon.Launcher)3 CtIf (spoon.reflect.code.CtIf)3 CtMethod (spoon.reflect.declaration.CtMethod)3 CtComment (spoon.reflect.code.CtComment)2 CtExpression (spoon.reflect.code.CtExpression)2 CtStatement (spoon.reflect.code.CtStatement)2 CompilationUnit (spoon.reflect.cu.CompilationUnit)2 NoSourcePosition (spoon.reflect.cu.position.NoSourcePosition)2 CtClass (spoon.reflect.declaration.CtClass)2 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 ArrayList (java.util.ArrayList)1 StringLiteralConcatenation (org.eclipse.jdt.internal.compiler.ast.StringLiteralConcatenation)1