这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ 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<String, Object> data = Map.of(
"title", "Map → Bean",
"author", "Baeldung Team"
);

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

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}