diff --git a/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/BeanUtilsDemo.java b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/BeanUtilsDemo.java index 4bfb512ae1ec..ce4e3256d2d3 100644 --- a/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/BeanUtilsDemo.java +++ b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/BeanUtilsDemo.java @@ -9,13 +9,8 @@ public class BeanUtilsDemo { public static void main(String[] args) throws Exception { Post post = new Post(); BeanUtils.setProperty(post, "title", "Commons BeanUtils Rocks"); - String title = BeanUtils.getProperty(post, "title"); - - Map data = Map.of( - "title", "Map → Bean", - "author", "Baeldung Team" - ); + Map data = Map.of("title", "Map → Bean", "author", "Baeldung Team"); BeanUtils.populate(post, data); Post source = new Post(); source.setTitle("Source"); @@ -23,7 +18,13 @@ public static void main(String[] args) throws Exception { Post target = new Post(); BeanUtils.copyProperties(target, source); - System.out.println(title); + if (target.getMetadata() == null) { + target.setMetadata(new Post.Metadata()); + } + BeanUtils.setProperty(target, "metadata.wordCount", 850); + System.out.println(target.getTitle()); + System.out.println(target.getMetadata() + .getWordCount()); } public static void safeCopy(Object target, Object source) { diff --git a/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/Post.java b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/Post.java index bb7b7f3865d3..2a9b9e360925 100644 --- a/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/Post.java +++ b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/Post.java @@ -4,10 +4,42 @@ public class Post { private String title; private String author; + private Metadata metadata; - public String getTitle() { return title; } - public void setTitle(String title) { this.title = title; } + public String getTitle() { + return title; + } - public String getAuthor() { return author; } - public void setAuthor(String author) { this.author = author; } + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public Metadata getMetadata() { + return metadata; + } + + public void setMetadata(Metadata metadata) { + this.metadata = metadata; + } + + public static class Metadata { + + private int wordCount; + + public int getWordCount() { + return wordCount; + } + + public void setWordCount(int wordCount) { + this.wordCount = wordCount; + } + } } diff --git a/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/PropertyDescriptorDemo.java b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/PropertyDescriptorDemo.java index 0e98fb5ebd7e..a9bb8f4a5b33 100644 --- a/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/PropertyDescriptorDemo.java +++ b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflectionbeans/PropertyDescriptorDemo.java @@ -20,10 +20,12 @@ public static void main(String[] args) throws Exception { Method write = titlePd.getWriteMethod(); Method read = titlePd.getReadMethod(); - - write.invoke(post, "Reflections in Java"); - String value = (String) read.invoke(post); - - System.out.println(value); + if (write != null) { + write.invoke(post, "Reflections in Java"); + } + if (read != null) { + String value = (String) read.invoke(post); + System.out.println(value); + } } } diff --git a/core-java-modules/core-java-reflection-3/src/test/java/com/baeldung/reflectionbeans/PropertyDescriptorUnitTest.java b/core-java-modules/core-java-reflection-3/src/test/java/com/baeldung/reflectionbeans/PropertyDescriptorUnitTest.java index f999d9a93465..5d56c703911b 100644 --- a/core-java-modules/core-java-reflection-3/src/test/java/com/baeldung/reflectionbeans/PropertyDescriptorUnitTest.java +++ b/core-java-modules/core-java-reflection-3/src/test/java/com/baeldung/reflectionbeans/PropertyDescriptorUnitTest.java @@ -13,7 +13,9 @@ public void givenPost_whenUsingPropertyDescriptor_thenReadAndWrite() throws Exce Post post = new Post(); PropertyDescriptor pd = new PropertyDescriptor("author", Post.class); - pd.getWriteMethod().invoke(post, "Chris"); - assertEquals("Chris", pd.getReadMethod().invoke(post)); + pd.getWriteMethod() + .invoke(post, "Chris"); + assertEquals("Chris", pd.getReadMethod() + .invoke(post)); } } \ No newline at end of file