相关文章推荐
强健的红薯  ·  Proguard keep static ...·  2 年前    · 
买醉的沙滩裤  ·  如何获取StatefulSet ...·  2 年前    · 

一.PMD/CPD介绍

PMD是一个源代码分析器。它寻找常见的编程缺陷,例如未使用的变量、空的catch块、多余的对象创建等等。支持Java、JavaScript、Salesforce.com Apex、PLSQL、Apache Velocity、XML、XSL。

另外,它包含CPD(copy-paste-detector),一个复制粘贴检测器。CPD寻找重复的代码,支持Java、C、C++、C#、Groovy、PHP、Ruby、Fortran、JavaScript、PLSQL、Apache Velocity、Scala、Objective C,Matlab,Python,Go,Swift和Salesforce.com Apex。

二.下载工具

1. 公网可以下载pmd-bin-6.39.0.zip,解压缩后进入bin目录

三 CPD功能

1.使用cpd的命令

./run.sh cpd --minimum-tokens 100 --files /vmdata/cloudci/XR_Server_BigSpace_New/XR_Server_BigSpace_New_DailyCI/1/xr-bigspace/ --format text --language java 

2.使用cpd生成的报告

Found a 26 line (141 tokens) duplication in the following files:
Starting at line 128 of /vmdata/cloudci/XR_Server_BigSpace_New/XR_Server_BigSpace_New_DailyCI/1/xr-bigspace/src/main/java/com/zte/bigspace/modules/centralControl/service/impl/CentralControlInfoImpl.java
Starting at line 172 of /vmdata/cloudci/XR_Server_BigSpace_New/XR_Server_BigSpace_New_DailyCI/1/xr-bigspace/src/main/java/com/zte/bigspace/modules/centralControl/service/impl/CentralControlInfoImpl.java
        String codec = centerDictionaryDao.getML();
        for (ClientLoginState clientLoginState : onlineUsers) {
            if (clientLoginState == null || clientLoginState.getState() == 0) {
                continue;
            res.add(ThreadPoolUtil.submit(() -> startRender(contentInfo, clientLoginState, codec, rateAdaptive)));
        int failed = 0;
        CommonResult<ClientLoginState> failedResult = null;
        // 处理结果
        for (Future<CommonResult<ClientLoginState>> resultFuture : res) {
            try {
                CommonResult<ClientLoginState> result = resultFuture.get();
                if (result.isSuccess()) {
                    clientDao.updateById(result.getData());
                } else {
                    failedResult = result;
                    failed ++;
            } catch (Throwable e) {
                failed ++;
        int success = res.size() - failed;
        if (success == 0){
=====================================================================
Found a 35 line (128 tokens) duplication in the following files:
Starting at line 136 of /vmdata/cloudci/XR_Server_BigSpace_New/XR_Server_BigSpace_New_DailyCI/1/xr-bigspace/src/main/java/com/zte/bigspace/modules/client/service/impl/ClientDelayRecordServiceImpl.java
Starting at line 103 of /vmdata/cloudci/XR_Server_BigSpace_New/XR_Server_BigSpace_New_DailyCI/1/xr-bigspace/src/main/java/com/zte/bigspace/modules/client/service/impl/ClientMetricsRecordServiceImpl.java
            response.setHeader("Content-disposition", "attachment; filename=" + File.separator + "excel" + File.separator + PROBEDATE);
            response.setContentType("application/vnd.ms-excel");
            out = response.getOutputStream();
//            out = new FileOutputStream("D:/probe-date.xlsx");
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
            if (null != wb) {
                try {
                    wb.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
            if (null != in) {
                try {
                    in.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
        return CommonResult.success();

3.cpd的报告解析

用=======隔开的是两处代码重复的地方,

第一个表示同一个类自身的代码重复CentralControlInfoImpl.java自身的代码有重复,该类的128-153(26行)和172-197(26行)行的代码完全重复,重复的代码可以抽出来,写成一个公用方法

第二个表示两个不同的类代码重复ClientDelayRecordServiceImpl.java的136-263(128行)ClientMetricsRecordServiceImpl.java的103-230(128行)的代码重复,提示重复的代码可以抽出来,写一个工具类

4.官网上的解决思路总结

Refactoring duplicates

Once you have located some duplicates, several refactoring strategies may apply depending of the scope and extent of the duplication. Here’s a quick summary:

  • If the duplication is local to a method or single class:

Extract a local variable if the duplicated logic is not prohibitively long

Extract the duplicated logic into a private method

  • If the duplication occurs in siblings within a class hierarchy:

Extract a method and pull it up in the class hierarchy, along with common fields

Use the Template Method design pattern

  • If the duplication occurs consistently in unrelated hierarchies:

Introduce a common ancestor to those class hierarchies

重构重复的代码:

一旦您找到了一些重复代码,可能会根据重复代码的范围和程度应用多种重构策略。以下是快速总结:

  • 如果重复代码属于方法或单个类:

    如果重复代码逻辑不长,提取局部变量
    将重复代码逻辑提取为一种私有方法

  • 如果重复代码发生在类层次结构中的同级中:

    提取方法并在类层次结构中将其与常见字段一起上拉
    使用模板方法设计模式

  • 如果重复代码在无关的组织存档中一致发生:

    将共同祖先介绍到这些类的层次结构中
    新手和高级读者可能想要在“重构大师”上阅读更深入的策略、用例和解释。

三 PMD的功能

1.使用pmd的命令

./run.sh pmd -dir /home/src/ -format html -reportfile /home/error.html -rulesets rulesets/java/quickstart.xml,category/java/codestyle.xml

2.使用pmd生成的报告

在这里插入图片描述
点蓝色的那几行,可以进入官网,了解代码的需要修改的提示,非常友好

3.官网的修复提示

一.PMD/CPD介绍PMD是一个源代码分析器。它寻找常见的编程缺陷,例如未使用的变量、空的catch块、多余的对象创建等等。支持Java、JavaScript、Salesforce.com Apex、PLSQL、Apache Velocity、XML、XSL。另外,它包含CPD(copy-paste-detector),一个复制粘贴检测器。CPD寻找重复的代码,支持Java、C、C++、C#、Groovy、PHP、Ruby、Fortran、JavaScript、PLSQL、Apache Velocit 好吧,我承认,我懒了,写了一天文档,到这里直接粘贴了,希望大家能看懂,如果需要一份格式完整的文档,请去我的百度文库下载,地址是: http://wenku.baidu.com/view/d2849ff04693daef5ef73d34.html 下面,开始 又搞一边质量扫描插件,之前做过一遍,然后后面各种忽略,然后就放弃了,所以,应该寻找一种方法,循序渐进的实施。本次将实施一个基本的打包扫描方案,包含 checkstyle 固定团队编码风格,固定命名风格以及换行风格等,原型配置出资googlestyle, 修改缩进为4个字符(原型是2个) surefile test report Junit Test结果报告 JaCoCo test cove... rulesets (可选):设置为覆盖默认规则集(有关更多详细信息,请参见“定义自己的规则集”) runOnFileOpen :每次在vscode中打开文件时运行 runOnFileSave :每次保存文件时运行 runOnFileChange :更改文件时运行。 注意:这是“去抖动”,以防止性能问题。 延迟可以通过onFileChangeDebounce进行调整。 priorityErrorThreshold :确定将在哪个优先级上添加“错误
PMD是一款采用BSD协议发布的Java程序代码检查工具。该工具可以做到检查Java代码中是否含有未使用的变量、是否含有空的抓取块、是否含有不必要的对象等。该软件功能强大,扫描效率高,是Java程序员debug的好帮手。   它可以为您检查Java代码中存在的如下问题:   1、隐藏的bug,例如空的try catch,switch   2、未调用的代码,例如没有使用的局部变量、参数和私有方法   3、未优化的代码,例如String的不正确使用   4、过于复杂的表达式,没有必要的表达式循环,判断   5、重复代码   PMD支持的编辑器包括:   JDeveloper、Eclipse、JEdit、JBuilder、BlueJ、CodeGuide、NetBeans/Sun Java Studio Enterprise/Creator、IntelliJ IDEA、TextPad、Maven、Ant,、Gel、JCreator和Emacs。   此次版本的主要变化:   1、修复了已有规则的一些bug   2、修改了 CPD 算法   3、JSP/JSF 解析器支持 Unicode   4、可处理 标签;   5、AST HtmlScript 节点包含内容,支持 Ecmascript 等等
PMDCPD都是IntelliJ IDEA中的插件,用于执行静态代码分析。PMD插件可以检查代码中的潜在问题,例如未使用的变量、未使用的方法、重复代码等。而CPD插件可以检测代码中的重复部分,并生成报告以帮助开发人员进行代码优化。这两个插件都可以通过插件管理器下载安装。在使用PMD插件时,它会生成两个主要的PMD Task,即pmdMain和pmdTest,分别对main和test两个项目源文件目录使用PMD进行代码检查。如果您想设置开发环境,请在构思中打开该项目并添加PMD库文件,然后您应该能够构建和测试插件。如果您有任何问题或建议,请随时提出请求请求!