use of spoon.reflect.code.CtFieldWrite in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method isInitializeStaticFinalField.
/**
* Check if the target expression is a static final field initialized in a static anonymous block.
*/
private <T> boolean isInitializeStaticFinalField(CtExpression<T> targetExp) {
final CtElement parent;
final CtAnonymousExecutable anonymousParent;
try {
parent = targetExp.getParent();
anonymousParent = targetExp.getParent(CtAnonymousExecutable.class);
} catch (ParentNotInitializedException e) {
return false;
}
if (parent instanceof CtFieldWrite && targetExp.equals(((CtFieldWrite) parent).getTarget()) && anonymousParent != null && ((CtFieldWrite) parent).getVariable() != null && ((CtFieldWrite) parent).getVariable().getModifiers().contains(ModifierKind.STATIC) && ((CtFieldWrite) parent).getVariable().getModifiers().contains(ModifierKind.FINAL)) {
return true;
}
return false;
}
use of spoon.reflect.code.CtFieldWrite in project spoon by INRIA.
the class FieldAccessTest method testFieldAccessOnUnknownType.
@Test
public void testFieldAccessOnUnknownType() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/FieldAccessRes.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
class CounterScanner extends CtScanner {
private int visited = 0;
@Override
public <T> void visitCtFieldWrite(CtFieldWrite<T> fieldWrite) {
visited++;
assertEquals("array", ((CtVariableWrite) fieldWrite.getTarget()).getVariable().getSimpleName());
assertEquals("length", fieldWrite.getVariable().getSimpleName());
}
}
CounterScanner scanner = new CounterScanner();
launcher.getFactory().Class().get("FieldAccessRes").accept(scanner);
assertEquals(1, scanner.visited);
}
use of spoon.reflect.code.CtFieldWrite 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());
}
Aggregations