class="java" name="code">
package com.enhance.reflect;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
public class ReflectTest {
public static void main(String[] args) throws Exception{
//反射List 集合
reflectCollection();
}
/**
* 反射获取集合属性 和 集合中的 泛型
* @throws Exception
*/
public static void reflectCollection() throws Exception{
List<Integer> nums=new ArrayList<Integer>();
nums.add(1);
Person p =new Person();
p.setAge(18);
p.setName("luck");
List<Person> list=new LinkedList<Person>();
list.add(p);
List<Address> address = new ArrayList<Address>();
Address addr=new Address();
addr.setAddName("上海人民广场");
addr.setAddNo("add001");
address.add(addr);
p.setAddress(address);
TestA a=new TestA();
a.setNums(nums);
a.setP(p);
a.setAge(16);
a.setBig(55);
a.setData(list);
TestB b=new TestB();
copyProperties(a,b);
//BeanUtils.copyProperties(b,a); //只能copy 类型相同的,
System.out.println("基本类型:"+b.getBig());
System.out.println("包装类型:"+b.getAge());
System.out.println("自定义类"+b.getP().getAge());
System.out.println("List<包装类型>:"+b.getNums().get(0));
System.out.println("List<自定义类>:"+b.getData().get(0).getAge());
System.out.println("List<自定义类.List>:"+b.getData().get(0).getAddress().get(0).getAddName());
}
/**
* 能 复制 相同类型的属性
* 也能复制 "属性名相同,类型不同" 的属性
* @param src 原对象
* @param dest 目标对象
* @throws Exception
* Person ==> People
* 注意:本程序省略 NoSuchMethodException 的判断
* 每获取 Method 都需要判断 NoSuchMethodException 后在 invoke
*/
public static void copyProperties(Object src,Object dest) throws Exception{
Class sclazz =src.getClass();
Class dclazz =dest.getClass();
//Object.class 为 stopClass 不反射的 Class
PropertyDescriptor[] ps=Introspector.getBeanInfo(dclazz,Object.class).getPropertyDescriptors();
for (PropertyDescriptor prop : ps) {
System.out.println(prop.getPropertyType());
if(prop.getPropertyType() == Class.class){ //Class 不反射
continue;
}else if(prop.getPropertyType().isPrimitive()){ //基本数据类型
prop.getWriteMethod().invoke(dest, sclazz.getMethod(prop.getReadMethod().getName()).invoke(src));
}else if(isWarpType(prop.getPropertyType().getName())){ //包装类型
prop.getWriteMethod().invoke(dest, sclazz.getMethod(prop.getReadMethod().getName()).invoke(src));
}else if(prop.getPropertyType() == List.class){ //List 集合类型
Object obj=sclazz.getMethod(prop.getReadMethod().getName()).invoke(src);
System.out.println("class:"+obj.getClass());
if(obj !=null){
List srcList=(List)obj;
Class listClazz=obj.getClass();
ParameterizedType pt= (ParameterizedType)prop.getReadMethod().getGenericReturnType();
Class type = (Class)pt.getActualTypeArguments()[0];
Object destList=listClazz.newInstance();
Method addMethod=listClazz.getMethod("add",Object.class);
Integer i=new Integer(1);
for (Object object : srcList) {
Object item=null;
if(isWarpType(type.getName())){
item=type.getConstructor((Class)object.getClass().getField("TYPE").get(object)).newInstance(object);
}else{
item=type.newInstance();
copyProperties(object,item);
}
addMethod.invoke(destList, item);
}
prop.getWriteMethod().invoke(dest, destList);
}
}else{//自定义类型 Class
Object d=prop.getPropertyType().newInstance();
copyProperties(sclazz.getMethod(prop.getReadMethod().getName()).invoke(src),d);
prop.getWriteMethod().invoke(dest, d);
}
}
}
/**
* 是否是包装类型
* @param typeName
* @return
*/
public static boolean isWarpType(String typeName){
String[] types = {"java.lang.Integer",
"java.lang.Double",
"java.lang.Float",
"java.lang.Long",
"java.lang.Short",
"java.lang.Byte",
"java.lang.Boolean",
"java.lang.Character",
"java.lang.String"};
return ArrayUtils.contains(types, typeName);
}
}
public class TestA {
private List<Integer> nums;
private Person p;
private Integer age;
private int big;
private List<Person> data;
}
public class TestB {
private List<Integer> nums;
private Integer age;
private People p;
private int big;
private List<People> data;
}
public class People {
private String name;
private int age;
List<Address> address;
}
public class Person {
private String name;
private int age;
List<Address> address;
}
public class Address {
private String addNo;
private String addName;
}
运行结果
- 大小: 26.7 KB