public abstract 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 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
Modifier and Type | Class and Description |
---|---|
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.
|
Modifier and Type | Field and Description |
---|---|
static Configurable<java.lang.Class<? extends Reflection>> |
CLASS
Holds the default implementation (configurable).
|
Modifier | Constructor and Description |
---|---|
protected |
Reflection()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
abstract void |
add(java.lang.Object classLoader)
Adds the specified class loader to the research tree.
|
abstract java.lang.Class |
getClass(java.lang.CharSequence name)
Returns the class having the specified name.
|
java.lang.Class |
getClass(java.lang.String name)
Equivalent to
getClass(CharSequence) (for J2ME compatibility). |
abstract Reflection.Constructor |
getConstructor(java.lang.String signature)
Returns the constructor having the specified signature.
|
abstract <T> T |
getField(java.lang.Class forClass,
java.lang.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 java.lang.Class[] |
getInterfaces(java.lang.Class forClass)
Returns the interfaces implemented by the specified class or interface.
|
abstract Reflection.Method |
getMethod(java.lang.String signature)
Returns the method having the specified signature.
|
abstract java.lang.Class |
getSuperclass(java.lang.Class forClass)
Returns the parent class of the specified class or interface.
|
abstract void |
remove(java.lang.Object classLoader)
Removes the specified class loader from the research tree.
|
abstract <T> void |
setField(T obj,
java.lang.Class forClass,
java.lang.Class<T> type)
Attaches a field of specified type to a class (the attached field is
dereferenced when the class is unloaded).
|
public static final Configurable<java.lang.Class<? extends Reflection>> CLASS
public static final Reflection getInstance()
CLASS
(configurable}.public abstract void add(java.lang.Object classLoader)
classLoader
- the class loader being added.public abstract void remove(java.lang.Object classLoader)
classLoader
- the class loader being removed.public abstract java.lang.Class getClass(java.lang.CharSequence name)
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.name
- the name of the class to search for.null
public java.lang.Class getClass(java.lang.String name)
getClass(CharSequence)
(for J2ME compatibility).public abstract java.lang.Class getSuperclass(java.lang.Class forClass)
forClass
- the class for which the parent class is returned.null
if none (e.g. Object.class or top interface).public abstract java.lang.Class[] getInterfaces(java.lang.Class forClass)
forClass
- the class for which the interfaces are returned.public abstract Reflection.Constructor getConstructor(java.lang.String signature)
signature
- the textual representation of the constructor signature.null
if none
found.public abstract Reflection.Method getMethod(java.lang.String signature)
signature
- the textual representation of the method signature.null
if none
found.public abstract <T> T getField(java.lang.Class forClass, java.lang.Class<T> type, boolean inherited)
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
.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.null
if none found.setField(java.lang.Object, java.lang.Class, java.lang.Class)
public abstract <T> void setField(T obj, java.lang.Class forClass, java.lang.Class<T> type)
obj
- the field object being attached.forClass
- the class to which the field is attached.type
- the category type of the field being attached.java.lang.IllegalArgumentException
- if a field of specified type is already
attached to the specified class.Copyright © 2005 - 2007 Javolution.