package com.example.demo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
/**
- @author xiefenghong
- @date 2022年02月14日 14:50
- @describe 反射属性赋值
*/
public class ReflectTest {
@Test
public void run(){
Person person = ClassInstanceFactory.createInstance(Person.class, "name:小明|id:1|cloth:{name:大衣|category:{name:羊毛衫}}|sex:male");
System.out.println(person);
}
}
/*
- @Author xiefenghong
- @Description 实例工厂,用于实例化对象
- @Date 18:20 2022/2/14
- @Param
- @return
**/
class ClassInstanceFactory{
private ClassInstanceFactory(){};
public static <T>T createInstance(Class<T> obj,String value){
T instance =null;
try {
instance = obj.getDeclaredConstructor().newInstance();
InstallBeanUtils.installBeanValue(instance,value);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return instance;
}
}
@Data
class Person{
private String name;
private Integer id;
private Cloth cloth;
private String sex;
}
@Data
class Cloth{
private String name;
private Category category;
}
@Data
class Category{
private String name;
}
/**
- @Author xiefenghong
- @Description 封装属性工具类
- @Date 18:20 2022/2/14
- @Param
- @return
**/
class InstallBeanUtils{
/*
* @Author xiefenghong
* @Description 属性封装
* @Date 18:20 2022/2/14
* @Param [obj, val]
* @return void
**/
public static void installBeanValue(Object obj,String val) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if (StringUtils.isBlank(val)){
return;
}
if(Objects.isNull(obj)){
throw new NullPointerException("实例对象为null");
}
String valParent=val;
String valChild=null;
if(val.contains("{")){
valParent=val.substring(0,val.indexOf("{"))+"?"+val.substring(val.lastIndexOf("}")+1);
valChild=val.substring(val.indexOf("{")+1,val.lastIndexOf("}"));
}
Class<?> aClass = obj.getClass();
String[] valuePairs = valParent.split("\\|");
for (String valuePair : valuePairs){
try {
String[] values = valuePair.split(":");
Field field = aClass.getDeclaredField(values[0]);
if(valuePair.contains("?")){
Method method = aClass.getDeclaredMethod(methodNameJoint("get",field));
Object childObj = method.invoke(obj);
if (Objects.isNull(childObj)){
Method setMethod = aClass.getDeclaredMethod(methodNameJoint("set", field), field.getType());
setMethod.invoke(obj,field.getType().getConstructor().newInstance());
childObj= method.invoke(obj);
}
try {
installBeanValue(childObj,valChild);
}catch (Exception e){
System.out.println("子类赋值失败");
}
continue;
}
Method method = aClass.getDeclaredMethod(methodNameJoint("set",field), field.getType());
Object value= methodTypeValue(field, values[1]);
method.invoke(obj,value);
}catch (Exception e){
System.out.println(valuePair+"赋值失败");
}
}
}
/**
* @Author xiefenghong
* @Description 属性的值类型转换
* @Date 18:23 2022/2/14
* @Param [field, val]
* @return java.lang.Object
**/
protected static Object methodTypeValue(Field field,String val){
Object obj=null;
if("java.lang.String".equals(field.getType().getName())){
return val;
}else if ("java.lang.Integer".equals(field.getType().getName())){
obj=Integer.valueOf(val);
}
return obj;
}
/*
* @Author xiefenghong
* @Description 获取get,set方法
* @Date 18:23 2022/2/14
* @Param [prefix, field]
* @return java.lang.String
**/
protected static String methodNameJoint(String prefix,Field field){
String name = field.getName().substring(0, 1).toUpperCase();
if (field.getName().length()==1){
return prefix+name;
}
return prefix+name+field.getName().substring(1);
}
}