Search in sources :

Example 21 with CtFieldAccess

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

the class AccessTest method testCanVisitFieldAccessAndSubClasses.

@Test
public void testCanVisitFieldAccessAndSubClasses() throws Exception {
    final Factory factory = build(FieldAccessSample.class);
    final List<CtFieldRead<?>> fieldsRead = Query.getElements(factory, new AbstractFilter<CtFieldRead<?>>(CtFieldRead.class) {

        @Override
        public boolean matches(CtFieldRead<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(2, fieldsRead.size());
    final List<CtFieldWrite<?>> fieldsWrite = Query.getElements(factory, new AbstractFilter<CtFieldWrite<?>>(CtFieldWrite.class) {

        @Override
        public boolean matches(CtFieldWrite<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(3, fieldsWrite.size());
    final List<CtFieldAccess<?>> fieldsAccess = Query.getElements(factory, new AbstractFilter<CtFieldAccess<?>>(CtFieldAccess.class) {

        @Override
        public boolean matches(CtFieldAccess<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(5, fieldsAccess.size());
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtFieldRead(spoon.reflect.code.CtFieldRead) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtFieldWrite(spoon.reflect.code.CtFieldWrite) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 22 with CtFieldAccess

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

Aggregations

CtFieldAccess (spoon.reflect.code.CtFieldAccess)22 Test (org.junit.Test)17 Factory (spoon.reflect.factory.Factory)11 Launcher (spoon.Launcher)7 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)7 CtInvocation (spoon.reflect.code.CtInvocation)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)4 Foo (spoon.test.targeted.testclasses.Foo)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 List (java.util.List)2 CtAssignment (spoon.reflect.code.CtAssignment)2 CtExpression (spoon.reflect.code.CtExpression)2 CtFieldWrite (spoon.reflect.code.CtFieldWrite)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtThisAccess (spoon.reflect.code.CtThisAccess)2 CtElement (spoon.reflect.declaration.CtElement)2 CtField (spoon.reflect.declaration.CtField)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtNamedElement (spoon.reflect.declaration.CtNamedElement)2 CtParameter (spoon.reflect.declaration.CtParameter)2