java - Reflection of setter, getter method -


i trying implement reflection code in java. new using reflections , have existing method this:

scheduleparams incomeresetfxschedule = performanceswapleg.getincomefxresetschedule();     if (performanceswapleg.getincomefxresetschedule().getdateroll() != null) {         incomeresetfxschedule.setdateroll(dateroll.valueof(performanceswapleg.getincomefxresetschedule().getdateroll().tostring()));     } else {         incomeresetfxschedule.setdateroll(dateroll.valueof(dateroll.s_preceding));     } 

i trying write reflection code above code , stuck @ point:

 try {         class<scheduleparams> incomefxresetschedule = scheduleparams.class;         class<dateroll> dateroll = dateroll.class;         try {             method m = performanceswapleg.class.getmethod("getincomefxresetschedule");             m.invoke(performanceswapleg);             method m1 = scheduleparams.class.getmethod("setdateroll", dateroll);             m1.invoke(performanceswapleg);         } catch (exception e) {             log.error(commonconstants.error_log, "failed invoke method" + e.getmessage());         }     } catch (exception e) {    //do nothing     } 

but not sure how call setter method , getter method. suggestions on how call kind of methods using reflections.

you calling 'em! in wrong way. when m.invoke you're calling method, you're doing following line:

getincomefxresetschedule(); 

if watch line you'll think? uuuhm, think missed variable i'll save value! method.invoke returns object, you'll need cast class. guess class scheduleparams.

scheduleparams scheduleparams = (scheduleparams) m.invoke(performanceswapleg); 

okay, great. want set it. once again, you're calling method receives params without passing param:

method m1 = scheduleparams.class.getmethod("setdateroll", dateroll); m1.invoke(performanceswapleg); 

it's following line:

setdateroll(); 

that throwing

java.lang.illegalargumentexception: wrong number of arguments 

because have no method called takes no arguments (i hope so), right way should be:

 method m1 = scheduleparams.class.getmethod("setdateroll", dateroll);  m1.invoke(performanceswapleg, new dateroll()); 

after call getter again , you'll whole new object.

i recommend read following related question:

and oracle's documentation reflection api.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -