For example, consider the following code which invokes
getDeclaredMethod():
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:
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 section 8 of the generics tutorial
for examples.