Class AnnotationSupport


  • public final class AnnotationSupport
    extends Object
    Support class that reads and writes fields decorated with storage annotations.
    • Field Detail

      • FIELD_CACHE

        private static final Map<String,​Field> FIELD_CACHE
        Simple cache of fields we have seen.
    • Constructor Detail

      • AnnotationSupport

        private AnnotationSupport()
        Private constructor of utility class.
    • Method Detail

      • getContext

        @Nonnull
        @NotEmpty
        public static String getContext​(@Nonnull
                                        Object target)
        Gets the value of the field indicated by the 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.

        Parameters:
        target - object from which to get context
        Returns:
        Context field value
        Throws:
        IllegalArgumentException - if the target object doesn't declare a Context annotation
        RuntimeException - 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 the Context annotation on the given object.
        Parameters:
        target - Object on which to set context
        context - Context value
        Throws:
        IllegalArgumentException - if the target object doesn't declare a Context annotation
        RuntimeException - 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 the 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.

        Parameters:
        target - object from which to get key
        Returns:
        Key field value
        Throws:
        IllegalArgumentException - if the target object doesn't declare a Key annotation
        RuntimeException - 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 the Key annotation on the given object.
        Parameters:
        target - Object on which to set key
        key - Key value
        Throws:
        IllegalArgumentException - if the target object doesn't declare a Key annotation
        RuntimeException - 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 the 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.

        Parameters:
        target - Object from which to get value
        Returns:
        Value field value
        Throws:
        IllegalArgumentException - if the target object doesn't declare a Value annotation
        RuntimeException - 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 the Value annotation on the given object.
        Parameters:
        target - object on which to set value
        value - Value field value
        Throws:
        IllegalArgumentException - if the target object doesn't declare a Value annotation
        RuntimeException - 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 the 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:

        Parameters:
        target - Object from which to get expiration
        Returns:
        Expiration field value, or null
        Throws:
        IllegalArgumentException - if the target object doesn't declare a Expiration annotation
        RuntimeException - 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 the 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:

        Parameters:
        target - object on which to set expiration
        expiration - value to set
        Throws:
        IllegalArgumentException - if the target object doesn't declare a Expiration annotation
        RuntimeException - 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 examine
        annotationType - 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 examine
        fieldName - 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 update
        fieldName - name of the field to set
        fieldValue - value to set
        Throws:
        RuntimeException - if the field cannot be set
      • getField

        @Nonnull
        private static Field getField​(@Nonnull
                                      Object target,
                                      @Nonnull
                                      String fieldName)
        Returns a Field from a target object.
        Parameters:
        target - target object to examine
        fieldName - name of the field to retrieve
        Returns:
        the specified field from the object
        Throws:
        RuntimeException - if the target object doesn't declare the annotation