java - How to get all fields along with value of Composite object? -
i facing issue value of fields of class reflaction.
i have 1 class can have number of fields, number of classes , classes in turn can have number of fields, below
public class a{ string string; int number; hashmap<string,string> map; b bclass; }
above b again class member of class a.
- i want fields of type string , their's values , if field class
- if field class again, fields , values , example, map property of type hashmap class type need both key , value properties of string type.
please see code snippet solve above problem
private void extractstringfields(object object) throws illegalaccessexception { (field field : object.getclass().getdeclaredfields()) { field.setaccessible(true); system.out.println("filesd : "+field.getname()); object value = field.get(object); if (field.gettype().equals(string.class)) { system.out.println(field.getname() + "=" + value); } extractstringfields(field); } }
but unfortunately, not working, me please.
when passing,
extractstringfields(field);
it sending field object. when 2nd call onwards object.getclass give field.class. never proper value.
better keep overloaded class, pass field there. in method can write this,
for (field field : field.gettype().getdeclaredfields())
hope give head start.
or can use this:
if(value != null){ extractstringfields(value); }
Comments
Post a Comment