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