public class ObjectUtil extends Object
Class.forName
Modifier and Type | Class and Description |
---|---|
protected static class |
ObjectUtil.ClassLoaderObjectInputStream |
Modifier and Type | Field and Description |
---|---|
static String |
BOOLEAN_FALSE |
static String |
BOOLEAN_NO |
static String |
BOOLEAN_OFF |
static String |
BOOLEAN_ON |
static String |
BOOLEAN_TRUE |
static String |
BOOLEAN_YES |
protected static String |
CREATE_OBJECT |
static String |
DOT_SIGN
Package/Class Name separator
|
protected static String |
GET_CLASS |
protected static ClassLoader |
m_classLoader
The current ClassLoader used by
createObject(java.lang.String) & getClass(java.lang.String) |
protected static Map |
m_Fields
Map of object fields, used for property caching
|
protected static Map |
m_Properties
Map of object properties, used for property caching
|
protected static boolean |
m_UsePropertyCache
Use the property cache when resolving properties via reflection
|
protected static String |
NEW_INSTANCE |
protected static Object |
NOT_CACHED_CLASS
Class used when unable to resolve a cached property
|
Constructor and Description |
---|
ObjectUtil() |
Modifier and Type | Method and Description |
---|---|
static Object |
clone(Object object)
Clone an object that implements serializable
|
static int |
compare(Object o1,
Object o2)
Compares two object.
|
static Object[] |
convertListTypes(Class[] types,
Object[] values)
Converts source object value to the target type
|
static Object |
convertToPropertyType(Object anInstance,
String aPropertyName,
String aStringValue) |
static Object |
convertToType(Object srcObj,
Class targetType)
Converts source object value to the target type
|
static Object |
createObject(String className)
A class factory method that uses the pluggable classLoader:
m_classLoader
Note, all exceptions are caught |
static Object |
createObject(String className,
Object[] parms)
A class factory method that uses the pluggable classLoader:
m_classLoader
Note, all exceptions are caught |
static boolean |
equal(Object obj1,
Object obj2)
Simply compares two objects using equals() method and takes care about nulls
null == null = false
|
static void |
flushPropertyDescriptorCache()
Clears the property descriptors cache
NOTE: needs to be called after cached classes are reloaded
|
static Object |
getCachedPropertyDescriptor(Object anInstance,
String aPropertyName)
Returns cached property descriptor from the cache.
|
static Class |
getClass(String className)
A class factory method that uses the pluggable classLoader:
m_classLoader |
static ClassLoader |
getClassLoader()
Get the current classloader, using the classloader of the current thread if not found
|
static Constructor |
getConstructor(Class clazz,
Object[] args)
Get a constructor for a class that uses the given set of arguments
|
static List |
getEnumerationValues(Class type)
Retrieve a list of all the possible values for the given
enumeration type
|
static Field |
getField(Class klass,
String property) |
static Object |
getField(Object instance,
String name)
Retrieves a data field, potentially defined by a super class.
|
static Field |
getFieldDescriptor(Class cls,
String property)
Returns field descriptor of the selected property for the
selected object instance.
|
static Method |
getMatchingMethod(Class<?> clas,
String methodName,
List arguments) |
static int |
getNamedValue(Class clazz,
String name)
use some reflection to get the named property
|
static Object |
getPrivilegedProperty(Object instance,
String propertyName)
Retrieves a privilged property
|
static Object |
getPrivilegedStaticProperty(Class clazz,
String propertyName)
Retrieves a privilged property from a static class
|
static Map |
getProperties(Object instance) |
static Object |
getProperty(Object anInstance,
String aPropertyName)
Obtains a value of the property specified by name for the specified instance
|
static boolean |
getPropertyAsBoolean(Object sourceObj,
String propertyName,
boolean defaultValue) |
static int |
getPropertyAsInt(Object sourceObj,
String propertyName,
int defaultValue) |
static String |
getPropertyAsString(Object sourceObj,
String propertyName,
String defaultValue) |
static PropertyDescriptor |
getPropertyDescriptor(Object anInstance,
String aPropertyName)
Returns property descriptor of the selected property for the
selected object instance.
|
static PropertyDescriptor[] |
getPropertyDescriptors(Class instanceClass)
Returns property descriptor list for a given object class
selected object instance.
|
static Object |
getPropertyOrFieldValue(Object target,
String property)
Obtains object property via getter method (preferred),
or directly via member field.
|
static Object |
getStaticProperty(Class clazz,
String fieldName)
Returns the named static variable.
|
static boolean |
hadValueChange(Object oldValue,
Object newValue)
Checks wether there was a change in the value
|
static boolean |
isArray(Object toCheck)
Checks whether given instance is an array
|
static boolean |
isArrayOf(Object toCheck,
Class arrayItem)
Checks whether given instance is an array of the given classes
|
static boolean |
isEnumeration(Class type)
Determine if the given class is an enumeration
|
static String |
justClassName(String aFullClassName)
Return short class name without package names
|
static Object |
load(byte[] buffer)
Load an object from an array of bytes.
|
static Object |
load(InputStream in)
Load an object from an inputstream
|
static Object |
load(InputStream in,
ClassLoader cl) |
static byte[] |
save(Object object)
Serialize an object to an array of bytes.
|
static void |
setClassLoader(ClassLoader classLoader)
Set the classloader to be used by this ObjectUtil instance
|
static boolean |
setPrivilegedProperty(Object instance,
Class clazz,
String propertyName,
Object value)
Gets a priviledged property from an object
|
static boolean |
setPrivilegedProperty(Object instance,
String propertyName,
Object value)
Gets a priviledged property from an object
|
static boolean |
setPrivilegedStaticProperty(Class clazz,
String propertyName,
Object value)
Gets a priviledged property from a static class
|
static void |
setPropertiesBatch(Object anInstance,
Map propertyValues) |
static void |
setPropertiesByMap(Object fromObject,
Object toObject,
String[][] propMap,
boolean reverse)
Sets object properties using translation map to resolve property names
|
static void |
setProperty(Object anInstance,
String aPropertyName,
Object aValue)
Set's a particular property of the instance
Usage:
SomeBean bean = new SomeBean();
Integer propertyValue = new Integer(10);
ClassTools.setProperty(bean, "propertyName", propertyValue);
is in fact equal to
bean.setPropertyValue(propertyValue);
Simple types are also supported
int propertyValue = 10;
ClassTools.setProperty(bean, "intPropertyName", new Integer(propertyValue));
|
static boolean |
setPropertyByString(Object anInstance,
String aPropertyName,
String aStringValue)
Set's a particular property of the instance by string value
Usage:
SomeBean bean = new SomeBean();
ObjectUtil.setPropertyByString(bean, "someProperty", "10");
|
static void |
setStaticProperty(Class clazz,
String fieldName,
Object value)
Sets a value into static member of the class
|
static Object |
StringToValue(String s,
Class t)
Deprecated.
use
convertToType(Object, Class) instead |
static String |
toString(String comment,
Object instance) |
static String |
toString(String comment,
Object instance,
boolean filterNulls) |
public static final String DOT_SIGN
public static final String BOOLEAN_TRUE
public static final String BOOLEAN_FALSE
public static final String BOOLEAN_YES
public static final String BOOLEAN_NO
public static final String BOOLEAN_ON
public static final String BOOLEAN_OFF
protected static final String CREATE_OBJECT
protected static final String GET_CLASS
protected static final String NEW_INSTANCE
protected static ClassLoader m_classLoader
createObject(java.lang.String)
& getClass(java.lang.String)
protected static boolean m_UsePropertyCache
protected static Map m_Properties
protected static Map m_Fields
protected static final Object NOT_CACHED_CLASS
public static Object createObject(String className)
m_classLoader
Note, all exceptions are caughtclassName
- getClass(java.lang.String)
public static Object createObject(String className, Object[] parms)
m_classLoader
Note, all exceptions are caughtclassName
- the class to create an instance ofparms
- the parameter array to pass to the object constructorgetClass(java.lang.String)
public static Constructor getConstructor(Class clazz, Object[] args)
clazz
- the class to retrieve the constructor forargs
- the arguments to be passed the constructorpublic static Class getClass(String className) throws ClassNotFoundException
m_classLoader
className
- ClassNotFoundException
createObject(java.lang.String)
public static ClassLoader getClassLoader()
m_classLoader
if set, the default ClassLoader otherwisem_classLoader
public static void setClassLoader(ClassLoader classLoader)
classLoader
- m_classLoader
public static Object StringToValue(String s, Class t)
convertToType(Object, Class)
insteads
- the source string to convertt
- the Class to attempt to convert the string topublic static boolean isArrayOf(Object toCheck, Class arrayItem)
toCheck
- the Object to check for typearrayItem
- the Class to check if the object is an array ofpublic static boolean isArray(Object toCheck)
toCheck
- the object to determine is an arraypublic static Object getCachedPropertyDescriptor(Object anInstance, String aPropertyName)
anInstance
- the object instance to get properties foraPropertyName
- the name of the property to retrieve the descriptor forpublic static void flushPropertyDescriptorCache()
public static Field getFieldDescriptor(Class cls, String property) throws Exception
cls
- the class to get the field descriptor fromproperty
- the property name of the descriptorException
public static PropertyDescriptor getPropertyDescriptor(Object anInstance, String aPropertyName) throws RuntimeException
anInstance
- RuntimeException
public static PropertyDescriptor[] getPropertyDescriptors(Class instanceClass)
instanceClass
- public static void setProperty(Object anInstance, String aPropertyName, Object aValue) throws Exception
SomeBean bean = new SomeBean();
Integer propertyValue = new Integer(10);
ClassTools.setProperty(bean, "propertyName", propertyValue);
is in fact equal to
bean.setPropertyValue(propertyValue);
Simple types are also supported
int propertyValue = 10;
ClassTools.setProperty(bean, "intPropertyName", new Integer(propertyValue));
Exception
public static boolean setPropertyByString(Object anInstance, String aPropertyName, String aStringValue)
public static Object getPropertyOrFieldValue(Object target, String property) throws Exception
Exception
public static Object getProperty(Object anInstance, String aPropertyName) throws RuntimeException
anInstance
- RuntimeException
public static Object getStaticProperty(Class clazz, String fieldName)
Object value = ObjectUtil.getStaticProperty(metaUtilClass, "PUBLIC");
public static void setStaticProperty(Class clazz, String fieldName, Object value)
clazz
- source classfieldName
- field namevalue
- value to be setpublic static Object convertToPropertyType(Object anInstance, String aPropertyName, String aStringValue)
public static boolean hadValueChange(Object oldValue, Object newValue)
public static String justClassName(String aFullClassName)
public static Object clone(Object object) throws CloneNotSupportedException
object
- Instance of object to cloneCloneNotSupportedException
public static boolean equal(Object obj1, Object obj2)
public static byte[] save(Object object)
object
- the object to serialize. Must implement Serializablepublic static Object load(byte[] buffer)
buffer
- the array of bytes that constitute the objectpublic static Object load(InputStream in, ClassLoader cl)
public static Object load(InputStream in)
in
- the InputStream that constitute the objectpublic static boolean setPrivilegedProperty(Object instance, String propertyName, Object value) throws IllegalAccessException
IllegalAccessException
public static boolean setPrivilegedProperty(Object instance, Class clazz, String propertyName, Object value) throws IllegalAccessException
IllegalAccessException
public static Object getPrivilegedProperty(Object instance, String propertyName) throws IllegalAccessException
IllegalAccessException
public static Object getField(Object instance, String name) throws IllegalAccessException
IllegalAccessException
public static boolean setPrivilegedStaticProperty(Class clazz, String propertyName, Object value) throws IllegalAccessException
IllegalAccessException
public static Object getPrivilegedStaticProperty(Class clazz, String propertyName) throws IllegalAccessException
IllegalAccessException
public static int getNamedValue(Class clazz, String name)
getStaticProperty(java.lang.Class, java.lang.String)
public static void setPropertiesByMap(Object fromObject, Object toObject, String[][] propMap, boolean reverse) throws RuntimeException
fromObject
- toObject
- propMap
- reverse
- RtlException
RuntimeException
public static int compare(Object o1, Object o2)
o1
- o2
- public static boolean isEnumeration(Class type)
type
- the class to checkpublic static List getEnumerationValues(Class type) throws RuntimeException
type
- RuntimeException
public static Method getMatchingMethod(Class<?> clas, String methodName, List arguments)
public static Object[] convertListTypes(Class[] types, Object[] values) throws RuntimeException
RuntimeException
public static Object convertToType(Object srcObj, Class targetType) throws RuntimeException
RuntimeException
public static boolean getPropertyAsBoolean(Object sourceObj, String propertyName, boolean defaultValue)
public static int getPropertyAsInt(Object sourceObj, String propertyName, int defaultValue)