use of spoon.reflect.code.CtAssignment in project spoon by INRIA.
the class ParentTest method testGetParentWithFilter.
@Test
public void testGetParentWithFilter() throws Exception {
// addType should set Parent
CtClass<Foo> clazz = (CtClass<Foo>) factory.Class().getAll().get(0);
CtMethod<Object> m = clazz.getMethod("m");
// get three = "" in one = two = three = "";
CtExpression statement = ((CtAssignment) ((CtAssignment) m.getBody().getStatement(3)).getAssignment()).getAssignment();
CtPackage ctPackage = statement.getParent(new TypeFilter<CtPackage>(CtPackage.class));
assertEquals(Foo.class.getPackage().getName(), ctPackage.getQualifiedName());
CtStatement ctStatement = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return element.getParent() instanceof CtStatementList && super.matches(element);
}
});
// the filter has to return one = two = three = ""
assertEquals(m.getBody().getStatement(3), ctStatement);
m = clazz.getMethod("internalClass");
CtStatement ctStatement1 = m.getElements(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return element instanceof CtLocalVariable && super.matches(element);
}
}).get(0);
// get the top class
ctStatement1.getParent(CtType.class);
CtType parent = ctStatement1.getParent(new AbstractFilter<CtType>(CtType.class) {
@Override
public boolean matches(CtType element) {
return !element.isAnonymous() && element.isTopLevel() && super.matches(element);
}
});
assertEquals(clazz, parent);
assertNotEquals(ctStatement1.getParent(CtType.class), parent);
// not present element
CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
assertEquals(null, ctWhile);
CtStatement statementParent = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return true;
}
});
// getParent must not return the current element
assertNotEquals(statement, statementParent);
}
use of spoon.reflect.code.CtAssignment 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