Top Description Fields Constructors Methods
org.apache.commons.lang3

public Class AnnotationUtils

extends Object
Class Inheritance
Imports
java.lang.annotation.Annotation, java.lang.reflect.Method, java.util.Arrays, org.apache.commons.lang3.builder.ToStringBuilder, .ToStringStyle, org.apache.commons.lang3.exception.UncheckedException

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

Modifier and TypeField and Description
private static final ToStringStyle
TO_STRING_STYLE

A style that prints annotations as recommended.

Constructor Summary

AccessConstructor and Description
public
AnnotationUtils()
Deprecated TODO Make private in 4.0.

AnnotationUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
private static boolean

Returns:

a flag whether these arrays are equal
annotationArrayMemberEquals
(final Annotation[]
the first array
a1
,
final Annotation[]
the second array
a2
)

Helper method for comparing two arrays of annotations.

private static boolean

Returns:

a flag whether these objects are equal
arrayMemberEquals
(final Class<?>
the component type of the array
componentType
,
final Object
the first object
o1
,
final Object
the second object
o2
)

Helper method for comparing two objects of an array type.

private static int

Returns:

a hash code for the specified array
arrayMemberHash
(final Class<?>
the component type of the array
componentType
,
final Object
the array
o
)

Helper method for generating a hash code for an array.

public static boolean

Returns:

true if the two annotations are equal or both null
equals
(final Annotation
the first Annotation to compare, null returns false unless both are null
a1
,
final Annotation
the second Annotation to compare, null returns false unless both are null
a2
)

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

public static int

Returns:

the calculated hash code
hashCode
(final Annotation
the Annotation for a hash code calculation is desired, not null
a
)

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

private static int

Returns:

a hash code for this member
hashMember
(final String
the name of the member
name
,
final Object
the value of the member
value
)

Helper method for generating a hash code for a member of an annotation.

public static boolean

Returns:

true if the type is a valid type to use in an annotation
isValidAnnotationMemberType
(Class<?>
the type to check, null
type
)

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

private static boolean

Returns:

a flag whether these objects are equal
memberEquals
(final Class<?>
the type of the objects to be compared
type
,
final Object
the first object
o1
,
final Object
the second object
o2
)

Helper method for checking whether two objects of the given type are equal.

public static String

Returns:

the standard string representation of an annotation, not null
toString
(final Annotation
the annotation of which a string representation is desired
a
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

TO_STRING_STYLEback to summary
private static final ToStringStyle TO_STRING_STYLE

A style that prints annotations as recommended.

Constructor Detail

AnnotationUtilsback to summary
public AnnotationUtils()

Deprecated

TODO Make private in 4.0.

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.

Annotations
@Deprecated

Method Detail

annotationArrayMemberEqualsback to summary
private static boolean annotationArrayMemberEquals(final Annotation[] a1, final Annotation[] a2)

Helper method for comparing two arrays of annotations.

Parameters
a1:Annotation[]

the first array

a2:Annotation[]

the second array

Returns:boolean

a flag whether these arrays are equal

arrayMemberEqualsback to summary
private static boolean arrayMemberEquals(final Class<?> componentType, final Object o1, final Object o2)

Helper method for comparing two objects of an array type.

Parameters
componentType:Class<?>

the component type of the array

o1:Object

the first object

o2:Object

the second object

Returns:boolean

a flag whether these objects are equal

arrayMemberHashback to summary
private static int arrayMemberHash(final Class<?> componentType, final Object o)

Helper method for generating a hash code for an array.

Parameters
componentType:Class<?>

the component type of the array

o:Object

the array

Returns:int

a hash code for the specified array

equalsback to summary
public static boolean equals(final Annotation a1, final Annotation a2)

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

Parameters
a1:Annotation

the first Annotation to compare, null returns false unless both are null

a2:Annotation

the second Annotation to compare, null returns false unless both are null

Returns:boolean

true if the two annotations are equal or both null

hashCodeback to summary
public static int hashCode(final Annotation a)

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

Parameters
a:Annotation

the Annotation for a hash code calculation is desired, not null

Returns:int

the calculated hash code

Exceptions
RuntimeException:
if an Exception is encountered during annotation member access
IllegalStateException:
if an annotation method invocation returns null
hashMemberback to summary
private static int hashMember(final String name, final Object value)

Helper method for generating a hash code for a member of an annotation.

Parameters
name:String

the name of the member

value:Object

the value of the member

Returns:int

a hash code for this member

isValidAnnotationMemberTypeback to summary
public static boolean isValidAnnotationMemberType(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:Class<?>

the type to check, null

Returns:boolean

true if the type is a valid type to use in an annotation

memberEqualsback to summary
private static boolean memberEquals(final Class<?> type, final Object o1, final 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:Class<?>

the type of the objects to be compared

o1:Object

the first object

o2:Object

the second object

Returns:boolean

a flag whether these objects are equal

toStringback to summary
public static String toString(final Annotation a)

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

Parameters
a:Annotation

the annotation of which a string representation is desired

Returns:String

the standard string representation of an annotation, not null