javolution.lang
Class Reflection

Object
  extended by Reflection

public abstract class Reflection
extends Object

This utility class greatly facilitates the use of reflection to invoke constructors or methods which may or may not exist at runtime or may be loaded/unloaded dynamically such as when running on a OSGi Platform. For example:

         public class Activator implements BundleActivator {
              public void start(BundleContext context) throws Exception {
                   Reflection.getInstance().add(Activator.class.getClassLoader());
                   ...
              }
              public void stop(BundleContext context) throws Exception {
                   Reflection.getInstance().remove(Activator.class.getClassLoader());
                   ...
              }
         }

The constructors/methods are identified through their signatures represented as a String. When the constructor/method does not exist (e.g. class not found) or when the platform does not support reflection, the constructor/method is null (no exception raised). Here is an example of timer taking advantage of the new (JRE1.5+) high resolution time when available:

     public static long microTime() {
         if (NANO_TIME_METHOD != null) { // JRE 1.5+
             Long time = (Long) NANO_TIME_METHOD.invoke(null); // Static method.
             return time.longValue() / 1000;
         } else { // Use the less accurate time in milliseconds.
             return System.currentTimeMillis() * 1000;
         }
     }
     private static final Reflection.Method NANO_TIME_METHOD 
         = Reflection.getInstance().getMethod("java.lang.System.nanoTime()");

Arrays and primitive types are supported. For example:

     Reflection.Constructor sbc = Reflection.getInstance().getConstructor("java.lang.StringBuilder(int)");
     if (sbc != null) { // JDK 1.5+
        Object sb = sbc.newInstance(new Integer(32));
        Reflection.Method append = Reflection.getInstance().getMethod("java.lang.StringBuilder.append(char[], int, int)");
        append.invoke(sb, new char[] { 'h', 'i' }, new Integer(0), new Integer(2));
        System.out.println(sb);
    }
 
    > hi

Version:
5.5, April 20, 2009
Author:
Jean-Marie Dautelle

Nested Class Summary
static interface Reflection.Constructor
          This interface represents a run-time constructor obtained through reflection.
static interface Reflection.Method
          This interface represents a run-time method obtained through reflection.
 
Field Summary
static Configurable<Class<? extends Reflection>> CLASS
          Holds the default implementation (configurable).
 
Constructor Summary
protected Reflection()
          Default constructor.
 
Method Summary
abstract  void add(Object classLoader)
          Adds the specified class loader to the research tree.
abstract  Class getClass(CharSequence name)
          Returns the class having the specified name.
 Class getClass(String name)
          Equivalent to getClass(CharSequence) (for J2ME compatibility).
abstract  Reflection.Constructor getConstructor(String signature)
          Returns the constructor having the specified signature.
abstract
<T> T
getField(Class forClass, Class<T> type, boolean inherited)
          Returns the field of specified type which has been attached to a class.
static Reflection getInstance()
          Returns the current reflection instance.
abstract  Class[] getInterfaces(Class forClass)
          Returns the interfaces implemented by the specified class or interface.
abstract  Reflection.Method getMethod(String signature)
          Returns the method having the specified signature.
abstract  Class getSuperclass(Class forClass)
          Returns the parent class of the specified class or interface.
abstract  void remove(Object classLoader)
          Removes the specified class loader from the research tree.
abstract
<T> void
setField(T obj, Class forClass, Class<T> type)
          Attaches a field of specified type to a class (the attached field is dereferenced when the class is unloaded).
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS

public static final Configurable<Class<? extends Reflection>> CLASS
Holds the default implementation (configurable).

Constructor Detail

Reflection

protected Reflection()
Default constructor.

Method Detail

getInstance

public static final Reflection getInstance()
Returns the current reflection instance. The implementation class is defined by CLASS (configurable}.

Returns:
the reflection instance.

add

public abstract void add(Object classLoader)
Adds the specified class loader to the research tree.

Parameters:
classLoader - the class loader being added.

remove

public abstract void remove(Object classLoader)
Removes the specified class loader from the research tree. This method clears any cache data to allow for classes associated to the specified class loader to be garbage collected.

Parameters:
classLoader - the class loader being removed.

getClass

public abstract Class getClass(CharSequence name)
Returns the class having the specified name. This method searches the class loader of the reflection implementation, then any additional class loaders. If the class is found, it is initialized and returned; otherwise null is returned. The class may be cached for performance reasons.

Parameters:
name - the name of the class to search for.
Returns:
the corresponding class or null

getClass

public Class getClass(String name)
Equivalent to getClass(CharSequence) (for J2ME compatibility).


getSuperclass

public abstract Class getSuperclass(Class forClass)
Returns the parent class of the specified class or interface.

Parameters:
forClass - the class for which the parent class is returned.
Returns:
the parent class of the specified class or null if none (e.g. Object.class or top interface).

getInterfaces

public abstract Class[] getInterfaces(Class forClass)
Returns the interfaces implemented by the specified class or interface.

Parameters:
forClass - the class for which the interfaces are returned.
Returns:
an array holding the interfaces implemented (empty if none).

getConstructor

public abstract Reflection.Constructor getConstructor(String signature)
Returns the constructor having the specified signature.

Parameters:
signature - the textual representation of the constructor signature.
Returns:
the corresponding constructor or null if none found.

getMethod

public abstract Reflection.Method getMethod(String signature)
Returns the method having the specified signature.

Parameters:
signature - the textual representation of the method signature.
Returns:
the corresponding constructor or null if none found.

getField

public abstract <T> T getField(Class forClass,
                               Class<T> type,
                               boolean inherited)
Returns the field of specified type which has been attached to a class. If inherited is true the class hierarchy of the given class (parent classes and implementing interfaces) is searched. The method forces the initialization of the specified forClass.

Parameters:
forClass - the base class for which the attached field is searched.
type - the type of field being searched for.
inherited - indicates if the class hierarchy is searched too.
Returns:
an attached field of specified type possibly inherited or null if none found.
See Also:
setField(java.lang.Object, java.lang.Class, java.lang.Class)

setField

public abstract <T> void setField(T obj,
                                  Class forClass,
                                  Class<T> type)
Attaches a field of specified type to a class (the attached field is dereferenced when the class is unloaded).

Parameters:
obj - the field object being attached.
forClass - the class to which the field is attached.
type - the category type of the field being attached.
Throws:
IllegalArgumentException - if a field of specified type is already attached to the specified class.


Copyright © 2005-2012 Javolution. All Rights Reserved.