public class Reflect
extends java.lang.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 java.lang.reflect.AccessibleObject> |
accessible(T accessible)
Conveniently render an
AccessibleObject accessible. |
<P> P |
as(java.lang.Class<P> proxyType)
Create a proxy for the wrapped object allowing to typesafely invoke
methods on it using a custom interface
|
Reflect |
call(java.lang.String name)
Call a method by its name.
|
Reflect |
call(java.lang.String name,
java.lang.Object... args)
Call a method by its name.
|
Reflect |
create()
Call a constructor.
|
Reflect |
create(java.lang.Object... args)
Call a constructor.
|
boolean |
equals(java.lang.Object obj) |
Reflect |
field(java.lang.String name)
Get a wrapped field.
|
java.util.Map<java.lang.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(java.lang.String name)
Get a field value.
|
int |
hashCode() |
static Reflect |
on(java.lang.Class<?> clazz)
Wrap a class.
|
static Reflect |
on(java.lang.Object object)
Wrap an object.
|
static Reflect |
on(java.lang.String name)
Wrap a class name.
|
Reflect |
set(java.lang.String name,
java.lang.Object value)
Set a field value.
|
java.lang.String |
toString() |
java.lang.Class<?> |
type()
Get the type of the wrapped object.
|
static java.lang.Class<?> |
wrapper(java.lang.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(java.lang.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(java.lang.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(java.lang.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 java.lang.reflect.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 accessiblepublic <T> T get()
T
- A convenience generic parameter for automatic unsafe castingpublic Reflect set(java.lang.String name, java.lang.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(java.lang.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 Reflect field(java.lang.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 java.util.Map<java.lang.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(java.lang.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(java.lang.String name, java.lang.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);
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(java.lang.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(java.lang.Class<P> proxyType)
proxyType
- The interface type that is implemented by the proxypublic int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.Class<?> type()
Object.getClass()
public static java.lang.Class<?> wrapper(java.lang.Class<?> type)
Copyright © 2014. All Rights Reserved.