Class AnnotationUtils


  • public class AnnotationUtils
    extends java.lang.Object

    Helper methods for working with Annotation instances.

    This class contains various utility methods that make working with annotations simpler.

    Annotation instances are always proxy objects; unfortunately dynamic proxies cannot be depended upon to know how to implement certain methods in the same manner as would be done by "natural" Annotations. The methods presented in this class can be used to avoid that possibility. It is of course also possible for dynamic proxies to actually delegate their e.g. Annotation.equals(Object)/Annotation.hashCode()/ Annotation.toString() implementations to AnnotationUtils.

    #ThreadSafe#

    Since:
    3.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static ToStringStyle TO_STRING_STYLE
      A style that prints annotations as recommended.
    • Constructor Summary

      Constructors 
      Constructor Description
      AnnotationUtils()
      AnnotationUtils instances should NOT be constructed in standard programming.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean annotationArrayMemberEquals​(java.lang.annotation.Annotation[] a1, java.lang.annotation.Annotation[] a2)
      Helper method for comparing two arrays of annotations.
      private static boolean arrayMemberEquals​(java.lang.Class<?> componentType, java.lang.Object o1, java.lang.Object o2)
      Helper method for comparing two objects of an array type.
      private static int arrayMemberHash​(java.lang.Class<?> componentType, java.lang.Object o)
      Helper method for generating a hash code for an array.
      static boolean equals​(java.lang.annotation.Annotation a1, java.lang.annotation.Annotation a2)
      Checks if two annotations are equal using the criteria for equality presented in the Annotation.equals(Object) API docs.
      static int hashCode​(java.lang.annotation.Annotation a)
      Generate a hash code for the given annotation using the algorithm presented in the Annotation.hashCode() API docs.
      private static int hashMember​(java.lang.String name, java.lang.Object value)
      Helper method for generating a hash code for a member of an annotation.
      static boolean isValidAnnotationMemberType​(java.lang.Class<?> type)
      Checks if the specified type is permitted as an annotation member.
      private static boolean memberEquals​(java.lang.Class<?> type, java.lang.Object o1, java.lang.Object o2)
      Helper method for checking whether two objects of the given type are equal.
      static java.lang.String toString​(java.lang.annotation.Annotation a)
      Generate a string representation of an Annotation, as suggested by Annotation.toString().
      • Methods inherited from class java.lang.Object

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

      • TO_STRING_STYLE

        private static final ToStringStyle TO_STRING_STYLE
        A style that prints annotations as recommended.
    • Constructor Detail

      • AnnotationUtils

        public AnnotationUtils()

        AnnotationUtils instances should NOT be constructed in standard programming. Instead, the class should be used statically.

        This constructor is public to permit tools that require a JavaBean instance to operate.

    • Method Detail

      • equals

        public static boolean equals​(java.lang.annotation.Annotation a1,
                                     java.lang.annotation.Annotation a2)

        Checks if two annotations are equal using the criteria for equality presented in the Annotation.equals(Object) API docs.

        Parameters:
        a1 - the first Annotation to compare, null returns false unless both are null
        a2 - the second Annotation to compare, null returns false unless both are null
        Returns:
        true if the two annotations are equal or both null
      • hashCode

        public static int hashCode​(java.lang.annotation.Annotation a)

        Generate a hash code for the given annotation using the algorithm presented in the Annotation.hashCode() API docs.

        Parameters:
        a - the Annotation for a hash code calculation is desired, not null
        Returns:
        the calculated hash code
        Throws:
        java.lang.RuntimeException - if an Exception is encountered during annotation member access
        java.lang.IllegalStateException - if an annotation method invocation returns null
      • toString

        public static java.lang.String toString​(java.lang.annotation.Annotation a)

        Generate a string representation of an Annotation, as suggested by Annotation.toString().

        Parameters:
        a - the annotation of which a string representation is desired
        Returns:
        the standard string representation of an annotation, not null
      • isValidAnnotationMemberType

        public static boolean isValidAnnotationMemberType​(java.lang.Class<?> type)

        Checks if the specified type is permitted as an annotation member.

        The Java language specification only permits certain types to be used in annotations. These include String, Class, primitive types, Annotation, Enum, and single-dimensional arrays of these types.

        Parameters:
        type - the type to check, null
        Returns:
        true if the type is a valid type to use in an annotation
      • hashMember

        private static int hashMember​(java.lang.String name,
                                      java.lang.Object value)
        Helper method for generating a hash code for a member of an annotation.
        Parameters:
        name - the name of the member
        value - the value of the member
        Returns:
        a hash code for this member
      • memberEquals

        private static boolean memberEquals​(java.lang.Class<?> type,
                                            java.lang.Object o1,
                                            java.lang.Object o2)
        Helper method for checking whether two objects of the given type are equal. This method is used to compare the parameters of two annotation instances.
        Parameters:
        type - the type of the objects to be compared
        o1 - the first object
        o2 - the second object
        Returns:
        a flag whether these objects are equal
      • arrayMemberEquals

        private static boolean arrayMemberEquals​(java.lang.Class<?> componentType,
                                                 java.lang.Object o1,
                                                 java.lang.Object o2)
        Helper method for comparing two objects of an array type.
        Parameters:
        componentType - the component type of the array
        o1 - the first object
        o2 - the second object
        Returns:
        a flag whether these objects are equal
      • annotationArrayMemberEquals

        private static boolean annotationArrayMemberEquals​(java.lang.annotation.Annotation[] a1,
                                                           java.lang.annotation.Annotation[] a2)
        Helper method for comparing two arrays of annotations.
        Parameters:
        a1 - the first array
        a2 - the second array
        Returns:
        a flag whether these arrays are equal
      • arrayMemberHash

        private static int arrayMemberHash​(java.lang.Class<?> componentType,
                                           java.lang.Object o)
        Helper method for generating a hash code for an array.
        Parameters:
        componentType - the component type of the array
        o - the array
        Returns:
        a hash code for the specified array