I am tring to get List<String>
of all values of POJO
Employee e = new Employee();
e.setAddr("addr");
e.setName("name");
List<String> values= new ArrayList();
Field[] declaredFields = e.getClass().getDeclaredFields();
for(Field f : declaredFields){
Class t = f.getType();
if(t == String.class) {
Object object = f.get(f.getName());
values.add(object);
}
}
// Expected values = ["name","addr"]
class Employee{
private String name;
private String address;
...
}
Getting exception
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36) at java.base/java.lang.reflect.Field.get(Field.java:418)
Aucun commentaire:
Enregistrer un commentaire