Search in sources :

Example 1 with CtVariableWrite

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

the class AccessTest method testCanVisitVariableAccessAndSubClasses.

@Test
public void testCanVisitVariableAccessAndSubClasses() throws Exception {
    final Factory factory = build(VariableAccessSample.class);
    final List<CtVariableRead<?>> variablesRead = Query.getElements(factory, new AbstractFilter<CtVariableRead<?>>(CtVariableRead.class) {

        @Override
        public boolean matches(CtVariableRead<?> element) {
            return super.matches(element);
        }
    });
    // System.out and s
    assertEquals(2, variablesRead.size());
    final List<CtVariableWrite<?>> variablesWrite = Query.getElements(factory, new AbstractFilter<CtVariableWrite<?>>(CtVariableWrite.class) {

        @Override
        public boolean matches(CtVariableWrite<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(1, variablesWrite.size());
    final List<CtVariableAccess<?>> variablesAccess = Query.getElements(factory, new AbstractFilter<CtVariableAccess<?>>(CtVariableAccess.class) {

        @Override
        public boolean matches(CtVariableAccess<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(3, variablesAccess.size());
}
Also used : CtVariableWrite(spoon.reflect.code.CtVariableWrite) CtVariableAccess(spoon.reflect.code.CtVariableAccess) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtVariableRead(spoon.reflect.code.CtVariableRead) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 2 with CtVariableWrite

use of spoon.reflect.code.CtVariableWrite 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)

Aggregations

Test (org.junit.Test)2 CtVariableWrite (spoon.reflect.code.CtVariableWrite)2 Launcher (spoon.Launcher)1 CtFieldWrite (spoon.reflect.code.CtFieldWrite)1 CtVariableAccess (spoon.reflect.code.CtVariableAccess)1 CtVariableRead (spoon.reflect.code.CtVariableRead)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