public class ReflectionUtil extends Object
Modifier and Type | Method and Description |
---|---|
static List<Field> |
getAllDeclaredFields(Class<?> clazz)
Gets all fields declared by the given
clazz including its super-classes; starting from the most concrete sub-class
(i.e. |
static Map<Field,Object> |
getAllDeclaredFieldValues(Object object) |
static Set<Class<?>> |
getAllInterfaces(Class<?> clazz) |
static <T> Constructor<T> |
getDeclaredConstructorOrFail(Class<T> clazz,
Class<?>[] parameterTypes) |
static Method |
getDeclaredMethod(Class<?> clazz,
String name,
Class<?>[] parameterTypes) |
static Method |
getDeclaredMethodOrFail(Class<?> clazz,
String name,
Class<?>[] parameterTypes) |
static List<Method> |
getDeclaredMethods(Class<?> clazz,
String name) |
static <V> V |
getFieldValue(Object object,
String fieldName) |
static <T> T |
invoke(Object object,
String methodName,
Class<?>[] parameterTypes,
Object... args) |
static <T> T |
invoke(Object object,
String methodName,
Object... args) |
static <T> T |
invokeConstructor(Class<T> clazz,
Class<?>[] parameterTypes,
Object... args) |
static <T> T |
invokeConstructor(Class<T> clazz,
Object... args) |
static <T> T |
invokeStatic(Class<?> clazz,
String methodName,
Class<?>[] parameterTypes,
Object... args) |
static <T> T |
invokeStatic(Class<?> clazz,
String methodName,
Object... args) |
static <T> Type[] |
resolveActualTypeArguments(Class<T> baseClass,
Class<? extends T> concreteClass)
Resolves the actual type arguments of a base-class declared in a concrete sub-class.
|
static <T> Type[] |
resolveActualTypeArguments(Class<T> baseClass,
T concreteObject)
Resolves the actual type arguments of a base-class declared in a concrete sub-class.
|
static void |
setFieldValue(Object object,
String fieldName,
Object value)
Sets the
object 's field – identified by fieldName – to the given value . |
public static <T> T invokeConstructor(Class<T> clazz, Object... args)
public static <T> T invokeConstructor(Class<T> clazz, Class<?>[] parameterTypes, Object... args)
public static <T> T invokeStatic(Class<?> clazz, String methodName, Object... args)
public static <T> T invokeStatic(Class<?> clazz, String methodName, Class<?>[] parameterTypes, Object... args)
public static <T> T invoke(Object object, String methodName, Class<?>[] parameterTypes, Object... args)
public static List<Method> getDeclaredMethods(Class<?> clazz, String name)
public static <T> Constructor<T> getDeclaredConstructorOrFail(Class<T> clazz, Class<?>[] parameterTypes)
public static Method getDeclaredMethodOrFail(Class<?> clazz, String name, Class<?>[] parameterTypes)
public static Method getDeclaredMethod(Class<?> clazz, String name, Class<?>[] parameterTypes)
public static List<Field> getAllDeclaredFields(Class<?> clazz)
clazz
including its super-classes; starting from the most concrete sub-class
(i.e. the given clazz
).
Please note that the order of the fields declared by one single class is unspecified according to the Java specification. The only guaranteed order is that between fields declared by one class and fields declared by its super-classes: The result of this method begins with fields of the most concrete class, followed by its super-class's fields, followed by the fields of the super-class' super-class and so on.
For example, consider the following classes: Roadster
extends Car
extends Vehicle
. This
method would thus return all fields declared by all three classes, starting with the fields of Roadster
,
followed by the fields of Car
and finally followed by the fields of Vehicle
.
clazz
- the class whose fields to obtain.null
, but maybe
empty.public static Map<Field,Object> getAllDeclaredFieldValues(Object object)
public static <V> V getFieldValue(Object object, String fieldName)
public static void setFieldValue(Object object, String fieldName, Object value)
object
's field – identified by fieldName
– to the given value
.
The fieldName
can be simple (e.g. "firstName") or fully qualified (e.g. "co.codewizards.bla.Person.firstName").
If it is simple, the most concrete sub-class' matching field is used.
object
- the object whose field to manipulate. Must not be null
.fieldName
- the simple or fully qualified field name (fully qualified means prefixed by the class name). Must not be null
.value
- the value to be assigned. May be null
.public static Set<Class<?>> getAllInterfaces(Class<?> clazz)
public static final <T> Type[] resolveActualTypeArguments(Class<T> baseClass, T concreteObject)
This is a convenience method delegating to resolveActualTypeArguments(Class, Class)
passing concreteObject.getClass()
as concreteClass
.
baseClass
- the base class. Must not be null
.concreteObject
- an instance of a sub-class of the generic baseClass
.null
(empty array for a non-generic base-class).public static final <T> Type[] resolveActualTypeArguments(Class<T> baseClass, Class<? extends T> concreteClass)
The length as well as the order of the resolved type arguments matches the declaration order
in the base-class. If a type argument could successfully be resolved, it is usually an instance of
Class
. If it could not be resolved (because the sub-class does not specify the generic type info
- directly or indirectly), it is an instance of TypeVariable
.
A typical use-case is this:
public abstract class MyBase<A, B, C> { final Class<A> actualTypeArgumentA; final Class<B> actualTypeArgumentB; final Class<C> actualTypeArgumentC; public MyBase() { final Type[] actualTypeArguments = resolveActualTypeArguments(MyBase.class, this); // The following assignments fail - of course -, if the concrete class lacks // generic type info - like the example class "MyFail" below. actualTypeArgumentA = (Class<A>) actualTypeArguments[0]; actualTypeArgumentB = (Class<B>) actualTypeArguments[1]; actualTypeArgumentC = (Class<C>) actualTypeArguments[2]; } } public class MyConcrete extends MyBase<Long, Boolean, String> { } public class MyFail extends MyBase { }
baseClass
- the base class. Must not be null
.concreteClass
- a sub-class of the generic baseClass
.null
(empty array for a non-generic base-class).Copyright © 2013–2019. All rights reserved.