通过反射实现对象不同命名格式互转及赋值:
项目开发过程中,内部使用驼峰,外部使用下划线,为方便开发,使用发射机制实现对象不同命名格式互转及赋值。
1、基本的反射机制使用
BeanUtil.copyProperties()是常见的用于对象属性拷贝的工具方法。它通常用于将一个对象的属性值复制到另一个对象中。
//使用`BeanUtil.copyProperties()`进行属性拷贝:
BeanUtils.copyProperties(source, target);
该方法会自动将源对象的属性值拷贝到目标对象中,前提是源对象和目标对象拥有相同的属性名和类型。如果属性名不一致,可以考虑使用@PropertyName
注解进行映射。
BeanUtils.copyProperties(source, target, "propertyName1", "propertyName2");
反射基本字段含义
1.1: 使用泛型 <T1, T2>,表示该方法可以处理不同类型的源对象和目标对象。
1.2: 通过 source.getClass().getDeclaredFields() 获取源对象的字段数组,通过 target.getClass().getDeclaredFields() 获取目标对象的字段数组。
1.3: 使用两个循环遍历源对象和目标对象的字段。
1.4: 使用两个循环遍历源对象和目标对象的字段。
1.5: 使用两个循环遍历源对象和目标对象的字段。
1.6: 使用两个循环遍历源对象和目标对象的字段。
1.7: 判断源对象和目标对象的字段名以及字段类型是否匹配。
sourceField.getName().equals(targetField.getName()) 判断字段名是否相同。
sourceField.getName().equals(targetField.getName()) 判断字段名是否相同。
1.8:设置可访问性为 true,以便在私有字段上进行操作:sourceField.setAccessible(true)、targetField.setAccessible(true)。
1.9: 使用 sourceField.get(source) 获取源对象字段的值。
1.10: 使用 targetField.set(target, value) 将源对象字段的值设置到目标对象字段上。
2、仿写:通过反射将 source(驼峰)实例中的字段值赋值给 destination(下划线)实例中的字段
Maven依赖
<!-- hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.7</version>
</dependency>
代码如下
package com.common.core.utils.reflect;
import cn.hutool.core.util.StrUtil;
import java.lang.reflect.Field;
/**
* CamelCase工具类
*/
public class CamelCaseUtils {
/**
* 将下划线方式命名的字符串转换为驼峰式
* @param camelCase
* @return
*/
private static String toCamelCase(String camelCase) {
return StrUtil.toCamelCase(camelCase);
}
/**
* 将驼峰式命名的字符串转换为下划线方式
* @param camelCase
* @return
*/
private static String toUnderlineCase(String camelCase) {
return StrUtil.toUnderlineCase(camelCase);
}
/**
* 通过反射将 source(驼峰)实例中的字段值赋值给 target(下划线)实例中的字段
* @param source
* @param target
*/
public static void assignFieldValues(Object source, Object target) {
Field[] sourceFields = source.getClass().getDeclaredFields();
for (Field sourceField : sourceFields) {
String sourceFieldName = sourceField.getName();
String targetFieldName = toUnderlineCase(sourceFieldName);
try {
if (sourceField.isAccessible() == false) {
sourceField.setAccessible(true); //使得字段在没有访问权限限制的情况下可以被访问
}
Field targetField = target.getClass().getDeclaredField(targetFieldName);
if (targetField.isAccessible() == false) {
targetField.setAccessible(true);
}
targetField.set(target, sourceField.get(source)); //赋值:将源对象字段的值设置到目标对象字段上
} catch (NoSuchFieldException | IllegalAccessException e) {
// Handle exception if the field does not exist or is not accessible
}
}
}
// Example
public static void main(String[] args) {
SourceObject source = new SourceObject();
source.setHumpToUnderlineField("value1");
source.setHumpToUnderlineFieldTwo(1);
DestinationObject destination = new DestinationObject();
//通过反射将 SourceObject 实例中的字段值赋值给 DestinationObject 实例中的字段
assignFieldValues(source, destination);
System.out.println(destination.getHump_to_underline_field());
System.out.println(destination.getHump_to_underline_field_two());
}
}
//两个驼峰命名的字段
class SourceObject {
private String humpToUnderlineField;
private Integer humpToUnderlineFieldTwo;
// getters and setters
public String getHumpToUnderlineField() {
return humpToUnderlineField;
}
public void setHumpToUnderlineField(String humpToUnderlineField) {
this.humpToUnderlineField = humpToUnderlineField;
}
public Integer getHumpToUnderlineFieldTwo() {
return humpToUnderlineFieldTwo;
}
public void setHumpToUnderlineFieldTwo(Integer humpToUnderlineFieldTwo) {
this.humpToUnderlineFieldTwo = humpToUnderlineFieldTwo;
}
}
//两个下划线命名的字段
class DestinationObject {
private String hump_to_underline_field;
private Integer hump_to_underline_field_two;
// getters and setters
public String getHump_to_underline_field() {
return hump_to_underline_field;
}
public void setHump_to_underline_field(String hump_to_underline_field) {
this.hump_to_underline_field = hump_to_underline_field;
}
public Integer getHump_to_underline_field_two() {
return hump_to_underline_field_two;
}
public void setHump_to_underline_field_two(Integer hump_to_underline_field_two) {
this.hump_to_underline_field_two = hump_to_underline_field_two;
}
}