org.jooq.tools.reflect
Class Reflect

java.lang.Object
  extended by org.jooq.tools.reflect.Reflect

public class Reflect
extends Object

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

Author:
Lukas Eder

Method Summary
<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.
 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 Reflect on(Class<?> clazz)
          Wrap a class.
static Reflect on(Object object)
          Wrap an object.
static Reflect on(String name)
          Wrap a class name.
 Reflect set(String name, Object value)
          Set a field value.
 String toString()
          
 Class<?> type()
          Get the type of the wrapped object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

on

public static Reflect on(String name)
                  throws ReflectException
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(Class)

on

public static Reflect on(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 - The class to be wrapped
Returns:
A wrapped class object, to be used for further reflection.

on

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 - The object to be wrapped
Returns:
A wrapped object, to be used for further reflection.

get

public <T> T get()
Get the wrapped object

Type Parameters:
T - A convenience generic parameter for automatic unsafe casting

set

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.

Parameters:
name - The field name
value - The new field value
Returns:
The same wrapped object, to be used for further reflection.
Throws:
ReflectException - If any reflection exception occurred.

get

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 - The field name
Returns:
The field value
Throws:
ReflectException - If any reflection exception occurred.
See Also:
field(String)

field

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 - The field name
Returns:
The wrapped field
Throws:
ReflectException - If any reflection exception occurred.

fields

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:
A map containing field names and wrapped values.

call

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 - 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(String, Object...)

call

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);
 

Parameters:
name - The method name
args - 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

public Reflect create()
               throws ReflectException
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(Object...)

create

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 - The constructor arguments
Returns:
The wrapped new object, to be used for further reflection.
Throws:
ReflectException - If any reflection exception occurred.

as

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 - The interface type that is implemented by the proxy
Returns:
A proxy for the wrapped object

hashCode

public int hashCode()

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

toString

public String toString()

Overrides:
toString in class Object

type

public Class<?> type()
Get the type of the wrapped object.

See Also:
Object.getClass()


Copyright © 2012. All Rights Reserved.