Search in sources :

Example 1 with CtFieldWrite

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;
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtElement(spoon.reflect.declaration.CtElement) CtFieldWrite(spoon.reflect.code.CtFieldWrite) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable)

Example 2 with CtFieldWrite

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);
}
Also used : CtVariableWrite(spoon.reflect.code.CtVariableWrite) CtFieldWrite(spoon.reflect.code.CtFieldWrite) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 3 with CtFieldWrite

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

Aggregations

CtFieldWrite (spoon.reflect.code.CtFieldWrite)3 Test (org.junit.Test)2 Launcher (spoon.Launcher)1 CtFieldAccess (spoon.reflect.code.CtFieldAccess)1 CtFieldRead (spoon.reflect.code.CtFieldRead)1 CtVariableWrite (spoon.reflect.code.CtVariableWrite)1 CtAnonymousExecutable (spoon.reflect.declaration.CtAnonymousExecutable)1 CtElement (spoon.reflect.declaration.CtElement)1 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)1 Factory (spoon.reflect.factory.Factory)1 CtScanner (spoon.reflect.visitor.CtScanner)1 MainTest (spoon.test.main.MainTest)1 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)1