Package com.jalios.util
Class ReflectUtil
- java.lang.Object
-
- com.jalios.util.ReflectUtil
-
public class ReflectUtil extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description ReflectUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
copyDeclaredFields(java.lang.Object src, java.lang.Object dest)
Copy all fields (whatever access status, and on whatever superclass they were defined) that can be found on this class.static java.lang.Object
fieldGetter(java.lang.Object instance, java.lang.reflect.Field field)
Return the return value of a getter call from a given field on a given instancestatic void
fieldSetter(java.lang.Object obj, java.lang.reflect.Field field, java.lang.Object fieldValue)
static java.lang.String
generateFieldName(java.lang.String label, int maxLength)
Returns a compliant java field name from the given label.static java.lang.reflect.Field[]
getAllFields(java.lang.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.static java.lang.reflect.Field
getField(java.lang.Class<?> clazz, java.lang.String attName)
Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.static java.lang.Object
getFieldValue(java.lang.Object instance, java.lang.reflect.Field field)
Returns the field value corresponding to the field argumentstatic java.lang.String
getGetterMethodName(java.lang.reflect.Field field)
Return the name of the getter method for a field.static java.lang.reflect.Method
getMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameters)
Return the return value of a getter call from a given field on a given instancestatic java.lang.String
getSetterMethodName(java.lang.reflect.Field field)
Return the name of the setter method for a field.static java.util.Map
introspectFields(java.lang.Class clazz)
Introspect a Class to retrieve public fieldsstatic java.util.Map
introspectFields(java.lang.Object obj)
Convenient method to introspect fields of an objectstatic java.util.Map
introspectFields(java.lang.Object instance, java.util.Map fields)
Introspect an Object by calling given fieldsstatic java.lang.Object
invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... args)
Invokes the underlying method represented by the method name in parameter, on the specified object with the specified parameters.static java.lang.Object
invokeStaticMethod(java.lang.String className, java.lang.String methodName, java.lang.Object... args)
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.static void
setFieldValue(java.lang.Object storable, java.lang.reflect.Field field, java.lang.Object fieldValue)
Set a field to a value
-
-
-
Method Detail
-
getGetterMethodName
public static java.lang.String getGetterMethodName(java.lang.reflect.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 java.lang.reflect.Field getField(java.lang.Class<?> clazz, java.lang.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 interfaceattName
- the name of the desired field- Returns:
- the field instance
-
getFieldValue
public static java.lang.Object getFieldValue(java.lang.Object instance, java.lang.reflect.Field field)
Returns the field value corresponding to the field argument- Parameters:
instance
- - the instance containing the fieldfield
- - the requested field- Returns:
- the value of the field
-
fieldGetter
public static java.lang.Object fieldGetter(java.lang.Object instance, java.lang.reflect.Field field)
Return the return value of a getter call from a given field on a given instance- Parameters:
instance
- the objectfield
- the field- Returns:
- the field getter result
- Since:
- jcms-4.2
-
getSetterMethodName
public static java.lang.String getSetterMethodName(java.lang.reflect.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(java.lang.Object storable, java.lang.reflect.Field field, java.lang.Object fieldValue)
Set a field to a value- Parameters:
storable
- - the instance containing the fieldfield
- - the field to setfieldValue
- - the new field value
-
fieldSetter
public static void fieldSetter(java.lang.Object obj, java.lang.reflect.Field field, java.lang.Object fieldValue)
-
introspectFields
public static java.util.Map introspectFields(java.lang.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 java.util.Map introspectFields(java.lang.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 java.util.Map introspectFields(java.lang.Object instance, java.util.Map fields)
Introspect an Object by calling given fields- Parameters:
instance
- the instance to introspectfields
- a Map of FieldName / Field to call- Returns:
- a Map of FieldName / Value
- Since:
- jcms-4.2
-
getAllFields
public static java.lang.reflect.Field[] getAllFields(java.lang.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 ofClass.getDeclaredFields()
which ignores and super-classes, andClass.getFields()
which ignored non-public fields.- Parameters:
clazz
- The class to introspect- Returns:
- The complete list of fields
-
copyDeclaredFields
public static void copyDeclaredFields(java.lang.Object src, java.lang.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 copydest
- Object to set
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... args) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.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:
java.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible via reflection
-
invokeStaticMethod
public static java.lang.Object invokeStaticMethod(java.lang.String className, java.lang.String methodName, java.lang.Object... args) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.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:
java.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.ClassNotFoundException
- if there is no such accessible Class with the class name in parameter.java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible via reflection
-
generateFieldName
public static java.lang.String generateFieldName(java.lang.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 labelmaxLength
- 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 java.lang.reflect.Method getMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameters)
Return the return value of a getter call from a given field on a given instance- Parameters:
clazz
- theClass
methodName
- the name of the methodparameters
- the parameters- Returns:
- the
Method
- Since:
- jcms-10.0.1
-
-