lundi 29 juin 2015

Reflection of setter, getter method

I am trying to implement reflection code in java. I am new to using reflections and I have an existing method like 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 was trying to write reflection code for the above code and I am stuck at this 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 to invoke the method" + e.getMessage());
        }
    } catch (Exception e) {
   //Do Nothing
    }

But I am not sure how to call the setter method and the getter method. Any suggestions on how to call this kind of methods using reflections.





Aucun commentaire:

Enregistrer un commentaire