zoukankan      html  css  js  c++  java
  • 【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"
  • 相关阅读:
    数据分析系统DIY1/3:CentOS7+MariaDB安装纪实
    NSArray与NSString、NSData,NSDictionary与NSString、NSData 相互转化
    Geek地生活,文艺地思考
    Android开发中遇到的问题(五)——Eclipse导入Android项目出现"Invalid project description overlaps the location of another project"错误的解决办法
    Android开发中遇到的问题(四)——Android中WARNING: Application does not specify an API level requirement!的解决方法
    Android开发中遇到的问题(三)——eclipse创建android项目无法正常预览布局文件
    Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局
    Android开发学习总结(三)——appcompat_v7项目说明
    Android开发学习总结(二)——使用Android Studio搭建Android集成开发环境
    Android开发学习总结(一)——搭建最新版本的Android开发环境
  • 原文地址:https://www.cnblogs.com/mindzone/p/15488744.html
Copyright © 2011-2022 走看看