package cn.tedu.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import org.junit.Before; import org.junit.Test; public class ReflectDemo { // 字节码对象, 备用 private Class<?> c; // person对象, 备用 Object objP; @Before public void before() { try { c = Class.forName("cn.tedu.reflect.Person"); objP = c.newInstance(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取Class对象三种方式 * @throws ClassNotFoundException */ @Test public void fun1() throws ClassNotFoundException { // 1. // Class c1 = new Person().getClass(); // 2. Class c2 = Person.class; // 3 Class c3 = Class.forName("cn.tedu.reflect.Person"); System.out.println(c2==c3); } /** Constructor对象 * 获取构造方法,并创建对象 * @throws SecurityException * @throws NoSuchMethodException */ @Test public void fun2() throws Exception { // 1.获取所有public修饰的构造 // Constructor<?>[] cons = c.getConstructors(); // cons = c.getDeclaredConstructors(); // for (Constructor<?> con : cons) { // System.out.println(con); // } // 2.获取所有构造,包括私有的 // 3.获取指定public构造, 并创建对象 // Constructor<?> con = c.getConstructor(); // System.out.println(con); // Object obj = con.newInstance(); // System.out.println(obj); // 4.获取指定私有构造, 并创建对象 // Constructor<?> con1 = c.getDeclaredConstructor(String.class); // con1.setAccessible(true); // Person p = (Person)con1.newInstance("张三"); // System.out.println(p); // 5.创建对象的最快方式(前提: 必须有空参构造) Person p1 = (Person)c.newInstance(); System.out.println(p1); } /** Field对象 * 获取成员变量, 并set和get * @throws SecurityException * @throws NoSuchFieldException */ @Test public void fun3() throws Exception { // 1.获取所有public成员变量 // Field[] fds = c.getFields(); // fds = c.getDeclaredFields(); // for (Field fd : fds) { // System.out.println(fd); // } // // 2.获取所有成员变量,包括私有的 // 3.获取指定类型的public修饰的成员变量, 并set值,get值, Field nameFd = c.getField("name"); System.out.println(nameFd); Object obj = c.newInstance(); nameFd.set(obj, "罗密欧"); System.out.println(nameFd.get(obj)); // 4.获取指定类型的成员变量,包括私有的 并set值,get值, Field weightFd = c.getDeclaredField("weight"); weightFd.setAccessible(true); weightFd.set(objP, 100.0); System.out.println(weightFd.get(objP)); } /** Method对象 * 获取成员方法, 并运行 * @throws SecurityException * @throws NoSuchMethodException */ @Test public void fun4() throws Exception { // 1.获取所有public修饰的成员方法 Method[] mhs = c.getMethods(); mhs = c.getDeclaredMethods(); for (Method method : mhs) { System.out.println(method); } // 2.获取所有成员方法, 包括私有的 // 3.获取指定类型的public修饰的成员方法, 并运行 Method mh = c.getMethod("walk"); mh.invoke(objP); Method mh1 = c.getMethod("watch", String.class, int.class); Object result = mh1.invoke(objP, "貂蝉", 18); System.out.println(result); // 4.获取指定类型的成员方法(包括私有的), 并运行 Method mh2 = c.getDeclaredMethod("run"); mh2.setAccessible(true); mh2.invoke(objP); } /** * 反射练习------集合泛型擦除 * @throws Exception */ @Test public void func5() throws Exception { ArrayList<Integer> array = new ArrayList<>(); array.add(10); array.add(20); // array.add("张三"); System.out.println(array); Class<?> c = Class.forName("java.util.ArrayList"); Method mh = c.getMethod("add", Object.class); mh.invoke(array, "张三"); System.out.println(array); } } |
|