Search in sources :

Example 16 with CtScanner

use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.

the class TemplateMatcher method getTemplateTypeParameters.

private List<CtTypeReference<?>> getTemplateTypeParameters(final CtClass<? extends Template<?>> templateType) {
    final List<CtTypeReference<?>> ts = new ArrayList<>();
    final Collection<String> c = Parameters.getNames(templateType);
    new CtScanner() {

        @Override
        public void visitCtTypeParameterReference(CtTypeParameterReference reference) {
            if (c.contains(reference.getSimpleName())) {
                ts.add(reference);
            }
        }

        @Override
        public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
            if (c.contains(reference.getSimpleName())) {
                ts.add(reference);
            }
        }
    }.scan(templateType);
    return ts;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) ArrayList(java.util.ArrayList) CtScanner(spoon.reflect.visitor.CtScanner)

Example 17 with CtScanner

use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.

the class TemplateMatcher method getBindedParameter.

@SuppressWarnings("unused")
private String getBindedParameter(String pname) {
    // HACK! jeje
    final String[] x = new String[1];
    x[0] = pname;
    new CtScanner() {

        @Override
        public <T> void visitCtField(CtField<T> f) {
            Parameter p = f.getAnnotation(Parameter.class);
            if ((p != null) && p.value().equals(x[0])) {
                x[0] = f.getSimpleName();
                return;
            }
            super.visitCtField(f);
        }
    }.scan(templateType);
    return x[0];
}
Also used : CtParameter(spoon.reflect.declaration.CtParameter) CtScanner(spoon.reflect.visitor.CtScanner)

Example 18 with CtScanner

use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.

the class MainTest method checkContractCtScanner.

private void checkContractCtScanner(CtPackage pack) {
    class Counter {

        int scan, enter, exit = 0;
    }
    final Counter counter = new Counter();
    final Counter counterInclNull = new Counter();
    new CtScanner() {

        @Override
        public void scan(CtElement element) {
            counterInclNull.scan++;
            if (element != null) {
                counter.scan++;
            }
            super.scan(element);
        }

        @Override
        public void enter(CtElement element) {
            counter.enter++;
            super.enter(element);
        }

        @Override
        public void exit(CtElement element) {
            counter.exit++;
            super.exit(element);
        }
    }.scan(pack);
    // contract: when enter is called, exit is also called
    assertTrue(counter.enter == counter.exit);
    // contract: all scanned elements call enter
    assertTrue(counter.enter == counter.scan);
    Counter counterBiScan = new Counter();
    class ActualCounterScanner extends CtBiScannerDefault {

        @Override
        public void biScan(CtElement element, CtElement other) {
            counterBiScan.scan++;
            if (element == null) {
                if (other != null) {
                    Assert.fail("element can't be null if other isn't null.");
                }
            } else if (other == null) {
                Assert.fail("other can't be null if element isn't null.");
            } else {
                // contract: all elements have been cloned and are still equal
                assertEquals(element, other);
                assertFalse(element == other);
            }
            super.biScan(element, other);
        }
    }
    final ActualCounterScanner actual = new ActualCounterScanner();
    actual.biScan(pack, pack.clone());
    // contract: scan and biscan are executed the same number of times
    assertEquals(counterInclNull.scan, counterBiScan.scan);
    // for pure beauty: parallel visit of the same tree!
    Counter counterBiScan2 = new Counter();
    new CtBiScannerDefault() {

        @Override
        public void biScan(CtElement element, CtElement other) {
            counterBiScan2.scan++;
            // we have the exact same element
            assertSame(element, other);
            super.biScan(element, other);
        }
    }.biScan(pack, pack);
    // contract: scan and biscan are executed the same number of times
    assertEquals(counterInclNull.scan, counterBiScan2.scan);
}
Also used : CtBiScannerDefault(spoon.reflect.visitor.CtBiScannerDefault) CtElement(spoon.reflect.declaration.CtElement) CtScanner(spoon.reflect.visitor.CtScanner)

Aggregations

CtScanner (spoon.reflect.visitor.CtScanner)18 CtElement (spoon.reflect.declaration.CtElement)9 Test (org.junit.Test)7 Launcher (spoon.Launcher)6 ArrayList (java.util.ArrayList)4 SpoonException (spoon.SpoonException)4 CtType (spoon.reflect.declaration.CtType)3 CtBlock (spoon.reflect.code.CtBlock)2 CtComment (spoon.reflect.code.CtComment)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtClass (spoon.reflect.declaration.CtClass)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 CtField (spoon.reflect.declaration.CtField)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtParameter (spoon.reflect.declaration.CtParameter)2 CtLocalVariableReference (spoon.reflect.reference.CtLocalVariableReference)2 CtTypeReference (spoon.reflect.reference.CtTypeReference)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 URLClassLoader (java.net.URLClassLoader)1 ArrayDeque (java.util.ArrayDeque)1