public final class ReflectionUtil extends Object
Modifier and Type | Method and Description |
---|---|
static <T> T |
callMethod(Object object,
Method method,
Object... parameters)
Calls the given method with the given parameters on the given object.
|
static <T> T |
callMethod(Object object,
String methodName)
Calls the parameterless method with the given name on the given object.
|
static <T> T |
createInstance(Class<T> objectType)
Creates a new instance of the given object type using the default constructor.
|
static Class<?> |
getActualTypeParameter(Field field,
int index)
Returns the actual type parameter with the given index of the given field or null if it does not exist.
|
static Collection<Field> |
getAllFields(Class<?> objectType)
Returns the fields of the given class.
|
static Class<?> |
getClass(String clazz)
Returns a Class object which represents the class with the given name.
|
static Constructor<?> |
getDefaultConstructor(Class<?> objectType)
Returns the default constructor of the given object type.
|
static Method |
getMethod(Class<?> objectType,
String methodName,
Class<?>... parameterTypes)
Returns the method on the objectType with the given name and given parameter types.
|
static <T> T |
getNearest(Class<?> objectType,
Map<Class<?>,T> map)
Returns the value mapped to the given object type or one of its supertypes (classes or interfaces) using a
nearest match algorithm.
|
static List<Class<?>> |
getTypesInReverseOrder(Class<?> objectType)
Returns the path of classes to the given object type in reverse order.
|
static boolean |
hasClass(String clazz)
Checks whether a given Class is available on the current platform.
|
public static Constructor<?> getDefaultConstructor(Class<?> objectType)
objectType
- the object typepublic static <T> T createInstance(Class<T> objectType)
T
- the type of the object to createobjectType
- the object typepublic static Collection<Field> getAllFields(Class<?> objectType)
objectType
- the classpublic static List<Class<?>> getTypesInReverseOrder(Class<?> objectType)
Object
is not included.objectType
- the object typepublic static Method getMethod(Class<?> objectType, String methodName, Class<?>... parameterTypes)
objectType
- the object typemethodName
- the method nameparameterTypes
- the parameter typespublic static <T> T callMethod(Object object, String methodName)
T
- the expected type of the return valueobject
- the object to invoke the method onmethodName
- the name of the methodpublic static <T> T callMethod(Object object, Method method, Object... parameters)
T
- the expected type of the return valueobject
- the object to invoke the method onmethod
- the methodparameters
- the parameterspublic static Class<?> getActualTypeParameter(Field field, int index)
field
- the fieldindex
- the index of the requested actual type parameterpublic static <T> T getNearest(Class<?> objectType, Map<Class<?>,T> map)
The used "nearest" relation has the following properties:
1. An object type is the nearest object type to itself.
2. Any direct super type (class or interface) is nearer to a given type then any indirect super type.
3. A direct super class of a class A is nearer than any interface implemented by A.
4. For two interfaces I1 and I2 directly implemented by a given class A, I1 is nearer to A than I2 if I1 is
mentioned before I2 in the the implements
clause of the declaration of the class.
T
- the expected type of the return valueobjectType
- the object typemap
- the mappublic static boolean hasClass(String clazz)
clazz
- The full qualified name of the class to check.true
if class is available, false
otherwise.Copyright © 2014. All rights reserved.