1
我想在java中實現反射代碼。我是新來使用反射,我有一個這樣的現有方法:反射的setter,getter方法
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));
}
我試圖寫上面的代碼反射代碼和我被困在這一點上:
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
}
但我我不知道如何調用setter方法和getter方法。有關如何使用反射調用這種方法的任何建議。
謝謝你,是有益的。 – user1658435
不客氣! –