Enhancements to the Java Reflection API (original) (raw)
Enhancements to the Reflection API including java.lang.Class
andjava.lang.reflect
- Enhancements in Java SE 8
- Enhancements in Java SE 6
- Enhancements in Java SE 5.0
- Enhancements in Java SE 1.4
Enhancements in Java SE 8
- Method Parameter Reflection (RFE: JDK-8004841): You can obtain the names of the formal parameters of any method or constructor with the method java.lang.reflect.Executable.getParameters. However,
.class
files do not store formal parameter names by default. To store formal parameter names in a particular.class
file, and thus enable the Reflection API to retrieve formal parameter names, compile the source file with the-parameters
option of thejavac
compiler.
Enhancements in Java SE 6
- The following methods in java.lang.Classwere generified: getInterfaces(),getClasses().getConstructors(). getMethod(String, Class...), getConstructor(Class...), getDeclaredClasses(),getDeclaredConstructors(), getDeclaredMethod(String, Class...), and getDeclaredConstructor(Class...). As a result, code which uses these methods now produces warnings during compilation.
For example, consider the following code which invokesgetDeclaredMethod()
:
import java.lang.reflect.Method;
public class Warning {
void m() {
try {
Warning warn = new Warning();
Class c = warn.getClass();
Method m = c.getDeclaredMethod("m");
} catch (NoSuchMethodException x) {
x.printStackTrace();
}
}
}
$ javac Warning.java
Note: Warning.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$ javac -Xlint:unchecked Warning.java
Warning.java:8: warning: [unchecked] unchecked call to getDeclaredMethod(java.lang.String,java.lang.Class<?>...) as a member of the raw type java.lang.Class
Method m = c.getDeclaredMethod("m");
^
1 warning
- To remove the warning, the declaration of
c
should be modified to include an appropriate generic type. In this case, the declation should be:
Class<?> c = warn.getClass(); - Method.toString()and Constructor.toString()now correctly display the set of modifiers.
- The final parameter of Array.newInstance(Class, int...) is of variable arity.
Enhancements in Java SE 5.0
The changes in java.lang.Class and java.lang.reflect include:
- Support for generics - In particular, the changes support structural program reflection on generic type information. In other words, they make it possible to examine a type, method, constructor or field declaration and obtain generic type information. Specifically, one can determine whether a type, method or constructor declares any type variables, and reflect those type variables to the application program (so that one may learn their names and bounds, for example). One can also obtain the non-erased form of the type of a field, a method or constructor parameter, a method return type or the throws clause of a method or constructor. The generified form of a type name is available via toGenericString(). However, note that we do not provide any support for determining which generic invocation an instance belongs to.
- Support for annotations - This includes the ability to obtain annotations on types, methods, fields, constructors and formal parameters of methods and constructors that have been marked as available at run-time, using the getAnnotation() method. One can also determine whether an interface is an annotation type.
- Support for enums - One can determine whether a class is an enum, and whether a field represents an enum constant.
- Support for var args - One can determine whether a method or constructor is a of variable arity.
- Convenience methods - for establishing whether a class is local, anonymous or a member class, and what a type's simple and cannoical names are. One can also determine whether a member is synthetic (an artifiact by the implementation).
- The generification of class java.lang.Class - This allows the use of instances of java.lang.Class as type tokens. See the Generics lesson in the Java Tutorials.
Enhancements in Java SE 1.4
- Performance enhancements - Certain reflective operations, specifically Field, Method.invoke(), Constructor.newInstance(), and Class.newInstance(), have been rewritten for higher performance. Reflective invocations and instantiations are several times faster than in previous releases.