public final class JkUtilsReflect
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
JkUtilsReflect.ReflectionInvocationHandler
Proxy Invocation handler, that delegate to target instance via reflection
|
Constructor and Description |
---|
JkUtilsReflect() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
createReflectionProxy(java.lang.Class<T> interfaze,
java.lang.Object target)
Create a Dynamic proxy with the specified interface, delagating call to the
target object.
|
static java.lang.reflect.Method |
findMethodMethodDeclaration(java.lang.Class<?> clazz,
java.lang.String name,
java.lang.Class<?>... argTypes)
Find a method of the given name and argument type on the specified class or its parent classes.
|
static java.util.List<java.lang.reflect.Field> |
getDeclaredFieldsWithAnnotation(java.lang.Class<?> clazz,
boolean includeSuperClass)
Returns all fields declared in the class passed as argument or and its super classes.
|
static java.util.List<java.lang.reflect.Field> |
getDeclaredFieldsWithAnnotation(java.lang.Class<?> clazz,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Returns all fields declared in the class passed as argument or its
super classes annotated with the supplied annotation.
|
static java.lang.reflect.Method |
getDeclaredMethod(java.lang.Class<?> clazz,
java.lang.String name,
java.lang.Class<?>... argTypes)
Returns the method of the given name and argument type on the specified
class.
|
static java.util.List<java.lang.reflect.Method> |
getDeclaredMethodsWithAnnotation(java.lang.Class<?> clazz,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves a list of methods declared in the specified class that are annotated
with a given annotation.
|
static java.lang.reflect.Field |
getField(java.lang.Class<?> clazz,
java.lang.String fieldName)
Gets the field having the specified name on the specified object.
|
static <T> T |
getFieldValue(java.lang.Object object,
java.lang.reflect.Field field)
Same as
Field.get(Object) but throwing only unchecked exceptions. |
static java.lang.Object |
getFieldValue(java.lang.Object object,
java.lang.String fieldName)
Gets the value of the field having the specified name on a given object.
|
static <T extends java.lang.annotation.Annotation> |
getInheritedAnnotation(java.lang.Class<?> clazz,
java.lang.Class<T> annotationClass,
java.lang.String methodName,
java.lang.Class<?>... argTypes)
Returns the annotation declared on a given method.
|
static <T extends java.lang.annotation.Annotation> |
getInheritedAnnotation(java.lang.reflect.Method method,
java.lang.Class<T> annotationClass)
Returns the annotation declared on a given method.
|
static java.lang.reflect.Method |
getMethod(java.lang.Class<?> clazz,
java.lang.String name,
java.lang.Class<?>... argTypes)
Same as
Class.getMethod(String, Class...) but throwing only
unchecked exceptions. |
static java.lang.reflect.Method |
getMethodOrNull(java.lang.Class<?> clazz,
java.lang.String name,
java.lang.Class<?>... argTypes)
Returns the method of the given name and argument type on the specified
class.
|
static java.lang.Class<?>[] |
getTypesOf(java.lang.Object[] params)
Returns an array of the types of specified objects.
|
static <T> T |
invoke(java.lang.Object target,
java.lang.reflect.Method method)
Invokes the specified method on the given object.
|
static <V> V |
invoke(java.lang.Object target,
java.lang.reflect.Method method,
java.lang.Object... params)
Invokes the specified method on the given object.
|
static <T> T |
invoke(java.lang.Object target,
java.lang.String methodName)
Invokes the specified method on the given object.
|
static <T> T |
invokeInstanceMethod(java.lang.Object instance,
java.lang.String methodName,
java.lang.Object... args)
Invokes an instance method with the specified arguments
|
static <T> T |
invokeStaticMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Object... args)
Invokes a static method with the specified arguments
|
static boolean |
isMethodPublicIn(java.lang.Class<?> clazz,
java.lang.String method,
java.lang.Class<?>... args)
Returns whether the specified method signature is declared in the
specified class.
|
static java.util.List<java.lang.reflect.Method> |
methodsHavingName(java.lang.Class clazz,
java.lang.String name) |
static java.lang.reflect.Method |
methodWithSameNameAndArgType(java.lang.reflect.Method original,
java.lang.Class<?> targetClass)
Returns a method from the target class that has the same name and same
argument types then the specified one.
|
static <T> T |
newInstance(java.lang.Class<T> clazz)
Instantiates the given class.
|
static <T> T |
newInstance(java.lang.Class<T> clazz,
java.lang.Class<?> parameterType,
java.lang.Object parameter)
Instantiates the given class using a constructor with specified single argument.
|
static void |
setAccessibleIfNeeded(java.lang.reflect.Field field)
Sets the specified field to accessible if not already done.
|
static void |
setFieldValue(java.lang.Object object,
java.lang.reflect.Field field,
java.lang.Object value)
Sets the specified value for the specified field and object.
|
static java.lang.String |
toString(java.lang.Object object)
Returns a string representation of the given object.
|
public static void setAccessibleIfNeeded(java.lang.reflect.Field field)
public static java.lang.reflect.Method methodWithSameNameAndArgType(java.lang.reflect.Method original, java.lang.Class<?> targetClass)
public static java.util.List<java.lang.reflect.Method> methodsHavingName(java.lang.Class clazz, java.lang.String name)
public static java.lang.Class<?>[] getTypesOf(java.lang.Object[] params)
public static <T> T getFieldValue(java.lang.Object object, java.lang.reflect.Field field)
Field.get(Object)
but throwing only unchecked exceptions.public static void setFieldValue(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value)
public static java.lang.Object getFieldValue(java.lang.Object object, java.lang.String fieldName)
public static java.lang.reflect.Field getField(java.lang.Class<?> clazz, java.lang.String fieldName)
public static <T> T newInstance(java.lang.Class<T> clazz)
public static <T> T newInstance(java.lang.Class<T> clazz, java.lang.Class<?> parameterType, java.lang.Object parameter)
public static <T> T invoke(java.lang.Object target, java.lang.String methodName)
public static <T> T invoke(java.lang.Object target, java.lang.reflect.Method method)
public static <V> V invoke(java.lang.Object target, java.lang.reflect.Method method, java.lang.Object... params)
public static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... argTypes)
Class.getMethod(String, Class...)
but throwing only
unchecked exceptions.public static java.lang.reflect.Method getMethodOrNull(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... argTypes)
null
.public static java.lang.reflect.Method getDeclaredMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... argTypes)
public static java.lang.reflect.Method findMethodMethodDeclaration(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... argTypes)
public static boolean isMethodPublicIn(java.lang.Class<?> clazz, java.lang.String method, java.lang.Class<?>... args)
public static <T extends java.lang.annotation.Annotation> T getInheritedAnnotation(java.lang.reflect.Method method, java.lang.Class<T> annotationClass)
public static <T extends java.lang.annotation.Annotation> T getInheritedAnnotation(java.lang.Class<?> clazz, java.lang.Class<T> annotationClass, java.lang.String methodName, java.lang.Class<?>... argTypes)
public static java.util.List<java.lang.reflect.Field> getDeclaredFieldsWithAnnotation(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
public static java.util.List<java.lang.reflect.Method> getDeclaredMethodsWithAnnotation(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
clazz
- the class to search for methodsannotationClass
- the annotation type to look for on the methodspublic static java.util.List<java.lang.reflect.Field> getDeclaredFieldsWithAnnotation(java.lang.Class<?> clazz, boolean includeSuperClass)
public static <T> T invokeStaticMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Object... args)
clazz
- The class the method is invoked on.methodName
- The method name to invokeargs
- The argument values the method is invoked with.public static <T> T invokeInstanceMethod(java.lang.Object instance, java.lang.String methodName, java.lang.Object... args)
instance
- The instance the method is invoked on.methodName
- The method name to invokeargs
- The argument values the method is invoked with.public static java.lang.String toString(java.lang.Object object)
public static <T> T createReflectionProxy(java.lang.Class<T> interfaze, java.lang.Object target)