类 Reflect
Object or Class upon which reflective calls
can be made.
An example of using Reflect is
// Wrap an Object / Class / class name with the on() method:
Reflect.on("java.lang.String")
// Invoke constructors using the create() method:
.create("Hello World")
// Invoke methods using the call() method:
.call("toString")
// Retrieve the wrapped object
- 作者:
- Lukas Eder
-
方法概要
修饰符和类型方法说明static <T extends AccessibleObject>
Taccessible(T accessible) Conveniently render anAccessibleObjectaccessible<P> PCreate a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interfaceCall a method by its name.Call a method by its name.create()Call a constructor.Call a constructor.booleanGet a wrapped field.fields()Get a Map containing field names and wrapped values for the fields' values.<T> Tget()Get the wrapped object<T> TGet a field value.static String获取调用本类中方法的其他类名inthashCode()static ReflectWrap a class.static ReflectWrap an object.static ReflectWrap a class name.Set a field value.toString()Class<?>type()Get the type of the wrapped object.
-
方法详细资料
-
on
Wrap a class name.This is the same as calling
on(Class.forName(name))- 参数:
name- A fully qualified class name- 返回:
- A wrapped class object, to be used for further reflection.
- 抛出:
ReflectException- If any reflection exception occurred.- 另请参阅:
-
on
Wrap a class.Use this when you want to access static fields and methods on a
Classobject, or as a basis for constructing objects of that class usingcreate(Object...)- 参数:
clazz- The class to be wrapped- 返回:
- A wrapped class object, to be used for further reflection.
-
on
Wrap an object.Use this when you want to access instance fields and methods on any
Object- 参数:
object- The object to be wrapped- 返回:
- A wrapped object, to be used for further reflection.
-
accessible
Conveniently render anAccessibleObjectaccessible- 参数:
accessible- The object to render accessible- 返回:
- The argument object rendered accessible
-
get
public <T> T get()Get the wrapped object- 类型参数:
T- A convenience generic parameter for automatic unsafe casting
-
set
Set a field value.This is roughly equivalent to
Field.set(Object, Object). If the wrapped object is aClass, then this will set a value to a static member field. If the wrapped object is any otherObject, then this will set a value to an instance member field.- 参数:
name- The field namevalue- The new field value- 返回:
- The same wrapped object, to be used for further reflection.
- 抛出:
ReflectException- If any reflection exception occurred.
-
get
Get a field value.This is roughly equivalent to
Field.get(Object). If the wrapped object is aClass, then this will get a value from a static member field. If the wrapped object is any otherObject, then this will get a value from an instance member field.If you want to "navigate" to a wrapped version of the field, use
field(String)instead.- 参数:
name- The field name- 返回:
- The field value
- 抛出:
ReflectException- If any reflection exception occurred.- 另请参阅:
-
field
Get a wrapped field.This is roughly equivalent to
Field.get(Object). If the wrapped object is aClass, then this will wrap a static member field. If the wrapped object is any otherObject, then this wrap an instance member field.- 参数:
name- The field name- 返回:
- The wrapped field
- 抛出:
ReflectException- If any reflection exception occurred.
-
fields
Get a Map containing field names and wrapped values for the fields' values.If the wrapped object is a
Class, then this will return static fields. If the wrapped object is any otherObject, then this will return instance fields.These two calls are equivalent
on(object).field("myField"); on(object).fields().get("myField");- 返回:
- A map containing field names and wrapped values.
-
call
Call a method by its name.This is a convenience method for calling
call(name, new Object[0])- 参数:
name- The method name- 返回:
- The wrapped method result or the same wrapped object if the
method returns
void, to be used for further reflection. - 抛出:
ReflectException- If any reflection exception occurred.- 另请参阅:
-
call
Call a method by its name.This is roughly equivalent to
Method.invoke(Object, Object...). If the wrapped object is aClass, then this will invoke a static method. If the wrapped object is any otherObject, then this will invoke an instance method.Just like
Method.invoke(Object, Object...), this will try to wrap primitive types or unwrap primitive type wrappers if applicable. If several methods are applicable, by that rule, the first one encountered is called. i.e. when callingThe first of the following methods will be called:on(...).call("method", 1, 1);public void method(int param1, Integer param2); public void method(Integer param1, int param2); public void method(Number param1, Number param2); public void method(Number param1, Object param2); public void method(int param1, Object param2);
- 参数:
name- The method nameargs- The method arguments- 返回:
- The wrapped method result or the same wrapped object if the
method returns
void, to be used for further reflection. - 抛出:
ReflectException- If any reflection exception occurred.
-
create
Call a constructor.This is a convenience method for calling
create(new Object[0])- 返回:
- The wrapped new object, to be used for further reflection.
- 抛出:
ReflectException- If any reflection exception occurred.- 另请参阅:
-
create
Call a constructor.This is roughly equivalent to
Constructor.newInstance(Object...). If the wrapped object is aClass, then this will create a new object of that class. If the wrapped object is any otherObject, then this will create a new object of the same type.Just like
Constructor.newInstance(Object...), this will try to wrap primitive types or unwrap primitive type wrappers if applicable. If several constructors are applicable, by that rule, the first one encountered is called. i.e. when callingThe first of the following constructors will be applied:on(C.class).create(1, 1);
public C(int param1, Integer param2); public C(Integer param1, int param2); public C(Number param1, Number param2); public C(Number param1, Object param2); public C(int param1, Object param2);
- 参数:
args- The constructor arguments- 返回:
- The wrapped new object, to be used for further reflection.
- 抛出:
ReflectException- If any reflection exception occurred.
-
as
Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface- 参数:
proxyType- The interface type that is implemented by the proxy- 返回:
- A proxy for the wrapped object
-
hashCode
public int hashCode() -
equals
-
toString
-
type
Get the type of the wrapped object.- 另请参阅:
-
getInvokeClassName
获取调用本类中方法的其他类名- 返回:
-