package 反射的学习; import java.util.Map; public interface Yinshe { public Map<String, String> getYinsheGx(); } package 反射的学习; public class User { private int age; private String name; private String name1; public User() { super(); } public User(String name) { super(); this.name = name; } public User(int age, String name) { super(); this.age = age; this.name = name; } public User(int age, String name,String name1) { super(); this.age = age; this.name = name; this.name1 = name1; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [age=" + age + ", name=" + name + "]"; } public String getName1() { return name1; } public void setName1(String name1) { this.name1 = name1; } } package 反射的学习; public class User1 { private int age; private String name; private String name1; public User1() { super(); } public User1(String name) { super(); this.name = name; } public User1(int age, String name) { super(); this.age = age; this.name = name; } public User1(int age, String name,String name1) { super(); this.age = age; this.name = name; this.setName1(name1); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User1 [age=" + age + ", name=" + name + "]"; } public String getName1() { return name1; } public void setName1(String name1) { this.name1 = name1; } } package 反射的学习; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class 反射获取属性 { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException { List<User> lt=new ArrayList<User>(); lt.add(new User(1212,"11")); lt.add(new User(1212,"11")); System.out.println(zhuanhuaObject(new User(1212,"11","11"))); System.out.println(zhuanhuaList(lt).size()); } /** * 对象的自转换 * @param user * @param user1 * @return * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws SecurityException * @throws NoSuchFieldException */ public static User1 zhuanhuaObject(Object obj) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException{ return (User1) zhuanghua(obj,new User1(),new Yinshe(){ @Override public Map<String, String> getYinsheGx() { Map<String, String> map=new HashMap<String,String>(); map.put("age", "age"); map.put("name1", "name"); return map; } }); } /** * 集合自转换 * @param arrayList * @return * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws SecurityException * @throws NoSuchFieldException */ public static List<User1> zhuanhuaList(List<User> useList) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException{ List<User1> user1List=new ArrayList<User1>(); for(User use:useList){ user1List.add((User1) zhuanghua(use,new User1(),new Yinshe(){ @Override public Map<String, String> getYinsheGx() { Map<String, String> map=new HashMap<String,String>(); map.put("age", "age"); map.put("name1", "name"); return map; } }));// } return user1List; } /** * 模型自转换底层实现 * @param obj * @param obj1 * @param YS * @return * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws SecurityException * @throws NoSuchFieldException */ static Object zhuanghua(Object obj,Object obj1,Yinshe YS) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException{ //获取整个类 Class<?> c = Class.forName(obj.getClass().getName()); Class<?> c1 = Class.forName(obj1.getClass().getName()); @SuppressWarnings("rawtypes") java.util.Iterator it = YS.getYinsheGx().entrySet().iterator(); while(it.hasNext()){ @SuppressWarnings("rawtypes") java.util.Map.Entry entry = (java.util.Map.Entry)it.next(); // 调用TestReflect类中的reflect1方法 Method method = c.getMethod( "get"+captureName((String)entry.getKey())); Field field1 = c1.getDeclaredField((String)entry.getValue()); Method method1 = c1.getMethod("set"+captureName( (String)entry.getValue()),field1.getType()); // 调用TestReflect的reflect2方法 method1.invoke(obj1, method.invoke(obj)); } return obj1; } private static String captureName(String name) { //name = name.substring(0, 1).toUpperCase() + name.substring(1); //return name; char[] cs=name.toCharArray(); cs[0]-=32; return String.valueOf(cs); } // 可以直接对 private 的属性赋值 // int length=fs.length<fs1.length ? fs.length:fs1.length; // for(int i=0;i<length;i++){ // // 调用TestReflect类中的reflect1方法 // Method method = c.getMethod( // "get"+captureName(fs[i].getName())); // Method method1 = c1.getMethod("set"+captureName( // fs1[i].getName()),fs1[i].getType()); // // 调用TestReflect的reflect2方法 // method1.invoke(obj1, method.invoke(obj)); // Field[] fs = c.getDeclaredFields(); // Field[] fs1 = c1.getDeclaredFields(); // fs1[i].setAccessible(true); // fs[i].setAccessible(true); // fs1[i].set(obj1, fs[i].get(obj)); // fs1[i].setAccessible(false); // fs[i].setAccessible(false); }
接口的使用达到了即用即设的效果,zhuanghua()方法是一个底层方法,本人认为可以存放在,模型包中设为包级访问权限。功能:实现了通用的在规定的映射关系下,将两个模型互相转换。
zhuanhuaObject方法的功能:定义模型转化的映射关系,可将任何其他类型的模型转化为this对象,实现时机:在你需要将任意一个模型转化为本模型时可将此方法的实现写在改模型类中,并且实现映射关系
zhuanhuaList的完善:达到可将任何其他类型的list集合数据转化为某对象的lit集合。zhuanhuaList方法的功能:定义模型转化的映射关系,可将两个给定的list类型的数据集合转换,实现时机:在你需要将任意一个模型转化为本模型时可将此方法的实现写在改模型类中,并且实现映射关系