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:[code] 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()); ... } }[/code]
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:[code]
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()");[/code]
Arrays and primitive types are supported. For example:[code] 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[/code]
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 |
CLASS
Holds the default implementation (configurable).
|
Modifier | Constructor and Description |
---|---|
protected |
Reflection()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
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 Object |
getField(Class forClass,
Class 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 void |
setField(Object obj,
Class forClass,
Class type)
Attaches a field of specified type to a class (the attached field is
dereferenced when the class is unloaded).
|
public static final Configurable CLASS
public static final Reflection getInstance()
CLASS
(configurable}.public abstract void add(Object classLoader)
classLoader
- the class loader being added.public abstract void remove(Object classLoader)
classLoader
- the class loader being removed.public abstract Class getClass(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 Class getClass(String name)
getClass(CharSequence)
(for J2ME compatibility).public abstract Class getSuperclass(Class forClass)
forClass
- the class for which the parent class is returned.null
if none (e.g. Object.class or top interface).public abstract Class[] getInterfaces(Class forClass)
forClass
- the class for which the interfaces are returned.public abstract Reflection.Constructor getConstructor(String signature)
signature
- the textual representation of the constructor signature.null
if none
found.public abstract Reflection.Method getMethod(String signature)
signature
- the textual representation of the method signature.null
if none
found.public abstract Object getField(Class forClass, Class 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 void setField(Object obj, Class forClass, Class 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.IllegalArgumentException
- if a field of specified type is already
attached to the specified class.Copyright © 2005–2015 Javolution. All rights reserved.