Class ObjectUtil

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

  • Constructor Details

    • ObjectUtil

      public ObjectUtil()
  • Method Details

    • createObject

      public static Object createObject(String className)
      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

      public static Object createObject(String className, Object[] parms)
      A class factory method that uses the pluggable classLoader: 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:
    • 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: m_classLoader
      Parameters:
      className -
      Returns:
      the Class if found
      Throws:
      ClassNotFoundException
      See Also:
    • getClassLoader

      public static ClassLoader 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

      public static void setClassLoader(ClassLoader classLoader)
      Set the classloader to be used by this ObjectUtil instance
      Parameters:
      classLoader -
      See Also:
    • StringToValue

      public static Object StringToValue(String s, Class t)
      Deprecated.
      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:
    • 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)