Package org.apache.empire.commons
Class ClassUtils
java.lang.Object
org.apache.empire.commons.ClassUtils
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean[]copy(boolean[] other) Copies a boolean arraystatic Object[]Copies an Object arraystatic String[]Copies a String arraystatic <T> Tcopy(T obj) Makes a copy of an object if possible or returns the object itself if copy is not supportedstatic <T> Tcopy(T obj, int flags) Makes a copy of an object if possible or returns null or self (depending on flags)static <T> TcopySmart(T obj) Makes a copy of an object if the object is an array or a collection (List,Set,Map,...)static <T> Constructor<T>findMatchingConstructor(Class<T> clazz, int minParams, Class<?>... parameterTypes) copied from org.apache.commons.beanutils.ConstructorUtils since it's private therestatic ObjectgetFieldValue(Class<?> clazz, Object object, String property, boolean includePrivateFields) Retrieve a field value using reflectionstatic ObjectgetFieldValue(Object object, String property) Retrieve a field value using reflection The field accessor must be publicstatic StringgetImplemenationJarName(Class<?> clazz) Returns the JAR name that contains the implementation of a specific classstatic StringgetImplemenationJarName(String className) Returns the JAR name that contains the implementation of a specific classstatic ObjectgetPrivateFieldValue(Object object, String property) Retrieve a field value using reflectionstatic ObjectinvokeMethod(Class<?> clazz, Object object, String methodName, Class<?>[] paramTypes, Object[] paramValues, boolean makeAccessible) Invoke a method on an object using reflectionstatic ObjectinvokeMethod(Object object, Method method, Object... params) Invoke a method with parameters on an objectstatic ObjectinvokeMethod(Object object, String methodName, Class<?> paramType, Object paramValue) Invokes a method with one parameter on an object using reflectionstatic ObjectinvokeSimpleMethod(Class<?> clazz, Object object, String methodName, boolean makeAccessible) Invoke a simple method (without parameters) on an object using reflectionstatic ObjectinvokeSimpleMethod(Object object, Method method) Invoke a simple method (without parameters) on an objectstatic ObjectinvokeSimpleMethod(Object object, String methodName) Invoke a simple method (without parameters) on an object using reflectionstatic ObjectinvokeSimplePrivateMethod(Object object, String methodName) Invoke a simple method (without parameters) on an object using reflectionstatic booleanisImmutableClass(Class<?> clazz) Checks if a class is an immutable class such as a wrapper class of a primitive type String and Class are also considered to be a immutable classesstatic <T> TnewInstance(Class<T> typeClass) Creates a new Object instancestatic <T> TnewInstance(Class<T> typeClass, Class<?> paramClass, Object paramValue) Creates a new Object instance with a single parameter constructorstatic <T> TnewInstance(Constructor<T> typeConstructor, Object... params) Creates a new Object instancestatic voidsetPrivateFieldValue(Class<?> clazz, Object object, String property, Object value) Modifies a private field value using reflectionstatic voidsetPrivateFieldValue(Object object, String property, Object value) Modifies a private field value using reflectionstatic <T> TtestSerialization(Class<T> clazz, T objToSerialize) Used to test Serialization
-
Field Details
-
EMPTY_ARGS
-
-
Method Details
-
testSerialization
Used to test Serialization- Type Parameters:
T- the class type- Parameters:
clazz- class to serializeobjToSerialize- objedt to serialize- Returns:
- the unserializedObject
-
isImmutableClass
Checks if a class is an immutable class such as a wrapper class of a primitive type String and Class are also considered to be a immutable classes- Parameters:
clazz- the class to check- Returns:
- true if the class is immutable
-
copy
public static boolean[] copy(boolean[] other) Copies a boolean array- Parameters:
other- array to copy- Returns:
- the copy
-
copy
Copies an Object array- Parameters:
other- array to copy- Returns:
- the copy
-
copy
Copies a String array- Parameters:
other- array to copy- Returns:
- the copy
-
copy
public static <T> T copy(T obj) Makes a copy of an object if possible or returns the object itself if copy is not supported- Type Parameters:
T- the class type- Parameters:
obj- the object to copy- Returns:
- either a copy of the object or the object itself if copy is not supported
-
copy
public static <T> T copy(T obj, int flags) Makes a copy of an object if possible or returns null or self (depending on flags)- Type Parameters:
T- the class type- Parameters:
obj- the object to copyflags- options for the copy- Returns:
- either a copy of the object or null the object itself
-
copySmart
public static <T> T copySmart(T obj) Makes a copy of an object if the object is an array or a collection (List,Set,Map,...) Otherwise the object itself is returned- Type Parameters:
T- the class type- Parameters:
obj- the object to copy- Returns:
- either a copy of the object or the object itself if copy is not supported
-
getFieldValue
public static Object getFieldValue(Class<?> clazz, Object object, String property, boolean includePrivateFields) Retrieve a field value using reflection- Parameters:
clazz- the class from which to obtain the fieldobject- the object instance from which to obtain the fieldproperty- the property to obtainincludePrivateFields- flag whether or not to include private fields- Returns:
- the property value
-
getFieldValue
Retrieve a field value using reflection The field accessor must be public- Parameters:
object- the object instance from which to obtain the fieldproperty- the property to obtain- Returns:
- the property value
-
getPrivateFieldValue
Retrieve a field value using reflection- Parameters:
object- the object instance from which to obtain the fieldproperty- the property to obatin- Returns:
- the property value
-
setPrivateFieldValue
public static void setPrivateFieldValue(Class<?> clazz, Object object, String property, Object value) Modifies a private field value using reflection- Parameters:
clazz- the class of the objectobject- the object or null if static fields are to be changedproperty- the field namevalue- the field value
-
setPrivateFieldValue
Modifies a private field value using reflection- Parameters:
object- the object or null if static fields are to be changedproperty- the field namevalue- the field value
-
newInstance
Creates a new Object instance- Type Parameters:
T- the class type- Parameters:
typeClass- the class of the object to instantiate- Returns:
- the instance
-
newInstance
Creates a new Object instance with a single parameter constructor- Type Parameters:
T- the type of the new object- Parameters:
typeClass- the type of the object to instantiateparamClass- the type of the constructor parameterparamValue- the value of the constructor parameter- Returns:
- the instance
-
newInstance
Creates a new Object instance- Type Parameters:
T- the type of the new object- Parameters:
typeConstructor- the constructor of the object to instantiateparams- the constructor params- Returns:
- the instance
-
findMatchingConstructor
public static <T> Constructor<T> findMatchingConstructor(Class<T> clazz, int minParams, Class<?>... parameterTypes) copied from org.apache.commons.beanutils.ConstructorUtils since it's private there- Type Parameters:
T- the class type- Parameters:
clazz- the class of the objectminParams- minimum number of paramsparameterTypes- the param types- Returns:
- the constructor or null
-
invokeMethod
public static Object invokeMethod(Class<?> clazz, Object object, String methodName, Class<?>[] paramTypes, Object[] paramValues, boolean makeAccessible) Invoke a method on an object using reflection- Parameters:
clazz- the class from which to obtain the fieldobject- the object instance on which to invoke the methodmethodName- the name of the method to invokeparamTypes- the list of parameter typesparamValues- the list of parameter valuesmakeAccessible- flag whether to make private methods accessible- Returns:
- the return value of the method
-
invokeMethod
public static Object invokeMethod(Object object, String methodName, Class<?> paramType, Object paramValue) Invokes a method with one parameter on an object using reflection- Parameters:
object- the object instance on which to invoke the methodmethodName- the name of the method to invokeparamType- the method parameter typeparamValue- the method parameter value- Returns:
- the return value of the method
-
invokeSimpleMethod
public static Object invokeSimpleMethod(Class<?> clazz, Object object, String methodName, boolean makeAccessible) Invoke a simple method (without parameters) on an object using reflection- Parameters:
clazz- the class on which to invoke the methodobject- the object instance on which to invoke the methodmethodName- the name of the method to invokemakeAccessible- flag whether or not to make the method accessible (eg. if private)- Returns:
- the return value of the method
-
invokeSimpleMethod
Invoke a simple method (without parameters) on an object using reflection- Parameters:
object- the object instance on which to invoke the methodmethodName- the name of the method to invoke- Returns:
- the return value of the method
-
invokeSimplePrivateMethod
Invoke a simple method (without parameters) on an object using reflection- Parameters:
object- the object instance on which to invoke the methodmethodName- the name of the method to invoke- Returns:
- the return value of the method
-
invokeSimpleMethod
Invoke a simple method (without parameters) on an object- Parameters:
object- the object on which to invoke the methodmethod- the method- Returns:
- the return value
-
invokeMethod
Invoke a method with parameters on an object- Parameters:
object- the object on which to invoke the methodmethod- the methodparams- the method params- Returns:
- the return value
-
getImplemenationJarName
Returns the JAR name that contains the implementation of a specific class- Parameters:
clazz- the class- Returns:
- the JAR File Name
-
getImplemenationJarName
Returns the JAR name that contains the implementation of a specific class- Parameters:
className- the class Name of the class- Returns:
- the JAR File Name or "[ClassNotFoundException]" if the class is not available
-