J avolution v5.2 (J2SE 1.5+)

javolution.lang
Class Reflection

java.lang.Object
  extended by javolution.lang.Reflection

public final class Reflection
extends java.lang.Object

This utility class greatly facilitates the use of reflection to invoke constructors or methods which may or may not exist at runtime.

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.getMethod("java.lang.System.nanoTime()");

Arrays and primitive types are supported. For example:

     Reflection.Constructor sbc = Reflection.getConstructor("java.lang.StringBuilder(int)");
     if (sbc != null) { // JDK 1.5+
        Object sb = sbc.newInstance(new Integer(32));
        Reflection.Method append = Reflection.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:
4.0, September 1, 2006
Author:
Jean-Marie Dautelle

Nested Class Summary
static class Reflection.Constructor
          This class represents a run-time constructor obtained through reflection.
static class Reflection.Method
          This class represents a run-time method obtained through reflection.
 
Method Summary
static java.lang.Class getClass(java.lang.CharSequence name)
          Returns the class having the specified name.
static java.lang.Class getClass(java.lang.String name)
          Equivalent to getClass(CharSequence) (for J2ME compatibility).
static Reflection.Constructor getConstructor(java.lang.String signature)
          Returns the constructor having the specified signature.
static Reflection.Method getMethod(java.lang.String signature)
          Returns the method having the specified signature.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getClass

public static java.lang.Class getClass(java.lang.CharSequence name)
                                throws java.lang.ClassNotFoundException
Returns the class having the specified name. This method searches a lookup table first, then diverse class loaders (caller, context, system); the newly found class is then initialized and added to the lookup table for future reference.

Parameters:
name - the name of the class to search for.
Returns:
the corresponding class
Throws:
java.lang.ClassNotFoundException - if the class is not found.

getClass

public static java.lang.Class getClass(java.lang.String name)
                                throws java.lang.ClassNotFoundException
Equivalent to getClass(CharSequence) (for J2ME compatibility).

Throws:
java.lang.ClassNotFoundException

getConstructor

public static Reflection.Constructor getConstructor(java.lang.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 static Reflection.Method getMethod(java.lang.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.

J avolution v5.2 (J2SE 1.5+)

Copyright © 2005 - 2007 Javolution.