java反射机制
1、什么是反射机制?
java反射机制是指在运行状态中,对于任意一个类,都能知道这个类的所有属性和方法;对于任意一个对象,都能调用它的任意一个方法;
这种动态获取信息和动态调用方法的功能称为java反射机制。
2、反射机制的几种方法
User代码:
package com.reflect; public class User { private String username; private String password; private Integer age; public Integer add(Integer i , Integer j){ return i + j; } public User(){} public User(String username,String password,Integer age){ this.username = username; this.password = password; this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
ReflectMethod方法:
package com.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class ReflectMethod { // 能过反射获取一个对象的方法和属性 public static void reflectTest(String className) throws ClassNotFoundException { // 获取Class对象 Class<?> c = Class.forName(className); // 属性 Field[] f = c.getDeclaredFields(); for (int i = 0; i < f.length; i++) { System.out.println("属性:" + f[i].getName()); } // 方法 Method[] m = c.getDeclaredMethods(); for (int i = 0; i < m.length; i++) { System.out.println("方法:" + m[i].getName()); } } // 通过反射调用方法 public static void useMethod(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { // 获取Class对象 Class<?> c = Class.forName(className); // 通过Class获取对象,用newInstance()方法 Object obj = c.newInstance(); // 能过反射调用方法,传入参数是方法名和这个方法所需要的参数的Class对象的数组,如果add()里需要三个Integer类型的参数, // new Class[]数组就要有3个参数 Method addMethod = c.getMethod("add", new Class[] { Integer.class, Integer.class }); // 调用目标方法,传入参数 Object result = addMethod.invoke(obj, new Object[] { 1, 2 }); System.out.println(result); } // 通过构造方法,获取对象,第一种方法 public static Object noneParamObj1(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException { Class<?> c = Class.forName(className); return c.newInstance(); } // 通过构造方法,获取对象,第二种方法 public static Object noneParamObj2(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { Class<?> c = Class.forName(className); Constructor<?> con = c.getConstructor(new Class[] {}); Object obj = con.newInstance(new Object[] {}); return obj; } // 通过反射克隆对象 public static Object cloneInstance(String className) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class<?> c = Class.forName(className); // 获取构造方法 Constructor<?> con = c.getConstructor(new Class[] {}); // 通过构造方法获取一个新的对象 Object obj = con.newInstance(new Object[] {}); // 获取属性数组 Field[] fields = c.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; // 获取属性名字 String fieldName = field.getName(); // 首字母大字 String firstLetter = fieldName.substring(0, 1).toUpperCase(); //获取set和get方法名字 String getMethodName = "get" + firstLetter + fieldName.substring(1); String setMethodName = "set" + firstLetter + fieldName.substring(1); //获取set和get方法 Method setMethod = c.getMethod(setMethodName, new Class[]{field.getType()}); Method getMethod = c.getMethod(getMethodName, new Class[]{}); // 调用原对象的getXXX()方法 Object value = getMethod.invoke(obj, new Object[] {}); // 调用拷贝对象的setXXX()方法 setMethod.invoke(obj, new Object[] { value }); } User user = (User)obj; user.setPassword("123"); System.out.println(user.getPassword()); return obj; } }
Main代码
package com.reflect; import java.lang.reflect.InvocationTargetException; public class Main { public static void main(String[] args) { try { ReflectMethod.cloneInstance("com.reflect.User"); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }