Skip to content

Commit b6f86e5

Browse files
rajat-gargrajatgarg
and
rajatgarg
authored
[BAEL-9175] Add various approaches for copying specific fields (#18430)
Co-authored-by: rajatgarg <[email protected]>
1 parent 0d82bc1 commit b6f86e5

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.copyproperties;
2+
3+
import org.springframework.beans.BeanUtils;
4+
5+
import java.beans.PropertyDescriptor;
6+
import java.util.Arrays;
7+
import java.util.Set;
8+
9+
public class BeanUtilsCopyProperties {
10+
public static void copySelectedPropertiesUsingCustomWrapper(Object source, Object target, Set<String> props) {
11+
String[] excludedProperties = Arrays.stream(BeanUtils.getPropertyDescriptors(source.getClass()))
12+
.map(PropertyDescriptor::getName)
13+
.filter(name -> !props.contains(name))
14+
.toArray(String[]::new);
15+
16+
BeanUtils.copyProperties(source, target, excludedProperties);
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.copyproperties;
2+
3+
import org.junit.Test;
4+
import org.springframework.beans.BeanUtils;
5+
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertNull;
11+
12+
public class BeanUtilsCopyPropertiesUnitTest {
13+
14+
@Test
15+
public void givenObjects_whenUsingIgnoreProperties_thenCopyProperties() {
16+
SourceBean sourceBean = new SourceBean("Peter", 30, "LA");
17+
TargetBean targetBean = new TargetBean();
18+
19+
BeanUtils.copyProperties(sourceBean, targetBean, "address");
20+
assertEquals(targetBean.getName(), sourceBean.getName());
21+
assertEquals(targetBean.getAge(), sourceBean.getAge());
22+
assertNull(targetBean.getAddress());
23+
}
24+
25+
@Test
26+
public void givenObjects_whenUsingIntermediateObject_thenCopyProperties() {
27+
SourceBean sourceBean = new SourceBean("Peter", 30, "LA");
28+
TempDTO tempDTO = new TempDTO();
29+
BeanUtils.copyProperties(sourceBean, tempDTO);
30+
31+
TargetBean targetBean = new TargetBean();
32+
BeanUtils.copyProperties(tempDTO, targetBean);
33+
assertEquals(targetBean.getName(), sourceBean.getName());
34+
assertEquals(targetBean.getAge(), sourceBean.getAge());
35+
assertNull(targetBean.getAddress());
36+
}
37+
38+
@Test
39+
public void givenObjects_whenUsingCustomWrapper_thenCopyProperties() {
40+
SourceBean sourceBean = new SourceBean("Peter", 30, "LA");
41+
TargetBean targetBean = new TargetBean();
42+
BeanUtilsCopyProperties.copySelectedPropertiesUsingCustomWrapper(sourceBean, targetBean, new HashSet<>(Arrays.asList("name", "age")));
43+
System.out.println(targetBean);
44+
assertEquals(targetBean.getName(), sourceBean.getName());
45+
assertEquals(targetBean.getAge(), sourceBean.getAge());
46+
assertNull(targetBean.getAddress());
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.copyproperties;
2+
3+
public class SourceBean {
4+
public String name;
5+
public int age;
6+
public String address;
7+
8+
public SourceBean(String name, int age, String address) {
9+
this.name = name;
10+
this.age = age;
11+
this.address = address;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public int getAge() {
23+
return age;
24+
}
25+
26+
public void setAge(int age) {
27+
this.age = age;
28+
}
29+
30+
public String getAddress() {
31+
return address;
32+
}
33+
34+
public void setAddress(String address) {
35+
this.address = address;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.copyproperties;
2+
3+
public class TargetBean {
4+
public String name;
5+
public int age;
6+
public String address;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public int getAge() {
17+
return age;
18+
}
19+
20+
public TargetBean() {
21+
}
22+
23+
public TargetBean(String name, int age, String address) {
24+
this.name = name;
25+
this.age = age;
26+
this.address = address;
27+
}
28+
29+
public void setAge(int age) {
30+
this.age = age;
31+
}
32+
33+
public String getAddress() {
34+
return address;
35+
}
36+
37+
public void setAddress(String address) {
38+
this.address = address;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "TargetBean{" +
44+
"name='" + name + '\'' +
45+
", age=" + age +
46+
", address='" + address + '\'' +
47+
'}';
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.copyproperties;
2+
3+
public class TempDTO {
4+
public String name;
5+
public int age;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
15+
public int getAge() {
16+
return age;
17+
}
18+
19+
public void setAge(int age) {
20+
this.age = age;
21+
}
22+
23+
public TempDTO(String name, int age) {
24+
this.name = name;
25+
this.age = age;
26+
}
27+
28+
public TempDTO() {
29+
}
30+
}

0 commit comments

Comments
 (0)