Class ObjectUtil
java.lang.Object
com.webmethods.caf.portalclient.util.ObjectUtil
ObjectUtil provides convenience functions for java reflection using
It also provides some System property convenience function(s) and reflection utilities.
Class.forName
It also provides some System property convenience function(s) and reflection utilities.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
static final String
static final String
static final String
static final String
static final String
protected static final String
static final String
Package/Class Name separatorprotected static final String
protected static ClassLoader
The current ClassLoader used bycreateObject(java.lang.String)
&getClass(java.lang.String)
protected static Map
Map of object fields, used for property cachingprotected static Map
Map of object properties, used for property cachingprotected static boolean
Use the property cache when resolving properties via reflectionprotected static final String
protected static final Object
Class used when unable to resolve a cached property -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
Clone an object that implements serializablestatic int
Compares two object.static Object[]
convertListTypes
(Class[] types, Object[] values) Converts source object value to the target typestatic Object
convertToPropertyType
(Object anInstance, String aPropertyName, String aStringValue) static Object
convertToType
(Object srcObj, Class targetType) Converts source object value to the target typestatic Object
createObject
(String className) A class factory method that uses the pluggable classLoader:m_classLoader
Note, all exceptions are caughtstatic Object
createObject
(String className, Object[] parms) A class factory method that uses the pluggable classLoader:m_classLoader
Note, all exceptions are caughtstatic boolean
Simply compares two objects using equals() method and takes care about nulls null == null = falsestatic void
Clears the property descriptors cache NOTE: needs to be called after cached classes are reloadedstatic Object
getCachedPropertyDescriptor
(Object anInstance, String aPropertyName) Returns cached property descriptor from the cache.static Class
A class factory method that uses the pluggable classLoader:m_classLoader
static ClassLoader
Get the current classloader, using the classloader of the current thread if not foundstatic Constructor
getConstructor
(Class clazz, Object[] args) Get a constructor for a class that uses the given set of argumentsstatic List
getEnumerationValues
(Class type) Retrieve a list of all the possible values for the given enumeration typestatic Field
static Object
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 propertystatic Object
getPrivilegedProperty
(Object instance, String propertyName) Retrieves a privilged propertystatic Object
getPrivilegedStaticProperty
(Class clazz, String propertyName) Retrieves a privilged property from a static classstatic Map
getProperties
(Object instance) static Object
getProperty
(Object anInstance, String aPropertyName) Obtains a value of the property specified by name for the specified instancestatic 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 valuestatic boolean
Checks whether given instance is an arraystatic boolean
Checks whether given instance is an array of the given classesstatic boolean
isEnumeration
(Class type) Determine if the given class is an enumerationstatic String
justClassName
(String aFullClassName) Return short class name without package namesstatic Object
load
(byte[] buffer) Load an object from an array of bytes.static Object
load
(InputStream in) Load an object from an inputstreamstatic Object
load
(InputStream in, ClassLoader cl) static byte[]
Serialize an object to an array of bytes.static void
setClassLoader
(ClassLoader classLoader) Set the classloader to be used by this ObjectUtil instancestatic boolean
setPrivilegedProperty
(Object instance, Class clazz, String propertyName, Object value) Gets a priviledged property from an objectstatic boolean
setPrivilegedProperty
(Object instance, String propertyName, Object value) Gets a priviledged property from an objectstatic boolean
setPrivilegedStaticProperty
(Class clazz, String propertyName, Object value) Gets a priviledged property from a static classstatic 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 namesstatic 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 tobean.setPropertyValue(propertyValue);
Simple types are also supportedint 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 classstatic Object
StringToValue
(String s, Class t) Deprecated.static String
static String
-
Field Details
-
DOT_SIGN
Package/Class Name separator- See Also:
-
BOOLEAN_TRUE
- See Also:
-
BOOLEAN_FALSE
- See Also:
-
BOOLEAN_YES
- See Also:
-
BOOLEAN_NO
- See Also:
-
BOOLEAN_ON
- See Also:
-
BOOLEAN_OFF
- See Also:
-
CREATE_OBJECT
- See Also:
-
GET_CLASS
- See Also:
-
NEW_INSTANCE
- See Also:
-
m_classLoader
The current ClassLoader used bycreateObject(java.lang.String)
&getClass(java.lang.String)
-
m_UsePropertyCache
protected static boolean m_UsePropertyCacheUse the property cache when resolving properties via reflection -
m_Properties
Map of object properties, used for property caching -
m_Fields
Map of object fields, used for property caching -
NOT_CACHED_CLASS
Class used when unable to resolve a cached property
-
-
Constructor Details
-
ObjectUtil
public ObjectUtil()
-
-
Method Details
-
createObject
A class factory method that uses the pluggable classLoader:m_classLoader
Note, all exceptions are caught- Parameters:
className
-- Returns:
- the Object if found, null otherwise
- See Also:
-
createObject
A class factory method that uses the pluggable classLoader:m_classLoader
Note, all exceptions are caught- Parameters:
className
- the class to create an instance ofparms
- the parameter array to pass to the object constructor- Returns:
- the Object if found, null otherwise
- See Also:
-
getConstructor
Get a constructor for a class that uses the given set of arguments- Parameters:
clazz
- the class to retrieve the constructor forargs
- the arguments to be passed the constructor- Returns:
- the object constructor if found, otherwise null
-
getClass
A class factory method that uses the pluggable classLoader:m_classLoader
- Parameters:
className
-- Returns:
- the Class if found
- Throws:
ClassNotFoundException
- See Also:
-
getClassLoader
Get the current classloader, using the classloader of the current thread if not found- Returns:
- the current
m_classLoader
if set, the default ClassLoader otherwise - See Also:
-
setClassLoader
Set the classloader to be used by this ObjectUtil instance- Parameters:
classLoader
-- See Also:
-
StringToValue
Deprecated.useconvertToType(Object, Class)
insteadConverts String value to the type specified by class- Parameters:
s
- the source string to convertt
- the Class to attempt to convert the string to- Returns:
- the converted object, or the original string if unable to convert
-
isArrayOf
Checks whether given instance is an array of the given classes- Parameters:
toCheck
- the Object to check for typearrayItem
- the Class to check if the object is an array of- Returns:
- true if the object is indeed an array of the specified class
-
isArray
Checks whether given instance is an array- Parameters:
toCheck
- the object to determine is an array- Returns:
- true if the specified object is indeed an array
-
getCachedPropertyDescriptor
Returns cached property descriptor from the cache. Returns null if property is not found. Returns NOT_CACHED_CLASS if cache for the given class does not exist- Parameters:
anInstance
- the object instance to get properties foraPropertyName
- the name of the property to retrieve the descriptor for- Returns:
- the Property descriptor for the specified property name, NOT_CACHED_CLASS if not found
-
flushPropertyDescriptorCache
public static void flushPropertyDescriptorCache()Clears the property descriptors cache NOTE: needs to be called after cached classes are reloaded -
getFieldDescriptor
Returns field descriptor of the selected property for the selected object instance.- Parameters:
cls
- the class to get the field descriptor fromproperty
- the property name of the descriptor- Returns:
- a Field object, null if not found
- Throws:
Exception
-
getPropertyDescriptor
public static PropertyDescriptor getPropertyDescriptor(Object anInstance, String aPropertyName) throws RuntimeException Returns property descriptor of the selected property for the selected object instance.- Parameters:
anInstance
-- Throws:
RuntimeException
-
getPropertyDescriptors
Returns property descriptor list for a given object class selected object instance.- Parameters:
instanceClass
-- Returns:
- PropertyDescriptor[] for passed in class
-
setProperty
public static void setProperty(Object anInstance, String aPropertyName, Object aValue) throws Exception 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 tobean.setPropertyValue(propertyValue);
Simple types are also supportedint propertyValue = 10; ClassTools.setProperty(bean, "intPropertyName", new Integer(propertyValue));
- Throws:
Exception
-
setPropertyByString
public 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"); -
getPropertyOrFieldValue
Obtains object property via getter method (preferred), or directly via member field.- Throws:
Exception
-
getProperty
Obtains a value of the property specified by name for the specified instance- Parameters:
anInstance
-- Throws:
RuntimeException
-
getStaticProperty
Returns the named static variable. Usage:Object value = ObjectUtil.getStaticProperty(metaUtilClass, "PUBLIC");
-
setStaticProperty
Sets a value into static member of the class- Parameters:
clazz
- source classfieldName
- field namevalue
- value to be set
-
convertToPropertyType
-
getProperties
-
setPropertiesBatch
-
hadValueChange
Checks wether there was a change in the value -
justClassName
Return short class name without package names -
clone
Clone an object that implements serializable- Parameters:
object
- Instance of object to clone- Throws:
CloneNotSupportedException
-
equal
Simply compares two objects using equals() method and takes care about nulls null == null = false -
save
Serialize an object to an array of bytes.- Parameters:
object
- the object to serialize. Must implement Serializable- Returns:
- array of bytes that constitute the serialized object or null if an error occurred
-
load
Load an object from an array of bytes.- Parameters:
buffer
- the array of bytes that constitute the object- Returns:
- an Object that was loaded from the array of bytes, or null if an error occurred
-
load
-
load
Load an object from an inputstream- Parameters:
in
- the InputStream that constitute the object- Returns:
- an Object that was loaded from the InputStream, or null if an error occurred
-
setPrivilegedProperty
public static boolean setPrivilegedProperty(Object instance, String propertyName, Object value) throws IllegalAccessException Gets a priviledged property from an object- Returns:
- true if successful, false otherwise
- Throws:
IllegalAccessException
-
setPrivilegedProperty
public static boolean setPrivilegedProperty(Object instance, Class clazz, String propertyName, Object value) throws IllegalAccessException Gets a priviledged property from an object- Returns:
- true if successful, false otherwise
- Throws:
IllegalAccessException
-
getPrivilegedProperty
public static Object getPrivilegedProperty(Object instance, String propertyName) throws IllegalAccessException Retrieves a privilged property- Returns:
- null if not found, the object otherwise
- Throws:
IllegalAccessException
-
getField
Retrieves a data field, potentially defined by a super class.- Returns:
- null if not found, the object otherwise
- Throws:
IllegalAccessException
-
getField
-
setPrivilegedStaticProperty
public static boolean setPrivilegedStaticProperty(Class clazz, String propertyName, Object value) throws IllegalAccessException Gets a priviledged property from a static class- Returns:
- true if successful, false otherwise
- Throws:
IllegalAccessException
-
getPrivilegedStaticProperty
public static Object getPrivilegedStaticProperty(Class clazz, String propertyName) throws IllegalAccessException Retrieves a privilged property from a static class- Returns:
- null if not found, the object otherwise
- Throws:
IllegalAccessException
-
toString
-
toString
-
getNamedValue
use some reflection to get the named property- Returns:
- the int if found, -1 otherwise
- See Also:
-
setPropertiesByMap
public static void setPropertiesByMap(Object fromObject, Object toObject, String[][] propMap, boolean reverse) throws RuntimeException Sets object properties using translation map to resolve property names- Parameters:
fromObject
-toObject
-propMap
-reverse
-- Throws:
RtlException
RuntimeException
-
compare
Compares two object. This method account for nulls or if objects are not Comparable- Parameters:
o1
-o2
-- Returns:
-
isEnumeration
Determine if the given class is an enumeration- Parameters:
type
- the class to check- Returns:
- true if the class represents an enumeration, false otherwise
-
getEnumerationValues
Retrieve a list of all the possible values for the given enumeration type- Parameters:
type
-- Returns:
- Throws:
RuntimeException
-
getMatchingMethod
-
convertListTypes
Converts source object value to the target type- Throws:
RuntimeException
-
convertToType
Converts source object value to the target type- Throws:
RuntimeException
-
getPropertyAsBoolean
-
getPropertyAsInt
-
getPropertyAsString
-
convertToType(Object, Class)
instead