public class ReflectionUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.lang.reflect.Constructor<?> |
getConstructor(java.lang.Class<?> type,
java.lang.Class<?> parameterType)
Finds the constructor for the specified type and
parameterType.
|
static java.lang.Object |
getFieldValue(java.lang.Object object,
java.lang.String fieldName)
Gets the public specified field value from object instance
|
static java.lang.reflect.Method |
getMethod(java.lang.Class<?> type,
java.lang.String name,
java.lang.Class<?> parameterType)
Finds the method on the specified type, with the name
name, and that takes a parameter of parameterType.
|
static java.lang.Object |
instantiate(java.lang.String className)
Instantiate an object from the specified class.
|
static java.lang.Object |
instantiate(java.lang.String className,
java.lang.Class<?> parameterType,
java.lang.Object parameterValue)
Instantiate an object from the specified class.
|
static java.lang.Object |
invoke(java.lang.Object object,
java.lang.String methodName)
Invokes the method with the specified methodName.
|
static java.lang.Object |
invoke(java.lang.Object object,
java.lang.String methodName,
java.lang.Class<?> parameterType,
java.lang.Object parameterValue)
Invokes the method with the specified methodName that takes a
parameter of type parameterType and passes the
parameterValue as an argument.
|
static java.lang.Object |
invokeStatic(java.lang.Class<?> type,
java.lang.String methodName,
java.lang.Class<?>[] parameterTypes,
java.lang.Object[] arguments) |
static java.lang.Class<?> |
loadPrimitiveClass(java.lang.String primitiveType)
Loads the class for the specified primitive data type.
|
static java.lang.Class<?> |
loadPrimitiveClassForWrapper(java.lang.Class<?> wrapperType)
Loads the primitive class for the wrapper class.
|
static java.lang.Class<?> |
loadPrimitiveOrObjectClass(java.lang.String className)
Loads a primitive or object class from the specified class name.
|
static java.lang.Class<?> |
loadWrapperClassForPrimitive(java.lang.Class<?> primitiveType)
Loads the wrapper class for the primitive data type.
|
static java.lang.Class<?> |
loadWrapperOrObjectClass(java.lang.String className)
Loads the wrapper class for a primitive type or object class.
|
public static java.lang.Object instantiate(java.lang.String className) throws ReflectionException
className
- the name of the class on instantiate.ReflectionException
- if unable to instantiate object from
specified class.public static java.lang.Object instantiate(java.lang.String className, java.lang.Class<?> parameterType, java.lang.Object parameterValue) throws ReflectionException
If the className is equal to a primitive data type (e.g., int, short, boolean, double), this method will try to instantiate the corresponding wrapper class (e.g., java.lang.Integer, java.lang.Short, java.lang.Boolean, java.lang.Double).
className
- the name of the class to instantiate.parameterType
- the parameter type.parameterValue
- the paramteter value.ReflectionException
- if unable to instantiate object from
specified class.public static java.lang.Class<?> loadPrimitiveOrObjectClass(java.lang.String className) throws java.lang.ClassNotFoundException
If the className is equal to a primitive type (e.g., int, short, char, boolean) the class of that primitive type will be returned. However, if the className doesn't match the name of a primitive data type this method will try to load the class as an object (non primitive) class.
className
- the name of the class to load.java.lang.ClassNotFoundException
- if unable to load the class.public static java.lang.Class<?> loadWrapperOrObjectClass(java.lang.String className) throws java.lang.ClassNotFoundException
If the className is equal to a primitive data type (e.g., int, short, boolean, double), this method will try to load the corresponding wrapper class (e.g., java.lang.Integer, java.lang.Short, java.lang.Boolean, java.lang.Double).
className
- the name of the class.java.lang.ClassNotFoundException
- if unable to load the class.public static java.lang.Class<?> loadPrimitiveClass(java.lang.String primitiveType) throws java.lang.ClassNotFoundException
primitiveType
- the primitive data type.java.lang.ClassNotFoundException
- if unable to load the class.public static java.lang.Class<?> loadWrapperClassForPrimitive(java.lang.Class<?> primitiveType) throws java.lang.ClassNotFoundException
If the primitiveType represents a primitive data type (e.g., int, short, boolean, double), this method will try to load the corresponding wrapper class (e.g., java.lang.Integer, java.lang.Short, java.lang.Boolean, java.lang.Double).
primitiveType
- the primitive data type.java.lang.ClassNotFoundException
- if unable to load the class.public static java.lang.Class<?> loadPrimitiveClassForWrapper(java.lang.Class<?> wrapperType) throws java.lang.ClassNotFoundException
If the wrapperType represents a wrapper class (e.g., java.lang.Integer, java.lang.Short, java.lang.Boolean, java.lang.Double), this method will try to load the corresponding primitive data type (e.g., int, short, boolean, double)
wrapperType
- the wrapper class.java.lang.ClassNotFoundException
- if unable to load the class.public static java.lang.Object invoke(java.lang.Object object, java.lang.String methodName, java.lang.Class<?> parameterType, java.lang.Object parameterValue) throws ReflectionException
object
- the object on which to invoke the method.methodName
- the name of the method.parameterType
- the parameter type.parameterValue
- the parameter value.ReflectionException
- if unable to invoke the method.public static java.lang.Object invoke(java.lang.Object object, java.lang.String methodName) throws ReflectionException
object
- the object on which to invoke the method.methodName
- the name of the method.ReflectionException
- if unable to invoke the method.public static java.lang.reflect.Constructor<?> getConstructor(java.lang.Class<?> type, java.lang.Class<?> parameterType) throws ReflectionException
type
- the type for which to get the constructor.parameterType
- the parameter type.ReflectionException
- if unable to find the constructor.public static java.lang.reflect.Method getMethod(java.lang.Class<?> type, java.lang.String name, java.lang.Class<?> parameterType) throws ReflectionException
type
- the class on which to find the method.name
- the name of the method.parameterType
- the parameter type.ReflectionException
- if unable to find the method.public static java.lang.Object invokeStatic(java.lang.Class<?> type, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Object[] arguments) throws ReflectionException
ReflectionException
public static java.lang.Object getFieldValue(java.lang.Object object, java.lang.String fieldName) throws ReflectionException
object
- field
- ReflectionException
Copyright ? Technia AB. All Rights Reserved.