首页 > 代码库 > Java反射

Java反射

通过反射获得对象的方法

准备工作:

有一个User类如下

 1 package o1;
 2 
 3 /**
 4  * Created by yesiming on 16-11-19.
 5  */
 6 public class User {
 7     private int id;
 8     private String name
 9 
10     // 无参构造方法
11     public User() {
12         System.out.println("new Instance() 1");
13     }
14     // 有参构造方法
15     public User(int id, String name) {
16         System.out.println(id + ":" + name + ":new Instance() 2");
17     }
18     // get set 省略
19 }

下面写得到User对象的几种方法:

 1 package o1;
 2 
 3 import org.junit.Test;
 4 
 5 import java.io.File;
 6 import java.lang.reflect.*;
 7 import java.lang.reflect.InvocationTargetException;
 8 import java.net.URI;
 9 import java.net.URL;
10 
11 /**
12  * Created by yesiming on 16-11-19.
13  */
14 public class MainTest {
15 
16     @Test
17     public void getClazz() {
18         Class clazz = User.class;  // 得到User的类描述
19         System.out.println(clazz.toString());
20     }
21 
22     @Test
23     public void getClazz1() {
24         Class clazz = User.class;
25         try {
26             Object obj = clazz.newInstance(); // 通过类描述的newInstance()方法可以得到该类的对象(内部会调用无参构造方法)
27             System.out.println(obj.toString());
28         } catch (Exception e) {
29 
30         } finally {
31         }
32     }
33 
34     @Test
35     public void getClazz2() {
36         Class clazz = User.class;
37         try {
38             // 通过类描述的getDeclaredConstructor(Class... clazz)方法得到对应的构造函数
39             Constructor constructor = clazz.getDeclaredConstructor(int.class, String.class);
40             // 通过对应的构造函数的newInstance(xxx, xxx)方法得到类对象
41             Object obj = constructor.newInstance(1, "iming"); // 通过
42             System.out.println(obj.toString());
43         } catch (InstantiationException e) {
44             e.printStackTrace();
45         } catch (IllegalAccessException e) {
46             e.printStackTrace();
47         } catch (InvocationTargetException e) {
48             e.printStackTrace();
49         } catch (NoSuchMethodException e) {
50             e.printStackTrace();
51         }
52     }
53 
54     /**
55     * 通过getMethod(String methodName)得到方法描述符
56     * 通过方法描述符的invode(类描述符 xxx )方法,执行该方法
57     */
58     @Test
59     public void getMethod() throws Exception {
60         Class clazz = User.class;
61         Constructor constructor = clazz.getDeclaredConstructor(int.class, String.class);
62         Object obj = constructor.newInstance(1, "yesiming");
63         Method m = clazz.getMethod("setName", String.class);
64         m.invoke(obj, "Haha");
65 
66         Method m2 = clazz.getMethod("getName");
67         Object obj1 = m2.invoke(obj);
68         System.out.println(obj1.toString());
69     }
70 
71     /**
72      * 得到指定包名下所有的类
73      */
74     @Test
75     public void getClasses() throws Exception {
76         Class clazz = User.class;
77         ClassLoader loader = clazz.getClassLoader();
78         // 1. 通过classloader载入包路径,得到url
79         URL url = loader.getResource("o1/o1_a");
80         URI uri = url.toURI();
81         // 2. 通过File获得uri下的所有文件
82         File file = new File(uri);
83         File[] files = file.listFiles();
84         for (File f : files) {
85             String fName = f.getName();
86             if (!fName.endsWith(".class")) {
87                 continue;
88             }
89             fName = fName.substring(0, fName.length() - 6);
90             String perfix = "o1.o1_a.";
91             String allName = perfix + fName;
92             // 3. 通过反射加载类
93             clazz = Class.forName(allName);
94             System.out.println(clazz);
95         }
96     }
97 
98 }

 

Java反射