【Java】SonarLint 疑难语法修正

 

规范驼峰命名使用:

提示信息

Local variable and method parameter names should comply with a naming convention

代码片段

        Map<String, List<ExcelExportColumn>> ColumnMap = new HashMap<>(16);
        ColumnMap.put("客诉权重KPI", exportColumnList);
        excelGenerator.generateExcelSheet(excelData, ColumnMap, "客诉权重KPI.xls", request, response);

绿色警告:

SonarLint: Rename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

使用正确的命名规则来更改变量即可

 

 

常量重复风险问题

提示信息:

String literals should not be duplicated

代码片段就不展示了,就是一些字符串常量出现过于频繁

IDEA中 双击选中任意其中一个,Ctrl + Alt + C 进行常量快速提取

红色警告:

SonarLint: Define a constant instead of duplicating this literal "首次重启数" 3 times. [+3 locations] 

 

移除多余的泛型参数类型

提示信息:

The diamond operator ("<>") should be used

应该使用钻石操作符来表示

 

代码片段:

List<ExcelExportColumn> exportColumnList = new ArrayList<ExcelExportColumn>();

绿色警告:

SonarLint: Replace the type specification in this constructor call with the diamond operator ("<>").

删除对象的参数类型即可

 

不应该使用SOUT打印信息

 提示信息

Standard outputs should not be used directly to log anything

代码片段

System.out.println("SXSSF Page Thread 导出数据,花费:" + second + "s/ " + millisEnd + "ms");

红色警告:

SonarLint: Replace this use of System.out or System.err by a logger.

替换成日志对象调用即可

 

日志对象的严谨调用

问题来自Controller层的日志打印调用:

    /**
     * 投诉率统计(厂端)
     * 1.通过售后小区分组统计
     * 2.通过经销商分组统计
     * @param kpiComplaintRateQueryVO 查询参数
     * @return 投诉率统计数据
     */
    @ApiOperation(value = "厂端投诉率统计维度查询")
    @PostMapping("/vcdc/ratestatis")
    public List<KpiComplaintRateVO> findKpiComplaintRateVOsByParams(@RequestBody KpiComplaintRateQueryVO kpiComplaintRateQueryVO) {
        logger.info("厂端投诉率统计传入查询参数kpiComplaintRateQueryVO={}", JSON.toJSONString(kpiComplaintRateQueryVO));
        return kpiComplaintRateService.findKpiComplaintRateVOsByParams(kpiComplaintRateQueryVO);
    }

SonarLint提示红色警告:

SonarLint: Invoke method(s) only conditionally.

大概意思是,此方法的调用需要符合条件的前提

除了打印对象要做判空处理,比较难找到的是这个日志对象要需要做一个条件判断

 

解决方案参考爆栈网的处理:

https://stackoverflow.com/questions/44324597/sonarqube-invoke-methods-only-conditionally#

需要先判断log对象和打印对象

if(logger.isInfoEnabled() && us != null){
    logger.info("Log this: {}", us.toString());
}

 

解决处理:

    /**
     * 投诉率统计(厂端)
     * 1.通过售后小区分组统计
     * 2.通过经销商分组统计
     * @param kpiComplaintRateQueryVO 查询参数
     * @return 投诉率统计数据
     */
    @ApiOperation(value = "厂端投诉率统计维度查询")
    @PostMapping("/vcdc/ratestatis")
    public List<KpiComplaintRateVO> findKpiComplaintRateVOsByParams(@RequestBody KpiComplaintRateQueryVO kpiComplaintRateQueryVO) {
        if (null != kpiComplaintRateQueryVO && logger.isInfoEnabled()) {
            logger.info("厂端投诉率统计传入查询参数kpiComplaintRateQueryVO={}", JSON.toJSONString(kpiComplaintRateQueryVO));
        }
        return kpiComplaintRateService.findKpiComplaintRateVOsByParams(kpiComplaintRateQueryVO);
    }

 

日志格式化输出要求

提示信息:

"Preconditions" and logging arguments should not require evaluation
Printf-style format strings should be used correctly

代码片段:

logger.info("SXSSF Page Thread 导出数据,花费:" + second + "s/ " + millisEnd + "ms");

红色警告:

SonarLint: Use the built-in formatting to construct this argument.
SonarLint: Format specifiers should be used instead of string concatenation.

解决语法:

String format = String.format("SXSSF Page Thread 导出数据,花费:%s s/ %s ms", second, millisEnd);
logger.info(format);

 

 

 

Controller映射路径简化声明

提示信息:

Composed "@RequestMapping" variants should be preferred

应首选组合的“@RequestMapping”变体

 

代码片段:

@RequestMapping(value = "/selectKPIWeight", method = RequestMethod.POST)

更改为:

@PostMapping("/selectKPIWeight")

绿色警告:

SonarLint: Replace "@RequestMapping(method = RequestMethod.POST)" with "@PostMapping"

 

posted @ 2021-10-31 11:06  emdzz  阅读(3768)  评论(0编辑  收藏  举报