Search in sources :

Example 6 with ScanningMode

use of spoon.reflect.visitor.chain.ScanningMode in project spoon by INRIA.

the class SuperInheritanceHierarchyFunction method visitSuperInterfaces.

/**
 * calls `outputConsumer.accept(interface)` for all superInterfaces of type recursively.
 */
protected void visitSuperInterfaces(CtTypeReference<?> type, CtConsumer<Object> outputConsumer) {
    Set<CtTypeReference<?>> superInterfaces;
    try {
        superInterfaces = type.getSuperInterfaces();
    } catch (SpoonClassNotFoundException e) {
        if (failOnClassNotFound) {
            throw e;
        }
        Launcher.LOGGER.warn("Cannot load class: " + type.getQualifiedName() + " with class loader " + Thread.currentThread().getContextClassLoader());
        return;
    }
    for (CtTypeReference<?> ifaceRef : superInterfaces) {
        ScanningMode mode = enter(ifaceRef, false);
        if (mode == SKIP_ALL) {
            continue;
        }
        sendResult(ifaceRef, outputConsumer);
        if (mode == NORMAL && query.isTerminated() == false) {
            visitSuperInterfaces(ifaceRef, outputConsumer);
        }
        exit(ifaceRef, false);
        if (query.isTerminated()) {
            return;
        }
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) ScanningMode(spoon.reflect.visitor.chain.ScanningMode)

Example 7 with ScanningMode

use of spoon.reflect.visitor.chain.ScanningMode in project spoon by INRIA.

the class ImportTest method testSuperInheritanceHierarchyFunctionListener.

@Test
public void testSuperInheritanceHierarchyFunctionListener() throws Exception {
    CtType<?> clientClass = (CtClass<?>) ModelUtils.buildClass(ClientClass.class);
    CtTypeReference<?> childClass = clientClass.getSuperclass();
    CtTypeReference<?> superClass = childClass.getSuperclass();
    // contract: the enter and exit are always called with CtTypeReference instance
    List<String> result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(true).setListener(new CtScannerListener() {

        @Override
        public ScanningMode enter(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
            return ScanningMode.NORMAL;
        }

        @Override
        public void exit(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
        }
    })).map(e -> {
        assertTrue(e instanceof CtType);
        return ((CtType) e).getQualifiedName();
    }).list();
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertTrue(result.contains(superClass.getQualifiedName()));
    assertTrue(result.contains(Object.class.getName()));
    // contract: if listener skips ALL, then skipped element and all super classes are not returned
    result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(true).setListener(new CtScannerListener() {

        @Override
        public ScanningMode enter(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
            if (superClass.getQualifiedName().equals(((CtTypeReference<?>) element).getQualifiedName())) {
                return ScanningMode.SKIP_ALL;
            }
            return ScanningMode.NORMAL;
        }

        @Override
        public void exit(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
        }
    })).map(e -> {
        assertTrue(e instanceof CtType);
        return ((CtType) e).getQualifiedName();
    }).list();
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertFalse(result.contains(superClass.getQualifiedName()));
    assertFalse(result.contains(Object.class.getName()));
    // contract: if listener skips CHIDLREN, then skipped element is returned but all super classes are not returned
    result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(true).setListener(new CtScannerListener() {

        @Override
        public ScanningMode enter(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
            if (superClass.getQualifiedName().equals(((CtTypeReference<?>) element).getQualifiedName())) {
                return ScanningMode.SKIP_CHILDREN;
            }
            return ScanningMode.NORMAL;
        }

        @Override
        public void exit(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
        }
    })).map(e -> {
        assertTrue(e instanceof CtType);
        return ((CtType) e).getQualifiedName();
    }).list();
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertTrue(result.contains(superClass.getQualifiedName()));
    assertFalse(result.contains(Object.class.getName()));
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) SortedList(spoon.support.util.SortedList) Assert.assertThat(org.junit.Assert.assertThat) CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) Reflection(spoon.test.imports.testclasses.Reflection) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Assert.fail(org.junit.Assert.fail) Query(spoon.reflect.visitor.Query) ClientClass(spoon.test.imports.testclasses.ClientClass) CtThisAccess(spoon.reflect.code.CtThisAccess) CtImport(spoon.reflect.declaration.CtImport) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtField(spoon.reflect.declaration.CtField) CtReference(spoon.reflect.reference.CtReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SpoonResourceHelper(spoon.compiler.SpoonResourceHelper) SpoonException(spoon.SpoonException) Collection(java.util.Collection) ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) Set(java.util.Set) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) IOUtils(org.apache.commons.io.IOUtils) NotImportExecutableType(spoon.test.imports.testclasses.NotImportExecutableType) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CompilationUnit(spoon.reflect.cu.CompilationUnit) CtStatement(spoon.reflect.code.CtStatement) ArrayList(java.util.ArrayList) SpoonModelBuilder(spoon.SpoonModelBuilder) ClassWithInvocation(spoon.test.imports.testclasses.ClassWithInvocation) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) ChildClass(spoon.test.imports.testclasses.internal.ChildClass) ModelUtils.canBeBuilt(spoon.testing.utils.ModelUtils.canBeBuilt) StringTokenizer(java.util.StringTokenizer) SpoonResource(spoon.compiler.SpoonResource) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) A(spoon.test.imports.testclasses.A) ImportScanner(spoon.reflect.visitor.ImportScanner) CtInvocation(spoon.reflect.code.CtInvocation) CtImportKind(spoon.reflect.declaration.CtImportKind) Assert.assertNotNull(org.junit.Assert.assertNotNull) SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Factory(spoon.reflect.factory.Factory) Mole(spoon.test.imports.testclasses.Mole) Tacos(spoon.test.imports.testclasses.Tacos) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Pozole(spoon.test.imports.testclasses.Pozole) CtTypeReference(spoon.reflect.reference.CtTypeReference) StaticNoOrdered(spoon.test.imports.testclasses.StaticNoOrdered) Assert.assertNull(org.junit.Assert.assertNull) SubClass(spoon.test.imports.testclasses.SubClass) CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtModel(spoon.reflect.CtModel) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) FileReader(java.io.FileReader) Assert.assertEquals(org.junit.Assert.assertEquals) SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) CtElement(spoon.reflect.declaration.CtElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClientClass(spoon.test.imports.testclasses.ClientClass) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Test(org.junit.Test)

Aggregations

ScanningMode (spoon.reflect.visitor.chain.ScanningMode)7 CtType (spoon.reflect.declaration.CtType)4 CtElement (spoon.reflect.declaration.CtElement)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 CtScannerListener (spoon.reflect.visitor.chain.CtScannerListener)3 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 Launcher (spoon.Launcher)2 SpoonException (spoon.SpoonException)2 CtClass (spoon.reflect.declaration.CtClass)2 SpoonClassNotFoundException (spoon.support.SpoonClassNotFoundException)2 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1