From 8743b6bb304e4af8a1d58b256139690d5a95c516 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 16 Jun 2015 22:04:16 +0100 Subject: [PATCH] Simplify AnnotatedElementUtils.searchWithFindSemantics() --- .../annotation/AnnotatedElementUtils.java | 154 ++++++------------ 1 file changed, 46 insertions(+), 108 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java index 956d1af67f..427489364f 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java @@ -673,35 +673,8 @@ public class AnnotatedElementUtils { * @since 4.2 */ private static T searchWithFindSemantics(AnnotatedElement element, String annotationName, Processor processor) { - return searchWithFindSemantics(element, annotationName, true, true, true, true, processor); - } - - /** - * Search for annotations of the specified {@code annotationName} on - * the specified {@code element}, following find semantics. - * - * @param element the annotated element; never {@code null} - * @param annotationName the fully qualified class name of the annotation - * type to find; never {@code null} or empty - * @param searchOnInterfaces whether to search on interfaces, if the - * annotated element is a class - * @param searchOnSuperclasses whether to search on superclasses, if - * the annotated element is a class - * @param searchOnMethodsInInterfaces whether to search on methods in - * interfaces, if the annotated element is a method - * @param searchOnMethodsInSuperclasses whether to search on methods - * in superclasses, if the annotated element is a method - * @param processor the processor to delegate to - * @return the result of the processor, potentially {@code null} - * @since 4.2 - */ - private static T searchWithFindSemantics(AnnotatedElement element, String annotationName, - boolean searchOnInterfaces, boolean searchOnSuperclasses, boolean searchOnMethodsInInterfaces, - boolean searchOnMethodsInSuperclasses, Processor processor) { - try { - return searchWithFindSemantics(element, annotationName, searchOnInterfaces, searchOnSuperclasses, - searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, processor, new HashSet(), 0); + return searchWithFindSemantics(element, annotationName, processor, new HashSet(), 0); } catch (Throwable ex) { AnnotationUtils.rethrowAnnotationConfigurationException(ex); @@ -721,14 +694,6 @@ public class AnnotatedElementUtils { * @param element the annotated element; never {@code null} * @param annotationName the fully qualified class name of the annotation * type to find; never {@code null} or empty - * @param searchOnInterfaces whether to search on interfaces, if the - * annotated element is a class - * @param searchOnSuperclasses whether to search on superclasses, if - * the annotated element is a class - * @param searchOnMethodsInInterfaces whether to search on methods in - * interfaces, if the annotated element is a method - * @param searchOnMethodsInSuperclasses whether to search on methods - * in superclasses, if the annotated element is a method * @param processor the processor to delegate to * @param visited the set of annotated elements that have already been visited * @param metaDepth the meta-depth of the annotation @@ -736,18 +701,15 @@ public class AnnotatedElementUtils { * @since 4.2 */ private static T searchWithFindSemantics(AnnotatedElement element, String annotationName, - boolean searchOnInterfaces, boolean searchOnSuperclasses, boolean searchOnMethodsInInterfaces, - boolean searchOnMethodsInSuperclasses, Processor processor, Set visited, int metaDepth) { + Processor processor, Set visited, int metaDepth) { Assert.notNull(element, "AnnotatedElement must not be null"); Assert.hasText(annotationName, "annotationName must not be null or empty"); if (visited.add(element)) { try { - - // Local annotations: declared or (declared + inherited). - Annotation[] annotations = (searchOnSuperclasses ? element.getDeclaredAnnotations() - : element.getAnnotations()); + // Locally declared annotations (ignoring @Inherited) + Annotation[] annotations = element.getDeclaredAnnotations(); // Search in local annotations for (Annotation annotation : annotations) { @@ -763,9 +725,8 @@ public class AnnotatedElementUtils { // Search in meta annotations on local annotations for (Annotation annotation : annotations) { if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) { - T result = searchWithFindSemantics(annotation.annotationType(), annotationName, - searchOnInterfaces, searchOnSuperclasses, searchOnMethodsInInterfaces, - searchOnMethodsInSuperclasses, processor, visited, metaDepth + 1); + T result = searchWithFindSemantics(annotation.annotationType(), annotationName, processor, + visited, metaDepth + 1); if (result != null) { processor.postProcess(annotation.annotationType(), annotation, result); return result; @@ -778,56 +739,45 @@ public class AnnotatedElementUtils { // Search on possibly bridged method Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method); - T result = searchWithFindSemantics(resolvedMethod, annotationName, searchOnInterfaces, - searchOnSuperclasses, searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, processor, - visited, metaDepth); + T result = searchWithFindSemantics(resolvedMethod, annotationName, processor, visited, metaDepth); if (result != null) { return result; } // Search on methods in interfaces declared locally - if (searchOnMethodsInInterfaces) { - Class[] ifcs = method.getDeclaringClass().getInterfaces(); - result = searchOnInterfaces(method, annotationName, searchOnInterfaces, searchOnSuperclasses, - searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, processor, visited, metaDepth, ifcs); - if (result != null) { - return result; - } + Class[] ifcs = method.getDeclaringClass().getInterfaces(); + result = searchOnInterfaces(method, annotationName, processor, visited, metaDepth, ifcs); + if (result != null) { + return result; } // Search on methods in class hierarchy and interface hierarchy - if (searchOnMethodsInSuperclasses) { - Class clazz = method.getDeclaringClass(); - while (true) { - clazz = clazz.getSuperclass(); - if (clazz == null || Object.class == clazz) { - break; - } + Class clazz = method.getDeclaringClass(); + while (true) { + clazz = clazz.getSuperclass(); + if (clazz == null || Object.class == clazz) { + break; + } - try { - Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), - method.getParameterTypes()); - Method resolvedEquivalentMethod = BridgeMethodResolver.findBridgedMethod(equivalentMethod); - result = searchWithFindSemantics(resolvedEquivalentMethod, annotationName, - searchOnInterfaces, searchOnSuperclasses, searchOnMethodsInInterfaces, - searchOnMethodsInSuperclasses, processor, visited, metaDepth); - if (result != null) { - return result; - } - } - catch (NoSuchMethodException ex) { - // No equivalent method found + try { + Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), + method.getParameterTypes()); + Method resolvedEquivalentMethod = BridgeMethodResolver.findBridgedMethod(equivalentMethod); + result = searchWithFindSemantics(resolvedEquivalentMethod, annotationName, processor, + visited, metaDepth); + if (result != null) { + return result; } + } + catch (NoSuchMethodException ex) { + // No equivalent method found + } - // Search on interfaces declared on superclass - if (searchOnMethodsInInterfaces) { - result = searchOnInterfaces(method, annotationName, searchOnInterfaces, - searchOnSuperclasses, searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, - processor, visited, metaDepth, clazz.getInterfaces()); - if (result != null) { - return result; - } - } + // Search on interfaces declared on superclass + result = searchOnInterfaces(method, annotationName, processor, visited, metaDepth, + clazz.getInterfaces()); + if (result != null) { + return result; } } } @@ -836,27 +786,19 @@ public class AnnotatedElementUtils { Class clazz = (Class) element; // Search on interfaces - if (searchOnInterfaces) { - for (Class ifc : clazz.getInterfaces()) { - T result = searchWithFindSemantics(ifc, annotationName, searchOnInterfaces, - searchOnSuperclasses, searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, - processor, visited, metaDepth); - if (result != null) { - return result; - } + for (Class ifc : clazz.getInterfaces()) { + T result = searchWithFindSemantics(ifc, annotationName, processor, visited, metaDepth); + if (result != null) { + return result; } } // Search on superclass - if (searchOnSuperclasses) { - Class superclass = clazz.getSuperclass(); - if (superclass != null && Object.class != superclass) { - T result = searchWithFindSemantics(superclass, annotationName, searchOnInterfaces, - searchOnSuperclasses, searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, - processor, visited, metaDepth); - if (result != null) { - return result; - } + Class superclass = clazz.getSuperclass(); + if (superclass != null && Object.class != superclass) { + T result = searchWithFindSemantics(superclass, annotationName, processor, visited, metaDepth); + if (result != null) { + return result; } } } @@ -871,18 +813,14 @@ public class AnnotatedElementUtils { /** * @since 4.2 */ - private static T searchOnInterfaces(Method method, String annotationName, boolean searchOnInterfaces, - boolean searchOnSuperclasses, boolean searchOnMethodsInInterfaces, boolean searchOnMethodsInSuperclasses, - Processor processor, Set visited, int metaDepth, Class[] ifcs) { + private static T searchOnInterfaces(Method method, String annotationName, Processor processor, + Set visited, int metaDepth, Class[] ifcs) { for (Class iface : ifcs) { if (AnnotationUtils.isInterfaceWithAnnotatedMethods(iface)) { try { Method equivalentMethod = iface.getMethod(method.getName(), method.getParameterTypes()); - T result = searchWithFindSemantics(equivalentMethod, annotationName, searchOnInterfaces, - searchOnSuperclasses, searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses, processor, - visited, metaDepth); - + T result = searchWithFindSemantics(equivalentMethod, annotationName, processor, visited, metaDepth); if (result != null) { return result; }