use of spoon.reflect.visitor.EarlyTerminatingScanner in project spoon by INRIA.
the class CtTypeImpl method getNestedType.
@Override
@SuppressWarnings("unchecked")
public <N extends CtType<?>> N getNestedType(final String name) {
class NestedTypeScanner extends EarlyTerminatingScanner<CtType<?>> {
private boolean checkType(CtType<?> type) {
if (type.getSimpleName().equals(name) && CtTypeImpl.this.equals(type.getDeclaringType())) {
setResult(type);
terminate();
return true;
}
return false;
}
@Override
public <U> void visitCtClass(spoon.reflect.declaration.CtClass<U> ctClass) {
if (!checkType(ctClass)) {
final List<CtTypeMember> typeMembers = new ArrayList<>();
for (CtTypeMember typeMember : ctClass.getTypeMembers()) {
if (typeMember instanceof CtType || typeMember instanceof CtConstructor || typeMember instanceof CtMethod) {
typeMembers.add(typeMember);
}
}
scan(typeMembers);
}
}
@Override
public <U> void visitCtInterface(spoon.reflect.declaration.CtInterface<U> intrface) {
if (!checkType(intrface)) {
final List<CtTypeMember> typeMembers = new ArrayList<>();
for (CtTypeMember typeMember : intrface.getTypeMembers()) {
if (typeMember instanceof CtType || typeMember instanceof CtMethod) {
typeMembers.add(typeMember);
}
}
scan(typeMembers);
}
}
@Override
public <U extends java.lang.Enum<?>> void visitCtEnum(spoon.reflect.declaration.CtEnum<U> ctEnum) {
if (!checkType(ctEnum)) {
final List<CtTypeMember> typeMembers = new ArrayList<>();
for (CtTypeMember typeMember : ctEnum.getTypeMembers()) {
if (typeMember instanceof CtType || typeMember instanceof CtConstructor || typeMember instanceof CtMethod) {
typeMembers.add(typeMember);
}
}
scan(typeMembers);
}
}
@Override
public <A extends Annotation> void visitCtAnnotationType(CtAnnotationType<A> annotationType) {
if (!checkType(annotationType)) {
scan(annotationType.getNestedTypes());
}
}
}
NestedTypeScanner scanner = new NestedTypeScanner();
scanner.scan(this);
return (N) scanner.getResult();
}
Aggregations