下面的例子展示类一些使用反射时可能会遇到的典型错误。
Compiler Warning: "Note: ... uses unchecked or unsafe operations"
当一个方法被调用时,会对参数的类型进行检查并可能进行转换。ClassWarning调用了Method()方法并造成了典型的未经检查的转换(unchecked conversion)警告:
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
许多库方法包括Class类中的一些方法都使用泛型声明进行了改进。由于c被声明为生类型(raw type)而对应的getMethod()方法参数是有类型要求的,所以产生了unchecked conversion警告。有两种解决方式。更好的方式是修改c的声明使其对应一种合适的泛型,这样c应该被声明如下:
Class<?> c = warn.getClass();
或者,可以使用预定义的@SupressWarnings注解取消这个警告,如下:
Class c = ClassWarning.class; @SuppressWarnings("unchecked") Method m = c.getMethod("m"); // warning gone
当构造方法无法访问时抛出InstantiationException (实例化异常)
Class.newInstance()方法会抛出InstantiationException如果在尝试创建一个类的新实例而该类的无参构造函数是不可见的。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()的行为跟new操作很相似,会因为与new操作失败的相同原因而失败。反射中的典型解决方式是利用java.lang.reflect.AccessibleObject类提取消类控制检查,然而,这种方式同样会失败因为java.lang.Class类没有扩展AccessibleObject。唯一的解决办法是在代码中使用扩展了AccessibleObject的Constructor.newInstance()方法
更多的使用Constructor.newInstance()方法可能出现的问题可以参考这篇文章(成员:常见问题)。