|
J avolution v5.4 (J2SE 1.6+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavolution.lang.Reflection
public abstract class Reflection
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 dynamically (such as when running on a OSGI Platform).
Applications using custom class loaders may add them to the research
tree. 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
Nested Class Summary | |
---|---|
class |
Reflection.Constructor
This class represents a run-time constructor obtained through reflection. |
class |
Reflection.Method
This class represents a run-time method obtained through reflection. |
Field Summary | |
---|---|
static Configurable<java.lang.Class<? extends Reflection>> |
CLASS
Holds the XMLOutputFactory default implementation (configurable). |
Constructor Summary | |
---|---|
Reflection()
|
Method Summary | |
---|---|
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). |
static Reflection.Constructor |
getConstructor(java.lang.Object signature)
Deprecated. To be replaced by Reflection.getInstance().getConstructor(signature) |
abstract Reflection.Constructor |
getConstructor(java.lang.String signature)
Returns the constructor having the specified signature. |
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. |
static Reflection.Method |
getMethod(java.lang.Object signature)
Deprecated. To be replaced by Reflection.getInstance().getMethod(signature) |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Configurable<java.lang.Class<? extends Reflection>> CLASS
Constructor Detail |
---|
public Reflection()
Method Detail |
---|
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 static Reflection.Constructor getConstructor(java.lang.Object signature)
Reflection.getInstance().getConstructor(signature)
public static Reflection.Method getMethod(java.lang.Object signature)
Reflection.getInstance().getMethod(signature)
|
J avolution v5.4 (J2SE 1.6+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |