Search in sources :

Example 1 with EarlyTerminatingScanner

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();
}
Also used : ArrayList(java.util.ArrayList) EarlyTerminatingScanner(spoon.reflect.visitor.EarlyTerminatingScanner) Annotation(java.lang.annotation.Annotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtConstructor(spoon.reflect.declaration.CtConstructor) CtType(spoon.reflect.declaration.CtType) CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtMethod(spoon.reflect.declaration.CtMethod)

Aggregations

Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 CtAnnotation (spoon.reflect.declaration.CtAnnotation)1 CtAnnotationType (spoon.reflect.declaration.CtAnnotationType)1 CtConstructor (spoon.reflect.declaration.CtConstructor)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtType (spoon.reflect.declaration.CtType)1 CtTypeMember (spoon.reflect.declaration.CtTypeMember)1 EarlyTerminatingScanner (spoon.reflect.visitor.EarlyTerminatingScanner)1