2
Reflection
Calvin Xiao edited this page 2017-01-20 16:20:01 +08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#反射

##1. Class信息

##2. 反射调用

提供反射调用函数和getter/setter函数以及直接绕过getter/setter直接访问属性的能力。
无视private/protected/public修饰符的关键是Class.getDeclaredMethods()函数它与Class.getMethods()比能返回private的函数但只能返回当前类的(不会继承于父类的函数一起返回)然后可以method.setAccessible(true)来强制转换当然这并不总是成功因为SecurityManager有时候会干预。

提供反射获取Class声明中的泛型参数例子是public UserDao extends HibernateDao获得User.class.注意这个User是定义在父类旁边的这也是Java泛型中唯一能反射获得Class的地方。