>Sonar在第17行(Date parsedStartDate = df.parse(startDate(;)上标记了以下错误:
//Null 传递给 magicservice.controller.MagicStoreController.getTradeMagicsByDateRange(HttpServletRequest, String, String, String( 中的 java.text.DateFormat.parse(String( 的非空参数//此方法调用为非空方法参数传递一个空值。要么将参数注释为应始终为非 null 的参数,要么分析表明该参数将始终被取消引用。
代码已经阻止传递空值,不确定我这里是否缺少什么
@PostMapping(value = "/Magics/MagiclistbyDateRange/{MagicStatus}")
public @ResponseBody List<MagicLog> getTradeMagicsByDateRange(HttpServletRequest request,
@PathVariable String MagicStatus, @RequestParam(value = "STARTDATE", required =false ) String startDate,@RequestParam(value = "ENDDATE", required =false) String endDate)
throws ESException {
logger.info("MagicLog received from client - MagicStatus is :: " + MagicStatus);
String inAppAuthorization = request.getHeader("InAppAuthorization");
validateRequest(request, inAppAuthorization);
List<MagicLog> MagicLogs = new ArrayList<>();
DateFormat df = new SimpleDateFormat("yyyyMMddHH:mm:ss");
df.setLenient(false);
if (startDate == null && endDate==null) {
throw new ESException(MSG_ERROR_NULL_INPUTS);
}
else{
try {
// THIS is the line causing issues (17)
Date parsedStartDate = df.parse(startDate);
//Null passed for non-null parameter of java.text.DateFormat.parse(String) in Magicservice.controller.MagicStoreController.getTradeMagicsByDateRange(HttpServletRequest, String, String, String)
//This method call passes a null value for a non-null method parameter. Either the parameter is annotated as a parameter that should always be non-null, or analysis has shown that it will always be dereferenced.
Date parsedEndDate = df.parse(endDate);
long diffInMillies = Math.abs(parsedEndDate.getTime() - parsedStartDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
if(diff >3 )
{
throw new ESException(MSG_ERROR_INVALID_START_AND_END_DATE);
}
else
{
MagicLogs = ESService.getMagicLogByDateRange(MagicStatus, parsedStartDate,parsedEndDate);
}
} catch (ParseException e) {
logger.error(MSG_ERROR_PARSING_STRING_TO_DATE + e.getMessage(), e);
throw new ESException(e.getMessage());
} catch (Exception e) {
logger.error(MSG_ERROR_FROM_SERVICE + e.getMessage(), e);
throw new ESException(e.getMessage());
}
}
logger.info("Number of records returned for source system " + MagicStatus + "from" +startDate+"to"+endDate+" "+ MagicLogs.size());
return MagicLogs;
}
您只处理startDate
和endDate
都是 null 的情况 相反,
您应该处理其中任何一个为 null 的情况:
if (startDate == null || endDate==null) {
throw new ESException(MSG_ERROR_NULL_INPUTS);
}