com.webmethods.caf.portalclient.util
Class ObjectUtil

java.lang.Object
  extended by com.webmethods.caf.portalclient.util.ObjectUtil

public class ObjectUtil
extends Object

ObjectUtil provides convenience functions for java reflection using Class.forName
It also provides some System property convenience function(s) and reflection utilities.


Nested Class Summary
protected static class ObjectUtil.ClassLoaderObjectInputStream
           
 
Field Summary
static String DOT_SIGN
          Package/Class Name separator
protected static ClassLoader m_classLoader
          The current ClassLoader used by ObjectUtil.createObject(java.lang.String) & ObjectUtil.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 Object NOT_CACHED_CLASS
          Class used when unable to resolve a cached property
 
Constructor Summary
ObjectUtil()
           
 
Method Summary
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: ObjectUtil.m_classLoader Note, all exceptions are caught
static Object createObject(String className, Object[] parms)
          A class factory method that uses the pluggable classLoader: ObjectUtil.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: ObjectUtil.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 ObjectUtil.convertToType(Object, Class) instead
static String toString(String comment, Object instance)
           
static String toString(String comment, Object instance, boolean filterNulls)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DOT_SIGN

public static final String DOT_SIGN
Package/Class Name separator

See Also:
Constant Field Values

m_classLoader

protected static ClassLoader m_classLoader
The current ClassLoader used by ObjectUtil.createObject(java.lang.String) & ObjectUtil.getClass(java.lang.String)


m_UsePropertyCache

protected static boolean m_UsePropertyCache
Use the property cache when resolving properties via reflection


m_Properties

protected static Map m_Properties
Map of object properties, used for property caching


m_Fields

protected static Map m_Fields
Map of object fields, used for property caching


NOT_CACHED_CLASS

protected static final Object NOT_CACHED_CLASS
Class used when unable to resolve a cached property

Constructor Detail

ObjectUtil

public ObjectUtil()
Method Detail

createObject

public static Object createObject(String className)
A class factory method that uses the pluggable classLoader: ObjectUtil.m_classLoader Note, all exceptions are caught

Parameters:
className -
Returns:
the Object if found, null otherwise
See Also:
ObjectUtil.getClass(java.lang.String)

createObject

public static Object createObject(String className,
                                  Object[] parms)
A class factory method that uses the pluggable classLoader: ObjectUtil.m_classLoader Note, all exceptions are caught

Parameters:
className - the class to create an instance of
parms - the parameter array to pass to the object constructor
Returns:
the Object if found, null otherwise
See Also:
ObjectUtil.getClass(java.lang.String)

getConstructor

public static Constructor getConstructor(Class clazz,
                                         Object[] args)
Get a constructor for a class that uses the given set of arguments

Parameters:
clazz - the class to retrieve the constructor for
args - the arguments to be passed the constructor
Returns:
the object constructor if found, otherwise null

getClass

public static Class getClass(String className)
                      throws ClassNotFoundException
A class factory method that uses the pluggable classLoader: ObjectUtil.m_classLoader

Parameters:
className -
Returns:
the Class if found
Throws:
ClassNotFoundException
See Also:
ObjectUtil.createObject(java.lang.String)

getClassLoader

public static ClassLoader getClassLoader()
Get the current classloader, using the classloader of the current thread if not found

Returns:
the current ObjectUtil.m_classLoader if set, the default ClassLoader otherwise
See Also:
ObjectUtil.m_classLoader

setClassLoader

public static void setClassLoader(ClassLoader classLoader)
Set the classloader to be used by this ObjectUtil instance

Parameters:
classLoader -
See Also:
ObjectUtil.m_classLoader

StringToValue

public static Object StringToValue(String s,
                                   Class t)
Deprecated. use ObjectUtil.convertToType(Object, Class) instead

Converts String value to the type specified by class

Parameters:
s - the source string to convert
t - the Class to attempt to convert the string to
Returns:
the converted object, or the original string if unable to convert

isArrayOf

public static boolean isArrayOf(Object toCheck,
                                Class arrayItem)
Checks whether given instance is an array of the given classes

Parameters:
toCheck - the Object to check for type
arrayItem - 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

public static boolean isArray(Object toCheck)
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

public static Object getCachedPropertyDescriptor(Object anInstance,
                                                 String aPropertyName)
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 for
aPropertyName - 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

public static Field getFieldDescriptor(Class cls,
                                       String property)
                                throws Exception
Returns field descriptor of the selected property for the selected object instance.

Parameters:
cls - the class to get the field descriptor from
property - 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

public static PropertyDescriptor[] getPropertyDescriptors(Class instanceClass)
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 to bean.setPropertyValue(propertyValue); Simple types are also supported int 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

public static Object getPropertyOrFieldValue(Object target,
                                             String property)
                                      throws Exception
Obtains object property via getter method (preferred), or directly via member field.

Throws:
Exception

getProperty

public static Object getProperty(Object anInstance,
                                 String aPropertyName)
                          throws RuntimeException
Obtains a value of the property specified by name for the specified instance

Parameters:
anInstance -
Throws:
RuntimeException

getStaticProperty

public static Object getStaticProperty(Class clazz,
                                       String fieldName)
Returns the named static variable. Usage: Object value = ObjectUtil.getStaticProperty(metaUtilClass, "PUBLIC");


setStaticProperty

public static void setStaticProperty(Class clazz,
                                     String fieldName,
                                     Object value)
Sets a value into static member of the class

Parameters:
clazz - source class
fieldName - field name
value - value to be set

convertToPropertyType

public static Object convertToPropertyType(Object anInstance,
                                           String aPropertyName,
                                           String aStringValue)

getProperties

public static Map getProperties(Object instance)

setPropertiesBatch

public static void setPropertiesBatch(Object anInstance,
                                      Map propertyValues)

hadValueChange

public static boolean hadValueChange(Object oldValue,
                                     Object newValue)
Checks wether there was a change in the value


justClassName

public static String justClassName(String aFullClassName)
Return short class name without package names


clone

public static Object clone(Object object)
                    throws CloneNotSupportedException
Clone an object that implements serializable

Parameters:
object - Instance of object to clone
Throws:
CloneNotSupportedException

equal

public static boolean equal(Object obj1,
                            Object obj2)
Simply compares two objects using equals() method and takes care about nulls null == null = false


save

public static byte[] save(Object object)
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

public static Object load(byte[] buffer)
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

public static Object load(InputStream in,
                          ClassLoader cl)

load

public static Object load(InputStream in)
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

public static Object getField(Object instance,
                              String name)
                       throws IllegalAccessException
Retrieves a data field, potentially defined by a super class.

Returns:
null if not found, the object otherwise
Throws:
IllegalAccessException

getField

public static Field getField(Class klass,
                             String property)

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

public static String toString(String comment,
                              Object instance,
                              boolean filterNulls)

toString

public static String toString(String comment,
                              Object instance)

getNamedValue

public static int getNamedValue(Class clazz,
                                String name)
use some reflection to get the named property

Returns:
the int if found, -1 otherwise
See Also:
ObjectUtil.getStaticProperty(java.lang.Class, java.lang.String)

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

public static int compare(Object o1,
                          Object o2)
Compares two object. This method account for nulls or if objects are not Comparable

Parameters:
o1 -
o2 -
Returns:

isEnumeration

public static boolean isEnumeration(Class type)
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

public static List getEnumerationValues(Class type)
                                 throws RuntimeException
Retrieve a list of all the possible values for the given enumeration type

Parameters:
type -
Returns:
Throws:
RuntimeException

getMatchingMethod

public static Method getMatchingMethod(Class clas,
                                       String methodName,
                                       List arguments)

convertListTypes

public static Object[] convertListTypes(Class[] types,
                                        Object[] values)
                                 throws RuntimeException
Converts source object value to the target type

Throws:
RuntimeException

convertToType

public static Object convertToType(Object srcObj,
                                   Class targetType)
                            throws RuntimeException
Converts source object value to the target type

Throws:
RuntimeException

getPropertyAsBoolean

public static boolean getPropertyAsBoolean(Object sourceObj,
                                           String propertyName,
                                           boolean defaultValue)

getPropertyAsInt

public static int getPropertyAsInt(Object sourceObj,
                                   String propertyName,
                                   int defaultValue)

getPropertyAsString

public static String getPropertyAsString(Object sourceObj,
                                         String propertyName,
                                         String defaultValue)