/*
* Copyright 2002-2016 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 org.springframework.core.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* General utility methods for working with annotations, handling meta-annotations,
* bridge methods (which the compiler generates for generic declarations) as well
* as super methods (for optional <em>annotation inheritance</em>).
*
* <p>Note that most of the features of this class are not provided by the
* JDK's introspection facilities themselves.
*
* <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)}).
*
* <h3>Terminology</h3>
* The terms <em>directly present</em>, <em>indirectly present</em>, and
* <em>present</em> have the same meanings as defined in the class-level
* Javadoc for {@link AnnotatedElement} (in Java 8).
*
* <p>An annotation is <em>meta-present</em> on an element if the annotation
* is declared as a meta-annotation on some other annotation which is
* <em>present</em> on the element. Annotation {@code A} is <em>meta-present</em>
* on another annotation if {@code A} is either <em>directly present</em> or
* <em>meta-present</em> on the other annotation.
*
* <h3>Meta-annotation Support</h3>
* <p>Most {@code find*()} methods and some {@code get*()} methods in this class
* provide support for finding annotations used as meta-annotations. Consult the
* javadoc for each method in this class for details. For fine-grained support for
* meta-annotations with <em>attribute overrides</em> in <em>composed annotations</em>,
* consider using {@link AnnotatedElementUtils}'s more specific methods instead.
*
* <h3>Attribute Aliases</h3>
* <p>All public methods in this class that return annotations, arrays of
* annotations, or {@link AnnotationAttributes} transparently support attribute
* aliases configured via {@link AliasFor @AliasFor}. Consult the various
* {@code synthesizeAnnotation*(..)} methods for details.
*
* <h3>Search Scope</h3>
* <p>The search algorithms used by methods in this class stop searching for
* an annotation once the first annotation of the specified type has been
* found. As a consequence, additional annotations of the specified type will
* be silently ignored.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @author Sam Brannen
* @author Mark Fisher
* @author Chris Beams
* @author Phillip Webb
* @since 2.0
* @see AliasFor
* @see AnnotationAttributes
* @see AnnotatedElementUtils
* @see BridgeMethodResolver
* @see java.lang.reflect.AnnotatedElement#getAnnotations()
* @see java.lang.reflect.AnnotatedElement#getAnnotation(Class)
* @see java.lang.reflect.AnnotatedElement#getDeclaredAnnotations()
*/
public abstract class AnnotationUtils {
/**
* The attribute name for annotations with a single element.
*/
public static final String VALUE = "value";
private static final String REPEATABLE_CLASS_NAME = "java.lang.annotation.Repeatable";
private static final Map<AnnotationCacheKey, Annotation> findAnnotationCache =
new ConcurrentReferenceHashMap<AnnotationCacheKey, Annotation>(256);
private static final Map<AnnotationCacheKey, Boolean> metaPresentCache =
new ConcurrentReferenceHashMap<AnnotationCacheKey, Boolean>(256);
private static final Map<Class<?>, Boolean> annotatedInterfaceCache =
new ConcurrentReferenceHashMap<Class<?>, Boolean>(256);
private static final Map<Class<? extends Annotation>, Boolean> synthesizableCache =
new ConcurrentReferenceHashMap<Class<? extends Annotation>, Boolean>(256);
private static final Map<Class<? extends Annotation>, Map<String, List<String>>> attributeAliasesCache =
new ConcurrentReferenceHashMap<Class<? extends Annotation>, Map<String, List<String>>>(256);
private static final Map<Class<? extends Annotation>, List<Method>> attributeMethodsCache =
new ConcurrentReferenceHashMap<Class<? extends Annotation>, List<Method>>(256);
private static final Map<Method, AliasDescriptor> aliasDescriptorCache =
new ConcurrentReferenceHashMap<Method, AliasDescriptor>(256);
private static transient Log logger;
/**
* Get a single {@link Annotation} of {@code annotationType} from the supplied
* annotation: either the given annotation itself or a direct meta-annotation
* thereof.
* <p>Note that this method supports only a single level of meta-annotations.
* For support for arbitrary levels of meta-annotations, use one of the
* {@code find*()} methods instead.
* @param ann the Annotation to check
* @param annotationType the annotation type to look for, both locally and as a meta-annotation
* @return the first matching annotation, or {@code null} if not found
* @since 4.0
*/
@SuppressWarnings("unchecked")
public static <A extends Annotation> A getAnnotation(Annotation ann, Class<A> annotationType) {
if (annotationType.isInstance(ann)) {
return synthesizeAnnotation((A) ann);
}
Class<? extends Annotation> annotatedElement = ann.annotationType();
try {
return synthesizeAnnotation(annotatedElement.getAnnotation(annotationType), annotatedElement);
}
catch (Exception ex) {
handleIntrospectionFailure(annotatedElement, ex);
}
return null;
}
/**
* Get a single {@link Annotation} of {@code annotationType} from the supplied
* {@link AnnotatedElement}, where the annotation is either <em>present</em> or
* <em>meta-present</em> on the {@code AnnotatedElement}.
* <p>Note that this method supports only a single level of meta-annotations.
* For support for arbitrary levels of meta-annotations, use
* {@link #findAnnotation(AnnotatedElement, Class)} instead.
* @param annotatedElement the {@code AnnotatedElement} from which to get the annotation
* @param annotationType the annotation type to look for, both locally and as a meta-annotation
* @return the first matching annotation, or {@code null} if not found
* @since 3.1
*/
public static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
try {
A annotation = annotatedElement.getAnnotation