java 反射List


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) {
Class sclazz =src.getClass();
Class dclazz =dest.getClass();

//Object.class 为 stopClass 不反射的 Class
PropertyDescriptor[] ps= new PropertyDescriptor[0];
try {
ps = Introspector.getBeanInfo(dclazz,Object.class).getPropertyDescriptors();
for (PropertyDescriptor prop : ps) {
Object srcVal=null;
try {

if(prop.getPropertyType() == Class.class){ //Class 不反射
continue;
}else if(prop.getPropertyType().isPrimitive()){ //基本数据类型
srcVal=getValue(src,sclazz,prop.getReadMethod().getName());
if(srcVal !=null){
prop.getWriteMethod().invoke(dest,srcVal);
}
}else if(isWrapType(prop.getPropertyType().getName())){ //包装类型
srcVal=getValue(src,sclazz,prop.getReadMethod().getName());
if(srcVal !=null){
if(prop.getPropertyType().getName().equals("java.lang.Double")){ //Double
BigDecimal newVal=new BigDecimal(srcVal.toString());
NumberUtil.setScale(newVal,2);
prop.getWriteMethod().invoke(dest, NumberUtil.setScale(newVal,2));
}else {
prop.getWriteMethod().invoke(dest, srcVal);
}
}
}else if(prop.getPropertyType().getInterfaces() !=null && prop.getPropertyType().getInterfaces().length > 0
&& prop.getPropertyType().getInterfaces()[0] == Collection.class){ //集合类型
Object obj=getValue(src,sclazz,prop.getReadMethod().getName());
if(obj !=null && obj instanceof Collection){
Collection srcList=(Collection)obj;
Class collClazz=obj.getClass();
ParameterizedType pt= (ParameterizedType)prop.getReadMethod().getGenericReturnType();
Class type = (Class)pt.getActualTypeArguments()[0];
Object destList=collClazz.newInstance();
Method addMethod=collClazz.getMethod("add",Object.class);
if(addMethod !=null) {
for (Object object : srcList) {
Object item = null;
if (isWrapType(type.getName())) {
Class parmClazz=String.class;
try{
parmClazz = (Class)object.getClass().getField("TYPE").get(object);
}catch (Exception e) {
}
item = type.getConstructor(parmClazz).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(getValue(src,sclazz,prop.getReadMethod().getName()),d);
prop.getWriteMethod().invoke(dest, d);
}
} catch (Exception e) {
log.info("copyProperties ERROR:"+ ThrowableUtil.getStackTrace(e));
}
}
} catch (Exception e) {
log.info("copyProperties ERROR:"+ ThrowableUtil.getStackTrace(e));
}
}

/**
* 获取属性值
* @param obj
* @param clazz
* @param methodName
* @return
*/
public static Object getValue(Object obj,Class clazz,String methodName){
Object value=null;
try {
Method method=clazz.getMethod(methodName);
if(method !=null){
value=method.invoke(obj);
}
} catch (Exception e) {
log.info("copyProperties.getValue method: "+methodName+",ERROR"+ ThrowableUtil.getStackTrace(e));
}
return value;
}

/**
* 是否是包装类型
* @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;
}


运行结果
[img]http://dl2.iteye.com/upload/attachment/0123/1492/be910c7f-4d2d-3162-bd0d-e383b5ec887b.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值