Class ReflectUtil

java.lang.Object
com.jalios.util.ReflectUtil

public class ReflectUtil extends Object
  • Constructor Details

    • ReflectUtil

      public ReflectUtil()
  • Method Details

    • getGetterMethodName

      public static String getGetterMethodName(Field field)
      Return the name of the getter method for a field. Field's name is supposed to be conformed with JavaBeans design patterns. Examples:
       
       int status; -> getStatus()
       boolean isEnabled -> isEnabled();
       
       
      Parameters:
      field - the field
      Returns:
      the getter method name
      Since:
      jcms-4.2
    • getField

      public static Field getField(Class<?> clazz, String attName)
      Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
      Parameters:
      clazz - specifies the class or interface
      attName - the name of the desired field
      Returns:
      the field instance
    • getFieldValue

      public static Object getFieldValue(Object instance, Field field)
      Returns the field value corresponding to the field argument
      Parameters:
      instance - - the instance containing the field
      field - - the requested field
      Returns:
      the value of the field
    • fieldGetter

      public static Object fieldGetter(Object instance, Field field)
      Return the return value of a getter call from a given field on a given instance
      Parameters:
      instance - the object
      field - the field
      Returns:
      the field getter result
      Since:
      jcms-4.2
    • getSetterMethodName

      public static String getSetterMethodName(Field field)
      Return the name of the setter method for a field. Field's name is supposed to be conformed with JavaBeans design patterns. Examples: Examples:
       int status; -> setStatus() 
       
      Parameters:
      field - the field
      Returns:
      the setter method name
      Since:
      jcms-4.2
    • setFieldValue

      public static void setFieldValue(Object storable, Field field, Object fieldValue)
      Set a field to a value
      Parameters:
      storable - - the instance containing the field
      field - - the field to set
      fieldValue - - the new field value
    • fieldSetter

      public static void fieldSetter(Object obj, Field field, Object fieldValue)
    • introspectFields

      public static Map introspectFields(Object obj)
      Convenient method to introspect fields of an object
      Parameters:
      obj - the object to introspect
      Returns:
      a Map of FieldName / Value
      Since:
      jcms-4.2
    • introspectFields

      public static Map introspectFields(Class clazz)
      Introspect a Class to retrieve public fields
      Parameters:
      clazz - the class to introspect
      Returns:
      a Map of FieldName / Field
      Since:
      jcms-4.2
    • introspectFields

      public static Map introspectFields(Object instance, Map fields)
      Introspect an Object by calling given fields
      Parameters:
      instance - the instance to introspect
      fields - a Map of FieldName / Field to call
      Returns:
      a Map of FieldName / Value
      Since:
      jcms-4.2
    • getAllFields

      public static Field[] getAllFields(Class<?> clazz)
      Return a list of all fields (whatever access status, and on whatever superclass they were defined) that can be found on this class. This is like a union of Class.getDeclaredFields() which ignores and super-classes, and Class.getFields() which ignored non-public fields.
      Parameters:
      clazz - The class to introspect
      Returns:
      The complete list of fields
    • copyDeclaredFields

      public static void copyDeclaredFields(Object src, Object dest)
      Copy all fields (whatever access status, and on whatever superclass they were defined) that can be found on this class.
      Parameters:
      src - Object to copy
      dest - Object to set
    • invokeMethod

      public static Object invokeMethod(Object object, String methodName, Object... args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
      Invokes the underlying method represented by the method name in parameter, on the specified object with the specified parameters.

      This is a convenient wrapper for MethodUtils.invokeMethod(Object, String, Object[]).

      Parameters:
      object - The object the underlying method is invoked from.
      methodName - The name of the method to invoke.
      args - The arguments used for the method call.
      Returns:
      The value returned by the invoked method.
      Throws:
      NoSuchMethodException - if there is no such accessible method
      InvocationTargetException - wraps an exception thrown by the method invoked
      IllegalAccessException - if the requested method is not accessible via reflection
    • invokeStaticMethod

      public static Object invokeStaticMethod(String className, String methodName, Object... args) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
      Invokes the underlying method represented by the method name in parameter, on the specified Class represented by the class name in parameter with the specified parameters.

      This is a convenient wrapper for MethodUtils.invokeStaticMethod(Class, String, Object[]).

      Parameters:
      className - The name of the Class.
      methodName - The name of the method to invoke.
      args - The arguments used for the method call.
      Returns:
      the result of dispatching the method represented by this object on obj with parameters params.
      Throws:
      NoSuchMethodException - if there is no such accessible method
      ClassNotFoundException - if there is no such accessible Class with the class name in parameter.
      InvocationTargetException - wraps an exception thrown by the method invoked
      IllegalAccessException - if the requested method is not accessible via reflection
    • generateFieldName

      public static String generateFieldName(String label, int maxLength)
      Returns a compliant java field name from the given label. Eg generate "myFieldName" from label "My field Name"
      Parameters:
      label - the label
      maxLength - the max length for the field name.
      Returns:
      a compliant java field name from the given label.
      Since:
      jcms-9.0.1
    • getMethod

      public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameters)
      Return the return value of a getter call from a given field on a given instance
      Parameters:
      clazz - the Class
      methodName - the name of the method
      parameters - the parameters
      Returns:
      the Method
      Since:
      jcms-10.0.1