use of spoon.reflect.cu.position.DeclarationSourcePosition in project spoon by INRIA.
the class PositionTest method testPositionMethod.
@Test
public void testPositionMethod() throws Exception {
final Factory build = build(FooMethod.class);
final CtClass<FooMethod> foo = build.Class().get(FooMethod.class);
String classContent = getClassContent(foo);
CtMethod<?> method1 = foo.getMethodsByName("m").get(0);
BodyHolderSourcePosition position1 = (BodyHolderSourcePosition) method1.getPosition();
assertEquals(5, position1.getLine());
assertEquals(7, position1.getEndLine());
assertEquals(69, position1.getSourceStart());
assertEquals(114, position1.getSourceEnd());
assertEquals("public static void m(int parm1) {\n" + "\t\treturn;\n" + "\t}", contentAtPosition(classContent, position1));
assertEquals("m", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
assertEquals("public static", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
// contract: body contains starting and ending brackets {}
assertEquals("{\n" + "\t\treturn;\n" + "\t}", contentAtPosition(classContent, position1.getBodyStart(), position1.getBodyEnd()));
DeclarationSourcePosition positionParam1 = (DeclarationSourcePosition) method1.getParameters().get(0).getPosition();
assertEquals(5, positionParam1.getLine());
assertEquals(5, positionParam1.getEndLine());
assertEquals(90, positionParam1.getSourceStart());
assertEquals(98, positionParam1.getSourceEnd());
assertEquals("int parm1", contentAtPosition(classContent, positionParam1));
assertEquals("parm1", contentAtPosition(classContent, positionParam1.getNameStart(), positionParam1.getNameEnd()));
assertEquals("", contentAtPosition(classContent, positionParam1.getModifierSourceStart(), positionParam1.getModifierSourceEnd()));
CtMethod method2 = foo.getMethodsByName("mWithDoc").get(0);
BodyHolderSourcePosition position2 = (BodyHolderSourcePosition) method2.getPosition();
assertEquals(13, position2.getLine());
assertEquals(15, position2.getEndLine());
assertEquals("/**\n" + "\t * Mathod with javadoc\n" + "\t * @param parm1 the parameter\n" + "\t */\n" + "\tint mWithDoc(int parm1) {\n" + "\t\treturn parm1;\n" + "\t}", contentAtPosition(classContent, position2));
assertEquals("mWithDoc", contentAtPosition(classContent, position2.getNameStart(), position2.getNameEnd()));
assertEquals("", contentAtPosition(classContent, position2.getModifierSourceStart(), position2.getModifierSourceEnd()));
CtConstructor<FooMethod> constructor = foo.getConstructor(build.Type().integerPrimitiveType());
SourcePosition position3 = constructor.getPosition();
contentAtPosition(classContent, position3);
CtMethod mWithLine = foo.getMethod("mWithLine", build.Type().integerPrimitiveType());
SourcePosition position4 = mWithLine.getPosition();
contentAtPosition(classContent, position4);
}
use of spoon.reflect.cu.position.DeclarationSourcePosition in project spoon by INRIA.
the class PositionTest method testPositionField.
@Test
public void testPositionField() throws Exception {
final Factory build = build(FooField.class);
final CtType<FooField> foo = build.Type().get(FooField.class);
String classContent = getClassContent(foo);
DeclarationSourcePosition position1 = (DeclarationSourcePosition) foo.getField("field1").getPosition();
assertEquals(5, position1.getLine());
assertEquals(5, position1.getEndLine());
assertEquals(68, position1.getSourceStart());
assertEquals(95, position1.getSourceEnd());
assertEquals("public final int field1 = 0;", contentAtPosition(classContent, position1));
assertEquals("field1", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
assertEquals("public final", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
DeclarationSourcePosition position2 = (DeclarationSourcePosition) foo.getField("field2").getPosition();
assertEquals(7, position2.getLine());
assertEquals(8, position2.getEndLine());
assertEquals(99, position2.getSourceStart());
assertEquals(116, position2.getSourceEnd());
assertEquals("int field2 =\n" + "\t\t\t0;", contentAtPosition(classContent, position2));
assertEquals("field2", contentAtPosition(classContent, position2.getNameStart(), position2.getNameEnd()));
assertEquals("", contentAtPosition(classContent, position2.getModifierSourceStart(), position2.getModifierSourceEnd()));
CtAssignment m = foo.getMethod("m").getBody().getStatement(0);
CtFieldAccess assigned = (CtFieldAccess) m.getAssigned();
SourcePosition position3 = assigned.getPosition();
assertEquals(13, position3.getLine());
assertEquals(13, position3.getEndLine());
assertEquals(168, position3.getSourceStart());
assertEquals(184, position3.getSourceEnd());
assertEquals("FooField.f.field2", contentAtPosition(classContent, position3));
CtFieldAccess target = (CtFieldAccess) assigned.getTarget();
SourcePosition position4 = target.getPosition();
assertEquals(13, position4.getLine());
assertEquals(13, position4.getEndLine());
assertEquals(168, position4.getSourceStart());
assertEquals(177, position4.getSourceEnd());
assertEquals("FooField.f", contentAtPosition(classContent, position4));
CtExpression typeAccess = target.getTarget();
SourcePosition position5 = typeAccess.getPosition();
assertEquals(13, position5.getLine());
assertEquals(13, position5.getEndLine());
assertEquals(168, position5.getSourceStart());
assertEquals(175, position5.getSourceEnd());
assertEquals("FooField", contentAtPosition(classContent, position5));
}
use of spoon.reflect.cu.position.DeclarationSourcePosition in project spoon by INRIA.
the class PositionTest method testPositionGeneric.
@Test
public void testPositionGeneric() throws Exception {
final Factory build = build(FooGeneric.class);
final CtClass<FooGeneric> foo = build.Class().get(FooGeneric.class);
String classContent = getClassContent(foo);
BodyHolderSourcePosition position = (BodyHolderSourcePosition) foo.getPosition();
assertEquals(3, position.getLine());
assertEquals(31, position.getEndLine());
assertEquals(42, position.getSourceStart());
assertEquals(411, position.getSourceEnd());
assertEquals("FooGeneric", contentAtPosition(classContent, position.getNameStart(), position.getNameEnd()));
assertEquals("public", contentAtPosition(classContent, position.getModifierSourceStart(), position.getModifierSourceEnd()));
DeclarationSourcePosition position1 = (DeclarationSourcePosition) foo.getField("variable").getPosition();
assertEquals(5, position1.getLine());
assertEquals(5, position1.getEndLine());
assertEquals(88, position1.getSourceStart());
assertEquals(118, position1.getSourceEnd());
assertEquals("public final T variable = null;", contentAtPosition(classContent, position1));
assertEquals("variable", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
assertEquals("public final", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
CtMethod<?> method1 = foo.getMethodsByName("m").get(0);
BodyHolderSourcePosition position2 = (BodyHolderSourcePosition) method1.getPosition();
assertEquals("public @Deprecated static <S> S m(int parm1) {\n" + "\t\treturn null;\n" + "\t}", contentAtPosition(classContent, position2));
assertEquals("m", contentAtPosition(classContent, position2.getNameStart(), position2.getNameEnd()));
// /!\ the annotations can be between two modifiers
assertEquals("public @Deprecated static", contentAtPosition(classContent, position2.getModifierSourceStart(), position2.getModifierSourceEnd()));
}
use of spoon.reflect.cu.position.DeclarationSourcePosition in project spoon by INRIA.
the class PositionTest method testPositionAbstractMethod.
@Test
public void testPositionAbstractMethod() throws Exception {
final Factory build = build(FooAbstractMethod.class);
final CtClass<FooMethod> foo = build.Class().get(FooAbstractMethod.class);
String classContent = getClassContent(foo);
CtMethod<?> method1 = foo.getMethodsByName("m").get(0);
BodyHolderSourcePosition position1 = (BodyHolderSourcePosition) method1.getPosition();
assertEquals(5, position1.getLine());
assertEquals(5, position1.getEndLine());
assertEquals(86, position1.getSourceStart());
assertEquals(125, position1.getSourceEnd());
assertEquals("public abstract void m(final int parm1);", contentAtPosition(classContent, position1));
assertEquals("m", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
assertEquals("public abstract", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
// contract: body of abstract method is empty
assertEquals("", contentAtPosition(classContent, position1.getBodyStart(), position1.getBodyEnd()));
DeclarationSourcePosition positionParam1 = (DeclarationSourcePosition) method1.getParameters().get(0).getPosition();
assertEquals(5, positionParam1.getLine());
assertEquals(5, positionParam1.getEndLine());
assertEquals(109, positionParam1.getSourceStart());
assertEquals(123, positionParam1.getSourceEnd());
assertEquals("final int parm1", contentAtPosition(classContent, positionParam1));
assertEquals("parm1", contentAtPosition(classContent, positionParam1.getNameStart(), positionParam1.getNameEnd()));
assertEquals("final", contentAtPosition(classContent, positionParam1.getModifierSourceStart(), positionParam1.getModifierSourceEnd()));
}
Aggregations