public final class AnnotationSupport
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
private static java.util.Map<java.lang.String,java.lang.reflect.Field> |
FIELD_CACHE
Simple cache of fields we have seen.
|
| Modifier | Constructor and Description |
|---|---|
private |
AnnotationSupport()
Private constructor of utility class.
|
| Modifier and Type | Method and Description |
|---|---|
private static <T extends java.lang.annotation.Annotation> |
getAnnotation(java.lang.Object target,
java.lang.Class<T> annotationType)
Returns an annotation of a specified type attached to a target object.
|
static java.lang.String |
getContext(java.lang.Object target)
Gets the value of the field indicated by the
Context annotation on the given object. |
static java.lang.Long |
getExpiration(java.lang.Object target)
Gets the value of the field indicated by the
Expiration annotation on the given object,
or null if none. |
private static java.lang.reflect.Field |
getField(java.lang.Object target,
java.lang.String fieldName)
Returns a
Field from a target object. |
private static java.lang.Object |
getFieldValue(java.lang.Object target,
java.lang.String fieldName)
Returns the value of a field from an object using reflection.
|
static java.lang.String |
getKey(java.lang.Object target)
Gets the value of the field indicated by the
Key annotation on the given object. |
static java.lang.String |
getValue(java.lang.Object target)
Gets the value of the field indicated by the
Value annotation on the given object. |
static void |
setContext(java.lang.Object target,
java.lang.String context)
Sets the value of the field indicated by the
Context annotation on the given object. |
static void |
setExpiration(java.lang.Object target,
java.lang.Long expiration)
Sets the value of the field indicated by the
Expiration annotation on the given object. |
private static void |
setFieldValue(java.lang.Object target,
java.lang.String fieldName,
java.lang.Object fieldValue)
Returns the value of a field from an object using reflection.
|
static void |
setKey(java.lang.Object target,
java.lang.String key)
Sets the value of the field indicated by the
Key annotation on the given object. |
static void |
setValue(java.lang.Object target,
java.lang.String value)
Sets the value of the field indicated by the
Value annotation on the given object. |
private static final java.util.Map<java.lang.String,java.lang.reflect.Field> FIELD_CACHE
private AnnotationSupport()
@Nonnull
@NotEmpty
public static java.lang.String getContext(@Nonnull
java.lang.Object target)
Context annotation on the given object.
If the value is not a String, then it is converted to one
by calling Object.toString() on the object.
target - object from which to get contextjava.lang.IllegalArgumentException - if the target object doesn't declare a Context annotationjava.lang.RuntimeException - if the field cannot be read on the target objectpublic static void setContext(@Nonnull
java.lang.Object target,
@Nonnull @NotEmpty
java.lang.String context)
Context annotation on the given object.target - Object on which to set contextcontext - Context valuejava.lang.IllegalArgumentException - if the target object doesn't declare a Context annotationjava.lang.RuntimeException - if the field cannot be set on the target object@Nonnull
@NotEmpty
public static java.lang.String getKey(@Nonnull
java.lang.Object target)
Key annotation on the given object.
If the value is not a String, then it is converted to one
by calling Object.toString() on the object.
target - object from which to get keyjava.lang.IllegalArgumentException - if the target object doesn't declare a Key annotationjava.lang.RuntimeException - if the field cannot be read on the target objectpublic static void setKey(@Nonnull
java.lang.Object target,
@Nonnull @NotEmpty
java.lang.String key)
Key annotation on the given object.target - Object on which to set keykey - Key valuejava.lang.IllegalArgumentException - if the target object doesn't declare a Key annotationjava.lang.RuntimeException - if the field cannot be set on the target object@Nonnull
@NotEmpty
public static java.lang.String getValue(@Nonnull
java.lang.Object target)
Value annotation on the given object.
If the value is not a String, then it is converted to one
by calling Object.toString() on the object.
target - Object from which to get valuejava.lang.IllegalArgumentException - if the target object doesn't declare a Value annotationjava.lang.RuntimeException - if the field cannot be read on the target objectpublic static void setValue(@Nonnull
java.lang.Object target,
@Nonnull @NotEmpty
java.lang.String value)
Value annotation on the given object.target - object on which to set valuevalue - Value field valuejava.lang.IllegalArgumentException - if the target object doesn't declare a Value annotationjava.lang.RuntimeException - if the field cannot be set on the target object@Nullable
public static java.lang.Long getExpiration(@Nonnull
java.lang.Object target)
Expiration annotation on the given object,
or null if none.
The value is returned as a long, in milliseconds since the beginning of the Unix epoch. The following data types are supported:
longDateReadableInstanttarget - Object from which to get expirationjava.lang.IllegalArgumentException - if the target object doesn't declare a Expiration annotationjava.lang.RuntimeException - if the field cannot be read on the target object or if it is an unsupported data typepublic static void setExpiration(@Nonnull
java.lang.Object target,
@Nullable
java.lang.Long expiration)
Expiration annotation on the given object.
The expiration is expressed in milliseconds since the beginning of the Unix epoch. The following data types are supported:
longDateReadableInstanttarget - object on which to set expirationexpiration - value to setjava.lang.IllegalArgumentException - if the target object doesn't declare a Expiration annotationjava.lang.RuntimeException - if the field cannot be set on the target object or if it is an unsupported data type@Nonnull
private static <T extends java.lang.annotation.Annotation> T getAnnotation(@Nonnull
java.lang.Object target,
@Nonnull
java.lang.Class<T> annotationType)
T - type of annotationtarget - target object to examineannotationType - class type of annotation to findjava.lang.IllegalArgumentException - if the target object doesn't declare the annotation@Nullable
private static java.lang.Object getFieldValue(@Nonnull
java.lang.Object target,
@Nonnull
java.lang.String fieldName)
target - target object to examinefieldName - name of the field to retrievejava.lang.RuntimeException - if the field cannot be readprivate static void setFieldValue(@Nonnull
java.lang.Object target,
@Nonnull
java.lang.String fieldName,
@Nullable
java.lang.Object fieldValue)
target - target object to updatefieldName - name of the field to setfieldValue - value to setjava.lang.RuntimeException - if the field cannot be set@Nonnull
private static java.lang.reflect.Field getField(@Nonnull
java.lang.Object target,
@Nonnull
java.lang.String fieldName)
Field from a target object.target - target object to examinefieldName - name of the field to retrievejava.lang.RuntimeException - if the target object doesn't declare the annotation