1 package reflect.chester;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.Field;
5 import java.lang.reflect.Method;
6
7 /**8 *9 * @ClassName Reflect.java10 * @describe 反射,利用反射得到字节码,然后利用字节码得到构造函数,Field,Method,利用构造函数创建对象11 *@authorChester12 * @date 2011-11-2613 *@versionV1.014 */
15 public class Reflect {
16
17 private int x;
18 int y;
19
20 /**21 *22 * @Title main23 * @Describe TODO24 *@param@paramargs25 *@returnvoid26 *@throwsTODO27 * @Author chester28 * @Date 2011-11-2429 */
30 public static void main(String[] args) {
31 //TODO Auto-generated method stub32 /*33 * int x = 0; x=2; System.out.println(x); String str1="shi";34 * System.out.println(String.class==str1.getClass());//true35 * System.out.println(str1.getClass());//java.lang.String36 * System.out.println(Reflect.class);//reflect.chester.Reflect37 */
38
39
40 /*41 * 声明类的字节码对象42 */
43 Class> reflect2Class;
44 /*45 * 声明构造函数对象46 */
47 Constructor>[] constructors2;
48 /*49 * 声明Field对象;50 */
51 Field xField,yField;
52 Field []fieldsField;
53 /*54 * 声明Method对象55 */
56 Method []methods;
57 Method method;
58
59
60 Reflect2 reflect2 = null;
61
62
63 try {
64
65 /**66 * 获取类的Class对象的3种方法67 * Reflect.class68 * getClass69 * static method Class.forName()70 */
71
72 reflect2Class = Class.forName("reflect.chester.Reflect2");
73
74 /*75 * 得到构造函数对象76 */
77
78 constructors2 = reflect2Class.getDeclaredConstructors();
79
80 for (int i = 0; i < constructors2.length; i++) {
81 System.out.println(constructors2[i].getName());//有多少个构造函数就输出多少个reflect.chester.Reflect字符串82 }
83
84 /*85 * 利用反射得到的构造函数对象new一个Reflect对象 newInstance()的参数必须Object类型数组86 * 注意参数的匹配87 *88 */
89
90 /*91 * 把私有的构造函数设为可访问92 */
93 constructors2[0].setAccessible(true);
94 reflect2 = Reflect2.class.cast(constructors2[0].newInstance(new Object[] {}));
95
96 } catch (Exception e) {
97 //TODO: handle exception98 e.printStackTrace();
99 }
100
101 /*102 * xField设置为可访问,不然会抛出异常103 */
104
105 try {
106
107 /*108 * 实例化Field,fieldsField对象109 * public Field getField(String name)返回一个 Field 对象,它反映此 Class 对象所表示的类或接口的指定 “公共" 成员字段110 * public Field[] getFields()返回一个包含某些 Field 对象的数组,这些对象反映此 Class 对象所表示的类或接口的所有可访问公共字段111 * public Field getDeclaredField(String name)返回一个 Field 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明字段112 * public Field[] getDeclaredFields()返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段113 */
114 xField = Reflect2.class.getDeclaredField("x");
115 //yField=Reflect2.class.getDeclaredField("y");116 yField=Reflect2.class.getField("y");
117 fieldsField=Reflect2.class.getDeclaredFields();
118
119 /*120 * 循环输出Reflect2类的Field的名字121 122 for (int i = 0; i < fieldsField.length; i++) {123 System.out.println(fieldsField[i].getName());124 }125 */
126 for(Field field:fieldsField){
127 System.out.println(field);
128 }
129 /*130 * 把私有变量X设置为可访问131 */
132 xField.setAccessible(true);
133 //这个非常重要reflect2,不然不知道作用于那个对象134 System.out.println("访问到reflect2对象的私有变量 x:" + xField.get(reflect2));
135 //System.out.println(xField.getDeclaringClass());//class reflect.chester.Reflect2136 System.out.println("直接访问reflect2对象的y域:"+reflect2.y);
137 System.out.println("通过yField.get(reflect2)访问y域:"+yField.get(reflect2));
138
139 /*140 * 利用发射改变了X的值141 */
142 xField.set(reflect2,"利用发射改变了X的值");
143 System.out.println("XField.set()后的值 x:" + xField.get(reflect2));
144
145 /*146 * 实例化methods对象147 * public Method getMethod(String name, Class>... parameterTypes) 返回一个 Method 对象,148 * 它反映此 Class 对象所表示的类或接口的指定" 公共成员 "方法149 *150 * public Method[] getMethods()返回一个包含某些 Method 对象的数组,这些对象反映此 Class 对象所表示的类或接口151 * (包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法152 *153 * public Method getDeclaredMethod(String name,Class>... parameterTypes)154 * 返回一个 Method 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明方法155 *156 * public Method[] getDeclaredMethods()返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,157 * 包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法158 */
159 methods=Reflect2.class.getDeclaredMethods();
160 /*161 * getDeclaredMethod()第2个参数是你要print方法参数的class,int.class,String.class,long.class或者是自定义的class162 * 当参数为空时使用new Class[]{ }163 */
164 method=Reflect2.class.getDeclaredMethod("print",new Class[]{ } );
165 methods[1].setAccessible(true);
166 methods[0].setAccessible(true);
167 method.setAccessible(true);
168 /*169 * invoke的时候,注意参数要一致170 */
171 methods[0].invoke(reflect2, new Object[]{});
172 methods[1].invoke(reflect2, new Object[]{});
173 method.invoke(reflect2, new Object[]{});
174
175
176 } catch (SecurityException e) {
177 //TODO Auto-generated catch block178 e.printStackTrace();
179 } catch (Exception e) {
180 //TODO Auto-generated catch block181 e.printStackTrace();
182 }
183
184 }
185
186 /**187 *188 * @Title189 * @Describe TODO190 *@param@paramx191 *@param@paramy192 *@authorchester193 * @Date 2011-11-24194 */
195 public Reflect(int x, int y) {//alt+shift+s 生成构造函数196 super();
197 this.x = x;
198 this.y = y;
199 }
200
201 /*202 * shift + alt + s 选择生成构造函数203 */
204 /**205 *206 * @Title207 * @Describe 无参数构造函数208 *@param209 *@authorchester210 * @Date 2011-11-26211 */
212 private Reflect() {
213 super();
214 this.x = 100;
215 this.y = 20;
216 }
217
218 /**219 *220 * @Title add221 * @Describe TODO222 *@param@paramx223 *@param@paramy224 *@returnvoid225 *@throwsTODO226 * @Author chester227 * @Date 2011-11-24228 */
229 public static void add(int x, int y) {
230 int z = x + y;
231 System.out.println(z);
232 String str1 = "shi";
233 System.out.println(String.class == str1.getClass());//true234 }
235
236 }