java通过反射获取对象中list属性值
反射的定义、机制等等概念⽹上有很多,这⾥就不做过多的赘述了,本⽂主要介绍通过反射机制获取对象中⼀个list属性的值,并将值赋值到另⼀个对象中。
上代码:
1.实体类
package;
import Data;
import NoArgsConstructor;
@Data
@NoArgsConstructor
public class Women {
private Integer id;
private String name;
private String address;
public Women(Integer id, String name, String address){
java类的概念this.id = id;
this.name = name;
this.address = address;
}
}
package;
import Data;
@Data
public class User {
private Integer id;
private String name;
private String address;
public User(){
}
public User(Integer id, String name, String address){
this.id = id;
this.name = name;
this.address = address;
}
}
说明:两个实体类的字段要⼀致才能进⾏下⾯的操作
2.公共基类
public interface BaseResponse {
}
3.实现类
package;
import Data;
import List;
@Data
public class TargeResponse  implements BaseResponse{ private int pageNumber;
private int pageSize;
private List<User> itmes;
}
package;
import Data;
import List;
@Data
public class WomenResponse implements BaseResponse{ private int pageNumber;
private int pageSize;
private List<Women> items;
}
4.响应基类
package;
import Data;
@Data
public class BaseResult<T>{
private boolean success;
private int code;
private T data;
}
测试类
package;
import JSON;
import*;
import Test;
import Field;
import ArrayList;
import Arrays;
import List;
import Collectors;
public class ReflexTest {
@Test
public void reflexTest()throws Exception {
WomenResponse womenResponse =buildSource();
BaseResult<BaseResponse> sourceResult =new BaseResult<>();
sourceResult.setCode(200);
sourceResult.setSuccess(true);
sourceResult.setData(womenResponse);
System.out.println("source:"+JSONString(sourceResult));
//将ReflexResponse中list中的对象值赋值给TargeResponse
BaseResult<TargeResponse> tar =buildTargeByReflex(sourceResult,Women.class);
System.out.println("targe:"+ JSONString(tar));
}
//这⾥使⽤泛型适⽤性更⼴
private<T,R> BaseResult<TargeResponse>buildTargeByReflex(BaseResult<T> baseResult, Class<R> targeClass)throws Exception {        TargeResponse targeResponse =new TargeResponse();
if(null!= baseResult){
List<User> list =new ArrayList<>();
//获取baseResult中data数据即:BaseResponse
T data = Data();
Class<?> aClass = Class();
//BaseResponse中items属性
Field items = DeclaredField("items");
items.setAccessible(true);
Object obj = (data);
//将object转成list
List<R> tarList =objCastList(obj,targeClass);
//遍历list
for(R r : tarList){
Class<?> rClass = r.getClass();
Field[] rFileds = DeclaredFields();
//new ⼀个⽬标对象
User user =new User();
Class<?extends User> userClass = Class();
//取⽬标对象所有属性名称
List<String> userFiledNames = Arrays.DeclaredFields()).map(f -> f.getName()).List());
for(Field rFiled : rFileds){
//取值
rFiled.setAccessible(true);
String name = Name();
Object vaule = (r);
//赋值
ains(name)){
Field targeField = DeclaredField(name);
targeField.setAccessible(true);
targeField.set(user,vaule);
}
}
list.add(user);
}
targeResponse.setItmes(list);
}
BaseResult<TargeResponse> result =new BaseResult<>();
result.setData(targeResponse);
return result;
}
private<R> List<R>objCastList(Object obj, Class<R> clazz){
List<R> list =new ArrayList<>();
if(obj instanceof List<?>){
for(Object o :(List<?>)obj){
list.add(clazz.cast(o));
}
}
return list;
}
private WomenResponse buildSource(){
Women women =new Women(1,"zhangsan","北海道");        List<Women> list =new ArrayList<>();
list.add(women);
WomenResponse source =new WomenResponse();        source.setItems(list);
return source;
}
}
测试结果:
在这⾥插⼊图⽚描述

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。