Package org.opensaml.storage.annotation
Class AnnotationSupport
- java.lang.Object
-
- org.opensaml.storage.annotation.AnnotationSupport
-
public final class AnnotationSupport extends Object
Support class that reads and writes fields decorated with storage annotations.
-
-
Field Summary
Fields Modifier and Type Field Description private static Map<String,Field>FIELD_CACHESimple cache of fields we have seen.
-
Constructor Summary
Constructors Modifier Constructor Description privateAnnotationSupport()Private constructor of utility class.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static <T extends Annotation>
TgetAnnotation(Object target, Class<T> annotationType)Returns an annotation of a specified type attached to a target object.static StringgetContext(Object target)Gets the value of the field indicated by theContextannotation on the given object.static LonggetExpiration(Object target)Gets the value of the field indicated by theExpirationannotation on the given object, or null if none.private static FieldgetField(Object target, String fieldName)Returns aFieldfrom a target object.private static ObjectgetFieldValue(Object target, String fieldName)Returns the value of a field from an object using reflection.static StringgetKey(Object target)Gets the value of the field indicated by theKeyannotation on the given object.static StringgetValue(Object target)Gets the value of the field indicated by theValueannotation on the given object.static voidsetContext(Object target, String context)Sets the value of the field indicated by theContextannotation on the given object.static voidsetExpiration(Object target, Long expiration)Sets the value of the field indicated by theExpirationannotation on the given object.private static voidsetFieldValue(Object target, String fieldName, Object fieldValue)Returns the value of a field from an object using reflection.static voidsetKey(Object target, String key)Sets the value of the field indicated by theKeyannotation on the given object.static voidsetValue(Object target, String value)Sets the value of the field indicated by theValueannotation on the given object.
-
-
-
Method Detail
-
getContext
@Nonnull @NotEmpty public static String getContext(@Nonnull Object target)
Gets the value of the field indicated by theContextannotation on the given object.If the value is not a
String, then it is converted to one by callingObject.toString()on the object.- Parameters:
target- object from which to get context- Returns:
- Context field value
- Throws:
IllegalArgumentException- if the target object doesn't declare aContextannotationRuntimeException- if the field cannot be read on the target object
-
setContext
public static void setContext(@Nonnull Object target, @Nonnull @NotEmpty String context)Sets the value of the field indicated by theContextannotation on the given object.- Parameters:
target- Object on which to set contextcontext- Context value- Throws:
IllegalArgumentException- if the target object doesn't declare aContextannotationRuntimeException- if the field cannot be set on the target object
-
getKey
@Nonnull @NotEmpty public static String getKey(@Nonnull Object target)
Gets the value of the field indicated by theKeyannotation on the given object.If the value is not a
String, then it is converted to one by callingObject.toString()on the object.- Parameters:
target- object from which to get key- Returns:
- Key field value
- Throws:
IllegalArgumentException- if the target object doesn't declare aKeyannotationRuntimeException- if the field cannot be read on the target object
-
setKey
public static void setKey(@Nonnull Object target, @Nonnull @NotEmpty String key)Sets the value of the field indicated by theKeyannotation on the given object.- Parameters:
target- Object on which to set keykey- Key value- Throws:
IllegalArgumentException- if the target object doesn't declare aKeyannotationRuntimeException- if the field cannot be set on the target object
-
getValue
@Nonnull @NotEmpty public static String getValue(@Nonnull Object target)
Gets the value of the field indicated by theValueannotation on the given object.If the value is not a
String, then it is converted to one by callingObject.toString()on the object.- Parameters:
target- Object from which to get value- Returns:
- Value field value
- Throws:
IllegalArgumentException- if the target object doesn't declare aValueannotationRuntimeException- if the field cannot be read on the target object
-
setValue
public static void setValue(@Nonnull Object target, @Nonnull @NotEmpty String value)Sets the value of the field indicated by theValueannotation on the given object.- Parameters:
target- object on which to set valuevalue- Value field value- Throws:
IllegalArgumentException- if the target object doesn't declare aValueannotationRuntimeException- if the field cannot be set on the target object
-
getExpiration
@Nullable public static Long getExpiration(@Nonnull Object target)
Gets the value of the field indicated by theExpirationannotation 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:
- Parameters:
target- Object from which to get expiration- Returns:
- Expiration field value, or null
- Throws:
IllegalArgumentException- if the target object doesn't declare aExpirationannotationRuntimeException- if the field cannot be read on the target object or if it is an unsupported data type
-
setExpiration
public static void setExpiration(@Nonnull Object target, @Nullable Long expiration)Sets the value of the field indicated by theExpirationannotation on the given object.The expiration is expressed in milliseconds since the beginning of the Unix epoch. The following data types are supported:
- Parameters:
target- object on which to set expirationexpiration- value to set- Throws:
IllegalArgumentException- if the target object doesn't declare aExpirationannotationRuntimeException- if the field cannot be set on the target object or if it is an unsupported data type
-
getAnnotation
@Nonnull private static <T extends Annotation> T getAnnotation(@Nonnull Object target, @Nonnull Class<T> annotationType)
Returns an annotation of a specified type attached to a target object.- Type Parameters:
T- type of annotation- Parameters:
target- target object to examineannotationType- class type of annotation to find- Returns:
- the annotation found on the object
- Throws:
IllegalArgumentException- if the target object doesn't declare the annotation
-
getFieldValue
@Nullable private static Object getFieldValue(@Nonnull Object target, @Nonnull String fieldName)
Returns the value of a field from an object using reflection.- Parameters:
target- target object to examinefieldName- name of the field to retrieve- Returns:
- the value of the field
- Throws:
RuntimeException- if the field cannot be read
-
setFieldValue
private static void setFieldValue(@Nonnull Object target, @Nonnull String fieldName, @Nullable Object fieldValue)Returns the value of a field from an object using reflection.- Parameters:
target- target object to updatefieldName- name of the field to setfieldValue- value to set- Throws:
RuntimeException- if the field cannot be set
-
getField
@Nonnull private static Field getField(@Nonnull Object target, @Nonnull String fieldName)
Returns aFieldfrom a target object.- Parameters:
target- target object to examinefieldName- name of the field to retrieve- Returns:
- the specified field from the object
- Throws:
RuntimeException- if the target object doesn't declare the annotation
-
-