Search in sources :

Example 16 with ClassTypingContext

use of spoon.support.visitor.ClassTypingContext in project spoon by INRIA.

the class CtTypeImpl method getAllMethods.

@Override
public Set<CtMethod<?>> getAllMethods() {
    final Set<CtMethod<?>> l = new HashSet<>();
    final ClassTypingContext ctc = new ClassTypingContext(this);
    map(new AllTypeMembersFunction(CtMethod.class)).forEach(new CtConsumer<CtMethod<?>>() {

        @Override
        public void accept(CtMethod<?> currentMethod) {
            for (CtMethod<?> alreadyVisitedMethod : l) {
                if (ctc.isSameSignature(currentMethod, alreadyVisitedMethod)) {
                    return;
                }
            }
            l.add(currentMethod);
        }
    });
    return Collections.unmodifiableSet(l);
}
Also used : ClassTypingContext(spoon.support.visitor.ClassTypingContext) AllTypeMembersFunction(spoon.reflect.visitor.filter.AllTypeMembersFunction) CtMethod(spoon.reflect.declaration.CtMethod) HashSet(java.util.HashSet)

Example 17 with ClassTypingContext

use of spoon.support.visitor.ClassTypingContext in project spoon by INRIA.

the class SpoonMetaModel method getInheritedAnnotation.

/**
 * @param method a start method
 * @param annotationType a searched annotation type
 * @return annotation from the first method in superClass and superInterface hierarchy for the method with required annotationType
 */
private static <A extends Annotation> CtAnnotation<A> getInheritedAnnotation(CtMethod<?> method, CtTypeReference<A> annotationType) {
    CtAnnotation<A> annotation = method.getAnnotation(annotationType);
    if (annotation == null) {
        CtType<?> declType = method.getDeclaringType();
        final ClassTypingContext ctc = new ClassTypingContext(declType);
        annotation = declType.map(new AllTypeMembersFunction(CtMethod.class)).map((CtMethod<?> currentMethod) -> {
            if (method == currentMethod) {
                return null;
            }
            if (ctc.isSameSignature(method, currentMethod)) {
                CtAnnotation<A> annotation2 = currentMethod.getAnnotation(annotationType);
                if (annotation2 != null) {
                    return annotation2;
                }
            }
            return null;
        }).first();
    }
    return annotation;
}
Also used : ClassTypingContext(spoon.support.visitor.ClassTypingContext) AllTypeMembersFunction(spoon.reflect.visitor.filter.AllTypeMembersFunction) CtMethod(spoon.reflect.declaration.CtMethod)

Aggregations

ClassTypingContext (spoon.support.visitor.ClassTypingContext)17 Test (org.junit.Test)11 MainTest (spoon.test.main.MainTest)11 CtMethod (spoon.reflect.declaration.CtMethod)10 CtClass (spoon.reflect.declaration.CtClass)6 LikeCtClass (spoon.test.generics.testclasses2.LikeCtClass)5 Launcher (spoon.Launcher)4 File (java.io.File)3 Factory (spoon.reflect.factory.Factory)3 AllTypeMembersFunction (spoon.reflect.visitor.filter.AllTypeMembersFunction)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 MethodTypingContext (spoon.support.visitor.MethodTypingContext)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CtTypeMember (spoon.reflect.declaration.CtTypeMember)2 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)2 CtLambda (spoon.reflect.code.CtLambda)1 CtNewClass (spoon.reflect.code.CtNewClass)1 CtReturn (spoon.reflect.code.CtReturn)1