dsto.dfc.util
Class Objects

java.lang.Object
  extended by dsto.dfc.util.Objects

public final class Objects
extends Object

Utility methods for manipulating objects.

Version:
$Revision: 1.20 $

Method Summary
static String className(Class<?> type)
          Return just the name (minus the package) of a class.
static String className(Object object)
          Return just the name (minus the package) of an object's class.
static Object cloneBySerialization(Object object)
          Clone an object graph by serializing and then deserializing it.
static Collection cloneCollection(Collection collection)
          Deep clone a collection by cloning the collection itself and then cloning each entry.
static Object cloneObject(Object object)
          Attempts to clone an object instance where generic cloning is required but only an Object reference (with protected clone () access) is held.
static Method findMethod(Class targetClass, String methodName)
          Search the superclass/interface tree of a given class for a method with a given name.
static Method findMethod(Class targetClass, String methodName, Class paramClass)
          Search for a method taking a parameter compatible with a given parameter class.
static Method findMethod(Class type, String methodName, Class[] argTypes)
           
static int getIntField(Object obj, String fieldName)
          Shortcut to get the value of an int field from an object using reflection.
static Method getMethod(Class theClass, String methodName, Class param)
          Get a method using reflection, returning null if not found.
static Object instantiate(Class type)
          Instantiate a value of the given class.
static boolean objectsEqual(Object o1, Object o2)
          Test for equality, handling shortcut when o1 == o2 and when one or both of the values are null.
static boolean objectsEqualEx(Object o1, Object o2)
          Extended version of objectsEqual () that does deep equality checking of arrays.
static Object simpleClone(Object object)
          Simple way to clone objects via dyanamic invokation of the clone () method.
static Throwable unwrapException(Throwable ex)
          Unwrap one of Java's various exceptions that exist to wrap another one (ie hide the real source of the problem).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

objectsEqual

public static boolean objectsEqual(Object o1,
                                   Object o2)
Test for equality, handling shortcut when o1 == o2 and when one or both of the values are null.

See Also:
objectsEqualEx(Object, Object)

objectsEqualEx

public static boolean objectsEqualEx(Object o1,
                                     Object o2)
Extended version of objectsEqual () that does deep equality checking of arrays.

See Also:
objectsEqual(Object, Object)

cloneObject

public static Object cloneObject(Object object)
                          throws CloneNotSupportedException
Attempts to clone an object instance where generic cloning is required but only an Object reference (with protected clone () access) is held. If the object supports the Copyable interface, this is used to directly invoke clone (), otherwise reflection is used. Standard Java classes (like String, Integer, Color etc) which inexplicably do not support cloning are fudged by using their constructors. IDataObject's are cloned using DataObjects.deepClone(IDataObject).

Throws:
CloneNotSupportedException - if object is not cloneable or clone () threw an exception (eg clone () is protected).
See Also:
simpleClone(Object)

simpleClone

public static Object simpleClone(Object object)
                          throws CloneNotSupportedException
Simple way to clone objects via dyanamic invokation of the clone () method.

Throws:
CloneNotSupportedException
See Also:
cloneObject(Object)

cloneCollection

public static Collection cloneCollection(Collection collection)
                                  throws CloneNotSupportedException
Deep clone a collection by cloning the collection itself and then cloning each entry. The entries in ordered collections in the new collection are added in the order defined by the default iterator.

Parameters:
collection - The collection to clone.
Returns:
A deep copy of the collection.
Throws:
CloneNotSupportedException

cloneBySerialization

public static Object cloneBySerialization(Object object)
                                   throws CloneNotSupportedException
Clone an object graph by serializing and then deserializing it. All objects in the graph must implement java.io.Serializable.

Parameters:
object - The object to clone.
Returns:
A copy of object.
Throws:
CloneNotSupportedException - if an error occurs while serializing or serializing.

unwrapException

public static Throwable unwrapException(Throwable ex)
Unwrap one of Java's various exceptions that exist to wrap another one (ie hide the real source of the problem).


getIntField

public static int getIntField(Object obj,
                              String fieldName)
                       throws SecurityException,
                              NoSuchFieldException,
                              IllegalArgumentException
Shortcut to get the value of an int field from an object using reflection.

Parameters:
obj - The object.
fieldName - The field name. May be private.
Returns:
The int value of field.
Throws:
SecurityException - If the security manager bounces us.
NoSuchFieldException - Guess
IllegalArgumentException - If the field is not an int.

instantiate

public static Object instantiate(Class type)
                          throws IllegalArgumentException
Instantiate a value of the given class.

Throws:
IllegalArgumentException

getMethod

public static Method getMethod(Class theClass,
                               String methodName,
                               Class param)
Get a method using reflection, returning null if not found.


findMethod

public static Method findMethod(Class targetClass,
                                String methodName,
                                Class paramClass)
Search for a method taking a parameter compatible with a given parameter class. Searches the class, its superclasses and all interfaces.


findMethod

public static Method findMethod(Class targetClass,
                                String methodName)
Search the superclass/interface tree of a given class for a method with a given name.


findMethod

public static Method findMethod(Class type,
                                String methodName,
                                Class[] argTypes)

className

public static String className(Object object)
Return just the name (minus the package) of an object's class.


className

public static String className(Class<?> type)
Return just the name (minus the package) of a class.



Copyright © 2008 Commonwealth of Australia