Class FieldHelper


  • public class FieldHelper
    extends Object
    Contains helper methods for accessing java fields
    • Constructor Detail

      • FieldHelper

        public FieldHelper()
    • Method Detail

      • copyFields

        public static <T,​E extends T> void copyFields​(T srcObj,
                                                            E targetObj)
        Copy all fields from the given source to the target objects
      • get

        public static <T> T get​(Class<?> clazz,
                                String name)
        Returns the value of a static field
      • get

        public static <T> T get​(Object object,
                                String name)
        Returns the value of a field
      • getAnnotatedField

        public static Field getAnnotatedField​(Class<? extends Object> clazz,
                                              Class<? extends Annotation> annotation)
        Returns the first field with the given annotation or null if there is no such field.
        If there are more than one field annotated it returns the first one according to the method
      • getAnnotation

        public static <T extends Annotation> T getAnnotation​(Class<? extends Object> clazz,
                                                             String name,
                                                             Class<T> annotation)
        Returns the given annotation for the given field, if any
      • getField

        public static Field getField​(Class<? extends Object> clazz,
                                     String name)
        Returns whether the given field has the given annotation class, or null if the field doesn't exist
      • hasAnnotation

        public static boolean hasAnnotation​(Class<? extends Object> clazz,
                                            String name,
                                            Class<? extends Annotation> annotation)
        Returns whether the given field has the given annotation class
      • readFields

        public static Map<Field,​Object> readFields​(Object object)
        Reads all fields (even private ones) of the given object
      • set

        public static void set​(Class<?> clazz,
                               String name,
                               Object value)
        Sets the value of a static field
      • set

        public static void set​(Object object,
                               String name,
                               Object value)
        Sets the value of a field