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<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.
|
static Reflect |
compile(String name,
String content)
Compile a class at runtime and reflect on it.
|
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.
|
int |
hashCode() |
static <T> T |
initValue(Class<T> type)
Get the initialisation or default value for any given type.
|
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.
|
static Reflect |
on(String name,
ClassLoader classLoader)
Wrap a class name, loading it via a given class loader.
|
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 compile(String name, String content) throws ReflectException
For example:
Supplier<String> supplier = Reflect.compile(
"org.joor.Test",
"package org.joor;\n" +
"class Test implements java.util.function.Supplier<String> {\n" +
" public String get() {\n" +
" return \"Hello World!\";\n" +
" }\n" +
"}\n").create().get();
name
- The qualified class namecontent
- The source code for the classClass
ReflectException
- if anything went wrong compiling the class.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(String name, ClassLoader classLoader) throws ReflectException
This is the same as calling
on(Class.forName(name, classLoader))
name
- A fully qualified class name.classLoader
- The class loader in whose context the class should be
loaded.ReflectException
- 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> T initValue(Class<T> type)
This returns:
null
for reference types (including wrapper types)0
for numeric primitive types (including
char
)false
for the boolean
primitive type.
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 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.
This method is also capable of setting the value of (static) final
fields. This may be convenient in situations where no
SecurityManager
is expected to prevent this, but do note that
(especially static) final fields may already have been inlined by the
javac and/or JIT and relevant code deleted from the runtime verison of
your program, so setting these fields might not have any effect on your
execution.
For restrictions of usage regarding setting values on final fields check: http://stackoverflow.com/questions/3301635/change-private-static-final-field-using-java-reflection ... and http://pveentjer.blogspot.co.at/2017/01/final-static-boolean-jit.html
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 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 © 2019. All rights reserved.