java:PropertyDescriptor+反射调用setter方法
1 前言
PropertyDescriptor文档如下:
https://docs.oracle.com/javase/7/docs/api/java/beans/PropertyDescriptor.html
参考文档的构造方法细节:
public PropertyDescriptor(String propertyName,
Class<?> beanClass)
throws IntrospectionException
Constructs a PropertyDescriptor for a property that follows the standard Java convention by having getFoo and setFoo accessor methods. Thus if the argument name is "fred", it will assume that the writer method is "setFred" and the reader method is "getFred" (or "isFred" for a boolean property). Note that the property name should start with a lower case character, which will be capitalized in the method names.
实体类中的变量名按照小驼峰的写法规范命名,setter方法命名规范即可。
2 使用
package com.xiaoxu.tool;
import com.google.common.collect.ImmutableMap;
import lombok.Data;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author xiaoxu
* @date 2022-03-24
* spring_boot:com.xiaoxu.tool.PropertyDescriptorTest
*/
public class PropertyDescriptorTest {
public static void main(String[] args) {
PropertyDescriptorTest propertyDescriptorTest = new PropertyDescriptorTest();
Map<String,Object> m = ImmutableMap.<String,Object>builder()
.put("num",1)
.put("number",3L)
.put("personName","xiaoxu")
.put("produceDate",new Date())
.build();
System.out.println(propertyDescriptorTest.test01(m, B.class));
System.out.println(propertyDescriptorTest.test01(new HashMap<>(), B.class));
}
public <T> T test01(Map<String,Object> dataMap, Class<T> clazz){
Field[] declaredFields = clazz.getDeclaredFields();
T obj = null;
try {
obj = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
String methodName;
for (Field declaredField : declaredFields) {
try {
methodName = declaredField.getName();
PropertyDescriptor p = new PropertyDescriptor(methodName,clazz);
Method writeMethod = p.getWriteMethod();
if(dataMap.get(methodName)!=null){
writeMethod.invoke(obj,dataMap.get(methodName));
}
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
return obj;
}
}
@Data
class B{
int num;
long number;
String personName;
Date produceDate;
}
结果如下:
B(num=1, number=3, personName=xiaoxu, produceDate=Thu Mar 24 13:56:36 CST 2022)
B(num=0, number=0, personName=null, produceDate=null)
3 简单封装
package com.xiaoxu.utils.descriptor;
import lombok.extern.slf4j.Slf4j;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author xiaoxu
* @date 2022-04-27
* spring_boot:com.xiaoxu.utils.descriptor.PropertyDescriptorUtil
*/
@Slf4j
public class PropertyDescriptorUtil {
public static <T> T writeDescriptor(T obj, Field field,Object value){
Class<?> clazz = obj.getClass();
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(),clazz);
Method writeMethod = propertyDescriptor.getWriteMethod();
writeMethod.invoke(obj,value);
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
log.info(String.format("调用%s方法失败,clazz调用set赋值失败,原因:%s",
Thread.currentThread().getStackTrace()[1].getMethodName(),
e.getMessage()));
}
return obj;
}
public static <T> Object readDescriptor(T obj,Field field){
Object read = null;
Class<?> clazz = obj.getClass();
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(),clazz);
Method writeMethod = propertyDescriptor.getReadMethod();
read = writeMethod.invoke(obj);
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
log.info(String.format("调用%s方法失败,clazz调用get取值失败,原因:%s",
Thread.currentThread().getStackTrace()[1].getMethodName(),
e.getMessage()));
}
return read;
}
}