com.webmethods.caf.common
Class ObjectUtil

java.lang.Object
  extended by com.webmethods.caf.common.ObjectUtil

public class ObjectUtil
extends Object

Utility class to deal with different objects Certain functions can be converting object type x to object type y Checking if an object can be converted State checking of an object etc.


Field Summary
static String BOOLEAN_FALSE
          String variable 'false'
static String BOOLEAN_NO
          String variable 'no'
static String BOOLEAN_OFF
          String variable 'off'
static String BOOLEAN_ON
          String variable 'on'
static String BOOLEAN_TRUE
          String variable 'true'
static String BOOLEAN_YES
          String variable 'yes'
static String CLASS_KEY
          String for class key
 
Constructor Summary
ObjectUtil()
           
 
Method Summary
static Object clone(Object object)
          Clone an object that implements serializable
static Object cloneObjectFromMap(Map map, Class objectClass, String[] properties, boolean createDefComplexTypes)
          Clones an object from Map
static Object cloneObjectFromMap(Map map, Class objectClass, String[] properties, boolean createDefComplexTypes, boolean useRealFieldNames)
          Clones an object from Map
static Object cloneObjectToMap(Object object, String[] properties)
          Clones object to the Map implementation contains
static Object cloneObjectToMap(Object object, String[] properties, boolean useRealFieldNames)
          Clones object to the Map implementation contains
static void closeURLClassLoader(URLClassLoader classLoader, boolean ignoreExceptions)
          Tries to close opened by URLClassLoader jars to release file lock that causes problems during hot deployment
static Object convertToType(Object srcObj, Class targetType)
          Converts source object value to the target type
static Object convertToType(Object srcObj, Class targetType, boolean throwsError)
          Converts source object value to the target type
static boolean equal(Object obj1, Object obj2)
          Simply compares two objects using equals() method and takes care about nulls null == null = false
static PropertyDescriptor findPropertyDescriptor(PropertyDescriptor[] anArray, String aPropertyName)
          Finds a specific PropertyDescriptor from an array of PropertyDescriptor.
static void flushClassCache()
          Flushes internal Class cache.
static Class getClass(String className)
          A class factory method that uses the pluggable classLoader: ObjectUtil.m_classLoader
static ClassLoader getClassLoader()
          Gets classLoader
static IDocumentStructureProvider getJavaClassStructure(Class clas)
          Grabs Java class structure of specific class in DocumentStuctureProdiver structure.
static PropertyDescriptor getPropertyDescriptor(Object anInstance, String aPropertyName)
          Finds the property descriptor of an object and specific property name.
static PropertyDescriptor[] getPropertyDescriptors(Class instanceClass)
          Returns property descriptor list for a given object class selected object instance.
static Object getPropertyValue(Object anInstance, String aPropertyName)
          Gets a property of an instance based on a property name
static Object getStaticProperty(Class clazz, String fieldName)
          Returns the named static variable.
static boolean isAssignableFrom(Class to, Class from)
          Extends Class.isAssignableFrom(Class) to work with primitives.
static boolean isAssignableFromObject(Class to, Object from)
          Extends Class.isAssignableFrom(Class) to work with primitives.
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(InputStream in, ClassLoader cl)
          Load an object from an inputstream
static boolean mapEqual(Map map1, Map map2)
          Special version of equals function for Maps, designed specifically to compare maps produced from cloneObjectToMap() methods.
static Object newInstance(Class _class)
          Creates new object instance handling for privimite types
static void setClassLoader(ClassLoader classLoader)
          Sets given classLoader
static void setPropertiesBatch(Object anInstance, Map propertyValues)
          Add multiple properties to a object at once.
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 setPropertyValue(Object anInstance, String aPropertyName, Object value)
          Sets property value of an instance with a value and name.
static void setStaticProperty(Class clazz, String fieldName, Object value)
          Sets a value into static member of the class
static void sortObjectArray(Object[] objects, String propName, boolean ascending)
          Sorts passed in object array according to natural order of specified object property
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BOOLEAN_TRUE

public static final String BOOLEAN_TRUE
String variable 'true'

See Also:
Constant Field Values

BOOLEAN_FALSE

public static final String BOOLEAN_FALSE
String variable 'false'

See Also:
Constant Field Values

BOOLEAN_YES

public static final String BOOLEAN_YES
String variable 'yes'

See Also:
Constant Field Values

BOOLEAN_NO

public static final String BOOLEAN_NO
String variable 'no'

See Also:
Constant Field Values

BOOLEAN_ON

public static final String BOOLEAN_ON
String variable 'on'

See Also:
Constant Field Values

BOOLEAN_OFF

public static final String BOOLEAN_OFF
String variable 'off'

See Also:
Constant Field Values

CLASS_KEY

public static final String CLASS_KEY
String for class key

See Also:
Constant Field Values
Constructor Detail

ObjectUtil

public ObjectUtil()
Method Detail

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

getClassLoader

public static ClassLoader getClassLoader()
Gets classLoader

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

setClassLoader

public static void setClassLoader(ClassLoader classLoader)
Sets given classLoader

Parameters:
classLoader -
See Also:
ObjectUtil.m_classLoader

convertToType

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

Throws:
CommonException
See Also:
ObjectUtil.convertToType(java.lang.Object, java.lang.Class)

convertToType

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

Parameters:
srcObj - object to be converted
targetType - class type to convert to
throwsError - if true, throw errors
Returns:
source object as a target object type
Throws:
CommonException

isAssignableFromObject

public static boolean isAssignableFromObject(Class to,
                                             Object from)
Extends Class.isAssignableFrom(Class) to work with primitives.

Parameters:
to - Class we want to assign object to
from - object we are checking if it can be assigned to 'to' class
Returns:
true if the first argument is assignable from the second's class, or if the second argument is null (and the first is not a primitive type).

isAssignableFrom

public static boolean isAssignableFrom(Class to,
                                       Class from)
Extends Class.isAssignableFrom(Class) to work with primitives. Returns true if the first argument is assignable from the second.


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

setPropertiesBatch

public static void setPropertiesBatch(Object anInstance,
                                      Map propertyValues)
                               throws Exception
Add multiple properties to a object at once.

Parameters:
anInstance - Object properties are being added to
propertyValues - map containing property names and values
Throws:
Exception

setPropertyByString

public static boolean setPropertyByString(Object anInstance,
                                          String aPropertyName,
                                          String aStringValue)
                                   throws Exception
Set's a particular property of the instance by string value Usage: SomeBean bean = new SomeBean(); ObjectUtil.setPropertyByString(bean, "someProperty", "10");

Throws:
Exception

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

getStaticProperty

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

Throws:
Exception

setStaticProperty

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

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

getPropertyValue

public static Object getPropertyValue(Object anInstance,
                                      String aPropertyName)
                               throws Exception
Gets a property of an instance based on a property name

Parameters:
anInstance - object that we are getting property of
aPropertyName - property name we are looking for
Returns:
specific property or null if it doesn't exist
Throws:
Exception

setPropertyValue

public static void setPropertyValue(Object anInstance,
                                    String aPropertyName,
                                    Object value)
                             throws Exception
Sets property value of an instance with a value and name.

Parameters:
anInstance - Object we are setting property for
aPropertyName - name of property
value - value of property
Throws:
Exception

getPropertyDescriptor

public static PropertyDescriptor getPropertyDescriptor(Object anInstance,
                                                       String aPropertyName)
                                                throws IntrospectionException
Finds the property descriptor of an object and specific property name.

Parameters:
anInstance - null or object
aPropertyName - String used to find result
Returns:
the found property descriptor
Throws:
IntrospectionException

getPropertyDescriptors

public static PropertyDescriptor[] getPropertyDescriptors(Class instanceClass)
                                                   throws IntrospectionException
Returns property descriptor list for a given object class selected object instance.

Parameters:
instanceClass -
Returns:
PropertyDescriptor[] for passed in class
Throws:
IntrospectionException

findPropertyDescriptor

public static PropertyDescriptor findPropertyDescriptor(PropertyDescriptor[] anArray,
                                                        String aPropertyName)
Finds a specific PropertyDescriptor from an array of PropertyDescriptor.

Parameters:
anArray - array or null
aPropertyName - Property specific name
Returns:
null or matching propertyDescriptor

cloneObjectToMap

public static Object cloneObjectToMap(Object object,
                                      String[] properties)
                               throws Exception
Clones object to the Map implementation contains

Parameters:
object - the object to clone
properties - selected top level properties of an object will be cloned
Returns:
map for the cloned object
Throws:
PortalException
Exception

cloneObjectToMap

public static Object cloneObjectToMap(Object object,
                                      String[] properties,
                                      boolean useRealFieldNames)
                               throws Exception
Clones object to the Map implementation contains

Parameters:
object - the object to clone
properties - selected top level properties of an object will be cloned
Returns:
map for the cloned object
Throws:
PortalException
Exception

cloneObjectFromMap

public static Object cloneObjectFromMap(Map map,
                                        Class objectClass,
                                        String[] properties,
                                        boolean createDefComplexTypes)
                                 throws Exception
Clones an object from Map

Parameters:
map -
objectClass -
properties - allowed properties to clone
Returns:
clone of the object
Throws:
PortalException
Exception

cloneObjectFromMap

public static Object cloneObjectFromMap(Map map,
                                        Class objectClass,
                                        String[] properties,
                                        boolean createDefComplexTypes,
                                        boolean useRealFieldNames)
                                 throws Exception
Clones an object from Map

Parameters:
map -
objectClass -
properties - allowed properties to clone
Returns:
clone of the object
Throws:
PortalException
Exception

clone

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

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

flushClassCache

public static void flushClassCache()
                            throws Exception
Flushes internal Class cache.

Throws:
Exception

getJavaClassStructure

public static IDocumentStructureProvider getJavaClassStructure(Class clas)
                                                        throws Exception
Grabs Java class structure of specific class in DocumentStuctureProdiver structure.

Parameters:
clas - null or Class
Returns:
null or class in DocumentStructureProvider structure
Throws:
Exception

equal

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


mapEqual

public static boolean mapEqual(Map map1,
                               Map map2)
Special version of equals function for Maps, designed specifically to compare maps produced from cloneObjectToMap() methods.

Parameters:
map1 -
map2 -

justClassName

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


sortObjectArray

public static void sortObjectArray(Object[] objects,
                                   String propName,
                                   boolean ascending)
                            throws Exception
Sorts passed in object array according to natural order of specified object property

Parameters:
propName - object property name to be sorted
ascending - sort order
Throws:
Exception

newInstance

public static Object newInstance(Class _class)
                          throws Exception
Creates new object instance handling for privimite types

Parameters:
_class - primitive class, like Integer, String, Float, etc
Returns:
primitive instance
Throws:
Exception - if className is invalid

closeURLClassLoader

public static void closeURLClassLoader(URLClassLoader classLoader,
                                       boolean ignoreExceptions)
                                throws ClassNotFoundException,
                                       NoSuchFieldException,
                                       IllegalAccessException,
                                       IOException
Tries to close opened by URLClassLoader jars to release file lock that causes problems during hot deployment

Parameters:
classLoader - URLClassLoader instance
ignoreExceptions - if true fail on first exception, if false, ignore errors and continue
Throws:
NoSuchFieldException
IllegalAccessException
IOException
ClassNotFoundException

load

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