Java反射备忘录
在看spring的时候发现很多都是通过反射实现的,所以也看了看java关于反射的部分,总结如下,方便以后查找
Arithmetic.java
/** * Class Arithmetic * 反射目标类 * 水平有限,所以此类纯是为了测试而写,无任何逻辑而言 * @author lgsun * Date: 2011-3-27 */package com.lgsun.target;@SuppressWarnings("all")public class Arithmetic{private intparmaerOne;private intparmaerTwo;public intsum;private Arithmetic(){this.parmaerOne = -1;this.parmaerTwo = -2;}public Arithmetic(int parmaerOne, int parmaerTwo){this.parmaerOne = parmaerOne;this.parmaerTwo = parmaerTwo;}public int getParmaerOne(){return parmaerOne;}public void setParmaerOne(int parmaerOne){this.parmaerOne = parmaerOne;}public int getParmaerTwo(){return parmaerTwo;}public void setParmaerTwo(int parmaerTwo){this.parmaerTwo = parmaerTwo;}public int getSum(){sum = sum(parmaerOne, parmaerTwo);return this.sum;}public double getDivision(){return (parmaerOne / parmaerTwo);}private int sum(int p1, int p2){return (p1 + p2);}public void getInfo(){System.out.println();System.out.println("parmaerOne=" + parmaerOne);System.out.println("parmaerTwo=" + parmaerTwo);System.out.println("sum=" + sum);}}
/** * Class ReflectConstructor * Constructor getConstructor(Class[] params) -- 获得使用特殊的参数类型的公共构造函数, * Constructor[] getConstructors() -- 获得类的所有公共构造函数 * Constructor getDeclaredConstructor(Class[] params) -- 获得使用特定参数类型的构造函数(与接入级别无关) * Constructor[] getDeclaredConstructors() -- 获得类的所有构造函数(与接入级别无关) * 前2个可以返回继承自父类的构造方法;后2个只能返回当前类定义的,并且包含私有方法 * @author lgsun * Date: 2011-3-27 */package com.lgsun.test;import java.lang.reflect.Constructor;import com.lgsun.target.Arithmetic;public class ReflectConstructor{@SuppressWarnings("all")public static void main(String[] args){try{// 返回Arithmetic类全部的构造方法,包含私有构造方法Constructor[] con = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructors();for (int i = 0; i < con.length; i++){System.out.println(con[i].toGenericString());}// 通过Arithmetic(int p1,int p2)构造方法创建实例Class[] type = new Class[] { int.class, int.class };Constructor pCon1 = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructor(type);Arithmetic arit1 = (Arithmetic) pCon1.newInstance(3, 4);arit1.getInfo();// 通过Arithmetic()构造方法创建实例Constructor pCon2 = Class.forName("com.lgsun.target.Arithmetic").getDeclaredConstructor();// 由于此构造方法是私有的,所以必须设置Accessible为truepCon2.setAccessible(true);Arithmetic arit2 = (Arithmetic) pCon2.newInstance();arit2.getInfo();}catch (Exception e){e.printStackTrace();}}}
/** * Class ReflectMethod * Method getMethod(String name, Class[] params) -- 使用特定的参数类型,获得命名的公共方法 * Method[] getMethods() -- 获得类的所有公共方法 * Method getDeclaredMethod(String name, Class[] params) -- 使用特写的参数类型,获得类声明的命名的方法 * Method[] getDeclaredMethods() -- 获得类声明的所有方法 * 前2个方法会返回所有的属性,包括父类属性;后2个只会返回此类中定义的属性,包含私有属性 * @author lgsun * Date: 2011-3-28 */package com.lgsun.test;import java.lang.reflect.Method;import com.lgsun.target.Arithmetic;@SuppressWarnings("all")public class ReflectMethod{public static void main(String[] args){Arithmetic arithmetic = new Arithmetic(7, 8);Method[] methods = arithmetic.getClass().getDeclaredMethods();for (int i = 0; i < methods.length; i++){System.out.println(methods[i].toGenericString());}try{Class[] types = new Class[] { int.class, int.class };Method method1 = arithmetic.getClass().getDeclaredMethod("sum", types);method1.setAccessible(true);// 这样写是错误的// int[] parms=new int[]{17,18};Object[] parms = new Object[] { 17, 18 };Object result = method1.invoke(arithmetic, parms);System.out.println(((Integer) result).intValue());arithmetic.getInfo();}catch (Exception e){e.printStackTrace();}}}
/** * Class ReflectFiled * Field getField(String name) -- 获得命名的公共字段 * Field[] getFields() -- 获得类的所有公共字段 * Field getDeclaredField(String name) -- 获得类声明的命名的字段 * Field[] getDeclaredFields() -- 获得类声明的所有字段 * 同样,前2个方法会返回所有的属性,包括父类属性;后2个只会返回此类中定义的属性,包含私有属性 * @author lgsun * Date: 2011-3-28 */package com.lgsun.test;import java.lang.reflect.Field;import com.lgsun.target.Arithmetic;public class ReflectFiled{@SuppressWarnings("all")public static void main(String[] args){// 返回Arithmetic类全部属性,包含私有属性Arithmetic arithmetic = new Arithmetic(4, 5);Field[] fields = arithmetic.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++){System.out.println(fields[i].toGenericString());}try{// 直接给public属性sum赋值Field field1 = arithmetic.getClass().getDeclaredField("sum");field1.set(arithmetic, 19);arithmetic.getInfo();// 给private属性parmaerOne赋值Field field2 = arithmetic.getClass().getDeclaredField("parmaerOne");// 必加field2.setAccessible(true);field2.set(arithmetic, 14);arithmetic.getInfo();}catch (Exception e){e.printStackTrace();}}}