Skip to content

Commit ee6c22f

Browse files
committed
IGNORE_ALL_NULL_SOURCE_VALUE for v1.0.7
1 parent 2471249 commit ee6c22f

File tree

10 files changed

+196
-6
lines changed

10 files changed

+196
-6
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This BeanUtils library is a Java bean copy utility with powerful functionality a
88
<dependency>
99
<groupId>com.github.yangtu222</groupId>
1010
<artifactId>BeanUtils</artifactId>
11-
<version>1.0.6</version>
11+
<version>1.0.7</version>
1212
</dependency>
1313
~~~
1414

@@ -26,6 +26,7 @@ This BeanUtils library is a Java bean copy utility with powerful functionality a
2626
* easy debug by dump the property mappings, and can be disabled using BeanCopyConfig. (version 1.0.2 )
2727
* support copy with Java Enum <=> String (v1.0.4, thanks TuWei1992)
2828
* support copy from JavaBean to String (v1.0.4, thanks TuWei1992, using Object.toString() )
29+
* support one copy feature (IGNORE_ALL_NULL_SOURCE_VALUE) (v1.0.7, thanks sj853)
2930

3031
## A full Sample:
3132

@@ -153,9 +154,13 @@ Specify the source object class.
153154
#### features
154155
Specify the copy features. The features are:
155156
* IGNORE_PRIMITIVE_NULL_SOURCE_VALUE
156-
Ignore copy object type null to primitive type. e.g. Copy null (Integer type) to int.
157+
Ignore copying source null property to target primitive type property. e.g. Copy null (Integer type) to int.
157158
By default this feature is disabled, so we can debug where the pointer is null by thrown exception.
158159

160+
* IGNORE_ALL_NULL_SOURCE_VALUE
161+
Ignore copying source null property to target property.
162+
By default this feature is disabled.
163+
159164
* IGNORE_ENUM_CONVERT_EXCEPTION
160165
Ignore exceptions when converting from String to Enum when call Enum.valueOf(). This will happens when
161166
converting an invalid String value to Enum, and null will be set in this situation.

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.yangtu222</groupId>
66
<artifactId>BeanUtils</artifactId>
77
<name>BeanUtils</name>
8-
<version>1.0.6</version>
8+
<version>1.0.7</version>
99
<description>BeanUtils library is a Java bean copy utility with powerful functionality and high performance.</description>
1010
<url>https://github.com/yangtu222/BeanUtils</url>
1111

@@ -27,7 +27,7 @@
2727
<connection>scm:git:https://github.com/yangtu222/BeanUtils.git</connection>
2828
<developerConnection>scm:git:https://github.com/yangtu222/BeanUtils.git</developerConnection>
2929
<url>https://github.com/yangtu222/BeanUtils.git</url>
30-
<tag>v1.0.2</tag>
30+
<tag>v1.0.7</tag>
3131
</scm>
3232

3333
<profiles>

src/main/java/com/tuyang/beanutils/annotation/CopyFeature.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public enum CopyFeature {
44
IGNORE_PRIMITIVE_NULL_SOURCE_VALUE,
5+
IGNORE_ALL_NULL_SOURCE_VALUE,
56
IGNORE_ENUM_CONVERT_EXCEPTION,
67
ENABLE_JAVA_BEAN_TO_STRING,
78
}

src/main/java/com/tuyang/beanutils/internal/dump/BeanCopyDump.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.Collection;
77
import java.util.List;
88

9+
import com.tuyang.beanutils.annotation.BeanCopySource;
10+
import com.tuyang.beanutils.annotation.CopyFeature;
911
import com.tuyang.beanutils.config.BeanCopyConfig;
1012
import com.tuyang.beanutils.config.BeanCopyConfig.DumpOption;
1113
import com.tuyang.beanutils.internal.cache.BeanCopyCache;
@@ -105,7 +107,24 @@ private static void dumpPropertyMappingInternal(Class<?> sourceClass, Class<?> t
105107
logger.info("---------------------------------------------------------------------------------------------");
106108
logger.info("From: [" + sourceClass.getSimpleName() + "] To: [" + targetClass.getSimpleName() + "] Option: [" + optionClass.getSimpleName() + "]");
107109
logger.info("---------------------------------------------------------------------------------------------");
108-
110+
CopyFeature[] features = null;
111+
if( optionClass != null && optionClass.isAnnotationPresent(BeanCopySource.class)) {
112+
BeanCopySource copyAnnotation = optionClass.getAnnotation(BeanCopySource.class);
113+
features = copyAnnotation.features();
114+
} else if( targetClass.isAnnotationPresent(BeanCopySource.class)) {
115+
BeanCopySource copyAnnotation = targetClass.getAnnotation(BeanCopySource.class);
116+
features = copyAnnotation.features();
117+
}
118+
if( features == null ) {
119+
logger.info("CopyFeature: (NONE)");
120+
} else {
121+
logger.info("CopyFeature:");
122+
for( CopyFeature f : features) {
123+
logger.info(" " + f.toString());
124+
}
125+
}
126+
logger.info("---------------------------------------------------------------------------------------------");
127+
109128
if( itemList == null )
110129
itemList = BeanCopyCache.buildBeanCopyPropertyItem(sourceClass, targetClass, optionClass);
111130

src/main/java/com/tuyang/beanutils/internal/javassist/JavassistBeanCopyFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,22 @@ else if( item.convertorClass != null ) {
178178
+ writeType.getName() +".class ) );\n");
179179
} else {
180180
if( writeType.isPrimitive() && !readType.isPrimitive() ) {
181-
boolean ignoreInvoke = findFeature(features, CopyFeature.IGNORE_PRIMITIVE_NULL_SOURCE_VALUE );
181+
boolean ignoreInvoke = findFeature(features, CopyFeature.IGNORE_ALL_NULL_SOURCE_VALUE );
182+
if( !ignoreInvoke )
183+
ignoreInvoke = findFeature(features, CopyFeature.IGNORE_PRIMITIVE_NULL_SOURCE_VALUE );
182184
if( ignoreInvoke ) sb.append("if ( "+sourceMethod +" != null ) { \n");
183185
sb.append("target." + item.writeMethod.getName() +"( "+sourceMethod + "." + writeType.toString() +"Value() );\n");
184186
if( ignoreInvoke ) sb.append("}\n");
185187
} else if( !writeType.isPrimitive() && readType.isPrimitive() ) {
186188
sb.append("target." + item.writeMethod.getName() +"( "+ getPrimitiveName(readType) +".valueOf(" + sourceMethod + " ) );\n");
189+
} else if( !readType.isPrimitive() ) {
190+
boolean ignoreInvoke = findFeature(features, CopyFeature.IGNORE_ALL_NULL_SOURCE_VALUE );
191+
if( ignoreInvoke ) sb.append("if ( "+sourceMethod +" != null ) { \n");
192+
sb.append("target." + item.writeMethod.getName() +"( "+sourceMethod + " );\n");
193+
if( ignoreInvoke ) sb.append("}\n");
187194
} else {
188195
sb.append("target." + item.writeMethod.getName() +"( "+sourceMethod + " );\n");
196+
189197
}
190198
}
191199

src/main/java/com/tuyang/beanutils/internal/reflect/ReflectBeanCopy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ else if( item.convertorClass != null ) {
130130
if( targetPropertyType.isPrimitive() && sourceData == null ) {
131131
ignoreInvoke = findFeature( CopyFeature.IGNORE_PRIMITIVE_NULL_SOURCE_VALUE );
132132
}
133+
if( !ignoreInvoke && sourceData == null) {
134+
ignoreInvoke = findFeature( CopyFeature.IGNORE_ALL_NULL_SOURCE_VALUE );
135+
}
133136
if( !ignoreInvoke ) {
134137
item.writeMethod.invoke(targetObject, sourceData );
135138
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.tuyang.test.testIgnoreAll;
2+
3+
public class FromBean {
4+
5+
private int beanInt;
6+
private long beanLong;
7+
8+
private String beanString;
9+
10+
private FromBean2 bean2;
11+
12+
public int getBeanInt() {
13+
return beanInt;
14+
}
15+
16+
public void setBeanInt(int beanInt) {
17+
this.beanInt = beanInt;
18+
}
19+
20+
public long getBeanLong() {
21+
return beanLong;
22+
}
23+
24+
public void setBeanLong(long beanLong) {
25+
this.beanLong = beanLong;
26+
}
27+
28+
public String getBeanString() {
29+
return beanString;
30+
}
31+
32+
public void setBeanString(String beanString) {
33+
this.beanString = beanString;
34+
}
35+
36+
public FromBean2 getBean2() {
37+
return bean2;
38+
}
39+
40+
public void setBean2(FromBean2 bean2) {
41+
this.bean2 = bean2;
42+
}
43+
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tuyang.test.testIgnoreAll;
2+
3+
public class FromBean2 {
4+
5+
private Float beanFloat;
6+
private String beanString;
7+
8+
public Float getBeanFloat() {
9+
return beanFloat;
10+
}
11+
public void setBeanFloat(Float beanFloat) {
12+
this.beanFloat = beanFloat;
13+
}
14+
public String getBeanString() {
15+
return beanString;
16+
}
17+
public void setBeanString(String beanString) {
18+
this.beanString = beanString;
19+
}
20+
21+
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.tuyang.test.testIgnoreAll;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.Test;
9+
10+
import com.tuyang.beanutils.BeanCopyUtils;
11+
12+
public class TestIgnoreAll {
13+
14+
private FromBean getFromBean() {
15+
FromBean fromBean = new FromBean();
16+
fromBean.setBeanInt(100);
17+
fromBean.setBeanLong(200);
18+
fromBean.setBeanString(null);
19+
20+
FromBean2 bean2 = new FromBean2();
21+
bean2.setBeanFloat(10.4f);
22+
bean2.setBeanString(null);
23+
24+
fromBean.setBean2(bean2);
25+
26+
return fromBean;
27+
}
28+
29+
@Test
30+
public void testIgnoreAll() {
31+
FromBean fromBean = getFromBean();
32+
ToBean toBean = new ToBean();
33+
toBean.setBeanString("Not Empty");
34+
toBean.setBean2String("Not Empty");
35+
BeanCopyUtils.copyBean(fromBean, toBean);
36+
assertEquals( toBean.getBeanString(), "Not Empty" );
37+
assertEquals( toBean.getBean2String(), "Not Empty" );
38+
}
39+
}
40+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.tuyang.test.testIgnoreAll;
2+
3+
import com.tuyang.beanutils.annotation.BeanCopySource;
4+
import com.tuyang.beanutils.annotation.CopyProperty;
5+
import com.tuyang.beanutils.annotation.CopyFeature;
6+
7+
@BeanCopySource(source=FromBean.class, features={CopyFeature.IGNORE_ALL_NULL_SOURCE_VALUE})
8+
public class ToBean {
9+
10+
private Integer beanInt;
11+
private long beanLong;
12+
private String beanString;
13+
14+
@CopyProperty(property="bean2.beanString")
15+
private String bean2String;
16+
17+
public Integer getBeanInt() {
18+
return beanInt;
19+
}
20+
21+
public void setBeanInt(Integer beanInt) {
22+
this.beanInt = beanInt;
23+
}
24+
25+
public long getBeanLong() {
26+
return beanLong;
27+
}
28+
29+
public void setBeanLong(long beanLong) {
30+
this.beanLong = beanLong;
31+
}
32+
33+
public String getBeanString() {
34+
return beanString;
35+
}
36+
37+
public void setBeanString(String beanString) {
38+
this.beanString = beanString;
39+
}
40+
41+
public String getBean2String() {
42+
return bean2String;
43+
}
44+
45+
public void setBean2String(String bean2String) {
46+
this.bean2String = bean2String;
47+
}
48+
}

0 commit comments

Comments
 (0)