org.nuiton.util
Class ObjectUtil

java.lang.Object
  extended by org.nuiton.util.ObjectUtil

public class ObjectUtil
extends Object

Outils pour manipuler des objets. Création d'un objet à partir d'une chaîne le décrivant, conversion d'un objet en Object, récupération de méthodes à partir de leur nom, de constructeurs à partir de leurs paramètres...

Created: 4 novembre 2004

Author:
bpoussin $Id: ObjectUtil.java 2111 2011-04-16 10:56:44Z fdesbois $

Field Summary
protected static Integer ZERO
           
protected static Byte ZEROB
           
protected static Character ZEROC
           
protected static Double ZEROD
           
protected static Float ZEROF
           
protected static Long ZEROL
           
 
Method Summary
static Object call(Object o, Method m, String... params)
          Call method m with params as String.
protected static Object choiceArgument(Class clazz, List args, boolean nullIfMissing)
          Permet de matcher un type d'agument attendu clazz parmi un ensemble possible de candidat.
static
<E extends Cloneable>
E
clone(E e)
          Clone object by introspection because Cloneable interface don't permit to call clone :(.
protected static Object convert(String v, Class<?> clazz)
           
static Object create(String classnameAndProperties)
          Create new object from string like org.nuiton.Toto(name=machine, int=10) where machine and int is properties on org.nuiton.Toto object.
static
<E> E
deepClone(E e)
          Use serialization/deserialization to do deep clone of object
static
<T> List<Constructor<T>>
getConstructor(Class<T> clazz, int paramNumber)
          Get all constructors that support paramNumber as parameters numbers.
static List<Method> getMethod(Class<?> clazz, String methodName, boolean ignoreCase)
          Get all methods with name given in argument without check parameters
static Object getNullValue(Class<?> type)
          Obtains the null value for the given type (works too with primitive types).
static boolean isNullValue(boolean value)
           
static boolean isNullValue(byte value)
           
static boolean isNullValue(char value)
           
static boolean isNullValue(double value)
           
static boolean isNullValue(float value)
           
static boolean isNullValue(int value)
           
static boolean isNullValue(Object value)
          Tests if the given value is null according to default value for primitive types if nedded.
static
<E> E
newInstance(Class<E> clazz, Collection args, boolean nullIfMissing)
          Invoke constructor on clazz to create new instance.
static
<T> T
newInstance(Class<T> clazz, String... params)
          Create new instance of clazz, call constructor with params as String.
static Object newInstance(String constructorWithParams)
           
static Object toObject(boolean b)
          Method toObject transform a boolean to a Boolean object
static Object toObject(byte b)
          Method toObject transform a byte to a Byte Object
static Object toObject(char c)
          Method toObject transform a char to a Character Object
static Object toObject(double d)
          Method toObject transform a double to a Double object
static Object toObject(float f)
          Method toObject transform a float to a Float Object
static Object toObject(int i)
          Method toObject transform an int to an Integer object
static Object toObject(long l)
          Method toObject transform a long to a Long object
static Object toObject(Object o)
          Method toObject
static Object toObject(short s)
          Method toObject transform a short to a Short object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZERO

protected static final Integer ZERO

ZEROC

protected static final Character ZEROC

ZEROF

protected static final Float ZEROF

ZEROL

protected static final Long ZEROL

ZEROD

protected static final Double ZEROD

ZEROB

protected static final Byte ZEROB
Method Detail

newInstance

public static <E> E newInstance(Class<E> clazz,
                                Collection args,
                                boolean nullIfMissing)
Invoke constructor on clazz to create new instance. Try to find argument for constructor in args parameter.

Parameters:
clazz - class of object to instanciate
args - all possible parameter that constructor can used
nullIfMissing - if no suitable class or object found in args, use null value (no exception)
Returns:
new instance
Throws:
IllegalArgumentException - if something is wrong during instanciation

choiceArgument

protected static Object choiceArgument(Class clazz,
                                       List args,
                                       boolean nullIfMissing)
Permet de matcher un type d'agument attendu clazz parmi un ensemble possible de candidat. Les candidats peuvent etre des classes qu'il faudra instancier pour satisfaire le type demande.

Parameters:
clazz -
args -
nullIfMissing -
Returns:

create

public static Object create(String classnameAndProperties)
                     throws ClassNotFoundException,
                            InstantiationException,
                            IllegalAccessException,
                            InvocationTargetException,
                            NoSuchMethodException
Create new object from string like org.nuiton.Toto(name=machine, int=10) where machine and int is properties on org.nuiton.Toto object. Conversion between 10 in string and 10 as integer as automaticaly done For String property you can use ex:
  • name="my string with , in string"
  • name='my string with , in string'

    Parameters:
    classnameAndProperties -
    Returns:
    the instanciated object
    Throws:
    ClassNotFoundException
    IllegalAccessException
    InstantiationException
    NoSuchMethodException
    InvocationTargetException

  • convert

    protected static Object convert(String v,
                                    Class<?> clazz)

    clone

    public static <E extends Cloneable> E clone(E e)
                                     throws CloneNotSupportedException
    Clone object by introspection because Cloneable interface don't permit to call clone :(. This methode replace next code that don't work :(
     if (o instanceof Cloneable) {
       Object n = ((Cloneable)o).clone();
     }
     

    Parameters:
    e - object to clone
    Returns:
    new instance of E
    Throws:
    CloneNotSupportedException - if some error occur during clone

    deepClone

    public static <E> E deepClone(E e)
                       throws CloneNotSupportedException
    Use serialization/deserialization to do deep clone of object

    Parameters:
    e - object to clone
    Returns:
    new instance of E
    Throws:
    CloneNotSupportedException - if some error occur during clone

    call

    public static Object call(Object o,
                              Method m,
                              String... params)
                       throws IllegalAccessException,
                              IllegalArgumentException,
                              InvocationTargetException,
                              InstantiationException
    Call method m with params as String. Each param is converted to required type for method with beanutils converter

    Parameters:
    o - object where method must be call
    m - method to call
    params - parameters for method call
    Returns:
    returned method's value
    Throws:
    IllegalAccessException
    IllegalArgumentException
    InvocationTargetException
    InstantiationException

    getMethod

    public static List<Method> getMethod(Class<?> clazz,
                                         String methodName,
                                         boolean ignoreCase)
    Get all methods with name given in argument without check parameters

    Parameters:
    clazz -
    methodName - method name to search
    ignoreCase - if true, ignore difference in method name case
    Returns:
    list of detected methods

    newInstance

    public static Object newInstance(String constructorWithParams)
                              throws ClassNotFoundException
    Throws:
    ClassNotFoundException

    newInstance

    public static <T> T newInstance(Class<T> clazz,
                                    String... params)
                         throws IllegalArgumentException
    Create new instance of clazz, call constructor with params as String. Each param is converted to required type for constructor with beanutils converter, first constructor that permit instanciation is used

    Type Parameters:
    T - type to instanciate
    Parameters:
    clazz - class to instanciate
    params - parameters for constructor call
    Returns:
    new instance of clazz
    Throws:
    IllegalArgumentException

    getConstructor

    public static <T> List<Constructor<T>> getConstructor(Class<T> clazz,
                                                          int paramNumber)
    Get all constructors that support paramNumber as parameters numbers. Varargs is supported

    Type Parameters:
    T - le type de la classe a inspecter
    Parameters:
    clazz - la classe sur lequel rechercher le constructeur
    paramNumber - le nombre de parametre souhaite pour le constructeur, -1 indique que tous les constructeur sont souhaite.
    Returns:
    list of constructors

    toObject

    public static Object toObject(Object o)
    Method toObject

    Parameters:
    o - Object to transform
    Returns:
    the same object

    toObject

    public static Object toObject(char c)
    Method toObject transform a char to a Character Object

    Parameters:
    c - the char to transform
    Returns:
    the Charactere object corresponding

    toObject

    public static Object toObject(byte b)
    Method toObject transform a byte to a Byte Object

    Parameters:
    b - the byte to transform
    Returns:
    the byte object corresponding

    toObject

    public static Object toObject(short s)
    Method toObject transform a short to a Short object

    Parameters:
    s - the short to transform
    Returns:
    the Short object corresponding

    toObject

    public static Object toObject(int i)
    Method toObject transform an int to an Integer object

    Parameters:
    i - the int to transform
    Returns:
    the Integer Object corresponding

    toObject

    public static Object toObject(long l)
    Method toObject transform a long to a Long object

    Parameters:
    l - the long to transform
    Returns:
    the Long Object corresponding

    toObject

    public static Object toObject(float f)
    Method toObject transform a float to a Float Object

    Parameters:
    f - the float to transform
    Returns:
    the Float Object corresponding

    toObject

    public static Object toObject(double d)
    Method toObject transform a double to a Double object

    Parameters:
    d - the double to transform
    Returns:
    the Double object corresponding

    toObject

    public static Object toObject(boolean b)
    Method toObject transform a boolean to a Boolean object

    Parameters:
    b - the boolean to transform
    Returns:
    the Boolean object corresponding

    getNullValue

    public static Object getNullValue(Class<?> type)
    Obtains the null value for the given type (works too with primitive types).

    Parameters:
    type - the type to test
    Returns:
    the null value or default value for primitive types
    Since:
    1.1.5

    isNullValue

    public static boolean isNullValue(Object value)
    Tests if the given value is null according to default value for primitive types if nedded.

    Parameters:
    value - the value to test
    Returns:
    true if value is null or default value on a primitive
    Since:
    1.1.5

    isNullValue

    public static boolean isNullValue(boolean value)

    isNullValue

    public static boolean isNullValue(byte value)

    isNullValue

    public static boolean isNullValue(int value)

    isNullValue

    public static boolean isNullValue(char value)

    isNullValue

    public static boolean isNullValue(float value)

    isNullValue

    public static boolean isNullValue(double value)


    Copyright © 2004-2011 CodeLutin. All Rights Reserved.