Skip to content

Commit fe6f65a

Browse files
Bael 9216 format (#18767)
* BAEL-9216 - format * BAEL-9216 - code review
1 parent 6723f14 commit fe6f65a

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/BeanUtilsDemo.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ public class BeanUtilsDemo {
99
public static void main(String[] args) throws Exception {
1010
Post post = new Post();
1111
BeanUtils.setProperty(post, "title", "Commons BeanUtils Rocks");
12-
String title = BeanUtils.getProperty(post, "title");
13-
14-
Map<String, Object> data = Map.of(
15-
"title", "Map → Bean",
16-
"author", "Baeldung Team"
17-
);
1812

13+
Map<String, Object> data = Map.of("title", "Map → Bean", "author", "Baeldung Team");
1914
BeanUtils.populate(post, data);
2015
Post source = new Post();
2116
source.setTitle("Source");
2217
source.setAuthor("Alice");
2318

2419
Post target = new Post();
2520
BeanUtils.copyProperties(target, source);
26-
System.out.println(title);
21+
if (target.getMetadata() == null) {
22+
target.setMetadata(new Post.Metadata());
23+
}
24+
BeanUtils.setProperty(target, "metadata.wordCount", 850);
25+
System.out.println(target.getTitle());
26+
System.out.println(target.getMetadata()
27+
.getWordCount());
2728
}
2829

2930
public static void safeCopy(Object target, Object source) {

core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/Post.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,42 @@ public class Post {
44

55
private String title;
66
private String author;
7+
private Metadata metadata;
78

8-
public String getTitle() { return title; }
9-
public void setTitle(String title) { this.title = title; }
9+
public String getTitle() {
10+
return title;
11+
}
1012

11-
public String getAuthor() { return author; }
12-
public void setAuthor(String author) { this.author = author; }
13+
public void setTitle(String title) {
14+
this.title = title;
15+
}
16+
17+
public String getAuthor() {
18+
return author;
19+
}
20+
21+
public void setAuthor(String author) {
22+
this.author = author;
23+
}
24+
25+
public Metadata getMetadata() {
26+
return metadata;
27+
}
28+
29+
public void setMetadata(Metadata metadata) {
30+
this.metadata = metadata;
31+
}
32+
33+
public static class Metadata {
34+
35+
private int wordCount;
36+
37+
public int getWordCount() {
38+
return wordCount;
39+
}
40+
41+
public void setWordCount(int wordCount) {
42+
this.wordCount = wordCount;
43+
}
44+
}
1345
}

core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/PropertyDescriptorDemo.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ public static void main(String[] args) throws Exception {
2020
Method write = titlePd.getWriteMethod();
2121
Method read = titlePd.getReadMethod();
2222

23-
24-
write.invoke(post, "Reflections in Java");
25-
String value = (String) read.invoke(post);
26-
27-
System.out.println(value);
23+
if (write != null) {
24+
write.invoke(post, "Reflections in Java");
25+
}
26+
if (read != null) {
27+
String value = (String) read.invoke(post);
28+
System.out.println(value);
29+
}
2830
}
2931
}

core-java-modules/core-java-reflection-3/src/test/java/com/baeldung/reflectionbeans/PropertyDescriptorUnitTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public void givenPost_whenUsingPropertyDescriptor_thenReadAndWrite() throws Exce
1313
Post post = new Post();
1414
PropertyDescriptor pd = new PropertyDescriptor("author", Post.class);
1515

16-
pd.getWriteMethod().invoke(post, "Chris");
17-
assertEquals("Chris", pd.getReadMethod().invoke(post));
16+
pd.getWriteMethod()
17+
.invoke(post, "Chris");
18+
assertEquals("Chris", pd.getReadMethod()
19+
.invoke(post));
1820
}
1921
}

0 commit comments

Comments
 (0)