Package com.webmethods.caf.common
Class ObjectUtil
java.lang.Object
com.webmethods.caf.common.ObjectUtil
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
Protected inner class that load objects from an input stream. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
String variable 'false'static final String
String variable 'no'static final String
String variable 'off'static final String
String variable 'on'static final String
String variable 'true'static final String
String variable 'yes'static final String
String for class keyprotected static ClassLoader
protected static final Object[]
protected static final Class<?>[]
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
Clone an object that implements serializablestatic Object
cloneObjectFromMap
(Map map, Class objectClass, String[] properties, boolean createDefComplexTypes) Clones an object from Mapstatic Object
cloneObjectFromMap
(Map map, Class objectClass, String[] properties, boolean createDefComplexTypes, boolean useRealFieldNames) Clones an object from Mapstatic Object
cloneObjectToMap
(Object object, String[] properties) Clones object to the Map implementation containsstatic Object
cloneObjectToMap
(Object object, String[] properties, boolean useRealFieldNames) Clones object to the Map implementation containsstatic void
closeURLClassLoader
(URLClassLoader classLoader, boolean ignoreExceptions) Tries to close opened by URLClassLoader jars to release file lock that causes problems during hot deploymentstatic Object
convertToType
(Object srcObj, Class targetType) Converts source object value to the target typestatic Object
convertToType
(Object srcObj, Class targetType, boolean throwsError) Converts source object value to the target typestatic boolean
Simply compares two objects using equals() method and takes care about nulls null == null = falsestatic PropertyDescriptor
findPropertyDescriptor
(PropertyDescriptor[] anArray, String aPropertyName) Finds a specific PropertyDescriptor from an array of PropertyDescriptor.static void
Flushes internal Class cache.static Class
A class factory method that uses the pluggable classLoader:m_classLoader
static ClassLoader
Gets classLoaderstatic 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 namestatic Object
getStaticProperty
(Class clazz, String fieldName) Returns the named static variable.static boolean
isAssignableFrom
(Class to, Class from) ExtendsClass.isAssignableFrom(Class)
to work with primitives.static boolean
isAssignableFromObject
(Class to, Object from) ExtendsClass.isAssignableFrom(Class)
to work with primitives.static 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
(InputStream in, ClassLoader cl) Load an object from an inputstreamstatic boolean
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 typesstatic byte[]
Serialize an object to an array of bytes.static void
setClassLoader
(ClassLoader classLoader) Sets given classLoaderstatic 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 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
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 classstatic void
sortObjectArray
(Object[] objects, String propName, boolean ascending) Sorts passed in object array according to natural order of specified object propertystatic <T> void
sortObjectList
(List<T> objects, String propName, boolean ascending) Sorts passed in object array according to natural order of specified object property
-
Field Details
-
BOOLEAN_TRUE
String variable 'true'- See Also:
-
BOOLEAN_FALSE
String variable 'false'- See Also:
-
BOOLEAN_YES
String variable 'yes'- See Also:
-
BOOLEAN_NO
String variable 'no'- See Also:
-
BOOLEAN_ON
String variable 'on'- See Also:
-
BOOLEAN_OFF
String variable 'off'- See Also:
-
CLASS_KEY
String for class key- See Also:
-
m_classLoader
-
ONE_STRING_ARG
-
ONE_EMPTY_STRING
-
CLASS_NAMES_TO_DEFAULT_INSTANCES
-
-
Constructor Details
-
ObjectUtil
public ObjectUtil()
-
-
Method Details
-
getClass
A class factory method that uses the pluggable classLoader:m_classLoader
- Parameters:
className
-- Returns:
- the Class if found
- Throws:
ClassNotFoundException
-
getClassLoader
Gets classLoader- Returns:
- the current
m_classLoader
if set, the default ClassLoader otherwise - See Also:
-
setClassLoader
Sets given classLoader- Parameters:
classLoader
-- See Also:
-
convertToType
Converts source object value to the target type- Throws:
CommonException
- See Also:
-
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 convertedtargetType
- class type to convert tothrowsError
- if true, throw errors- Returns:
- source object as a target object type
- Throws:
CommonException
-
isAssignableFromObject
ExtendsClass.isAssignableFrom(Class)
to work with primitives.- Parameters:
to
- Class we want to assign object tofrom
- 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
ExtendsClass.isAssignableFrom(Class)
to work with primitives. Returns true if the first argument is assignable from the second. -
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
-
setPropertiesBatch
Add multiple properties to a object at once.- Parameters:
anInstance
- Object properties are being added topropertyValues
- 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 tobean.setPropertyValue(propertyValue);
Simple types are also supportedint propertyValue = 10; ClassTools.setProperty(bean, "intPropertyName", new Integer(propertyValue));
- Throws:
Exception
-
getStaticProperty
Returns the named static variable. Usage:Object value = ObjectUtil.getStaticProperty(metaUtilClass, "PUBLIC");
- Throws:
Exception
-
setStaticProperty
Sets a value into static member of the class- Parameters:
clazz
- source classfieldName
- field namevalue
- value to be set- Throws:
Exception
-
getPropertyValue
Gets a property of an instance based on a property name- Parameters:
anInstance
- object that we are getting property ofaPropertyName
- 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 foraPropertyName
- name of propertyvalue
- 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 objectaPropertyName
- 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 nullaPropertyName
- Property specific name- Returns:
- null or matching propertyDescriptor
-
cloneObjectToMap
Clones object to the Map implementation contains- Parameters:
object
- the object to cloneproperties
- 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 cloneproperties
- 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
Clone an object that implements serializable- Parameters:
object
- Instance of object to clone- Throws:
CloneNotSupportedException
-
flushClassCache
Flushes internal Class cache.- Throws:
Exception
-
getJavaClassStructure
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
Simply compares two objects using equals() method and takes care about nulls null == null = false -
mapEqual
Special version of equals function for Maps, designed specifically to compare maps produced from cloneObjectToMap() methods.- Parameters:
map1
-map2
-
-
justClassName
Return short class name without package names -
sortObjectList
public static <T> void sortObjectList(List<T> objects, String propName, boolean ascending) throws Exception Sorts passed in object array according to natural order of specified object property- Type Parameters:
T
-- Parameters:
propName
- object property name to be sortedascending
- sort order- Throws:
Exception
-
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 sortedascending
- sort order- Throws:
Exception
-
newInstance
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 instanceignoreExceptions
- if true fail on first exception, if false, ignore errors and continue- Throws:
NoSuchFieldException
IllegalAccessException
IOException
ClassNotFoundException
-
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
-
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
-