Class ObjectUtil

java.lang.Object
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 Details

  • Constructor Details

    • ObjectUtil

      public ObjectUtil()
  • Method Details

    • getClass

      public static Class getClass(String className) throws ClassNotFoundException
      A class factory method that uses the pluggable classLoader: m_classLoader
      Parameters:
      className -
      Returns:
      the Class if found
      Throws:
      ClassNotFoundException
    • getClassLoader

      public static ClassLoader getClassLoader()
      Gets classLoader
      Returns:
      the current m_classLoader if set, the default ClassLoader otherwise
      See Also:
    • setClassLoader

      public static void setClassLoader(ClassLoader classLoader)
      Sets given classLoader
      Parameters:
      classLoader -
      See Also:
    • convertToType

      public static Object convertToType(Object srcObj, Class targetType) throws CommonException
      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 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
    • 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 sorted
      ascending - 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 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
    • 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