org.joe_e.reflect
Class Reflection

java.lang.Object
  extended by org.joe_e.reflect.Reflection

public final class Reflection
extends Object

The reflection interface.

This API provides reflective access to all the public constructors, public fields and public methods of public Joe-E classes and interfaces. The API provides no more permission than is provided by static Joe-E program code. If you can do something with the reflection API, you could also have done it using static Joe-E code. The only difference is expressivity.


Method Summary
static
<T> T
construct(Constructor<T> ctor, Object... args)
          Invokes a reflected constructor.
static
<T> Constructor<T>
constructor(Class<T> type, Class<?>... args)
          Gets a public constructor.
static PowerlessArray<Constructor<?>> constructors(Class<?> type)
          Gets all declared public constructors.
static Field field(Class<?> type, String name)
          Gets a public field.
static PowerlessArray<Field> fields(Class<?> type)
          Gets all public fields.
static Object get(Field field, Object self)
          Gets the value of a field.
static String getName(Class<?> c)
          Get the name of the entity represented by a Class object, in the same format as returned by Class.getName().
static Object invoke(Method method, Object self, Object... args)
          Invokes a reflected method.
static Method method(Class<?> type, String name, Class<?>... args)
          Gets a public method.
static PowerlessArray<Method> methods(Class<?> type)
          Gets all public methods.
static void set(Field field, Object self, Object value)
          Sets the value of a field.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

field

public static Field field(Class<?> type,
                          String name)
                   throws NoSuchFieldException
Gets a public field.

This method wraps Class.getField(java.lang.String).

Parameters:
type - class to search
name - field name
Returns:
described field
Throws:
NoSuchFieldException - no matching field found

fields

public static PowerlessArray<Field> fields(Class<?> type)
Gets all public fields.

This method wraps Class.getFields().

Parameters:
type - object type
Returns:
described fields

constructor

public static <T> Constructor<T> constructor(Class<T> type,
                                             Class<?>... args)
                                  throws NoSuchMethodException
Gets a public constructor.

This method wraps Class.getConstructor(java.lang.Class...).

Parameters:
type - class to search
args - each parameter type
Returns:
described constructor
Throws:
NoSuchMethodException - no matching constructor found

constructors

public static PowerlessArray<Constructor<?>> constructors(Class<?> type)
Gets all declared public constructors.

This method wraps Class.getConstructors().

Parameters:
type - class to search
Returns:
all public constructors

method

public static Method method(Class<?> type,
                            String name,
                            Class<?>... args)
                     throws NoSuchMethodException
Gets a public method.

This method wraps Class.getMethod(java.lang.String, java.lang.Class...).

Parameters:
type - class to search
name - method name
args - each parameter type
Returns:
described method
Throws:
NoSuchMethodException - no matching method found

methods

public static PowerlessArray<Method> methods(Class<?> type)
Gets all public methods.

This method wraps Class.getMethods().

Parameters:
type - object type
Returns:
described methods

getName

public static String getName(Class<?> c)
Get the name of the entity represented by a Class object, in the same format as returned by Class.getName(). This wrapper exists to avoid exposing the number of proxy interfaces that have been generated.

Parameters:
c - the class to get the name of
Returns:
the name of class c
Throws:
IllegalArgumentException - if c is a proxy class

get

public static Object get(Field field,
                         Object self)
                  throws IllegalAccessException
Gets the value of a field.

Parameters:
field - field to access
self - target object
Returns:
field value
Throws:
IllegalAccessException - field is inaccessible

set

public static void set(Field field,
                       Object self,
                       Object value)
                throws IllegalAccessException
Sets the value of a field.

Parameters:
field - field to access
self - target object
value - new value
Throws:
IllegalAccessException - field is inaccessible

construct

public static <T> T construct(Constructor<T> ctor,
                              Object... args)
                   throws Exception
Invokes a reflected constructor.

Parameters:
ctor - constructor to invoke
args - each argument
Returns:
constructed object
Throws:
IllegalAccessException - ctor is inaccessible
ClassCastException - ctor.newInstance() throws an IllegalArgumentException, usually due to mismatched types
Exception - an exception thrown by the invoked constructor

invoke

public static Object invoke(Method method,
                            Object self,
                            Object... args)
                     throws Exception
Invokes a reflected method.

Parameters:
method - method to invoke
self - target object
args - each argument
Returns:
invocation return
Throws:
IllegalAccessException - method is inaccessible
ClassCastException - method.invoke() throws an IllegalArgumentException, usually due to mismatched types
Exception - an exception thrown by the invoked method