@@ -1050,17 +1050,48 @@ private static <T> T searchWithFindSemantics(AnnotatedElement element,
1050
1050
try {
1051
1051
// Locally declared annotations (ignoring @Inherited)
1052
1052
Annotation [] annotations = element .getDeclaredAnnotations ();
1053
- List <T > aggregatedResults = (processor .aggregates () ? new ArrayList <>() : null );
1054
-
1055
- // Search in local annotations
1056
- for (Annotation annotation : annotations ) {
1057
- Class <? extends Annotation > currentAnnotationType = annotation .annotationType ();
1058
- if (!AnnotationUtils .isInJavaLangAnnotationPackage (currentAnnotationType )) {
1059
- if (currentAnnotationType == annotationType ||
1060
- currentAnnotationType .getName ().equals (annotationName ) ||
1061
- processor .alwaysProcesses ()) {
1062
- T result = processor .process (element , annotation , metaDepth );
1053
+ if (annotations .length > 0 ) {
1054
+ List <T > aggregatedResults = (processor .aggregates () ? new ArrayList <>() : null );
1055
+
1056
+ // Search in local annotations
1057
+ for (Annotation annotation : annotations ) {
1058
+ Class <? extends Annotation > currentAnnotationType = annotation .annotationType ();
1059
+ if (!AnnotationUtils .isInJavaLangAnnotationPackage (currentAnnotationType )) {
1060
+ if (currentAnnotationType == annotationType ||
1061
+ currentAnnotationType .getName ().equals (annotationName ) ||
1062
+ processor .alwaysProcesses ()) {
1063
+ T result = processor .process (element , annotation , metaDepth );
1064
+ if (result != null ) {
1065
+ if (aggregatedResults != null && metaDepth == 0 ) {
1066
+ aggregatedResults .add (result );
1067
+ }
1068
+ else {
1069
+ return result ;
1070
+ }
1071
+ }
1072
+ }
1073
+ // Repeatable annotations in container?
1074
+ else if (currentAnnotationType == containerType ) {
1075
+ for (Annotation contained : getRawAnnotationsFromContainer (element , annotation )) {
1076
+ T result = processor .process (element , contained , metaDepth );
1077
+ if (aggregatedResults != null && result != null ) {
1078
+ // No need to post-process since repeatable annotations within a
1079
+ // container cannot be composed annotations.
1080
+ aggregatedResults .add (result );
1081
+ }
1082
+ }
1083
+ }
1084
+ }
1085
+ }
1086
+
1087
+ // Recursively search in meta-annotations
1088
+ for (Annotation annotation : annotations ) {
1089
+ Class <? extends Annotation > currentAnnotationType = annotation .annotationType ();
1090
+ if (hasSearchableMetaAnnotations (currentAnnotationType , annotationType , annotationName )) {
1091
+ T result = searchWithFindSemantics (currentAnnotationType , annotationType , annotationName ,
1092
+ containerType , processor , visited , metaDepth + 1 );
1063
1093
if (result != null ) {
1094
+ processor .postProcess (currentAnnotationType , annotation , result );
1064
1095
if (aggregatedResults != null && metaDepth == 0 ) {
1065
1096
aggregatedResults .add (result );
1066
1097
}
@@ -1069,43 +1100,14 @@ private static <T> T searchWithFindSemantics(AnnotatedElement element,
1069
1100
}
1070
1101
}
1071
1102
}
1072
- // Repeatable annotations in container?
1073
- else if (currentAnnotationType == containerType ) {
1074
- for (Annotation contained : getRawAnnotationsFromContainer (element , annotation )) {
1075
- T result = processor .process (element , contained , metaDepth );
1076
- if (aggregatedResults != null && result != null ) {
1077
- // No need to post-process since repeatable annotations within a
1078
- // container cannot be composed annotations.
1079
- aggregatedResults .add (result );
1080
- }
1081
- }
1082
- }
1083
1103
}
1084
- }
1085
1104
1086
- // Recursively search in meta-annotations
1087
- for (Annotation annotation : annotations ) {
1088
- Class <? extends Annotation > currentAnnotationType = annotation .annotationType ();
1089
- if (hasSearchableMetaAnnotations (currentAnnotationType , annotationType , annotationName )) {
1090
- T result = searchWithFindSemantics (currentAnnotationType , annotationType , annotationName ,
1091
- containerType , processor , visited , metaDepth + 1 );
1092
- if (result != null ) {
1093
- processor .postProcess (currentAnnotationType , annotation , result );
1094
- if (aggregatedResults != null && metaDepth == 0 ) {
1095
- aggregatedResults .add (result );
1096
- }
1097
- else {
1098
- return result ;
1099
- }
1100
- }
1105
+ if (!CollectionUtils .isEmpty (aggregatedResults )) {
1106
+ // Prepend to support top-down ordering within class hierarchies
1107
+ processor .getAggregatedResults ().addAll (0 , aggregatedResults );
1101
1108
}
1102
1109
}
1103
1110
1104
- if (!CollectionUtils .isEmpty (aggregatedResults )) {
1105
- // Prepend to support top-down ordering within class hierarchies
1106
- processor .getAggregatedResults ().addAll (0 , aggregatedResults );
1107
- }
1108
-
1109
1111
if (element instanceof Method ) {
1110
1112
Method method = (Method ) element ;
1111
1113
T result ;
@@ -1123,8 +1125,8 @@ else if (currentAnnotationType == containerType) {
1123
1125
// Search on methods in interfaces declared locally
1124
1126
Class <?>[] ifcs = method .getDeclaringClass ().getInterfaces ();
1125
1127
if (ifcs .length > 0 ) {
1126
- result = searchOnInterfaces (method , annotationType , annotationName , containerType ,
1127
- processor , visited , metaDepth , ifcs );
1128
+ result = searchOnInterfaces (method , annotationType , annotationName ,
1129
+ containerType , processor , visited , metaDepth , ifcs );
1128
1130
if (result != null ) {
1129
1131
return result ;
1130
1132
}
@@ -1137,23 +1139,23 @@ else if (currentAnnotationType == containerType) {
1137
1139
if (clazz == null || Object .class == clazz ) {
1138
1140
break ;
1139
1141
}
1140
-
1141
- try {
1142
- Method equivalentMethod = clazz .getDeclaredMethod (method .getName (), method .getParameterTypes ());
1143
- Method resolvedEquivalentMethod = BridgeMethodResolver .findBridgedMethod (equivalentMethod );
1144
- result = searchWithFindSemantics (resolvedEquivalentMethod , annotationType , annotationName ,
1145
- containerType , processor , visited , metaDepth );
1146
- if (result != null ) {
1147
- return result ;
1142
+ Set <Method > annotatedMethods = AnnotationUtils .getAnnotatedMethodsInBaseType (clazz );
1143
+ if (!annotatedMethods .isEmpty ()) {
1144
+ for (Method annotatedMethod : annotatedMethods ) {
1145
+ if (annotatedMethod .getName ().equals (method .getName ()) &&
1146
+ Arrays .equals (annotatedMethod .getParameterTypes (), method .getParameterTypes ())) {
1147
+ Method resolvedSuperMethod = BridgeMethodResolver .findBridgedMethod (annotatedMethod );
1148
+ result = searchWithFindSemantics (resolvedSuperMethod , annotationType , annotationName ,
1149
+ containerType , processor , visited , metaDepth );
1150
+ if (result != null ) {
1151
+ return result ;
1152
+ }
1153
+ }
1148
1154
}
1149
1155
}
1150
- catch (NoSuchMethodException ex ) {
1151
- // No equivalent method found
1152
- }
1153
-
1154
1156
// Search on interfaces declared on superclass
1155
- result = searchOnInterfaces (method , annotationType , annotationName , containerType , processor ,
1156
- visited , metaDepth , clazz .getInterfaces ());
1157
+ result = searchOnInterfaces (method , annotationType , annotationName ,
1158
+ containerType , processor , visited , metaDepth , clazz .getInterfaces ());
1157
1159
if (result != null ) {
1158
1160
return result ;
1159
1161
}
@@ -1164,8 +1166,8 @@ else if (element instanceof Class) {
1164
1166
1165
1167
// Search on interfaces
1166
1168
for (Class <?> ifc : clazz .getInterfaces ()) {
1167
- T result = searchWithFindSemantics (ifc , annotationType , annotationName , containerType ,
1168
- processor , visited , metaDepth );
1169
+ T result = searchWithFindSemantics (ifc , annotationType , annotationName ,
1170
+ containerType , processor , visited , metaDepth );
1169
1171
if (result != null ) {
1170
1172
return result ;
1171
1173
}
@@ -1174,8 +1176,8 @@ else if (element instanceof Class) {
1174
1176
// Search on superclass
1175
1177
Class <?> superclass = clazz .getSuperclass ();
1176
1178
if (superclass != null && Object .class != superclass ) {
1177
- T result = searchWithFindSemantics (superclass , annotationType , annotationName , containerType ,
1178
- processor , visited , metaDepth );
1179
+ T result = searchWithFindSemantics (superclass , annotationType , annotationName ,
1180
+ containerType , processor , visited , metaDepth );
1179
1181
if (result != null ) {
1180
1182
return result ;
1181
1183
}
@@ -1195,18 +1197,18 @@ private static <T> T searchOnInterfaces(Method method, @Nullable Class<? extends
1195
1197
Processor <T > processor , Set <AnnotatedElement > visited , int metaDepth , Class <?>[] ifcs ) {
1196
1198
1197
1199
for (Class <?> ifc : ifcs ) {
1198
- if (AnnotationUtils .isInterfaceWithAnnotatedMethods (ifc )) {
1199
- try {
1200
- Method equivalentMethod = ifc .getMethod (method .getName (), method .getParameterTypes ());
1201
- T result = searchWithFindSemantics (equivalentMethod , annotationType , annotationName , containerType ,
1202
- processor , visited , metaDepth );
1203
- if (result != null ) {
1204
- return result ;
1200
+ Set <Method > annotatedMethods = AnnotationUtils .getAnnotatedMethodsInBaseType (ifc );
1201
+ if (!annotatedMethods .isEmpty ()) {
1202
+ for (Method annotatedMethod : annotatedMethods ) {
1203
+ if (annotatedMethod .getName ().equals (method .getName ()) &&
1204
+ Arrays .equals (annotatedMethod .getParameterTypes (), method .getParameterTypes ())) {
1205
+ T result = searchWithFindSemantics (annotatedMethod , annotationType , annotationName ,
1206
+ containerType , processor , visited , metaDepth );
1207
+ if (result != null ) {
1208
+ return result ;
1209
+ }
1205
1210
}
1206
1211
}
1207
- catch (NoSuchMethodException ex ) {
1208
- // Skip this interface - it doesn't have the method...
1209
- }
1210
1212
}
1211
1213
}
1212
1214
0 commit comments