Search in sources :

Example 86 with CtElement

use of spoon.reflect.declaration.CtElement 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

CtElement (spoon.reflect.declaration.CtElement)86 Test (org.junit.Test)39 Launcher (spoon.Launcher)23 Factory (spoon.reflect.factory.Factory)18 ArrayList (java.util.ArrayList)17 CtMethod (spoon.reflect.declaration.CtMethod)17 CtType (spoon.reflect.declaration.CtType)15 CtStatement (spoon.reflect.code.CtStatement)14 CtTypeReference (spoon.reflect.reference.CtTypeReference)14 List (java.util.List)12 CtClass (spoon.reflect.declaration.CtClass)12 CtField (spoon.reflect.declaration.CtField)12 CtIf (spoon.reflect.code.CtIf)11 CtInvocation (spoon.reflect.code.CtInvocation)11 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)11 CtLocalVariable (spoon.reflect.code.CtLocalVariable)10 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)9 CtScanner (spoon.reflect.visitor.CtScanner)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9 Collection (java.util.Collection)8