Top Description Inners Fields Constructors Methods
org.jooq.tools.reflect

public Class Reflect

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandles, .MethodHandles.Lookup, java.lang.reflect.AccessibleObject, .Constructor, .Field, .InvocationHandler, .Member, .Method, .Modifier, .Proxy, java.util.Arrays, .LinkedHashMap, .Map, .Optional

A wrapper for an 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
Authors
Lukas Eder, Irek Matysiewicz, Thomas Darimont

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
pack-priv static final Constructor<MethodHandles.Lookup>
private final Object
object

The wrapped object.

private final Class<?>
type

The type of the wrapped object.

Constructor Summary

AccessConstructor and Description
private
Reflect(Class<?> type)

private
Reflect(Class<?> type, Object object)

Method Summary

Modifier and TypeMethod and Description
public static <T extends AccessibleObject> T

Returns:

The argument object rendered accessible
accessible
(T
The object to render accessible
accessible
)

Conveniently render an AccessibleObject accessible.

public <P> P

Returns:

A proxy for the wrapped object
as
(Class<P>
The interface type that is implemented by the proxy
proxyType
)

Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface.

public <P> P

Returns:

A proxy for the wrapped object
as
(final Class<P>
The interface type that is implemented by the proxy
proxyType
,
final Class<?>...
Additional interfaces that are implemented by the proxy
additionalInterfaces
)

Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface.

public Reflect

Returns:

The wrapped method result or the same wrapped object if the method returns void, to be used for further reflection.
call
(String
The method name
name
)

Call a method by its name.

public Reflect

Returns:

The wrapped method result or the same wrapped object if the method returns void, to be used for further reflection.
call
(String
The method name
name
,
Object...
The method arguments
args
)

Call a method by its name.

public static Reflect

Returns:

A wrapped Class
compile
(String
The qualified class name
name
,
String
The source code for the class
content
)

Compile a class at runtime and reflect on it.

public static Reflect

Returns:

A wrapped Class
compile
(String
The qualified class name
name
,
String
The source code for the class
content
,
CompileOptions
compiler options
options
)

Compile a class at runtime and reflect on it.

public Reflect

Returns:

The wrapped new object, to be used for further reflection.
create
()

Call a constructor.

public Reflect

Returns:

The wrapped new object, to be used for further reflection.
create
(Object...
The constructor arguments
args
)

Call a constructor.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
private Method
exactMethod(String name, Class<?>[] types)

Searches a method with the exact same signature as desired.

public Reflect

Returns:

The wrapped field
field
(String
The field name
name
)

Get a wrapped field.

private Field
field0(String name)

public Map<String, Reflect>

Returns:

A map containing field names and wrapped values.
fields
()

Get a Map containing field names and wrapped values for the fields' values.

private static Class<?>
forName(String name)

Load a class

private static Class<?>
forName(String name, ClassLoader classLoader)

public <
A convenience generic parameter for automatic unsafe casting
T
>
T
get()

Get the wrapped object

public <T> T

Returns:

The field value
get
(String
The field name
name
)

Get a field value.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public static <T> T
initValue(Class<T> type)

Get the initialisation or default value for any given type.

private boolean
isSimilarSignature(Method possiblyMatchingMethod, String desiredMethodName, Class<?>[] desiredParamTypes)

Determines if a method has a "similar" signature, especially if wrapping primitive argument types would result in an exactly matching signature.

private boolean
match(Class<?>[] declaredTypes, Class<?>[] actualTypes)

Check whether two arrays of types match, converting primitive types to their corresponding wrappers.

public static Reflect

Returns:

A wrapped class object, to be used for further reflection.
on
(String
A fully qualified class name
name
)

Deprecated [#78] 0.9.11, use onClass(String) instead.
Wrap a class name.
public static Reflect

Returns:

A wrapped class object, to be used for further reflection.
on
(String
A fully qualified class name.
name
,
ClassLoader
The class loader in whose context the class should be loaded.
classLoader
)

Deprecated [#78] 0.9.11, use onClass(String, ClassLoader) instead.
Wrap a class name, loading it via a given class loader.
public static Reflect

Returns:

A wrapped class object, to be used for further reflection.
on
(Class<?>
The class to be wrapped
clazz
)

Deprecated [#78] 0.9.11, use onClass(Class) instead.
Wrap a class.
public static Reflect

Returns:

A wrapped object, to be used for further reflection.
on
(Object
The object to be wrapped
object
)

Wrap an object.

private static Reflect
on(Class<?> type, Object object)

private static Reflect
on(Constructor<?> constructor, Object... args)

Wrap an object created from a constructor

private static Reflect
on(Method method, Object object, Object... args)

Wrap an object returned from a method

public static Reflect

Returns:

A wrapped class object, to be used for further reflection.
onClass
(String
A fully qualified class name
name
)

Wrap a class name.

public static Reflect

Returns:

A wrapped class object, to be used for further reflection.
onClass
(String
A fully qualified class name.
name
,
ClassLoader
The class loader in whose context the class should be loaded.
classLoader
)

Wrap a class name, loading it via a given class loader.

public static Reflect

Returns:

A wrapped class object, to be used for further reflection.
onClass
(Class<?>
The class to be wrapped
clazz
)

Wrap a class.

private static String
property(String string)

Get the POJO property name of an getter/setter

public Reflect

Returns:

The same wrapped object, to be used for further reflection.
set
(String
The field name
name
,
Object
The new field value
value
)

Set a field value.

private Method
similarMethod(String name, Class<?>[] types)

Searches a method with a similar signature as desired using isSimilarSignature(java.lang.reflect.Method, String, Class[]).

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Class<?>
type()

Get the type of the wrapped object.

private static Class<?>[]
types(Object... values)

Get an array of types for an array of objects

private static Object
unwrap(Object object)

Unwrap an object

public static <T> Class<T>
wrapper(Class<T> type)

Get a wrapper type for a primitive type, or the argument type itself, if it is not a primitive type.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

CACHED_LOOKUP_CONSTRUCTORback to summary
pack-priv static final Constructor<MethodHandles.Lookup> CACHED_LOOKUP_CONSTRUCTOR
objectback to summary
private final Object object

The wrapped object.

typeback to summary
private final Class<?> type

The type of the wrapped object.

Constructor Detail

Reflectback to summary
private Reflect(Class<?> type)
Reflectback to summary
private Reflect(Class<?> type, Object object)

Method Detail

accessibleback to summary
public static <T extends AccessibleObject> T accessible(T accessible)

Conveniently render an AccessibleObject accessible.

To prevent SecurityException, this is only done if the argument object and its declaring class are non-public.

Parameters
accessible:T

The object to render accessible

Returns:T

The argument object rendered accessible

asback to summary
public <P> P as(Class<P> proxyType)

Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface.

Parameters
proxyType:Class<P>

The interface type that is implemented by the proxy

Returns:P

A proxy for the wrapped object

asback to summary
public <P> P as(final Class<P> proxyType, final Class<?>... additionalInterfaces)

Create a proxy for the wrapped object allowing to typesafely invoke methods on it using a custom interface.

Parameters
proxyType:Class<P>

The interface type that is implemented by the proxy

additionalInterfaces:Class<?>[]

Additional interfaces that are implemented by the proxy

Returns:P

A proxy for the wrapped object

Annotations
@SuppressWarnings:unchecked
callback to summary
public Reflect call(String name) throws ReflectException

Call a method by its name.

This is a convenience method for calling call(name, new Object[0])

Parameters
name:String

The method name

Returns:Reflect

The wrapped method result or the same wrapped object if the method returns void, to be used for further reflection.

Exceptions
ReflectException:
If any reflection exception occurred.
See Also
call(String, Object...)
callback to summary
public Reflect call(String name, Object... args) throws ReflectException

Call a method by its name.

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:

  1. public method with exact signature match in class hierarchy
  2. non-public method with exact signature match on declaring class
  3. public method with similar signature in class hierarchy
  4. non-public method with similar signature on declaring class
Parameters
name:String

The method name

args:Object[]

The method arguments

Returns:Reflect

The wrapped method result or the same wrapped object if the method returns void, to be used for further reflection.

Exceptions
ReflectException:
If any reflection exception occurred.
compileback to summary
public static Reflect compile(String name, String content) 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:String

The qualified class name

content:String

The source code for the class

Returns:Reflect

A wrapped Class

Exceptions
ReflectException:
if anything went wrong compiling the class.
compileback to summary
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:String

The qualified class name

content:String

The source code for the class

options:CompileOptions

compiler options

Returns:Reflect

A wrapped Class

Exceptions
ReflectException:
if anything went wrong compiling the class.
createback to summary
public Reflect create() throws ReflectException

Call a constructor.

This is a convenience method for calling create(new Object[0])

Returns:Reflect

The wrapped new object, to be used for further reflection.

Exceptions
ReflectException:
If any reflection exception occurred.
See Also
create(Object...)
createback to summary
public Reflect create(Object... args) throws ReflectException

Call a constructor.

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);
Parameters
args:Object[]

The constructor arguments

Returns:Reflect

The wrapped new object, to be used for further reflection.

Exceptions
ReflectException:
If any reflection exception occurred.
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
exactMethodback to summary
private Method exactMethod(String name, Class<?>[] types) throws NoSuchMethodException

Searches a method with the exact same signature as desired.

If a public method is found in the class hierarchy, this method is returned. Otherwise a private method with the exact same signature is returned. If no exact match could be found, we let the NoSuchMethodException pass through.

fieldback to summary
public Reflect field(String name) throws ReflectException

Get a wrapped field.

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.

Parameters
name:String

The field name

Returns:Reflect

The wrapped field

Exceptions
ReflectException:
If any reflection exception occurred.
field0back to summary
private Field field0(String name) throws ReflectException
fieldsback to summary
public Map<String, Reflect> 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 other Object, then this will return instance fields.

These two calls are equivalent


on(object).field("myField");
on(object).fields().get("myField");
Returns:Map<String, Reflect>

A map containing field names and wrapped values.

forNameback to summary
private static Class<?> forName(String name) throws ReflectException

Load a class

See Also
Class#forName(String)
forNameback to summary
private static Class<?> forName(String name, ClassLoader classLoader) throws ReflectException
getback to summary
public <T> T get()

Get the wrapped object

Parameters
<T>
A convenience generic parameter for automatic unsafe casting
Annotations
@SuppressWarnings:unchecked
getback to summary
public <T> T get(String name) throws ReflectException

Get a field value.

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.

Parameters
name:String

The field name

Returns:T

The field value

Exceptions
ReflectException:
If any reflection exception occurred.
See Also
field(String)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
initValueback to summary
public static <T> T initValue(Class<T> type)

Get the initialisation or default value for any given type.

This returns:

  • null for reference types (including wrapper types)
  • 0 for numeric primitive types (including char)
  • false for the boolean primitive type.
Annotations
@SuppressWarnings:unchecked
isSimilarSignatureback to summary
private boolean isSimilarSignature(Method possiblyMatchingMethod, String desiredMethodName, Class<?>[] desiredParamTypes)

Determines if a method has a "similar" signature, especially if wrapping primitive argument types would result in an exactly matching signature.

matchback to summary
private boolean match(Class<?>[] declaredTypes, Class<?>[] actualTypes)

Check whether two arrays of types match, converting primitive types to their corresponding wrappers.

onback to summary
public static Reflect on(String name) throws ReflectException

Deprecated

[#78] 0.9.11, use onClass(String) instead.

Wrap a class name.

This is the same as calling on(Class.forName(name))

Parameters
name:String

A fully qualified class name

Returns:Reflect

A wrapped class object, to be used for further reflection.

Annotations
@Deprecated
Exceptions
ReflectException:
If any reflection exception occurred.
See Also
onClass(Class)
onback to summary
public static Reflect on(String name, ClassLoader classLoader) throws ReflectException

Deprecated

[#78] 0.9.11, use onClass(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:String

A fully qualified class name.

classLoader:ClassLoader

The class loader in whose context the class should be loaded.

Returns:Reflect

A wrapped class object, to be used for further reflection.

Annotations
@Deprecated
Exceptions
ReflectException:
If any reflection exception occurred.
See Also
onClass(Class)
onback to summary
public static Reflect on(Class<?> clazz)

Deprecated

[#78] 0.9.11, use onClass(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 using create(Object...)

Parameters
clazz:Class<?>

The class to be wrapped

Returns:Reflect

A wrapped class object, to be used for further reflection.

Annotations
@Deprecated
onback to summary
public static Reflect on(Object object)

Wrap an object.

Use this when you want to access instance fields and methods on any Object

Parameters
object:Object

The object to be wrapped

Returns:Reflect

A wrapped object, to be used for further reflection.

onback to summary
private static Reflect on(Class<?> type, Object object)
onback to summary
private static Reflect on(Constructor<?> constructor, Object... args) throws ReflectException

Wrap an object created from a constructor

onback to summary
private static Reflect on(Method method, Object object, Object... args) throws ReflectException

Wrap an object returned from a method

onClassback to summary
public static Reflect onClass(String name) throws ReflectException

Wrap a class name.

This is the same as calling onClass(Class.forName(name))

Parameters
name:String

A fully qualified class name

Returns:Reflect

A wrapped class object, to be used for further reflection.

Exceptions
ReflectException:
If any reflection exception occurred.
See Also
onClass(Class)
onClassback to summary
public static Reflect onClass(String name, ClassLoader classLoader) throws ReflectException

Wrap a class name, loading it via a given class loader.

This is the same as calling onClass(Class.forName(name, classLoader))

Parameters
name:String

A fully qualified class name.

classLoader:ClassLoader

The class loader in whose context the class should be loaded.

Returns:Reflect

A wrapped class object, to be used for further reflection.

Exceptions
ReflectException:
If any reflection exception occurred.
See Also
onClass(Class)
onClassback to summary
public static Reflect onClass(Class<?> clazz)

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 using create(Object...)

Parameters
clazz:Class<?>

The class to be wrapped

Returns:Reflect

A wrapped class object, to be used for further reflection.

propertyback to summary
private static String property(String string)

Get the POJO property name of an getter/setter

setback to summary
public Reflect set(String name, Object value) throws ReflectException

Set a field value.

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

Parameters
name:String

The field name

value:Object

The new field value

Returns:Reflect

The same wrapped object, to be used for further reflection.

Exceptions
ReflectException:
If any reflection exception occurred.
similarMethodback to summary
private Method similarMethod(String name, Class<?>[] types) throws NoSuchMethodException

Searches a method with a similar signature as desired using isSimilarSignature(java.lang.reflect.Method, String, Class[]).

First public methods are searched in the class hierarchy, then private methods on the declaring class. If a method could be found, it is returned, otherwise a NoSuchMethodException is thrown.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
typeback to summary
public Class<?> type()

Get the type of the wrapped object.

See Also
Object#getClass()
typesback to summary
private static Class<?>[] types(Object... values)

Get an array of types for an array of objects

See Also
Object#getClass()
unwrapback to summary
private static Object unwrap(Object object)

Unwrap an object

wrapperback to summary
public static <T> Class<T> wrapper(Class<T> type)

Get a wrapper type for a primitive type, or the argument type itself, if it is not a primitive type.

Annotations
@SuppressWarnings:unchecked
org.jooq.tools.reflect back to summary

private Class Reflect.NULL

extends Object
Class Inheritance

Constructor Summary

AccessConstructor and Description
private
NULL()

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

NULLback to summary
private NULL()