Search in sources :

Example 1 with BodyHolderSourcePosition

use of spoon.reflect.cu.position.BodyHolderSourcePosition 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);
}
Also used : FooMethod(spoon.test.position.testclasses.FooMethod) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) SourcePosition(spoon.reflect.cu.SourcePosition) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) Test(org.junit.Test)

Example 2 with BodyHolderSourcePosition

use of spoon.reflect.cu.position.BodyHolderSourcePosition in project spoon by INRIA.

the class PositionTest method testPositionClass.

@Test
public void testPositionClass() throws Exception {
    final Factory build = build(new File("src/test/java/spoon/test/position/testclasses/"));
    final CtType<FooClazz> foo = build.Type().get(FooClazz.class);
    String classContent = getClassContent(foo);
    BodyHolderSourcePosition position = (BodyHolderSourcePosition) foo.getPosition();
    assertEquals(4, position.getLine());
    assertEquals(6, position.getEndLine());
    assertEquals(42, position.getSourceStart());
    assertEquals(79, position.getSourceEnd());
    assertEquals("@Deprecated\n" + "public class FooClazz {\n" + "\n" + "}", contentAtPosition(classContent, position));
    assertEquals("{\n\n}", contentAtPosition(classContent, position.getBodyStart(), position.getBodyEnd()));
    // this specifies that getLine starts at name (and not at Javadoc or annotation)
    final CtType<FooClazz> foo2 = build.Type().get(FooClazz2.class);
    assertEquals(42, foo2.getPosition().getSourceStart());
    assertEquals(4, foo2.getPosition().getLine());
    assertEquals(4, foo2.getPosition().getEndLine());
    assertEquals("FooClazz", contentAtPosition(classContent, position.getNameStart(), position.getNameEnd()));
    assertEquals("public", contentAtPosition(classContent, position.getModifierSourceStart(), position.getModifierSourceEnd()));
}
Also used : FooClazz(spoon.test.position.testclasses.FooClazz) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) Factory(spoon.reflect.factory.Factory) File(java.io.File) Test(org.junit.Test)

Example 3 with BodyHolderSourcePosition

use of spoon.reflect.cu.position.BodyHolderSourcePosition in project spoon by INRIA.

the class PositionTest method testPositionInterface.

@Test
public void testPositionInterface() throws Exception {
    final Factory build = build(new File("src/test/java/spoon/test/position/testclasses/"));
    final CtType<FooInterface> foo = build.Type().get(FooInterface.class);
    String classContent = getClassContent(foo);
    BodyHolderSourcePosition position = (BodyHolderSourcePosition) foo.getPosition();
    assertEquals(7, position.getLine());
    assertEquals(9, position.getEndLine());
    assertEquals(96, position.getSourceStart());
    assertEquals(169, position.getSourceEnd());
    assertEquals("@Deprecated\n" + "@InnerAnnot(value=\"machin\")\n" + "public interface FooInterface {\n" + "\n" + "}", contentAtPosition(classContent, position));
    assertEquals("{\n\n}", contentAtPosition(classContent, position.getBodyStart(), position.getBodyEnd()));
    assertEquals("FooInterface", contentAtPosition(classContent, position.getNameStart(), position.getNameEnd()));
    assertEquals("public", contentAtPosition(classContent, position.getModifierSourceStart(), position.getModifierSourceEnd()));
    {
        SourcePosition annPosition = foo.getAnnotations().get(0).getPosition();
        assertEquals("@Deprecated", contentAtPosition(classContent, annPosition.getSourceStart(), annPosition.getSourceEnd()));
    }
    {
        SourcePosition annPosition = foo.getAnnotations().get(1).getPosition();
        assertEquals("@InnerAnnot(value=\"machin\")", contentAtPosition(classContent, annPosition.getSourceStart(), annPosition.getSourceEnd()));
    }
}
Also used : BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) SourcePosition(spoon.reflect.cu.SourcePosition) Factory(spoon.reflect.factory.Factory) File(java.io.File) FooInterface(spoon.test.position.testclasses.FooInterface) Test(org.junit.Test)

Example 4 with BodyHolderSourcePosition

use of spoon.reflect.cu.position.BodyHolderSourcePosition 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()));
}
Also used : BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) FooGeneric(spoon.test.position.testclasses.FooGeneric) Factory(spoon.reflect.factory.Factory) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) Test(org.junit.Test)

Example 5 with BodyHolderSourcePosition

use of spoon.reflect.cu.position.BodyHolderSourcePosition in project spoon by INRIA.

the class PositionTest method testPositionAnnotation.

@Test
public void testPositionAnnotation() throws Exception {
    final Factory build = build(new File("src/test/java/spoon/test/position/testclasses/"));
    final CtType<FooAnnotation> foo = build.Type().get(FooAnnotation.class);
    String classContent = getClassContent(foo);
    BodyHolderSourcePosition position = (BodyHolderSourcePosition) foo.getPosition();
    assertEquals(9, position.getLine());
    assertEquals(11, position.getEndLine());
    assertEquals(163, position.getSourceStart());
    assertEquals(279, position.getSourceEnd());
    assertEquals("@Target(value={})\n" + "@Retention(RetentionPolicy.RUNTIME)  \n" + "public abstract @interface FooAnnotation {\n" + "\tString value();\n" + "}", contentAtPosition(classContent, position));
    assertEquals("{\n" + "\tString value();\n" + "}", contentAtPosition(classContent, position.getBodyStart(), position.getBodyEnd()));
    assertEquals("FooAnnotation", contentAtPosition(classContent, position.getNameStart(), position.getNameEnd()));
    assertEquals("public abstract", contentAtPosition(classContent, position.getModifierSourceStart(), position.getModifierSourceEnd()));
    CtMethod<?> method1 = foo.getMethodsByName("value").get(0);
    BodyHolderSourcePosition position1 = (BodyHolderSourcePosition) method1.getPosition();
    assertEquals(10, position1.getLine());
    assertEquals(10, position1.getEndLine());
    assertEquals(263, position1.getSourceStart());
    assertEquals(277, position1.getSourceEnd());
    assertEquals("String value();", contentAtPosition(classContent, position1));
    assertEquals("value", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
    assertEquals("", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
    // contract: body of abstract method is empty
    assertEquals("", contentAtPosition(classContent, position1.getBodyStart(), position1.getBodyEnd()));
}
Also used : FooAnnotation(spoon.test.position.testclasses.FooAnnotation) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) Factory(spoon.reflect.factory.Factory) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 BodyHolderSourcePosition (spoon.reflect.cu.position.BodyHolderSourcePosition)6 Factory (spoon.reflect.factory.Factory)6 DeclarationSourcePosition (spoon.reflect.cu.position.DeclarationSourcePosition)4 File (java.io.File)3 SourcePosition (spoon.reflect.cu.SourcePosition)2 FooMethod (spoon.test.position.testclasses.FooMethod)2 CtMethod (spoon.reflect.declaration.CtMethod)1 FooAnnotation (spoon.test.position.testclasses.FooAnnotation)1 FooClazz (spoon.test.position.testclasses.FooClazz)1 FooGeneric (spoon.test.position.testclasses.FooGeneric)1 FooInterface (spoon.test.position.testclasses.FooInterface)1