反射的定义、机制等等概念网上有很多,这里就不做过多的赘述了,本文主要介绍通过反射机制获取对象中一个list属性的值,并将值赋值到另一个对象中。
上代码:
1.实体类
package com.study.reflex;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class Women {
private Integer id;
private String name;
private String address;
public Women(Integer id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
}
package com.study.reflex;
import lombok.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 com.study.reflex;
import lombok.Data;
import java.util.List;
@Data
public class TargeResponse implements BaseResponse{
private int pageNumber;
private int pageSize;
private List<User> itmes;
}
package com.study.reflex;
import lombok.Data;
import java.util.List;
@Data
public class WomenResponse implements BaseResponse{
private int pageNumber;
private int pageSize;
private List<Women> items;
}
4.响应基类
package com.study.reflex;
import lombok.Data;
@Data
public class BaseResult<T> {
private boolean success;
private int code;
private T data;
}
测试类
package com.lubang.test;
import com.alibaba.fastjson.JSON;
import com.study.reflex.*;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.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:"+JSON.toJSONString(sourceResult));
//将ReflexResponse中list中的对象值赋值给TargeResponse
BaseResult<TargeResponse> tar = buildTargeByReflex(sourceResult,Women.class);
System.out.println("targe:"+ JSON.toJSONString(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 = baseResult.getData();
Class<?> aClass = data.getClass();
//BaseResponse中items属性
Field items = aClass.getDeclaredField("items");
items.setAccessible(true);
Object obj = items.get(data);
//将object转成list
List<R> tarList = objCastList(obj,targeClass);
//遍历list
for (R r : tarList) {
Class<?> rClass = r.getClass();
Field[] rFileds = rClass.getDeclaredFields();
//new 一个目标对象
User user = new User();
Class<? extends User> userClass = user.getClass();
//取目标对象所有属性名称
List<String> userFiledNames = Arrays.stream(userClass.getDeclaredFields()).map(f -> f.getName()).collect(Collectors.toList());
for (Field rFiled : rFileds) {
//取值
rFiled.setAccessible(true);
String name = rFiled.getName();
Object vaule = rFiled.get(r);
//赋值
if(userFiledNames.contains(name)){
Field targeField = userClass.getDeclaredField(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_41999004原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。