Wednesday, August 5, 2009

Java Reflections API (example)

Looping through Objects in a list, checking whether each Object contains a method called getId(). If so, invoke the method and show the result:


if (result instanceof List) {

for (Object o : (List) result) {
Method[] mArr = o.getClass().getMethods();

for (Method m : mArr) {
if ("getId".equals(m.getName())) {
Object obj = m.invoke(o);
LOG.info("Value of getId() " + obj.toString());
}
}

}
}

0 comments:

Blog Archive

About Me