Package com.jalios.util
Class ReflectUtil
java.lang.Object
com.jalios.util.ReflectUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopyDeclaredFields(Object src, Object dest) Copy all fields (whatever access status, and on whatever superclass they were defined) that can be found on this class.static ObjectfieldGetter(Object instance, Field field) Return the return value of a getter call from a given field on a given instancestatic voidfieldSetter(Object obj, Field field, Object fieldValue) static StringgenerateFieldName(String label, int maxLength) Returns a compliant java field name from the given label.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.static FieldReturns a Field object that reflects the specified declared field of the class or interface represented by this Class object.static ObjectgetFieldValue(Object instance, Field field) Returns the field value corresponding to the field argumentstatic StringgetGetterMethodName(Field field) Return the name of the getter method for a field.static MethodReturn the return value of a getter call from a given field on a given instancestatic StringgetSetterMethodName(Field field) Return the name of the setter method for a field.static MapintrospectFields(Class clazz) Introspect a Class to retrieve public fieldsstatic MapintrospectFields(Object obj) Convenient method to introspect fields of an objectstatic MapintrospectFields(Object instance, Map fields) Introspect an Object by calling given fieldsstatic ObjectinvokeMethod(Object object, String methodName, Object... args) Invokes the underlying method represented by the method name in parameter, on the specified object with the specified parameters.static ObjectinvokeStaticMethod(String className, String methodName, 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 voidsetFieldValue(Object storable, Field field, Object fieldValue) Set a field to a value
-
Constructor Details
-
ReflectUtil
public ReflectUtil()
-
-
Method Details
-
getGetterMethodName
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
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
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
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
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
Set a field to a value- Parameters:
storable- - the instance containing the fieldfield- - the field to setfieldValue- - the new field value
-
fieldSetter
-
introspectFields
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
Introspect a Class to retrieve public fields- Parameters:
clazz- the class to introspect- Returns:
- a Map of FieldName / Field
- Since:
- jcms-4.2
-
introspectFields
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
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
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 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 methodInvocationTargetException- wraps an exception thrown by the method invokedIllegalAccessException- 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 methodClassNotFoundException- if there is no such accessible Class with the class name in parameter.InvocationTargetException- wraps an exception thrown by the method invokedIllegalAccessException- if the requested method is not accessible via reflection
-
generateFieldName
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
Return the return value of a getter call from a given field on a given instance
-