Repository navigation
Expand file tree
/
Copy pathAnnotationUtils.java
More file actions
256 lines (241 loc) · 9.48 KB
/
Copy pathAnnotationUtils.java
File metadata and controls
256 lines (241 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.appulse.utils;
import static java.util.Optional.ofNullable;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
/**
* Different annotation helpers.
*
* @since 1.7.0
* @author Artem Labazin
*/
public final class AnnotationUtils {
/**
* Find a single {@link Annotation} of {@code annotationType} on the
* supplied {@link Class}, traversing its interfaces, annotations, and
* superclasses if the annotation is not <em>directly present</em> on
* the given class itself.
* <p>
* This method explicitly handles class-level annotations which are not
* declared as {@link java.lang.annotation.Inherited inherited} <em>as well
* as meta-annotations and annotations on interfaces</em>.
* <p>
* The algorithm operates as follows:
* <ol>
* <li>Search for the annotation on the given class and return it if found.
* <li>Recursively search through all annotations that the given class declares.
* <li>Recursively search through all interfaces that the given class declares.
* <li>Recursively search through the superclass hierarchy of the given class.
* </ol>
* <p>
* Note: in this context, the term <em>recursively</em> means that the search
* process continues by returning to step #1 with the current interface,
* annotation, or superclass as the class to look for annotations on.
*
* @param clazz the class to look for annotations on
*
* @param annotationType the annotation type to look for, both locally and as a meta-annotation
*
* @param <T> the annotation type.
*
* @return the first matching annotation
*
* @since 1.7.0
*/
public static <T extends Annotation> Optional<T> findAnnotation (Class<?> clazz, Class<T> annotationType) {
T result = findAnnotation(clazz, annotationType, new HashSet<>());
return ofNullable(result);
}
/**
* Find a single {@link Annotation} of {@code annotationType} on the
* supplied {@link AnnotatedElement}.
* <p>
* Meta-annotations will be searched if the annotation is not
* <em>directly present</em> on the supplied element.
* <p>
* <strong>Warning</strong>: this method operates generically on
* annotated elements. In other words, this method does not execute
* specialized search algorithms for classes or methods. If you require
* the more specific semantics of {@link #findAnnotation(Class, Class)}
* or {@link #findAnnotation(Method, Class)}, invoke one of those methods
* instead.
*
* @param annotatedElement the {@code AnnotatedElement} on which to find the annotation
*
* @param annotationType the annotation type to look for, both locally and as a meta-annotation
*
* @param <T> the annotation type.
*
* @return the first matching annotation
*
* @since 1.7.0
*/
public static <T extends Annotation> Optional<T> findAnnotation (AnnotatedElement annotatedElement,
Class<T> annotationType
) {
T result = findAnnotation(annotatedElement, annotationType, new HashSet<>());
return ofNullable(result);
}
/**
* Find a single {@link Annotation} of {@code annotationType} on the supplied
* {@link Method}, traversing its super methods (i.e., from superclasses and
* interfaces) if the annotation is not <em>directly present</em> on the given
* method itself.
* <p>
* Correctly handles bridge {@link Method Methods} generated by the compiler.
* <p>
* Meta-annotations will be searched if the annotation is not
* <em>directly present</em> on the method.
* <p>
* Annotations on methods are not inherited by default, so we need to handle
* this explicitly.
*
* @param method the method to look for annotations on
*
* @param annotationType the annotation type to look for
*
* @param <T> the annotation type.
*
* @return the first matching annotation
*
* @since 1.7.0
*/
public static <T extends Annotation> Optional<T> findAnnotation (Method method, Class<T> annotationType) {
T result = findAnnotation(method, annotationType, new HashSet<>());
return ofNullable(result);
}
/**
* Determine if the supplied {@link Annotation} is defined in the core JDK {@code java.lang.annotation} package.
*
* @param annotation the annotation to check
*
* @return {@code true} if the annotation is in the {@code java.lang.annotation} package
*
* @since 1.7.0
*/
public static boolean isInJavaLangAnnotationPackage (Annotation annotation) {
return annotation != null && isInJavaLangAnnotationPackage(annotation.annotationType());
}
/**
* Determine if the {@link Annotation} with the supplied name is defined in
* the core JDK {@code java.lang.annotation} package.
*
* @param annotationType the annotation type to check
*
* @return {@code true} if the annotation is in the {@code java.lang.annotation} package
*
* @since 1.7.0
*/
public static boolean isInJavaLangAnnotationPackage (Class<? extends Annotation> annotationType) {
return annotationType != null && isInJavaLangAnnotationPackage(annotationType.getName());
}
/**
* Determine if the {@link Annotation} with the supplied name is defined in
* the core JDK {@code java.lang.annotation} package.
*
* @param annotationType the name of the annotation type to check
*
* @return {@code true} if the annotation is in the {@code java.lang.annotation} package
*
* @since 1.7.0
*/
public static boolean isInJavaLangAnnotationPackage (String annotationType) {
return annotationType != null && annotationType.startsWith("java.lang.annotation");
}
private static <T extends Annotation> T findAnnotation (Method method,
Class<T> annotationType,
Set<Annotation> visited
) {
T result = findAnnotation((AnnotatedElement) method, annotationType, visited);
if (result != null) {
return result;
}
Class<?> declaringClass = method.getDeclaringClass();
return ofNullable(declaringClass.getSuperclass())
.filter(it -> it != Object.class)
.map(it -> findAnnotation(it, method, annotationType, visited))
.filter(Objects::nonNull)
.orElseGet(() -> Stream.of(declaringClass.getInterfaces())
.map(it -> findAnnotation(it, method, annotationType, visited))
.filter(Objects::nonNull)
.findAny()
.orElse(null));
}
private static <T extends Annotation> T findAnnotation (Class<?> clazz,
Method method,
Class<T> annotationType,
Set<Annotation> visited
) {
return Stream.of(clazz.getDeclaredMethods())
.filter(it -> method.getName().equals(it.getName()))
.filter(it -> Arrays.equals(method.getParameterTypes(), it.getParameterTypes()))
.findAny()
.map(it -> findAnnotation(it, annotationType, visited))
.orElse(null);
}
private static <T extends Annotation> T findAnnotation (Class<?> clazz,
Class<T> annotationType,
Set<Annotation> visited
) {
T result = findAnnotation((AnnotatedElement) clazz, annotationType, visited);
if (result != null) {
return result;
}
for (Class<?> type : clazz.getInterfaces()) {
T annotation = findAnnotation(type, annotationType, visited);
if (annotation != null) {
return annotation;
}
}
Class<?> superclass = clazz.getSuperclass();
if (superclass == null || Object.class == superclass) {
return null;
}
return findAnnotation(superclass, annotationType, visited);
}
@SuppressWarnings("unchecked")
private static <T extends Annotation> T findAnnotation (AnnotatedElement annotatedElement,
Class<T> annotationType,
Set<Annotation> visited
) {
Annotation[] declaredAnnotations = annotatedElement.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (annotation.annotationType() == annotationType) {
return (T) annotation;
}
}
for (Annotation annotation : declaredAnnotations) {
if (isInJavaLangAnnotationPackage(annotation) || !visited.add(annotation)) {
continue;
}
T result = findAnnotation(annotation.annotationType(), annotationType, visited);
if (result != null) {
return result;
}
}
return null;
}
private AnnotationUtils () {
}
}