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) |
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() |
<T> T |
get(String name)
Get a field value.
|
Field |
getDeclaredField(Class<?> clazz,
String name) |
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() |
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
on(Class.forName(name))name - A fully qualified class nameReflectException - If any reflection exception occurred.on(Class)public static Reflect on(Class<?> clazz)
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)
Objectobject - The object to be wrappedpublic static <T extends AccessibleObject> T accessible(T accessible)
AccessibleObject accessibleT - Classaccessible - The object to render accessiblepublic <T> T get()
T - Classpublic 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.
T - Classname - The field nameReflectException - If any reflection exception occurred.field(String)public Field getDeclaredField(Class<?> clazz, String name) throws NoSuchFieldException
public 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
on(...).call("method", 1, 1);
The first of the following methods will be called:
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
on(C.class).create(1, 1);
The first of the following constructors will be applied:
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)
public Class<?> type()
Object.getClass()Copyright © 2017 Liloo. All rights reserved.