public class Reflections extends Object
Annotated metadata.| Modifier and Type | Method and Description |
|---|---|
static <T> T |
cast(Object obj)
Perform a runtime cast.
|
static Method |
findDeclaredMethod(Class<?> clazz,
String name,
Class<?>... args)
Search the class hierarchy for a method with the given name and arguments.
|
static Set<Method> |
getAllDeclaredMethods(Class<?> clazz)
Get all the declared methods on the class hierarchy.
|
static Object |
getFieldValue(Field field,
Object instance) |
static <T> T |
getFieldValue(Field field,
Object instance,
Class<T> expectedType)
Get the value of the field, on the specified instance, casting the value of the field to the expected type.
|
static String |
getPropertyName(Method method)
Gets the property name from a getter method.
|
static <T> T |
invokeMethod(boolean setAccessible,
Method method,
Class<T> expectedReturnType,
Object instance,
Object... args)
Invoke the method on the instance, with any arguments specified, casting the result of invoking the method to the
expected return type.
|
static Object |
invokeMethod(boolean setAccessible,
Method method,
Object instance,
Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as
arguments to the specified method.
|
static <T> T |
invokeMethod(Method method,
Class<T> expectedReturnType,
Object instance,
Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as
arguments to the specified method.
|
static Object |
invokeMethod(Method method,
Object instance,
Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as
arguments to the specified method.
|
static boolean |
isSerializable(Class<?> clazz)
Check if a class is serializable.
|
static boolean |
methodExists(Class<?> clazz,
String name)
Determine if a method exists in a specified class hierarchy
|
static <A extends AccessibleObject> |
setAccessible(A member)
Set the accessibility flag on the
AccessibleObject as described in
AccessibleObject.setAccessible(boolean) within the context of a PrivilegedAction. |
static void |
setFieldValue(boolean setAccessible,
Field field,
Object instance,
Object value)
Sets the value of a field on the instance to the specified value.
|
static void |
setFieldValue(Field field,
Object instance,
Object value)
Set the value of a field on the instance to the specified value.
|
public static <T> T cast(Object obj)
Perform a runtime cast. Similar to Class.cast(Object), but useful when you do not have a Class
object for type you wish to cast to.
Class.cast(Object) should be used if possible
T - the type to cast toobj - the object to perform the cast onClassCastException - if the type T is not a subtype of the objectClass.cast(Object)public static boolean methodExists(Class<?> clazz, String name)
clazz - The class to searchname - The name of the methodpublic static Set<Method> getAllDeclaredMethods(Class<?> clazz)
clazz - The class to searchpublic static Method findDeclaredMethod(Class<?> clazz, String name, Class<?>... args)
clazz - The class to searchname - The name of the method to search forargs - The arguments of the method to search forpublic static <A extends AccessibleObject> A setAccessible(A member)
AccessibleObject as described in
AccessibleObject.setAccessible(boolean) within the context of a PrivilegedAction.A - member the accessible object typemember - the accessible objectpublic static Object invokeMethod(Method method, Object instance, Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.
This method provides the same functionality and throws the same exceptions as
invokeMethod(boolean, Method, Class, Object, Object...), with the expected return type set to
Object and no change to the method's accessibility.
public static Object invokeMethod(boolean setAccessible, Method method, Object instance, Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.
This method attempts to set the accessible flag of the method in a PrivilegedAction before invoking the
method if the first argument is true.
This method provides the same functionality and throws the same exceptions as
invokeMethod(boolean, Method, Class, Object, Object...), with the expected return type set to
Object.
public static <T> T invokeMethod(Method method, Class<T> expectedReturnType, Object instance, Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.
This method provides the same functionality and throws the same exceptions as
invokeMethod(boolean, Method, Class, Object, Object...), with the expected return type set to
Object and honoring the accessibility of the method.
public static <T> T invokeMethod(boolean setAccessible,
Method method,
Class<T> expectedReturnType,
Object instance,
Object... args)
Invoke the method on the instance, with any arguments specified, casting the result of invoking the method to the expected return type.
This method wraps Method.invoke(Object, Object...), converting the checked exceptions that
Method.invoke(Object, Object...) specifies to runtime exceptions.
If instructed, this method attempts to set the accessible flag of the method in a PrivilegedAction before
invoking the method.
setAccessible - flag indicating whether method should first be set as accessiblemethod - the method to invokeinstance - the instance to invoke the methodargs - the arguments to the methodRuntimeException - if this Method object enforces Java language access control and the underlying method is
inaccessible or if the underlying method throws an exception or if the initialization provoked by
this method fails.IllegalArgumentException - if the method is an instance method and the specified instance argument is not an
instance of the class or interface declaring the underlying method (or of a subclass or implementor
thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for
primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to
the corresponding formal parameter type by a method invocation conversion.NullPointerException - if the specified instance is null and the method is an instance method.ClassCastException - if the result of invoking the method cannot be cast to the expectedReturnTypeExceptionInInitializerError - if the initialization provoked by this method fails.Method.invoke(Object, Object...)public static void setFieldValue(Field field, Object instance, Object value)
Set the value of a field on the instance to the specified value.
This method provides the same functionality and throws the same exceptions as
Reflections#setFieldValue(boolean, Method, Class, Object, Object...), honoring the accessibility of the
field.
public static void setFieldValue(boolean setAccessible,
Field field,
Object instance,
Object value)
Sets the value of a field on the instance to the specified value.
This method wraps Field.set(Object, Object), converting the checked exceptions that
Field.set(Object, Object) specifies to runtime exceptions.
If instructed, this method attempts to set the accessible flag of the method in a PrivilegedAction before
invoking the method.
field - the field on which to operate, or null if the field is staticinstance - the instance on which the field value should be set uponvalue - the value to set the field toRuntimeException - if the underlying field is inaccessible.IllegalArgumentException - if the specified instance is not an instance of the class or interface declaring the
underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails.NullPointerException - if the specified instance is null and the field is an instance field.ExceptionInInitializerError - if the initialization provoked by this method fails.Field.set(Object, Object)public static <T> T getFieldValue(Field field, Object instance, Class<T> expectedType)
Get the value of the field, on the specified instance, casting the value of the field to the expected type.
This method wraps Field.get(Object), converting the checked exceptions that Field.get(Object)
specifies to runtime exceptions.
T - the type of the field's valuefield - the field to operate oninstance - the instance from which to retrieve the valueexpectedType - the expected type of the field's valueRuntimeException - if the underlying field is inaccessible.IllegalArgumentException - if the specified instance is not an instance of the class or interface declaring the
underlying field (or a subclass or implementor thereof).NullPointerException - if the specified instance is null and the field is an instance field.ExceptionInInitializerError - if the initialization provoked by this method fails.public static boolean isSerializable(Class<?> clazz)
clazz - The class to checkpublic static String getPropertyName(Method method)
method - The getter methodCopyright © 2011-2013 The Apache Software Foundation. All Rights Reserved.