java 反射创建list_java 反射List

本文演示了如何使用Java反射API创建并操作List集合,包括添加元素和获取泛型类型。示例代码展示了如何创建ArrayList和LinkedList,以及向它们添加不同类型的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 nums=new ArrayList();

nums.add(1);

Person p =new Person();

p.setAge(18);

p.setName("luck");

List list=new LinkedList();

list.add(p);

List

address = new ArrayList
();

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:"+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 nums;

private Person p;

private Integer age;

private int big;

private List data;

}

public class TestB {

private List nums;

private Integer age;

private People p;

private int big;

private List data;

}

public class People {

private String name;

private int age;

List

address;

}

public class Person {

private String name;

private int age;

List

address;

}

public class Address {

private String addNo;

private String addName;

}

运行结果

20b81a4ecc55b294c6171959e1054504.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值