What should be a best way to create key/value pairs for all the string instance variables of a class? Is there any library exists doing such a task?
I am doing the same using a reflection based backward look-up. But it seems doing to much work.
For example:
I have a class
public class Account implements ManagedEntity {
private String accountNumber;
public Account(String accountNumber) {
this.accountNumber = accountNumber;
}
}
If an instance of Account class is created like blelow,
Account ac = new Account(“abc”);
The output map should be like: [{“accountNumber”, “abc”}].
As Peter Lawrey already pointed out in one of the comments, if you are looking for a library that can do that transformation for you then one possible answer is Apache Commons BeanUtils:
You could use the
BeanUtilsclass from that library:Or using the
BeanMapimplementation it would be even easier: