public class Reflect extends Object
Object or Class upon which reflective calls can be made.
An example of using Reflect is
// Static import all reflection methods to decrease verbosity
import static org.joor.Reflect.*;
// Wrap an Object / Class / class name with the on() method:
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
| 限定符和类型 | 方法和说明 |
|---|---|
static <T extends AccessibleObject> |
accessible(T accessible)
Conveniently render an
AccessibleObject accessible |
<P> P |
as(Class<P> proxyType)
Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a
custom interface
|
Reflect |
call(String name)
Call a method by its name.
|
Reflect |
call(String name,
Object... args)
Call a method by its name.
|
Reflect |
create()
Call a constructor.
|
Reflect |
create(Object... args)
Call a constructor.
|
boolean |
equals(Object obj) |
Reflect |
field(String name)
Get a wrapped field.
|
Map<String,Reflect> |
fields()
Get a Map containing field names and wrapped values for the fields' values.
|
<T> T |
get()
Get the wrapped object
|
<T> T |
get(String name)
Get a field value.
|
Field |
getDeclaredField(Class<?> clazz,
String name)
TODO:(简单描述方法作用).
|
int |
hashCode() |
static Reflect |
on(Class<?> clazz)
Wrap a class.
|
static Reflect |
on(Object object)
Wrap an object.
|
static Reflect |
on(String name)
Wrap a class name.
|
Reflect |
set(String name,
Object value)
Set a field value.
|
String |
toString() |
Class<?> |
type()
Get the type of the wrapped object.
|
static Class<?> |
wrapper(Class<?> type)
Get a wrapper type for a primitive type, or the argument type itself, if it is not a
primitive type.
|
public static Reflect on(String name) throws ReflectException
This is the same as calling on(Class.forName(name))
name - A fully qualified class nameReflectException - If any reflection exception occurred.on(Class)public static Reflect on(Class<?> clazz)
Use this when you want to access static fields and methods on a Class object, or as a
basis for constructing objects of that class using create(Object...)
clazz - The class to be wrappedpublic static Reflect on(Object object)
Use this when you want to access instance fields and methods on any Object
object - The object to be wrappedpublic static <T extends AccessibleObject> T accessible(T accessible)
AccessibleObject accessibleaccessible - The object to render accessiblepublic <T> T get()
T - A convenience generic parameter for automatic unsafe castingpublic Reflect set(String name, Object value) throws ReflectException
This is roughly equivalent to Field.set(Object, Object). If the wrapped object is a
Class, then this will set a value to a static member field. If the wrapped object is
any other Object, then this will set a value to an instance member field.
name - The field namevalue - The new field valueReflectException - If any reflection exception occurred.public <T> T get(String name) throws ReflectException
This is roughly equivalent to Field.get(Object). If the wrapped object is a
Class, then this will get a value from a static member field. If the wrapped object
is any other Object, 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 nameReflectException - If any reflection exception occurred.field(String)public Field getDeclaredField(Class<?> clazz, String name) throws NoSuchFieldException
clazz - name - NoSuchFieldExceptionpublic Reflect field(String name) throws ReflectException
This is roughly equivalent to Field.get(Object). If the wrapped object is a
Class, then this will wrap a static member field. If the wrapped object is any other
Object, then this wrap an instance member field.
name - The field nameReflectException - If any reflection exception occurred.public Map<String,Reflect> fields()
If the wrapped object is a Class, then this will return static fields. If the wrapped
object is any other Object, then this will return instance fields.
These two calls are equivalent
on(object).field("myField");
on(object).fields().get("myField");
public Reflect call(String name) throws ReflectException
This is a convenience method for calling call(name, new Object[0])
name - The method namevoid, to be used for further reflection.ReflectException - If any reflection exception occurred.call(String, Object...)public Reflect call(String name, Object... args) throws ReflectException
This is roughly equivalent to Method.invoke(Object, Object...). If the wrapped object
is a Class, then this will invoke a static method. If the wrapped object is any other
Object, 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 calling The 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);
The best matching method is searched for with the following strategy:
name - The method nameargs - The method argumentsvoid, to be used for further reflection.ReflectException - If any reflection exception occurred.public Reflect create() throws ReflectException
This is a convenience method for calling create(new Object[0])
ReflectException - If any reflection exception occurred.create(Object...)public Reflect create(Object... args) throws ReflectException
This is roughly equivalent to Constructor.newInstance(Object...). If the wrapped
object is a Class, then this will create a new object of that class. If the wrapped
object is any other Object, 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 calling The 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 argumentsReflectException - If any reflection exception occurred.public <P> P as(Class<P> proxyType)
proxyType - The interface type that is implemented by the proxypublic Class<?> type()
Object.getClass()Copyright © 2015. All rights reserved.