Reputation: 11
My code did a SONAR scan and I got a major error that said "Method parameters, caught exceptions and foreach variables should not be reassigned". I don't understand how they are being reassigned. Is their a fix for this? I get the error where it says "reportForm = new ReportForm();" in the code below.
public ModelAndView getPcVolumeTopOffendersShortpaid(@Valid @ModelAttribute("reportForm") ReportForm reportForm, BindingResult result) {
// Handle invalid request date cleanly ...
// when invalid or doesn't exist, use the current date for selection
// otherwise, selection is valid and can be used in criteria for query
if (reportForm == null || result.hasErrors()) {
reportForm = new ReportForm();
reportForm.setMonthYear(DateFormatterUtil.getAbbreviatedMonthYearFromCurrentDateTime());
}
// Create the criteria for the specified, and retrieve the report
reportForm.setReportType(ReportType.SHORTPAID_TOP_OFFENDERS_BY_VOLUME);
TopOffendersReportCriteria topOffendersReportCriteria = reportService.getTopOffendersReportCriteria(reportForm);
List<TopOffendersReport> list = reportService.getTopOffendersReport(topOffendersReportCriteria);
// Return the reports view and the supporting page objects
ModelAndView mv = new ModelAndView("reports/topOffendersShortpaidVolume");
mv.addObject("reportList", list);
mv.addObject("reportForm", reportForm);
return mv;
}
Upvotes: 1
Views: 4159
Reputation: 1
You can fix this error by creating a new local variable say reportFormLocal and then assign the method parameter value to it based on your checks (null, hasErrors etc). Use the reportFormLocal in rest of the method.
Upvotes: 0
Reputation: 3706
In mentioned line
reportForm = new ReportForm();
reportForm is method parameter. Means it's visible only locally. If reportForm referenced some object passed as an argument with this line you lose this reference by pointing to another object. And most likely you want to do something else. When method finished, freshly created object is lost.
If you want to delegate object creation to your method - it won't work.
Upvotes: 3