Package org.nuiton.util
Class ObjectUtil
java.lang.Object
org.nuiton.util.ObjectUtil
public class ObjectUtil
extends java.lang.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:
- Benjamin Poussin - poussin@codelutin.com
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCLASS_METHOD_SEPARATORUsed to know what is separator between class and methodprotected static java.lang.IntegerZEROprotected static java.lang.ByteZEROBprotected static java.lang.CharacterZEROCprotected static java.lang.DoubleZERODprotected static java.lang.FloatZEROFprotected static java.lang.LongZEROL -
Method Summary
Modifier and Type Method Description static java.lang.Objectcall(java.lang.Object o, java.lang.reflect.Method m, java.lang.String... params)Call method m with params as String.protected static java.lang.ObjectchoiceArgument(java.lang.Class<?> clazz, java.util.List args, boolean nullIfMissing)Permet de matcher un type d'argument attendu clazz parmi un ensemble possible de candidat.static <E extends java.lang.Cloneable>
Eclone(E e)Clone object by introspection because Cloneable interface don't permit to call clone :(.protected static java.lang.Objectconvert(java.lang.String v, java.lang.Class<?> clazz)static java.lang.Objectcreate(java.lang.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> EdeepClone(E e)Use serialization/deserialization to do deep clone of objectstatic <T> java.util.List<java.lang.reflect.Constructor<T>>getConstructor(java.lang.Class<T> clazz, int paramNumber)Get all constructors that support paramNumber as parameters numbers.static java.util.List<java.lang.reflect.Method>getMethod(java.lang.Class<?> clazz, java.lang.String methodName, boolean ignoreCase)Get all methods with name given in argument without check parameters.static java.util.List<java.lang.reflect.Method>getMethod(java.lang.String name, boolean ignoreCase)List method that match name, name must be [package.]static booleanisNullValue(boolean value)static booleanisNullValue(byte value)static booleanisNullValue(char value)static booleanisNullValue(double value)static booleanisNullValue(float value)static booleanisNullValue(int value)static booleanisNullValue(java.lang.Object value)Tests if the given value is null according to default value for primitive types if nedded.static booleanisPrimitive(java.lang.Class<?> clazz)Verifie si la classe est de type primitif.static <E> EnewInstance(java.lang.Class<E> clazz, java.util.Collection<?> args, boolean nullIfMissing)Invoke constructor on clazz to create new instance.static <T> TnewInstance(java.lang.Class<T> clazz, java.lang.String... params)Create new instance of clazz, call constructor with params as String.static java.lang.ObjectnewInstance(java.lang.String constructorWithParams)
-
Field Details
-
CLASS_METHOD_SEPARATOR
public static final java.lang.String CLASS_METHOD_SEPARATORUsed to know what is separator between class and method- See Also:
- Constant Field Values
-
ZERO
protected static final java.lang.Integer ZERO -
ZEROC
protected static final java.lang.Character ZEROC -
ZEROF
protected static final java.lang.Float ZEROF -
ZEROL
protected static final java.lang.Long ZEROL -
ZEROD
protected static final java.lang.Double ZEROD -
ZEROB
protected static final java.lang.Byte ZEROB
-
-
Method Details
-
newInstance
public static <E> E newInstance(java.lang.Class<E> clazz, java.util.Collection<?> args, boolean nullIfMissing)Invoke constructor on clazz to create new instance. Try to find argument for constructor in args parameter.- Type Parameters:
E- FIXME- Parameters:
clazz- class of object to instanciateargs- all possible parameter that constructor can usednullIfMissing- if no suitable class or object found in args, use null value (no exception)- Returns:
- new instance
- Throws:
java.lang.IllegalArgumentException- if something is wrong during instanciation
-
choiceArgument
protected static java.lang.Object choiceArgument(java.lang.Class<?> clazz, java.util.List args, boolean nullIfMissing)Permet de matcher un type d'argument 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- le type recherchéargs- la liste des arguments ou des typesnullIfMissing- pour retourner nulle si l'argument n'est pas trouvé- Returns:
- le type d'argument trouvé
-
create
public static java.lang.Object create(java.lang.String classnameAndProperties) throws java.lang.ClassNotFoundException, java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodExceptionCreate 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- FIXME- Returns:
- the instanciated object FIXME
- Throws:
java.lang.ClassNotFoundException- FIXMEjava.lang.IllegalAccessException- FIXMEjava.lang.InstantiationException- FIXMEjava.lang.NoSuchMethodException- FIXMEjava.lang.reflect.InvocationTargetException- FIXME
-
convert
protected static java.lang.Object convert(java.lang.String v, java.lang.Class<?> clazz) -
clone
public static <E extends java.lang.Cloneable> E clone(E e) throws java.lang.CloneNotSupportedExceptionClone 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(); }- Type Parameters:
E- FIXME- Parameters:
e- object to clone- Returns:
- new instance of E
- Throws:
java.lang.CloneNotSupportedException- if some error occur during clone
-
deepClone
public static <E> E deepClone(E e) throws java.lang.CloneNotSupportedExceptionUse serialization/deserialization to do deep clone of object- Type Parameters:
E- FIXME- Parameters:
e- object to clone- Returns:
- new instance of E
- Throws:
java.lang.CloneNotSupportedException- if some error occur during clone
-
call
public static java.lang.Object call(java.lang.Object o, java.lang.reflect.Method m, java.lang.String... params) throws java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationExceptionCall 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 callm- method to callparams- parameters for method call- Returns:
- returned method's value
- Throws:
java.lang.IllegalAccessException- FIXMEjava.lang.IllegalArgumentException- FIXMEjava.lang.reflect.InvocationTargetException- FIXMEjava.lang.InstantiationException- FIXME
-
getMethod
public static java.util.List<java.lang.reflect.Method> getMethod(java.lang.Class<?> clazz, java.lang.String methodName, boolean ignoreCase)Get all methods with name given in argument without check parameters.- Parameters:
clazz- where to search methodmethodName- method name to searchignoreCase- if true, ignore difference in method name case- Returns:
- list of detected methods
-
getMethod
public static java.util.List<java.lang.reflect.Method> getMethod(java.lang.String name, boolean ignoreCase)List method that match name, name must be [package.][class][#][method] if package, class or method missing, exception throw- Parameters:
name- name of the methodignoreCase- check exact method name if false- Returns:
- list of method that match name
- Since:
- 2.6.9
-
newInstance
public static java.lang.Object newInstance(java.lang.String constructorWithParams) throws java.lang.ClassNotFoundException- Throws:
java.lang.ClassNotFoundException
-
newInstance
public static <T> T newInstance(java.lang.Class<T> clazz, java.lang.String... params) throws java.lang.IllegalArgumentExceptionCreate 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 instanciateparams- parameters for constructor call- Returns:
- new instance of clazz
- Throws:
java.lang.IllegalArgumentException- FIXME
-
getConstructor
public static <T> java.util.List<java.lang.reflect.Constructor<T>> getConstructor(java.lang.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 constructeurparamNumber- le nombre de parametre souhaite pour le constructeur, -1 indique que tous les constructeur sont souhaite.- Returns:
- list of constructors
-
isNullValue
public static boolean isNullValue(java.lang.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:
trueif 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) -
isPrimitive
public static boolean isPrimitive(java.lang.Class<?> clazz)Verifie si la classe est de type primitif.- Parameters:
clazz- nom de la classe a tester- Returns:
- vrai si le classe est de type primitif faux sinon
-