Object utilities.
*
* @version $Revision: 1.1 $
* @author Jason Dillon
*/
public final class Objects
{
/////////////////////////////////////////////////////////////////////////
// Coercion Methods //
/////////////////////////////////////////////////////////////////////////
/**
* Get a compatible constructor for the given value type
*
* @param type Class to look for constructor in
* @param valueType Argument type for constructor
* @return Constructor or null
*/
public static Constructor getCompatibleConstructor(final Class type,
final Class valueType)
{
// first try and find a constructor with the exact argument type
try {
return type.getConstructor(new Class[] { valueType });
}
catch (Exception ignore) {
// if the above failed, then try and find a constructor with
// an compatible argument type
// get an array of compatible types
Class[] types = type.getClasses();
for (int i=0; iIf type is an array, then an array of that type is returned * else the first element from values is used. * *
Coerce will handle primative array types correctly by using
* the reflection mechanism to unwrap primative values from their
* wrapper classes.
*
* @param values Values to coerce.
* @param type Class type to coerce object to.
* @return Coerced object.
*
* @exception NotCoercibleException Value is not corecible.
* @exception CoercionException Failed to coerce.
* @exception IllegalArgumentException Indexed value is null
* (values contains a null element).
*/
public static Object coerce(Object values[], Class type)
throws CoercionException
{
// if the desired type is not an array, the use the first element
// from the values list
if (! type.isArray())
return coerce(values[0], type);
// create a new array that can hold objects of the desired type
type = type.getComponentType();
Object array = Array.newInstance(type, values.length);
// coerce each element in the values array into the new array
for (int i=0; iReference. If the object is null
* then null is returned. If the object is not an instance of
* Reference, then the object is returned.
*
* @param obj Object to dereference.
* @return Dereferenced object.
*/
public static Object deref(final Object obj) {
if (obj != null && obj instanceof Reference) {
Reference ref = (Reference)obj;
return ref.get();
}
return obj;
}
/**
* Check if the given object is an array (primitve or native).
*
* @param obj Object to test.
* @return True of the object is an array.
*/
public static boolean isArray(final Object obj) {
if (obj != null)
return obj.getClass().isArray();
return false;
}
/**
* Return an Object array for the given object.
*
* @param obj Object to convert to an array. Converts primitive
* arrays to Object arrays consisting of their wrapper
* classes. If the object is not an array (object or primitve)
* then a new array of the given type is created and the
* object is set as the sole element.
*/
public static Object[] toArray(final Object obj) {
// if the object is an array, the cast and return it.
if (obj instanceof Object[]) {
return (Object[])obj;
}
// if the object is an array of primitives then wrap the array
Class type = obj.getClass();
Object array;
if (type.isArray()) {
int length = Array.getLength(obj);
Class componentType = type.getComponentType();
array = Array.newInstance(componentType, length);
for (int i=0; i