I have class with some of variables.
How I can create map where key will be name of variable and value will be value of this variable ?
So I want to create:
Map<String, String>
from custom object:
I create something like this:
protected Map<String, String> getObjectParams(Object object) throws Exception {
Map<String, String> result = new HashMap<>();
for (String field : (Iterable<String>) BeanUtils.describe(object).keySet()) {
Object value = PropertyUtils.getProperty(object, field);
if ((value != null) && (!"CLASS".equalsIgnoreCase(field)) {
result.put(field, value.toString());
}
}
return result;
}
but this will works only when class contains primitive objects and their wrapped equivalent. But when there will be another custom object with generated toString then there will value which I dont want. So how should I rewrite this example ?
UPDATE:
public Class MyCustomClass1 {
MyCustomClass2 customClass2;
}
public Class MyCustomClass2 {
String a;
String b;
}
then map should contains value MyCustomClass2A and MyCustomClass2B and their values or something similar