Package io.vertigo.core.util
Class ClassUtil
java.lang.Object
io.vertigo.core.util.ClassUtil
The ClassUtil class provides methods to determine the structure of a class or to create instances.
- Author:
- pchretien
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?> classForName(String javaClassName) Retrieves an untyped class from its name.static <J> Class<? extends J> classForName(String javaClassName, Class<J> type) Retrieves a typed class from its name.static <J> Constructor<J> findConstructor(Class<J> clazz, Class<?>[] parameterTypes) Retrieves the constructor corresponding to the given signature.static MethodfindMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) Retrieves the method corresponding to the name and signature among the passed methods.static ObjectDynamic retrieval of a field's value.static Collection<Field> getAllFields(Class<?> clazz) Returns all declared fields (including parent fields) for a given class.static Collection<Field> getAllFields(Class<?> clazz, Class<? extends Annotation> annotation) Returns all declared fields (including parent fields) annotated for a given class.getAllInterfaces(Class<?> clazz) Returns all interfaces (including those of the parents) for a given class.static Collection<Method> getAllMethods(Class<?> clazz) Returns all declared methods for a given class (including parent methods).static Collection<Method> getAllMethods(Class<?> clazz, Class<? extends Annotation> annotation) Returns all declared methods annotated by the given annotation.static Class<?> getGeneric(Constructor<?> constructor, int i) Retrieves the generic type of a parameterized field.static Class<?> getGeneric(Field field) Retrieves the generic type of a parameterized field.static Class<?> getGeneric(Method method, int i) Retrieves the generic type of a parameterized method.static ObjectDynamic invocation of a method on a specific instance.static <J> JnewInstance(Class<J> clazz) Creates a new typed instance via a class (empty constructor).static <J> JnewInstance(Constructor<J> constructor, Object[] args) Creates a new typed instance via a constructor and its arguments.static ObjectnewInstance(String javaClassName) Creates a new untyped instance via class name (empty constructor).static <J> JnewInstance(String javaClassName, Class<J> type) Creates a new typed instance via class name (empty constructor).static voidDynamic assignment of a field's value (even private).
-
Method Details
-
newInstance
Creates a new untyped instance via class name (empty constructor). Please favor methods returning a typed instance as soon as the type is known.- Parameters:
javaClassName- Class name- Returns:
- New instance
-
newInstance
Creates a new typed instance via class name (empty constructor).- Type Parameters:
J- Type of the returned instance- Parameters:
javaClassName- Class nametype- Returned type- Returns:
- New instance
-
newInstance
Creates a new typed instance via a class (empty constructor).- Type Parameters:
J- Type of the returned instance- Parameters:
clazz- Class- Returns:
- New instance
-
newInstance
Creates a new typed instance via a constructor and its arguments.- Type Parameters:
J- Type of the returned instance- Parameters:
constructor- Constructorargs- Construction arguments- Returns:
- New instance
-
findConstructor
Retrieves the constructor corresponding to the given signature.- Type Parameters:
J- Class type- Parameters:
clazz- Class to search for the constructorparameterTypes- Signature of the searched constructor- Returns:
- Searched constructor
-
classForName
Retrieves an untyped class from its name.- Parameters:
javaClassName- Class name- Returns:
- Java class
-
classForName
Retrieves a typed class from its name.- Type Parameters:
J- Type of the returned instance- Parameters:
javaClassName- Class nametype- Type.- Returns:
- Java class
-
invoke
Dynamic invocation of a method on a specific instance.- Parameters:
instance- Objectmethod- method which is invokedargs- Args- Returns:
- value provided as the result by the method
-
set
Dynamic assignment of a field's value (even private).- Parameters:
instance- Object on which the method is invokedfield- Field concernedvalue- New value
-
get
Dynamic retrieval of a field's value.- Parameters:
instance- Object on which the method is invokedfield- Concerned field- Returns:
- Value
-
findMethod
Retrieves the method corresponding to the name and signature among the passed methods.- Parameters:
clazz- Class on which we are searching for methodsmethodName- Name of the searched methodparameterTypes- Signature of the searched method- Returns:
- Searched method
-
getAllFields
public static Collection<Field> getAllFields(Class<?> clazz, Class<? extends Annotation> annotation) Returns all declared fields (including parent fields) annotated for a given class.- Parameters:
clazz- Classannotation- Expected annotation- Returns:
- All declared fields (including parent fields)
-
getAllMethods
public static Collection<Method> getAllMethods(Class<?> clazz, Class<? extends Annotation> annotation) Returns all declared methods annotated by the given annotation.- Parameters:
clazz- Classannotation- Expected annotation- Returns:
- All declared fields (including parent fields)
-
getAllFields
Returns all declared fields (including parent fields) for a given class.- Parameters:
clazz- Class- Returns:
- All declared fields (including parent fields)
-
getAllMethods
Returns all declared methods for a given class (including parent methods).- Parameters:
clazz- Class- Returns:
- All declared methods (including parent methods)
-
getAllInterfaces
Returns all interfaces (including those of the parents) for a given class.- Parameters:
clazz- Class- Returns:
- All implemented interfaces
-
getGeneric
Retrieves the generic type of a parameterized field. It is expected that there is ONE and only ONE declared generic. Example: List=> Car Option => Car - Parameters:
constructor- Constructori- Index of the parameter in the component- Returns:
- Class of the generic type
-
getGeneric
Retrieves the generic type of a parameterized method. It is expected that there is ONE and only ONE declared generic. Example: List=> Car Option => Car - Parameters:
method- Methodi- Index of the parameter in the component- Returns:
- Class of the generic type
-
getGeneric
Retrieves the generic type of a parameterized field. It is expected that there is ONE and only ONE declared generic. Example: List=> Car Option => Car - Parameters:
field- Field- Returns:
- Class of the generic type
-