what is the preferred way of getting data from an unknown class
data is an ArrayList of Class MyType, If use just field.get(object) the
code doesnt compile saying unhandled exception. When I run it, I get
cannot access private members. Then I change all member fields of MyType
to public. Then this code works. But surely there must be a better way of
getting data?
for (Object object : data)//get one object
{
ArrayList<Field> fields =
new
ArrayList<Field>(Arrays.asList(object.getClass().getDeclaredFields()));//get
all its fields
for(Field field : fields)
{
try {
System.out.println(field.get(object));//print its fields
value
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
No comments:
Post a Comment