Skip to content

Commit 17aa1b5

Browse files
Java reflection api (#18738)
* BAEL-9216 - Java Reflection Beans Property API * BAEL-9216 - Java Reflection Beans Property API * BAEL-9216 - Java Reflection Beans Property API
1 parent 7923551 commit 17aa1b5

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

core-java-modules/core-java-reflection-3/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<artifactId>reflections</artifactId>
3131
<version>${reflections.version}</version>
3232
</dependency>
33+
<dependency>
34+
<groupId>commons-beanutils</groupId>
35+
<artifactId>commons-beanutils</artifactId>
36+
<version>1.11.0</version>
37+
</dependency>
3338
</dependencies>
3439

3540
<build>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.reflectionbeans;
2+
3+
import org.apache.commons.beanutils.BeanUtils;
4+
5+
import java.util.Map;
6+
7+
public class BeanUtilsDemo {
8+
9+
public static void main(String[] args) throws Exception {
10+
Post post = new Post();
11+
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+
);
18+
19+
BeanUtils.populate(post, data);
20+
Post source = new Post();
21+
source.setTitle("Source");
22+
source.setAuthor("Alice");
23+
24+
Post target = new Post();
25+
BeanUtils.copyProperties(target, source);
26+
System.out.println(title);
27+
}
28+
29+
public static void safeCopy(Object target, Object source) {
30+
try {
31+
BeanUtils.copyProperties(target, source);
32+
} catch (ReflectiveOperationException ex) {
33+
throw new IllegalStateException(ex);
34+
}
35+
}
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.reflectionbeans;
2+
3+
public class Post {
4+
5+
private String title;
6+
private String author;
7+
8+
public String getTitle() { return title; }
9+
public void setTitle(String title) { this.title = title; }
10+
11+
public String getAuthor() { return author; }
12+
public void setAuthor(String author) { this.author = author; }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.reflectionbeans;
2+
3+
import java.beans.BeanInfo;
4+
import java.beans.Introspector;
5+
import java.beans.PropertyDescriptor;
6+
import java.lang.reflect.Method;
7+
8+
public class PropertyDescriptorDemo {
9+
10+
public static void main(String[] args) throws Exception {
11+
BeanInfo beanInfo = Introspector.getBeanInfo(Post.class);
12+
13+
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
14+
System.out.println(pd.getName());
15+
}
16+
17+
Post post = new Post();
18+
PropertyDescriptor titlePd = new PropertyDescriptor("title", Post.class);
19+
20+
Method write = titlePd.getWriteMethod();
21+
Method read = titlePd.getReadMethod();
22+
23+
24+
write.invoke(post, "Reflections in Java");
25+
String value = (String) read.invoke(post);
26+
27+
System.out.println(value);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.reflectionbeans;
2+
3+
import org.apache.commons.beanutils.BeanUtils;
4+
import org.junit.Test;
5+
6+
import java.util.Map;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class BeanUtilsUnitTest {
11+
12+
@Test
13+
public void givenPost_whenPopulate_thenFieldsAreSet() throws Exception {
14+
Post post = new Post();
15+
Map<String, Object> data = Map.of("title", "Populate Test", "author", "Dana");
16+
17+
BeanUtils.populate(post, data);
18+
19+
assertEquals("Populate Test", BeanUtils.getProperty(post, "title"));
20+
assertEquals("Dana", BeanUtils.getProperty(post, "author"));
21+
}
22+
23+
@Test
24+
public void givenTwoPosts_whenCopyProperties_thenTargetMatchesSource() throws Exception {
25+
Post source = new Post();
26+
source.setTitle("Copy");
27+
source.setAuthor("Eve");
28+
29+
Post target = new Post();
30+
BeanUtils.copyProperties(target, source);
31+
32+
assertEquals("Copy", target.getTitle());
33+
assertEquals("Eve", target.getAuthor());
34+
}
35+
}
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.reflectionbeans;
2+
3+
import org.junit.Test;
4+
5+
import java.beans.PropertyDescriptor;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
public class PropertyDescriptorUnitTest {
10+
11+
@Test
12+
public void givenPost_whenUsingPropertyDescriptor_thenReadAndWrite() throws Exception {
13+
Post post = new Post();
14+
PropertyDescriptor pd = new PropertyDescriptor("author", Post.class);
15+
16+
pd.getWriteMethod().invoke(post, "Chris");
17+
assertEquals("Chris", pd.getReadMethod().invoke(post));
18+
}
19+
}

0 commit comments

Comments
 (0)