The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
The following examples show typical errors which may be encountered when reflecting on classes.
When a method is invoked, the types of the argument values are checked and possibly converted.
invokes
ClassWarninggetMethod() to cause a typical unchecked conversion warning:
import java.lang.reflect.Method;
public class ClassWarning {
void m() {
try {
Class c = ClassWarning.class;
Method m = c.getMethod("m"); // warning
// production code should handle this exception more gracefully
} catch (NoSuchMethodException x) {
x.printStackTrace();
}
}
}
$ javac ClassWarning.java
Note: ClassWarning.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$ javac -Xlint:unchecked ClassWarning.java
ClassWarning.java:6: warning: [unchecked] unchecked call to getMethod
(String,Class<?>...) as a member of the raw type Class
Method m = c.getMethod("m"); // warning
^
1 warning
Many library methods have been retrofitted with generic declarations including several in
Class. Since c is declared as a raw type (has no type parameters) and the corresponding parameter of
getMethod() is a parameterized type, an unchecked conversion occurs. The compiler is required to generate a warning. (See The Java Language Specification, Java SE 7 Edition, sections Unchecked Conversion and Method Invocation Conversion.)
There are two possible solutions. The more preferable it to modify the declaration of c to include an appropriate generic type. In this case, the declaration should be:
Class<?> c = warn.getClass();
Alternatively, the warning could be explicitly suppressed using the predefined annotation
@SuppressWarnings preceding the problematic statement.
Class c = ClassWarning.class;
@SuppressWarnings("unchecked")
Method m = c.getMethod("m");
// warning gone
@SuppressWarnings.
Class.newInstance() will throw an
InstantiationException if an attempt is made to create a new instance of the class and the zero-argument constructor is not visible. The
example illustrates the resulting stack trace.ClassTrouble
class Cls {
private Cls() {}
}
public class ClassTrouble {
public static void main(String... args) {
try {
Class<?> c = Class.forName("Cls");
c.newInstance(); // InstantiationException
// production code should handle these exceptions more gracefully
} catch (InstantiationException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
} catch (ClassNotFoundException x) {
x.printStackTrace();
}
}
}
$ java ClassTrouble
java.lang.IllegalAccessException: Class ClassTrouble can not access a member of
class Cls with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at ClassTrouble.main(ClassTrouble.java:9)
Class.newInstance() behaves very much like the new keyword and will fail for the same reasons new would fail. The typical solution in reflection is to take advantage of the
java.lang.reflect.AccessibleObject class which provides the ability to suppress access control checks; however, this approach will not work because
java.lang.Class does not extend
AccessibleObject. The only solution is to modify the code to use
Constructor.newInstance() which does extend
AccessibleObject.
Constructor.newInstance() for the reasons described in the Creating New Class Instances section in the Members lesson.
Additional examples of potential problems using
Constructor.newInstance() may be found in the Constructor Troubleshooting section of the Members lesson.