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
| Modifier and Type | Method and Description |
|---|---|
static <T extends AccessibleObject |
accessible(T accessible)
Conveniently render an
AccessibleObject accessible.
|
<P> P |
as(Class
Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface
|
Reflect |
call(String
Call a method by its name.
|
Reflect |
call(String
Call a method by its name.
|
Reflect |
create()
Call a constructor.
|
Reflect |
create(Object
Call a constructor.
|
boolean |
equals(Object
|
Reflect |
field(String
Get a wrapped field.
|
Map |
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
Get a field value.
|
int |
hashCode()
|
static Reflect |
on(Class
Wrap a class.
|
static Reflect |
on(Object
Wrap an object.
|
static Reflect |
on(String
Wrap a class name.
|
Reflect |
set(String
Set a field value.
|
String |
toString()
|
Class |
type()
Get the type of the wrapped object.
|
static Class |
wrapper(Class
Get a wrapper type for a primitive type, or the argument type itself, if it is not a primitive type.
|
public static Reflecton(String name) throws ReflectException
This is the same as calling on(Class.forName(name))
name - A fully qualified class name
ReflectException - If any reflection exception occurred.
on(Class)
public static Reflecton(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 wrapped
public static Reflecton(Object object)
Use this when you want to access instance fields and methods on any Object
object - The object to be wrapped
public static <T extends AccessibleObject> T accessible(T accessible)
AccessibleObject accessible.
To prevent SecurityException, this is only done if the argument object and its declaring class are non-public.
accessible - The object to render accessible
public <T> T get()
T - A convenience generic parameter for automatic unsafe casting
public Reflectset(String name, Object value) throws ReflectException
This is roughly equivalent to Field. 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 name
value - The new field value
ReflectException - If any reflection exception occurred.
public <T> T get(Stringname) throws ReflectException
This is roughly equivalent to Field. 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 name
ReflectException - If any reflection exception occurred.
field(String)
public Reflectfield(String name) throws ReflectException
This is roughly equivalent to Field. 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 name
ReflectException - 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 Reflectcall(String name) throws ReflectException
This is a convenience method for calling call(name, new Object[0])
name - The method name
void, to be used for further reflection.
ReflectException - If any reflection exception occurred.
call(String, Object...)
public Reflectcall(String name, Object ... args) throws ReflectException
This is roughly equivalent to Method. 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, 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 name
args - The method arguments
void, to be used for further reflection.
ReflectException - If any reflection exception occurred.
public Reflectcreate() throws ReflectException
This is a convenience method for calling create(new Object[0])
ReflectException - If any reflection exception occurred.
create(Object...)
public Reflectcreate(Object ... args) throws ReflectException
This is roughly equivalent to Constructor. 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, 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 arguments
ReflectException - If any reflection exception occurred.
public <P> P as(Class<P> proxyType)
proxyType - The interface type that is implemented by the proxy
public int hashCode()
public boolean equals(Objectobj)
public StringtoString()
public Class<?> type()
Object.getClass()