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
- Author:
- Lukas Eder, Irek Matysiewicz, Thomas Darimont
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends AccessibleObject>
Taccessible
(T accessible) Conveniently render anAccessibleObject
accessible.<P> P
Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface.<P> P
Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface.Call a method by its name.Call a method by its name.static Reflect
Compile a class at runtime and reflect on it.static Reflect
compile
(String name, String content, CompileOptions options) Compile a class at runtime and reflect on it.create()
Call a constructor.Call a constructor.boolean
Get a wrapped field.fields()
Get a Map containing field names and wrapped values for the fields' values.<T> T
get()
Get the wrapped object<T> T
Get a field value.int
hashCode()
static <T> T
Get the initialisation or default value for any given type.static Reflect
Deprecated.static Reflect
Wrap an object.static Reflect
Deprecated.[#78] 0.9.11, useonClass(String)
instead.static Reflect
on
(String name, ClassLoader classLoader) Deprecated.[#78] 0.9.11, useonClass(String, ClassLoader)
instead.static Reflect
Wrap a class.static Reflect
Wrap a class name.static Reflect
onClass
(String name, ClassLoader classLoader) Wrap a class name, loading it via a given class loader.Set a field value.toString()
Class<?>
type()
Get the type of the wrapped object.static <T> Class<T>
Get a wrapper type for a primitive type, or the argument type itself, if it is not a primitive type.
-
Method Details
-
compile
Compile a class at runtime and reflect on it.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();
- Parameters:
name
- The qualified class namecontent
- The source code for the class- Returns:
- A wrapped
Class
- Throws:
ReflectException
- if anything went wrong compiling the class.
-
compile
public static Reflect compile(String name, String content, CompileOptions options) throws ReflectException Compile a class at runtime and reflect on it.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();
- Parameters:
name
- The qualified class namecontent
- The source code for the classoptions
- compiler options- Returns:
- A wrapped
Class
- Throws:
ReflectException
- if anything went wrong compiling the class.
-
on
Deprecated.[#78] 0.9.11, useonClass(String)
instead.Wrap a class name.This is the same as calling
on(Class.forName(name))
- Parameters:
name
- A fully qualified class name- Returns:
- A wrapped class object, to be used for further reflection.
- Throws:
ReflectException
- If any reflection exception occurred.- See Also:
-
on
Deprecated.[#78] 0.9.11, useonClass(String, ClassLoader)
instead.Wrap a class name, loading it via a given class loader.This is the same as calling
on(Class.forName(name, classLoader))
- Parameters:
name
- A fully qualified class name.classLoader
- The class loader in whose context the class should be loaded.- Returns:
- A wrapped class object, to be used for further reflection.
- Throws:
ReflectException
- If any reflection exception occurred.- See Also:
-
on
Deprecated.[#78] 0.9.11, useonClass(Class)
instead.Wrap a class.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 usingcreate(Object...)
- Parameters:
clazz
- The class to be wrapped- Returns:
- A wrapped class object, to be used for further reflection.
-
onClass
Wrap a class name.This is the same as calling
onClass(Class.forName(name))
- Parameters:
name
- A fully qualified class name- Returns:
- A wrapped class object, to be used for further reflection.
- Throws:
ReflectException
- If any reflection exception occurred.- See Also:
-
onClass
Wrap a class name, loading it via a given class loader.This is the same as calling
onClass(Class.forName(name, classLoader))
- Parameters:
name
- A fully qualified class name.classLoader
- The class loader in whose context the class should be loaded.- Returns:
- A wrapped class object, to be used for further reflection.
- Throws:
ReflectException
- If any reflection exception occurred.- See Also:
-
onClass
Wrap a class.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 usingcreate(Object...)
- Parameters:
clazz
- The class to be wrapped- Returns:
- A wrapped class object, to be used for further reflection.
-
on
Wrap an object.Use this when you want to access instance fields and methods on any
Object
- Parameters:
object
- The object to be wrapped- Returns:
- A wrapped object, to be used for further reflection.
-
initValue
Get the initialisation or default value for any given type.This returns:
null
for reference types (including wrapper types)0
for numeric primitive types (includingchar
)false
for theboolean
primitive type.
-
accessible
Conveniently render anAccessibleObject
accessible.To prevent
SecurityException
, this is only done if the argument object and its declaring class are non-public.- Parameters:
accessible
- The object to render accessible- Returns:
- The argument object rendered accessible
-
get
public <T> T get()Get the wrapped object- Type Parameters:
T
- A convenience generic parameter for automatic unsafe casting
-
set
Set a field value.This is roughly equivalent to
Field.set(Object, Object)
. If the wrapped object is aClass
, then this will set a value to a static member field. If the wrapped object is any otherObject
, 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
- Parameters:
name
- The field namevalue
- The new field value- Returns:
- The same wrapped object, to be used for further reflection.
- Throws:
ReflectException
- If any reflection exception occurred.
-
get
Get a field value.This is roughly equivalent to
Field.get(Object)
. If the wrapped object is aClass
, then this will get a value from a static member field. If the wrapped object is any otherObject
, 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.- Parameters:
name
- The field name- Returns:
- The field value
- Throws:
ReflectException
- If any reflection exception occurred.- See Also:
-
field
Get a wrapped field.This is roughly equivalent to
Field.get(Object)
. If the wrapped object is aClass
, then this will wrap a static member field. If the wrapped object is any otherObject
, then this wrap an instance member field.- Parameters:
name
- The field name- Returns:
- The wrapped field
- Throws:
ReflectException
- If any reflection exception occurred.
-
fields
Get a Map containing field names and wrapped values for the fields' values.If the wrapped object is a
Class
, then this will return static fields. If the wrapped object is any otherObject
, then this will return instance fields.These two calls are equivalent
on(object).field("myField"); on(object).fields().get("myField");
- Returns:
- A map containing field names and wrapped values.
-
call
Call a method by its name.This is a convenience method for calling
call(name, new Object[0])
- Parameters:
name
- The method name- Returns:
- The wrapped method result or the same wrapped object if the
method returns
void
, to be used for further reflection. - Throws:
ReflectException
- If any reflection exception occurred.- See Also:
-
call
Call a method by its name.This is roughly equivalent to
Method.invoke(Object, Object...)
. If the wrapped object is aClass
, then this will invoke a static method. If the wrapped object is any otherObject
, 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:
- public method with exact signature match in class hierarchy
- non-public method with exact signature match on declaring class
- public method with similar signature in class hierarchy
- non-public method with similar signature on declaring class
- Parameters:
name
- The method nameargs
- The method arguments- Returns:
- The wrapped method result or the same wrapped object if the
method returns
void
, to be used for further reflection. - Throws:
ReflectException
- If any reflection exception occurred.
-
create
Call a constructor.This is a convenience method for calling
create(new Object[0])
- Returns:
- The wrapped new object, to be used for further reflection.
- Throws:
ReflectException
- If any reflection exception occurred.- See Also:
-
create
Call a constructor.This is roughly equivalent to
Constructor.newInstance(Object...)
. If the wrapped object is aClass
, then this will create a new object of that class. If the wrapped object is any otherObject
, 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);
- Parameters:
args
- The constructor arguments- Returns:
- The wrapped new object, to be used for further reflection.
- Throws:
ReflectException
- If any reflection exception occurred.
-
as
Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface.- Parameters:
proxyType
- The interface type that is implemented by the proxy- Returns:
- A proxy for the wrapped object
-
as
Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface.- Parameters:
proxyType
- The interface type that is implemented by the proxyadditionalInterfaces
- Additional interfaces that are implemented by the proxy- Returns:
- A proxy for the wrapped object
-
hashCode
public int hashCode() -
equals
-
toString
-
type
Get the type of the wrapped object.- See Also:
-
wrapper
Get a wrapper type for a primitive type, or the argument type itself, if it is not a primitive type.
-
onClass(Class)
instead.