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());
}
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));
}
Aggregations