Sunday, 15 September 2013

java - Dynamically calling methods on interface -



java - Dynamically calling methods on interface -

i'm newbie in java programming , i'm trying write simple mxbean servlet. want to dump info runtimemxbean.

as know, there couple methods interface, fe: - getvmname - getvmvendor , more..

i create array couple method names , later utilize foreach phone call one-by-one , print value.

unfortunately not work me. tried obj.invoke, getmethod , nil - looks not work regular class or (sorry, i'm noob)

some code:

runtimemxbean rmx = managementfactory.getruntimemxbean();

if utilize rmx.getvmvendor() fine, don't know how can utilize (maybe else?) invoke method string name on object (needed loop) i'm using latest netbeans glassfish.

thank you.

you can utilize reflection api:

try { method method = rmx.getclass().getdeclaredmethod("amethodname"); object result = method.invoke(rmx); } grab (nosuchmethodexception | securityexception | illegalaccessexception | illegalargumentexception | invocationtargetexception ex) { system.out.println(ex); }

java

No comments:

Post a Comment