Class AnnotationAnalyzer


  • public class AnnotationAnalyzer
    extends java.lang.Object
    Given an AnnotatedElement, fetch all of the inherited annotations. This include annotations on methods that may be overridden. Uses ClassAnalyzer to linearize the inheritance hierarchy. This also includes any added annotations for Class and Method. Constructor, Field, Parameter, and Package annotations are just translated from the standard reflective calls.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.reflect.AnnotatedElement,​java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation>> addedAnnotations  
      private java.util.Map<java.lang.reflect.AnnotatedElement,​java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation>> annotationCache  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addAnnotation​(java.lang.reflect.AnnotatedElement element, java.lang.annotation.Annotation annotation)
      Add an annotation to element, which must be a Class, Method, or Constructor.
      void addInheritedAnnotations​(java.lang.Class<?> cls, java.lang.Class<?> ancestor)
      Add all annotations on cls (including inherited annotations and its methods (including overridden methods in super classes and interfaces) to super (which must be a super class or interface of cls).
      <A extends java.lang.annotation.Annotation>
      A
      getAnnotation​(java.lang.reflect.AnnotatedElement elem, java.lang.Class<A> cls)  
      java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.Class<?> cls)
      Return a map of all annotations defined on cls and its super classes and interfaces in ClassAnalyzer order.
      private java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.Class<?> cls, boolean includeAddedAnnotations)  
      java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.Package pkg)
      Same as pkg.getAnnotations, with the result converted to a map.
      java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.AnnotatedElement elem)
      Return all annotations on the element, including any added annotations.
      java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.Constructor<?> cons)
      Same as cons.getAnnotations, with the result converted to a map.
      java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.Field fld)
      Same as fld.getAnnotations, with the result converted to a map.
      java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.Method method)
      Return a map of all annotations defined in method and its overriden methods in the inheritance order of the ClassAnalyzer for the method's defining class.
      private java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.Method method, boolean includeAddedAnnotations)  
      java.util.List<java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation>> getParameterAnnotations​(java.lang.reflect.Constructor<?> cons)
      Same as cons.getParameterAnnotations, with the result converted to a list of maps.
      java.util.List<java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation>> getParameterAnnotations​(java.lang.reflect.Method method)
      Same as cons.getParameterAnnotations, with the result converted to a list of maps.
      private java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> makeAnnoMap​(java.lang.annotation.Annotation[] annos)  
      private <K,​V>
      void
      putIfNotPresent​(java.util.Map<K,​V> map, K key, V value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • annotationCache

        private final java.util.Map<java.lang.reflect.AnnotatedElement,​java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation>> annotationCache
      • addedAnnotations

        private final java.util.Map<java.lang.reflect.AnnotatedElement,​java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation>> addedAnnotations
    • Constructor Detail

      • AnnotationAnalyzer

        public AnnotationAnalyzer()
    • Method Detail

      • putIfNotPresent

        private <K,​V> void putIfNotPresent​(java.util.Map<K,​V> map,
                                                 K key,
                                                 V value)
      • addAnnotation

        public void addAnnotation​(java.lang.reflect.AnnotatedElement element,
                                  java.lang.annotation.Annotation annotation)
        Add an annotation to element, which must be a Class, Method, or Constructor.
        Parameters:
        element -
        annotation -
      • addInheritedAnnotations

        public void addInheritedAnnotations​(java.lang.Class<?> cls,
                                            java.lang.Class<?> ancestor)
        Add all annotations on cls (including inherited annotations and its methods (including overridden methods in super classes and interfaces) to super (which must be a super class or interface of cls).
        Parameters:
        cls -
      • getAnnotations

        public java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.Class<?> cls)
        Return a map of all annotations defined on cls and its super classes and interfaces in ClassAnalyzer order. Annotations nearer the front of the list replace those later in the list of the same type.
        Parameters:
        cls - Class to analyze.
        Returns:
        Map from annotation class to annotation value.
      • getAnnotations

        private java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.Class<?> cls,
                                                                                                       boolean includeAddedAnnotations)
      • getAnnotations

        public java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.Method method)
        Return a map of all annotations defined in method and its overriden methods in the inheritance order of the ClassAnalyzer for the method's defining class.
        Parameters:
        method - The method to analyze
        Returns:
        A map from annotation class to annotation
      • getAnnotations

        private java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.Method method,
                                                                                                       boolean includeAddedAnnotations)
      • makeAnnoMap

        private java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> makeAnnoMap​(java.lang.annotation.Annotation[] annos)
      • getParameterAnnotations

        public java.util.List<java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation>> getParameterAnnotations​(java.lang.reflect.Method method)
        Same as cons.getParameterAnnotations, with the result converted to a list of maps.
        Parameters:
        method - A Java Method
        Returns:
        A list of maps from annotation class to annotation value
      • getAnnotations

        public java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.Constructor<?> cons)
        Same as cons.getAnnotations, with the result converted to a map.
        Parameters:
        cons - A Java Constructor
        Returns:
        A map from annotation class to annotation value
      • getParameterAnnotations

        public java.util.List<java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation>> getParameterAnnotations​(java.lang.reflect.Constructor<?> cons)
        Same as cons.getParameterAnnotations, with the result converted to a list of maps.
        Parameters:
        cons - A Java Constructor
        Returns:
        A list of maps from annotation class to annotation value
      • getAnnotations

        public java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.Field fld)
        Same as fld.getAnnotations, with the result converted to a map.
        Parameters:
        fld - A Java Field
        Returns:
        A map from annotation class to annotation value
      • getAnnotations

        public java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.Package pkg)
        Same as pkg.getAnnotations, with the result converted to a map.
        Parameters:
        pkg - A Java Package
        Returns:
        A map from annotation class to annotation value
      • getAnnotations

        public java.util.Map<java.lang.Class<?>,​java.lang.annotation.Annotation> getAnnotations​(java.lang.reflect.AnnotatedElement elem)
        Return all annotations on the element, including any added annotations. Really just a convenience wrapper for the other getAnnotations methods.
        Parameters:
        elem - AnnotatedElement
        Returns:
        A map from annotation class to annotation value.
      • getAnnotation

        public <A extends java.lang.annotation.Annotation> A getAnnotation​(java.lang.reflect.AnnotatedElement elem,
                                                                           java.lang.Class<A> cls)