AnnotationUtils.java 29.7 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2014 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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 org.springframework.core.annotation;

import java.lang.annotation.Annotation;
20
import java.lang.reflect.AnnotatedElement;
A
Arjen Poutsma 已提交
21
import java.lang.reflect.Method;
22 23 24 25
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
26
import java.util.List;
A
Arjen Poutsma 已提交
27
import java.util.Map;
28
import java.util.Set;
29
import java.util.WeakHashMap;
A
Arjen Poutsma 已提交
30 31 32

import org.springframework.core.BridgeMethodResolver;
import org.springframework.util.Assert;
33
import org.springframework.util.ObjectUtils;
34
import org.springframework.util.ReflectionUtils;
A
Arjen Poutsma 已提交
35 36

/**
S
Sam Brannen 已提交
37 38 39 40
 * General utility methods for working with annotations, handling bridge methods
 * (which the compiler generates for generic declarations) as well as super methods
 * (for optional "annotation inheritance"). Note that none of this is
 * provided by the JDK's introspection facilities themselves.
A
Arjen Poutsma 已提交
41
 *
S
Sam Brannen 已提交
42 43 44 45 46 47 48 49
 * <p>As a general rule for runtime-retained annotations (e.g. for transaction
 * control, authorization, or service exposure), always use the lookup methods
 * on this class (e.g., {@link #findAnnotation(Method, Class)},
 * {@link #getAnnotation(Method, Class)}, and {@link #getAnnotations(Method)})
 * instead of the plain annotation lookup methods in the JDK. You can still
 * explicitly choose between a <em>get</em> lookup on the given class level only
 * ({@link #getAnnotation(Method, Class)}) and a <em>find</em> lookup in the entire
 * inheritance hierarchy of the given method ({@link #findAnnotation(Method, Class)}).
A
Arjen Poutsma 已提交
50 51 52 53 54
 *
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @author Mark Fisher
C
Chris Beams 已提交
55
 * @author Chris Beams
56
 * @author Phillip Webb
A
Arjen Poutsma 已提交
57 58 59 60 61 62 63
 * @since 2.0
 * @see java.lang.reflect.Method#getAnnotations()
 * @see java.lang.reflect.Method#getAnnotation(Class)
 */
public abstract class AnnotationUtils {

	/** The attribute name for annotations with a single element */
64
	public static final String VALUE = "value";
A
Arjen Poutsma 已提交
65

66
	private static final Map<Class<?>, Boolean> annotatedInterfaceCache = new WeakHashMap<Class<?>, Boolean>();
67

J
Juergen Hoeller 已提交
68

69 70 71 72
	/**
	 * Get a single {@link Annotation} of {@code annotationType} from the supplied
	 * annotation: either the given annotation itself or a meta-annotation thereof.
	 * @param ann the Annotation to check
J
Juergen Hoeller 已提交
73 74
	 * @param annotationType the annotation type to look for, both locally and as a meta-annotation
	 * @return the matching annotation, or {@code null} if none found
75 76 77 78 79 80 81
	 * @since 4.0
	 */
	@SuppressWarnings("unchecked")
	public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> annotationType) {
		if (annotationType.isInstance(ann)) {
			return (T) ann;
		}
82 83 84 85 86 87 88 89
		try {
			return ann.annotationType().getAnnotation(annotationType);
		}
		catch (Exception ex) {
			// Assuming nested Class values not resolvable within annotation attributes...
			// We're probably hitting a non-present optional arrangement - let's back out.
			return null;
		}
90 91
	}

92
	/**
C
Chris Beams 已提交
93 94 95 96
	 * Get a single {@link Annotation} of {@code annotationType} from the supplied
	 * Method, Constructor or Field. Meta-annotations will be searched if the annotation
	 * is not declared locally on the supplied element.
	 * @param ae the Method, Constructor or Field from which to get the annotation
J
Juergen Hoeller 已提交
97 98
	 * @param annotationType the annotation type to look for, both locally and as a meta-annotation
	 * @return the matching annotation, or {@code null} if none found
C
Chris Beams 已提交
99
	 * @since 3.1
100 101
	 */
	public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<T> annotationType) {
102 103 104 105 106 107 108 109
		try {
			T ann = ae.getAnnotation(annotationType);
			if (ann == null) {
				for (Annotation metaAnn : ae.getAnnotations()) {
					ann = metaAnn.annotationType().getAnnotation(annotationType);
					if (ann != null) {
						break;
					}
110 111
				}
			}
112 113 114 115 116 117
			return ann;
		}
		catch (Exception ex) {
			// Assuming nested Class values not resolvable within annotation attributes...
			// We're probably hitting a non-present optional arrangement - let's back out.
			return null;
118 119 120
		}
	}

A
Arjen Poutsma 已提交
121 122 123 124 125 126 127 128
	/**
	 * Get all {@link Annotation Annotations} from the supplied {@link Method}.
	 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
	 * @param method the method to look for annotations on
	 * @return the annotations found
	 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
	 */
	public static Annotation[] getAnnotations(Method method) {
129 130 131 132 133 134 135 136
		try {
			return BridgeMethodResolver.findBridgedMethod(method).getAnnotations();
		}
		catch (Exception ex) {
			// Assuming nested Class values not resolvable within annotation attributes...
			// We're probably hitting a non-present optional arrangement - let's back out.
			return null;
		}
A
Arjen Poutsma 已提交
137 138 139
	}

	/**
140
	 * Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method}.
A
Arjen Poutsma 已提交
141 142
	 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
	 * @param method the method to look for annotations on
J
Juergen Hoeller 已提交
143
	 * @param annotationType the annotation type to look for
A
Arjen Poutsma 已提交
144 145 146 147
	 * @return the annotations found
	 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
	 */
	public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
148
		Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
149
		return getAnnotation((AnnotatedElement) resolvedMethod, annotationType);
A
Arjen Poutsma 已提交
150 151
	}

152 153 154 155 156 157 158
	/**
	 * Get the possibly repeating {@link Annotation}s of {@code annotationType} from the
	 * supplied {@link Method}. Deals with both a single direct annotation and repeating
	 * annotations nested within a containing annotation.
	 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
	 * @param method the method to look for annotations on
	 * @param containerAnnotationType the class of the container that holds the annotations
J
Juergen Hoeller 已提交
159
	 * @param annotationType the annotation type to look for
160 161
	 * @return the annotations found
	 * @since 4.0
J
Juergen Hoeller 已提交
162
	 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
163 164 165
	 */
	public static <A extends Annotation> Set<A> getRepeatableAnnotation(Method method,
			Class<? extends Annotation> containerAnnotationType, Class<A> annotationType) {
J
Juergen Hoeller 已提交
166

167
		Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
J
Juergen Hoeller 已提交
168
		return getRepeatableAnnotation((AnnotatedElement) resolvedMethod, containerAnnotationType, annotationType);
169 170 171 172 173 174 175 176 177
	}

	/**
	 * Get the possibly repeating {@link Annotation}s of {@code annotationType} from the
	 * supplied {@link AnnotatedElement}. Deals with both a single direct annotation and
	 * repeating annotations nested within a containing annotation.
	 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
	 * @param annotatedElement the element to look for annotations on
	 * @param containerAnnotationType the class of the container that holds the annotations
J
Juergen Hoeller 已提交
178
	 * @param annotationType the annotation type to look for
179 180
	 * @return the annotations found
	 * @since 4.0
J
Juergen Hoeller 已提交
181
	 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
182 183 184
	 */
	public static <A extends Annotation> Set<A> getRepeatableAnnotation(AnnotatedElement annotatedElement,
			Class<? extends Annotation> containerAnnotationType, Class<A> annotationType) {
J
Juergen Hoeller 已提交
185

186 187 188 189
		try {
			if (annotatedElement.getAnnotations().length > 0) {
				return new AnnotationCollector<A>(containerAnnotationType, annotationType).getResult(annotatedElement);
			}
190
		}
191 192 193 194 195
		catch (Exception ex) {
			// Assuming nested Class values not resolvable within annotation attributes...
			// We're probably hitting a non-present optional arrangement - let's back out.
		}
		return Collections.emptySet();
196 197
	}

A
Arjen Poutsma 已提交
198
	/**
S
Sam Brannen 已提交
199
	 * Find a single {@link Annotation} of {@code annotationType} from the supplied
J
Juergen Hoeller 已提交
200
	 * {@link Method}, traversing its super methods (i.e., from superclasses and
S
Sam Brannen 已提交
201 202 203
	 * interfaces) if no annotation can be found on the given method itself.
	 * <p>Annotations on methods are not inherited by default, so we need to handle
	 * this explicitly.
A
Arjen Poutsma 已提交
204
	 * @param method the method to look for annotations on
J
Juergen Hoeller 已提交
205 206
	 * @param annotationType the annotation type to look for
	 * @return the annotation found, or {@code null} if none
A
Arjen Poutsma 已提交
207 208 209
	 */
	public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
		A annotation = getAnnotation(method, annotationType);
210
		Class<?> clazz = method.getDeclaringClass();
J
Juergen Hoeller 已提交
211
		if (annotation == null) {
212
			annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
213
		}
A
Arjen Poutsma 已提交
214
		while (annotation == null) {
215 216
			clazz = clazz.getSuperclass();
			if (clazz == null || clazz.equals(Object.class)) {
A
Arjen Poutsma 已提交
217 218 219
				break;
			}
			try {
220
				Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
A
Arjen Poutsma 已提交
221 222 223
				annotation = getAnnotation(equivalentMethod, annotationType);
			}
			catch (NoSuchMethodException ex) {
224 225 226
				// No equivalent method found
			}
			if (annotation == null) {
227
				annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
A
Arjen Poutsma 已提交
228 229 230 231 232
			}
		}
		return annotation;
	}

233
	private static <A extends Annotation> A searchOnInterfaces(Method method, Class<A> annotationType, Class<?>[] ifcs) {
234
		A annotation = null;
J
Juergen Hoeller 已提交
235
		for (Class<?> iface : ifcs) {
236 237 238 239 240 241 242 243 244 245 246
			if (isInterfaceWithAnnotatedMethods(iface)) {
				try {
					Method equivalentMethod = iface.getMethod(method.getName(), method.getParameterTypes());
					annotation = getAnnotation(equivalentMethod, annotationType);
				}
				catch (NoSuchMethodException ex) {
					// Skip this interface - it doesn't have the method...
				}
				if (annotation != null) {
					break;
				}
247
			}
248 249 250 251 252 253 254 255 256
		}
		return annotation;
	}

	private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
		synchronized (annotatedInterfaceCache) {
			Boolean flag = annotatedInterfaceCache.get(iface);
			if (flag != null) {
				return flag;
257
			}
258 259
			boolean found = false;
			for (Method ifcMethod : iface.getMethods()) {
260 261 262 263 264 265 266 267 268
				try {
					if (ifcMethod.getAnnotations().length > 0) {
						found = true;
						break;
					}
				}
				catch (Exception ex) {
					// Assuming nested Class values not resolvable within annotation attributes...
					// We're probably hitting a non-present optional arrangement - let's back out.
269
				}
270
			}
271 272
			annotatedInterfaceCache.put(iface, found);
			return found;
273 274 275
		}
	}

A
Arjen Poutsma 已提交
276
	/**
277 278 279 280
	 * 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>present</em> on the given class
	 * itself.
S
Sam Brannen 已提交
281
	 * <p>This method explicitly handles class-level annotations which are not
282 283
	 * declared as {@link java.lang.annotation.Inherited inherited} <em>as well
	 * as meta-annotations and annotations on interfaces</em>.
S
Sam Brannen 已提交
284 285
	 * <p>The algorithm operates as follows:
	 * <ol>
286 287 288 289
	 * <li>Search for the annotation on the given class and return it if found.
	 * <li>Recursively search through all interfaces that the given class declares.
	 * <li>Recursively search through all annotations that the given class declares.
	 * <li>Recursively search through the superclass hierarchy of the given class.
S
Sam Brannen 已提交
290
	 * </ol>
291 292 293
	 * <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.
A
Arjen Poutsma 已提交
294
	 * @param clazz the class to look for annotations on
295 296
	 * @param annotationType the type of annotation to look for
	 * @return the annotation if found, or {@code null} if not found
A
Arjen Poutsma 已提交
297 298
	 */
	public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) {
299 300 301 302 303 304
		return findAnnotation(clazz, annotationType, new HashSet<Annotation>());
	}

	/**
	 * Perform the search algorithm for {@link #findAnnotation(Class, Class)},
	 * avoiding endless recursion by tracking which annotations have already
S
Sam Brannen 已提交
305
	 * been <em>visited</em>.
306 307
	 * @param clazz the class to look for annotations on
	 * @param annotationType the type of annotation to look for
S
Sam Brannen 已提交
308
	 * @param visited the set of annotations that have already been visited
309 310
	 * @return the annotation if found, or {@code null} if not found
	 */
J
Juergen Hoeller 已提交
311
	private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
A
Arjen Poutsma 已提交
312
		Assert.notNull(clazz, "Class must not be null");
313
		if (isAnnotationDeclaredLocally(annotationType, clazz)) {
314 315 316 317 318 319 320 321
			try {
				return clazz.getAnnotation(annotationType);
			}
			catch (Exception ex) {
				// Assuming nested Class values not resolvable within annotation attributes...
				// We're probably hitting a non-present optional arrangement - let's back out.
				return null;
			}
A
Arjen Poutsma 已提交
322 323
		}
		for (Class<?> ifc : clazz.getInterfaces()) {
324
			A annotation = findAnnotation(ifc, annotationType, visited);
A
Arjen Poutsma 已提交
325 326 327 328
			if (annotation != null) {
				return annotation;
			}
		}
329
		for (Annotation ann : clazz.getDeclaredAnnotations()) {
330
			if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) {
J
Juergen Hoeller 已提交
331
				A annotation = findAnnotation(ann.annotationType(), annotationType, visited);
332 333 334 335 336
				if (annotation != null) {
					return annotation;
				}
			}
		}
337 338
		Class<?> superclass = clazz.getSuperclass();
		if (superclass == null || superclass.equals(Object.class)) {
A
Arjen Poutsma 已提交
339 340
			return null;
		}
S
Sam Brannen 已提交
341
		return findAnnotation(superclass, annotationType, visited);
A
Arjen Poutsma 已提交
342 343 344
	}

	/**
345 346 347 348 349
	 * Find the first {@link Class} in the inheritance hierarchy of the specified {@code clazz}
	 * (including the specified {@code clazz} itself) which declares an annotation for the
	 * specified {@code annotationType}, or {@code null} if not found. If the supplied
	 * {@code clazz} is {@code null}, {@code null} will be returned.
	 * <p>If the supplied {@code clazz} is an interface, only the interface itself will be checked;
J
Juergen Hoeller 已提交
350 351 352 353
	 * the inheritance hierarchy for interfaces will not be traversed.
	 * <p>The standard {@link Class} API does not provide a mechanism for determining which class
	 * in an inheritance hierarchy actually declares an {@link Annotation}, so we need to handle
	 * this explicitly.
J
Juergen Hoeller 已提交
354 355
	 * @param annotationType the annotation type to look for, both locally and as a meta-annotation
	 * @param clazz the class on which to check for the annotation (may be {@code null})
356 357
	 * @return the first {@link Class} in the inheritance hierarchy of the specified {@code clazz}
	 * which declares an annotation for the specified {@code annotationType}, or {@code null}
J
Juergen Hoeller 已提交
358
	 * if not found
A
Arjen Poutsma 已提交
359 360
	 * @see Class#isAnnotationPresent(Class)
	 * @see Class#getDeclaredAnnotations()
361 362
	 * @see #findAnnotationDeclaringClassForTypes(List, Class)
	 * @see #isAnnotationDeclaredLocally(Class, Class)
A
Arjen Poutsma 已提交
363 364 365 366 367 368
	 */
	public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz) {
		Assert.notNull(annotationType, "Annotation type must not be null");
		if (clazz == null || clazz.equals(Object.class)) {
			return null;
		}
369 370 371 372
		if (isAnnotationDeclaredLocally(annotationType, clazz)) {
			return clazz;
		}
		return findAnnotationDeclaringClass(annotationType, clazz.getSuperclass());
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	}

	/**
	 * Find the first {@link Class} in the inheritance hierarchy of the specified
	 * {@code clazz} (including the specified {@code clazz} itself) which declares
	 * at least one of the specified {@code annotationTypes}, or {@code null} if
	 * none of the specified annotation types could be found.
	 * <p>If the supplied {@code clazz} is {@code null}, {@code null} will be
	 * returned.
	 * <p>If the supplied {@code clazz} is an interface, only the interface itself
	 * will be checked; the inheritance hierarchy for interfaces will not be traversed.
	 * <p>The standard {@link Class} API does not provide a mechanism for determining
	 * which class in an inheritance hierarchy actually declares one of several
	 * candidate {@linkplain Annotation annotations}, so we need to handle this
	 * explicitly.
	 * @param annotationTypes the list of Class objects corresponding to the
	 * annotation types
	 * @param clazz the Class object corresponding to the class on which to check
	 * for the annotations, or {@code null}
	 * @return the first {@link Class} in the inheritance hierarchy of the specified
	 * {@code clazz} which declares an annotation of at least one of the specified
	 * {@code annotationTypes}, or {@code null} if not found
395
	 * @since 3.2.2
396 397 398 399 400
	 * @see Class#isAnnotationPresent(Class)
	 * @see Class#getDeclaredAnnotations()
	 * @see #findAnnotationDeclaringClass(Class, Class)
	 * @see #isAnnotationDeclaredLocally(Class, Class)
	 */
401
	public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes, Class<?> clazz) {
402 403 404 405 406 407 408 409 410 411
		Assert.notEmpty(annotationTypes, "The list of annotation types must not be empty");
		if (clazz == null || clazz.equals(Object.class)) {
			return null;
		}
		for (Class<? extends Annotation> annotationType : annotationTypes) {
			if (isAnnotationDeclaredLocally(annotationType, clazz)) {
				return clazz;
			}
		}
		return findAnnotationDeclaringClassForTypes(annotationTypes, clazz.getSuperclass());
A
Arjen Poutsma 已提交
412 413 414
	}

	/**
415 416
	 * Determine whether an annotation for the specified {@code annotationType} is
	 * declared locally on the supplied {@code clazz}. The supplied {@link Class}
J
Juergen Hoeller 已提交
417 418
	 * may represent any type.
	 * <p>Note: This method does <strong>not</strong> determine if the annotation is
S
Sam Brannen 已提交
419 420 421
	 * {@linkplain java.lang.annotation.Inherited inherited}. For greater clarity
	 * regarding inherited annotations, consider using
	 * {@link #isAnnotationInherited(Class, Class)} instead.
A
Arjen Poutsma 已提交
422
	 * @param annotationType the Class object corresponding to the annotation type
J
Juergen Hoeller 已提交
423
	 * @param clazz the Class object corresponding to the class on which to check for the annotation
424 425
	 * @return {@code true} if an annotation for the specified {@code annotationType}
	 * is declared locally on the supplied {@code clazz}
A
Arjen Poutsma 已提交
426 427 428 429 430 431 432
	 * @see Class#getDeclaredAnnotations()
	 * @see #isAnnotationInherited(Class, Class)
	 */
	public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
		Assert.notNull(annotationType, "Annotation type must not be null");
		Assert.notNull(clazz, "Class must not be null");
		boolean declaredLocally = false;
433 434 435 436 437 438
		try {
			for (Annotation annotation : clazz.getDeclaredAnnotations()) {
				if (annotation.annotationType().equals(annotationType)) {
					declaredLocally = true;
					break;
				}
A
Arjen Poutsma 已提交
439 440
			}
		}
441 442 443 444
		catch (Exception ex) {
			// Assuming nested Class values not resolvable within annotation attributes...
			// We're probably hitting a non-present optional arrangement - let's back out.
		}
A
Arjen Poutsma 已提交
445 446 447 448
		return declaredLocally;
	}

	/**
449
	 * Determine whether an annotation for the specified {@code annotationType} is present
S
Sam Brannen 已提交
450 451
	 * on the supplied {@code clazz} and is {@linkplain java.lang.annotation.Inherited inherited}
	 * (i.e., not declared locally for the class).
452
	 * <p>If the supplied {@code clazz} is an interface, only the interface itself will be checked.
J
Juergen Hoeller 已提交
453
	 * In accordance with standard meta-annotation semantics, the inheritance hierarchy for interfaces
S
Sam Brannen 已提交
454 455
	 * will not be traversed. See the {@linkplain java.lang.annotation.Inherited Javadoc} for the
	 * {@code @Inherited} meta-annotation for further details regarding annotation inheritance.
A
Arjen Poutsma 已提交
456
	 * @param annotationType the Class object corresponding to the annotation type
J
Juergen Hoeller 已提交
457
	 * @param clazz the Class object corresponding to the class on which to check for the annotation
458
	 * @return {@code true} if an annotation for the specified {@code annotationType} is present
S
Sam Brannen 已提交
459
	 * on the supplied {@code clazz} and is <em>inherited</em>
A
Arjen Poutsma 已提交
460 461 462 463 464 465 466 467 468
	 * @see Class#isAnnotationPresent(Class)
	 * @see #isAnnotationDeclaredLocally(Class, Class)
	 */
	public static boolean isAnnotationInherited(Class<? extends Annotation> annotationType, Class<?> clazz) {
		Assert.notNull(annotationType, "Annotation type must not be null");
		Assert.notNull(clazz, "Class must not be null");
		return (clazz.isAnnotationPresent(annotationType) && !isAnnotationDeclaredLocally(annotationType, clazz));
	}

469
	/**
J
Juergen Hoeller 已提交
470
	 * Determine if the supplied {@link Annotation} is defined in the core JDK
471
	 * {@code java.lang.annotation} package.
J
Juergen Hoeller 已提交
472
	 * @param annotation the annotation to check (never {@code null})
473 474 475 476 477 478 479
	 * @return {@code true} if the annotation is in the {@code java.lang.annotation} package
	 */
	public static boolean isInJavaLangAnnotationPackage(Annotation annotation) {
		Assert.notNull(annotation, "Annotation must not be null");
		return annotation.annotationType().getName().startsWith("java.lang.annotation");
	}

A
Arjen Poutsma 已提交
480
	/**
J
Juergen Hoeller 已提交
481 482 483 484
	 * Retrieve the given annotation's attributes as a {@link Map}, preserving all
	 * attribute types as-is.
	 * <p>Note: This method actually returns an {@link AnnotationAttributes} instance.
	 * However, the {@code Map} signature has been preserved for binary compatibility.
A
Arjen Poutsma 已提交
485
	 * @param annotation the annotation to retrieve the attributes for
J
Juergen Hoeller 已提交
486 487
	 * @return the Map of annotation attributes, with attribute names as keys and
	 * corresponding attribute values as values
A
Arjen Poutsma 已提交
488 489
	 */
	public static Map<String, Object> getAnnotationAttributes(Annotation annotation) {
490
		return getAnnotationAttributes(annotation, false, false);
491 492 493
	}

	/**
J
Juergen Hoeller 已提交
494 495
	 * Retrieve the given annotation's attributes as a {@link Map}. Equivalent to
	 * calling {@link #getAnnotationAttributes(Annotation, boolean, boolean)} with
496
	 * the {@code nestedAnnotationsAsMap} parameter set to {@code false}.
J
Juergen Hoeller 已提交
497 498
	 * <p>Note: This method actually returns an {@link AnnotationAttributes} instance.
	 * However, the {@code Map} signature has been preserved for binary compatibility.
499
	 * @param annotation the annotation to retrieve the attributes for
500
	 * @param classValuesAsString whether to turn Class references into Strings (for
J
Juergen Hoeller 已提交
501 502
	 * compatibility with {@link org.springframework.core.type.AnnotationMetadata}
	 * or to preserve them as Class references
J
Juergen Hoeller 已提交
503 504
	 * @return the Map of annotation attributes, with attribute names as keys and
	 * corresponding attribute values as values
505
	 */
506
	public static Map<String, Object> getAnnotationAttributes(Annotation annotation, boolean classValuesAsString) {
507 508 509 510 511
		return getAnnotationAttributes(annotation, classValuesAsString, false);
	}

	/**
	 * Retrieve the given annotation's attributes as an {@link AnnotationAttributes}
J
Juergen Hoeller 已提交
512 513 514
	 * map structure.
	 * <p>This method provides fully recursive annotation reading capabilities on par with
	 * the reflection-based {@link org.springframework.core.type.StandardAnnotationMetadata}.
515 516
	 * @param annotation the annotation to retrieve the attributes for
	 * @param classValuesAsString whether to turn Class references into Strings (for
J
Juergen Hoeller 已提交
517 518
	 * compatibility with {@link org.springframework.core.type.AnnotationMetadata}
	 * or to preserve them as Class references
519 520 521 522 523 524 525 526
	 * @param nestedAnnotationsAsMap whether to turn nested Annotation instances into
	 * {@link AnnotationAttributes} maps (for compatibility with
	 * {@link org.springframework.core.type.AnnotationMetadata} or to preserve them as
	 * Annotation instances
	 * @return the annotation attributes (a specialized Map) with attribute names as keys
	 * and corresponding attribute values as values
	 * @since 3.1.1
	 */
527 528
	public static AnnotationAttributes getAnnotationAttributes(Annotation annotation, boolean classValuesAsString,
			boolean nestedAnnotationsAsMap) {
529 530

		AnnotationAttributes attrs = new AnnotationAttributes();
A
Arjen Poutsma 已提交
531
		Method[] methods = annotation.annotationType().getDeclaredMethods();
532
		for (Method method : methods) {
A
Arjen Poutsma 已提交
533 534
			if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) {
				try {
535
					Object value = method.invoke(annotation);
536
					if (classValuesAsString) {
537
						if (value instanceof Class) {
538
							value = ((Class<?>) value).getName();
539 540
						}
						else if (value instanceof Class[]) {
541
							Class<?>[] clazzArray = (Class[]) value;
542 543 544 545 546 547 548
							String[] newValue = new String[clazzArray.length];
							for (int i = 0; i < clazzArray.length; i++) {
								newValue[i] = clazzArray[i].getName();
							}
							value = newValue;
						}
					}
549
					if (nestedAnnotationsAsMap && value instanceof Annotation) {
550
						attrs.put(method.getName(),
J
Juergen Hoeller 已提交
551
							getAnnotationAttributes((Annotation) value, classValuesAsString, true));
552 553
					}
					else if (nestedAnnotationsAsMap && value instanceof Annotation[]) {
554
						Annotation[] realAnnotations = (Annotation[]) value;
555 556
						AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length];
						for (int i = 0; i < realAnnotations.length; i++) {
J
Juergen Hoeller 已提交
557
							mappedAnnotations[i] = getAnnotationAttributes(realAnnotations[i], classValuesAsString, true);
558 559 560 561 562 563
						}
						attrs.put(method.getName(), mappedAnnotations);
					}
					else {
						attrs.put(method.getName(), value);
					}
A
Arjen Poutsma 已提交
564 565 566 567 568 569 570 571 572 573
				}
				catch (Exception ex) {
					throw new IllegalStateException("Could not obtain annotation attribute values", ex);
				}
			}
		}
		return attrs;
	}

	/**
574
	 * Retrieve the <em>value</em> of the {@code &quot;value&quot;} attribute of a
J
Juergen Hoeller 已提交
575
	 * single-element Annotation, given an annotation instance.
A
Arjen Poutsma 已提交
576
	 * @param annotation the annotation instance from which to retrieve the value
577
	 * @return the attribute value, or {@code null} if not found
A
Arjen Poutsma 已提交
578 579 580 581 582 583 584
	 * @see #getValue(Annotation, String)
	 */
	public static Object getValue(Annotation annotation) {
		return getValue(annotation, VALUE);
	}

	/**
J
Juergen Hoeller 已提交
585
	 * Retrieve the <em>value</em> of a named attribute, given an annotation instance.
A
Arjen Poutsma 已提交
586 587
	 * @param annotation the annotation instance from which to retrieve the value
	 * @param attributeName the name of the attribute value to retrieve
588
	 * @return the attribute value, or {@code null} if not found
J
Juergen Hoeller 已提交
589
	 * @see #getValue(Annotation)
A
Arjen Poutsma 已提交
590 591 592
	 */
	public static Object getValue(Annotation annotation, String attributeName) {
		try {
J
Juergen Hoeller 已提交
593
			Method method = annotation.annotationType().getDeclaredMethod(attributeName);
594
			ReflectionUtils.makeAccessible(method);
A
Arjen Poutsma 已提交
595 596 597 598 599 600 601 602
			return method.invoke(annotation);
		}
		catch (Exception ex) {
			return null;
		}
	}

	/**
603
	 * Retrieve the <em>default value</em> of the {@code &quot;value&quot;} attribute
J
Juergen Hoeller 已提交
604 605
	 * of a single-element Annotation, given an annotation instance.
	 * @param annotation the annotation instance from which to retrieve the default value
606
	 * @return the default value, or {@code null} if not found
A
Arjen Poutsma 已提交
607 608 609 610 611 612 613
	 * @see #getDefaultValue(Annotation, String)
	 */
	public static Object getDefaultValue(Annotation annotation) {
		return getDefaultValue(annotation, VALUE);
	}

	/**
J
Juergen Hoeller 已提交
614
	 * Retrieve the <em>default value</em> of a named attribute, given an annotation instance.
J
Juergen Hoeller 已提交
615
	 * @param annotation the annotation instance from which to retrieve the default value
A
Arjen Poutsma 已提交
616
	 * @param attributeName the name of the attribute value to retrieve
617
	 * @return the default value of the named attribute, or {@code null} if not found
A
Arjen Poutsma 已提交
618 619 620 621 622 623 624
	 * @see #getDefaultValue(Class, String)
	 */
	public static Object getDefaultValue(Annotation annotation, String attributeName) {
		return getDefaultValue(annotation.annotationType(), attributeName);
	}

	/**
625
	 * Retrieve the <em>default value</em> of the {@code &quot;value&quot;} attribute
J
Juergen Hoeller 已提交
626 627
	 * of a single-element Annotation, given the {@link Class annotation type}.
	 * @param annotationType the <em>annotation type</em> for which the default value should be retrieved
628
	 * @return the default value, or {@code null} if not found
A
Arjen Poutsma 已提交
629 630 631 632 633 634 635
	 * @see #getDefaultValue(Class, String)
	 */
	public static Object getDefaultValue(Class<? extends Annotation> annotationType) {
		return getDefaultValue(annotationType, VALUE);
	}

	/**
J
Juergen Hoeller 已提交
636 637
	 * Retrieve the <em>default value</em> of a named attribute, given the
	 * {@link Class annotation type}.
J
Juergen Hoeller 已提交
638
	 * @param annotationType the <em>annotation type</em> for which the default value should be retrieved
A
Arjen Poutsma 已提交
639
	 * @param attributeName the name of the attribute value to retrieve.
640
	 * @return the default value of the named attribute, or {@code null} if not found
A
Arjen Poutsma 已提交
641 642 643 644
	 * @see #getDefaultValue(Annotation, String)
	 */
	public static Object getDefaultValue(Class<? extends Annotation> annotationType, String attributeName) {
		try {
J
Juergen Hoeller 已提交
645
			return annotationType.getDeclaredMethod(attributeName).getDefaultValue();
A
Arjen Poutsma 已提交
646 647 648 649 650 651
		}
		catch (Exception ex) {
			return null;
		}
	}

652 653 654 655 656 657 658 659 660 661 662

	private static class AnnotationCollector<A extends Annotation> {

		private final Class<? extends Annotation> containerAnnotationType;

		private final Class<A> annotationType;

		private final Set<AnnotatedElement> visited = new HashSet<AnnotatedElement>();

		private final Set<A> result = new LinkedHashSet<A>();

J
Juergen Hoeller 已提交
663
		public AnnotationCollector(Class<? extends Annotation> containerAnnotationType, Class<A> annotationType) {
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
			this.containerAnnotationType = containerAnnotationType;
			this.annotationType = annotationType;
		}

		public Set<A> getResult(AnnotatedElement element) {
			process(element);
			return Collections.unmodifiableSet(this.result);
		}

		@SuppressWarnings("unchecked")
		private void process(AnnotatedElement annotatedElement) {
			if (this.visited.add(annotatedElement)) {
				for (Annotation annotation : annotatedElement.getAnnotations()) {
					if (ObjectUtils.nullSafeEquals(this.annotationType, annotation.annotationType())) {
						this.result.add((A) annotation);
					}
					else if (ObjectUtils.nullSafeEquals(this.containerAnnotationType, annotation.annotationType())) {
681
						this.result.addAll(getValue(annotation));
682
					}
683
					else if (!isInJavaLangAnnotationPackage(annotation)) {
684 685 686 687 688 689 690
						process(annotation.annotationType());
					}
				}
			}
		}

		@SuppressWarnings("unchecked")
691
		private List<A> getValue(Annotation annotation) {
692 693
			try {
				Method method = annotation.annotationType().getDeclaredMethod("value");
694
				ReflectionUtils.makeAccessible(method);
695
				return Arrays.asList((A[]) method.invoke(annotation));
696 697
			}
			catch (Exception ex) {
698 699
				// Unable to read value from repeating annotation container -> ignore it.
				return Collections.emptyList();
700 701 702
			}
		}
	}
J
Juergen Hoeller 已提交
703

A
Arjen Poutsma 已提交
704
}