反射
/**
* 全类名 com.unreal.demo.Student
* 包名+类名
* 获取Class字节码对象的三种方法,推荐使用第三种:
* Class.forName("com.unreal.demo.Student")
*/
@Test
public void demo() {
Student student = new Student();
Class c = student.getClass();
System.out.println(c);
}
@Test
public void demo2() {
Class c = Student.class;
System.out.println(c);
}
Class c;
@Before
public void before() throws Exception {
c = Class.forName("com.unreal.demo.Student");
}
getConstructors获取所有构造
/**
* 获取所有构造
* @throws Exception
*/
@Test
public void demo3() throws Exception {
Constructor[] cs = c.getConstructors();
for(Constructor cr : cs){
System.out.println(cr);
}
}
constructor 获取单个构造
@Test
public void demo4() throws Exception {
Constructor constructor = c.getConstructor(String.class,int.class);
Object o = constructor.newInstance("小华",5);
System.out.println(o);
}
@Test
public void demo5() throws Exception {
Constructor constructor = c.getConstructor();
Object o = constructor.newInstance();
System.out.println(o);
}
getMethods反射方法
/**
* 反射方法
*/
@Test
public void demo6() throws Exception {
Method[] methods = c.getMethods();
for( Method me: methods){
System.out.println(me);
}
}
反射到后用invoke执行
执行前需要一个Object参数,所以用newInstance得到
@Test
public void demo7() throws Exception {
// set
Method me = c.getMethod("setName", String.class);
// 执行方法
Object o = c.newInstance();
// 执行方法
me.invoke(o, "小红");
// get
Method getName = c.getMethod("getName");
Object invoke = getName.invoke(o);
System.out.println(invoke);
}
// 反射静态方法
@Test
public void demo8() throws Exception {
Method demoStudent = c.getMethod("demoStudent");
Object o = c.newInstance();
demoStudent.invoke(o);
}
用getDeclaredMethod反射私有方法
// 反射私有方法
@Test
public void demo9() throws Exception {
/*
getDeclaredMethod 可以操作所有的方法,包括私有
getMethod 只能操作非私有的方法
*/
Method getMoney = c.getDeclaredMethod("getMoney");
// 取消 Java 语言访问检查(暴力反射,打破了封装的特性)
getMoney.setAccessible(true);
Object o = c.newInstance();
getMoney.invoke(o);
}
/**
* 反射私有构造
*/
@Test
public void demo91() throws Exception {
Constructor cr = c.getDeclaredConstructor(int.class);
cr.setAccessible(true);
Object o = cr.newInstance(5);
}
/**
* 反射属性
*/
@Test
public void demo92(){
/*
公共字段
*/
Field[] fields = c.getFields();
for(Field f:fields){
System.out.println(f);
}
}
@Test
public void demo93(){
/*
获取所有的字段,包括私有
*/
Field[] fs = c.getDeclaredFields();
for(Field f : fs) {
System.out.println(f);
}
}
@Test
public void demo94()throws Exception {
Field f = c.getDeclaredField("PhoneNumber");
Object o = c.newInstance();
// 给属性赋值
f.set(o,"13437278267");
// 获取属性的值
Object o1 = f.get(o);
System.out.println(o1);
}
@Test
public void demo95() throws Exception {
Field name = c.getDeclaredField("name");
Object o = c.newInstance();
name.setAccessible(true);
name.set(o,"小凤");
Object o1 = name.get(o);
System.out.println(o1);
}
反射擦除泛型
/**
* 反射擦除泛型
*/
@Test
public void demo96() throws Exception {
ArrayList<Integer> lists = new ArrayList<Integer>();
lists.add(1);
lists.add(2);
lists.add(3);
System.out.println(lists);
Class c = Class.forName("java.util.ArrayList");
Method add = c.getDeclaredMethod("add", Object.class);
add.invoke(lists,"哈哈哈哈");
System.out.println(lists);
}
@Test
public void demo97() throws Exception {
Properties properties = new Properties();
properties.load(new FileInputStream("config.properties"));
String methodName = properties.getProperty("methodName");
String className = properties.getProperty("className");
Class cls = Class.forName(className);
Method method = cls.getDeclaredMethod(methodName);
method.setAccessible(true);
Object o = cls.newInstance();
method.invoke(o);
}