java中的反射 2.4——类:常见问题@译自Oracle官方文档
2015-07-22 16:42
525 查看
译自:http://docs.oracle.com/javase/tutorial/reflect/class/classTrouble.html
常见问题
下面的例子展示类一些使用反射时可能会遇到的典型错误。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
提醒:作为一个基本的原则,警告信息不应该被忽略因为他们可能指出潜在的bug。更好的方式是使用参数化的声明。如果具体的情况不允许这样做(可能因为程序需要与library vendor's
code 进行交互),那就用@SupressWarnings注解取消警告。
当构造方法无法访问时抛出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()方法是更好的办法。
更多的使用Constructor.newInstance()方法可能出现的问题可以参考这篇文章(成员:常见问题)。
相关文章推荐
- oracle中一些命令
- ORACLE 12C新特性——CDB与PDB
- oracle官方中文博客地址
- Oracle 11g 数据类型
- oracle语句块
- oracle 分区
- ORACLE日期时间函数大全
- mybatis按时间查询oracle数据
- 关于Oracle表连接
- oracle sql 汉字在库中占的字节数
- 处理 Oracle SQL in 超过1000 的解决方案
- oracle批量执行sql文件的方法
- [VirtualBox] - Install Oracle Linux 7 on Oracle VirtualBox
- Delphi连接Oracle控件ODAC的安装及使用
- 一个非常标准的连接Oracle数据库的示例代码
- Oracle查询前10条记录及分页查询(第5条到第10记录)
- Oracle学习与开发基本概念汇总
- oracle 数据汞导入导出dmp文件
- oracle学习笔记
- 高速决心linux上oracle安装垃圾问题