|
@@ -1,38 +1,54 @@
|
|
|
package cn.hutool.core.bean.copier;
|
|
package cn.hutool.core.bean.copier;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
|
|
+
|
|
|
import java.io.Serializable;
|
|
import java.io.Serializable;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
-import cn.hutool.core.map.MapUtil;
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 属性拷贝选项<br>
|
|
* 属性拷贝选项<br>
|
|
|
* 包括:<br>
|
|
* 包括:<br>
|
|
|
* 1、限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性,例如一个类我只想复制其父类的一些属性,就可以将editable设置为父类<br>
|
|
* 1、限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性,例如一个类我只想复制其父类的一些属性,就可以将editable设置为父类<br>
|
|
|
* 2、是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null<br>
|
|
* 2、是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null<br>
|
|
|
* 3、忽略的属性列表,设置一个属性列表,不拷贝这些属性值<br>
|
|
* 3、忽略的属性列表,设置一个属性列表,不拷贝这些属性值<br>
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @author Looly
|
|
* @author Looly
|
|
|
*/
|
|
*/
|
|
|
-public class CopyOptions implements Serializable{
|
|
|
|
|
|
|
+public class CopyOptions implements Serializable {
|
|
|
private static final long serialVersionUID = 1L;
|
|
private static final long serialVersionUID = 1L;
|
|
|
-
|
|
|
|
|
- /** 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性,例如一个类我只想复制其父类的一些属性,就可以将editable设置为父类 */
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性,例如一个类我只想复制其父类的一些属性,就可以将editable设置为父类
|
|
|
|
|
+ */
|
|
|
protected Class<?> editable;
|
|
protected Class<?> editable;
|
|
|
- /** 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
|
|
|
+ */
|
|
|
protected boolean ignoreNullValue;
|
|
protected boolean ignoreNullValue;
|
|
|
- /** 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值 */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
|
|
|
|
+ */
|
|
|
protected String[] ignoreProperties;
|
|
protected String[] ignoreProperties;
|
|
|
- /** 是否忽略字段注入错误 */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 是否忽略字段注入错误
|
|
|
|
|
+ */
|
|
|
protected boolean ignoreError;
|
|
protected boolean ignoreError;
|
|
|
- /** 是否忽略字段大小写 */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 是否忽略字段大小写
|
|
|
|
|
+ */
|
|
|
protected boolean ignoreCase;
|
|
protected boolean ignoreCase;
|
|
|
- /** 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用 */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
|
|
|
|
|
+ */
|
|
|
protected Map<String, String> fieldMapping;
|
|
protected Map<String, String> fieldMapping;
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
|
|
|
|
+ */
|
|
|
|
|
+ private boolean transientSupport = true;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 创建拷贝选项
|
|
* 创建拷贝选项
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @return 拷贝选项
|
|
* @return 拷贝选项
|
|
|
*/
|
|
*/
|
|
|
public static CopyOptions create() {
|
|
public static CopyOptions create() {
|
|
@@ -41,9 +57,9 @@ public class CopyOptions implements Serializable{
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 创建拷贝选项
|
|
* 创建拷贝选项
|
|
|
- *
|
|
|
|
|
- * @param editable 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
|
|
|
|
|
- * @param ignoreNullValue 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param editable 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
|
|
|
|
|
+ * @param ignoreNullValue 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
|
* @param ignoreProperties 忽略的属性列表,设置一个属性列表,不拷贝这些属性值
|
|
* @param ignoreProperties 忽略的属性列表,设置一个属性列表,不拷贝这些属性值
|
|
|
* @return 拷贝选项
|
|
* @return 拷贝选项
|
|
|
*/
|
|
*/
|
|
@@ -59,9 +75,9 @@ public class CopyOptions implements Serializable{
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 构造拷贝选项
|
|
* 构造拷贝选项
|
|
|
- *
|
|
|
|
|
- * @param editable 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
|
|
|
|
|
- * @param ignoreNullValue 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param editable 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
|
|
|
|
|
+ * @param ignoreNullValue 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
|
* @param ignoreProperties 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
|
* @param ignoreProperties 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
|
|
*/
|
|
*/
|
|
|
public CopyOptions(Class<?> editable, boolean ignoreNullValue, String... ignoreProperties) {
|
|
public CopyOptions(Class<?> editable, boolean ignoreNullValue, String... ignoreProperties) {
|
|
@@ -72,7 +88,7 @@ public class CopyOptions implements Serializable{
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
|
|
* 设置限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param editable 限制的类或接口
|
|
* @param editable 限制的类或接口
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
*/
|
|
*/
|
|
@@ -83,7 +99,7 @@ public class CopyOptions implements Serializable{
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
* 设置是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param ignoreNullVall 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
* @param ignoreNullVall 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
*/
|
|
*/
|
|
@@ -91,10 +107,10 @@ public class CopyOptions implements Serializable{
|
|
|
this.ignoreNullValue = ignoreNullVall;
|
|
this.ignoreNullValue = ignoreNullVall;
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 设置忽略空值,当源对象的值为null时,忽略而不注入此值
|
|
* 设置忽略空值,当源对象的值为null时,忽略而不注入此值
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
* @since 4.5.7
|
|
* @since 4.5.7
|
|
|
*/
|
|
*/
|
|
@@ -104,7 +120,7 @@ public class CopyOptions implements Serializable{
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
|
* 设置忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param ignoreProperties 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
|
* @param ignoreProperties 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
*/
|
|
*/
|
|
@@ -115,7 +131,7 @@ public class CopyOptions implements Serializable{
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置是否忽略字段的注入错误
|
|
* 设置是否忽略字段的注入错误
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param ignoreError 是否忽略注入错误
|
|
* @param ignoreError 是否忽略注入错误
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
*/
|
|
*/
|
|
@@ -123,20 +139,20 @@ public class CopyOptions implements Serializable{
|
|
|
this.ignoreError = ignoreError;
|
|
this.ignoreError = ignoreError;
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 设置忽略字段的注入错误
|
|
* 设置忽略字段的注入错误
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
* @since 4.5.7
|
|
* @since 4.5.7
|
|
|
*/
|
|
*/
|
|
|
public CopyOptions ignoreError() {
|
|
public CopyOptions ignoreError() {
|
|
|
return setIgnoreError(true);
|
|
return setIgnoreError(true);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 设置是否忽略字段的大小写
|
|
* 设置是否忽略字段的大小写
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param ignoreCase 是否忽略大小写
|
|
* @param ignoreCase 是否忽略大小写
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
*/
|
|
*/
|
|
@@ -144,10 +160,10 @@ public class CopyOptions implements Serializable{
|
|
|
this.ignoreCase = ignoreCase;
|
|
this.ignoreCase = ignoreCase;
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 设置忽略字段的大小写
|
|
* 设置忽略字段的大小写
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
* @since 4.5.7
|
|
* @since 4.5.7
|
|
|
*/
|
|
*/
|
|
@@ -157,7 +173,7 @@ public class CopyOptions implements Serializable{
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
|
|
* 设置拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param fieldMapping 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
|
|
* @param fieldMapping 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
|
|
|
* @return CopyOptions
|
|
* @return CopyOptions
|
|
|
*/
|
|
*/
|
|
@@ -165,13 +181,36 @@ public class CopyOptions implements Serializable{
|
|
|
this.fieldMapping = fieldMapping;
|
|
this.fieldMapping = fieldMapping;
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 是否支持
|
|
|
|
|
+ * @since 5.4.2
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean isTransientSupport() {
|
|
|
|
|
+ return this.transientSupport;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 设置是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param transientSupport 是否支持
|
|
|
|
|
+ * @return this
|
|
|
|
|
+ * @since 5.4.2
|
|
|
|
|
+ */
|
|
|
|
|
+ public CopyOptions setTransientSupport(boolean transientSupport) {
|
|
|
|
|
+ this.transientSupport = transientSupport;
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取反转之后的映射
|
|
* 获取反转之后的映射
|
|
|
|
|
+ *
|
|
|
* @return 反转映射
|
|
* @return 反转映射
|
|
|
* @since 4.1.10
|
|
* @since 4.1.10
|
|
|
*/
|
|
*/
|
|
|
protected Map<String, String> getReversedMapping() {
|
|
protected Map<String, String> getReversedMapping() {
|
|
|
- return (null != this.fieldMapping) ? MapUtil.reverse(this.fieldMapping) : null;
|
|
|
|
|
|
|
+ return (null != this.fieldMapping) ? MapUtil.reverse(this.fieldMapping) : null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|