标签: pmd

CollapsibleIfStatements

我最近使用PMD(嵌入在哈德森中)发现了以下警告,我的代码似乎遭受了CollapsibleIfStatements,我并不完全理解.代码看起来像这样

// list to be filled with unique Somethingness
List list = new ArrayList();

// fill list
for (SomeObject obj : getSomeObjects()) { // interating 
  if (!obj.getSomething().isEmpty()) { // check if "Something" is empty *
    if (!list.contains(obj.getSomething())) { // check if "Something" is already in my list **
      list.add(obj.getSomething()); // add "Something" to my list
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在我看来,这段代码并不是"可折叠的"(否则对于下一个阅读代码的人来说,这将更加难以理解).另一方面,我想要解决此警告(不停用PMD;).

java pmd statements

5
推荐指数
1
解决办法
1549
查看次数

如何避免PMD CloseResource违规?

我正在修改我的应用程序代码以尊重pmd规则.我在此代码中有一个关闭资源错误:

Connection c = DataSourceUtils.getConnection(dataSource);
Statement request = null;
try {
    request = c.createStatement();
    request.execute(loadDataRequest);
} catch (SQLException e) {
    dataLogger.error(e);
    throw e;
}
Run Code Online (Sandbox Code Playgroud)

所以我搜索并找到了一个apache实用程序来避免它:DButils我的代码变成了这样

Connection c = DataSourceUtils.getConnection(dataSource);
Statement request = null;
try {
    request = c.createStatement();
    request.execute(loadDataRequest);
} catch (SQLException e) {
    dataLogger.error(e);
    throw e;
} finally {
    DbUtils.closeQuietly(request);
    DbUtils.closeQuietly(c);            
}
Run Code Online (Sandbox Code Playgroud)

但是,我仍然在日食和声纳报告中获得PMD警报!你知道如何永久修复它吗?

pmd

5
推荐指数
1
解决办法
2362
查看次数

PMD规则冲突:一个方法应该只有一个退出点,这应该是方法中的最后一个语句

有时我发现一些PMD规则相互冲突,因此您无法编写满足所有PMD规则的代码.

例如,似乎以下两个规则彼此排除:"将对象分配给null是代码气味.考虑重构." 和"方法应该只有一个退出点,这应该是方法中的最后一个语句"

以下是我的示例代码:

在此输入图像描述

如果我使用get1(),我将违反前一条规则,如果我使用get2(),那么我将违反后一条规则.我更喜欢A方法应该只有一个退出点,但我不希望PMD报告"将对象分配给null是代码味道",有没有人有任何好主意?非常感谢 :)

java pmd

5
推荐指数
1
解决办法
2111
查看次数

使用PMD ant目标时找不到ruleset/basic.xml

使用pmd-5.0.1.我有一些自定义规则集:

<target name="pmd">
    <condition property="rules.file" else="${data}\pmdrules.xml,${data}\madcustompmdrules.xml">
        <isset property="rules.file"/>
    </condition>        

    <pmd rulesetfiles="${rules.file}">
        <formatter type="xml" toFile="${report.file}"/>
        <fileset dir="${src}">
            <include name="**/*.java"/>
        </fileset>
    </pmd>
</target>
Run Code Online (Sandbox Code Playgroud)

规则集引用了这样的基本规则:

<rule ref="rulesets/basic.xml/EmptySwitchStatements"/>
<rule ref="rulesets/basic.xml/JumbledIncrementer"/>
<rule ref="rulesets/basic.xml/ForLoopShouldBeWhileLoop"/>
<rule ref="rulesets/basic.xml/UnnecessaryConversionTemporary"/>
<rule ref="rulesets/basic.xml/OverrideBothEqualsAndHashcode"/>
<rule ref="rulesets/basic.xml/DoubleCheckedLocking"/>
Run Code Online (Sandbox Code Playgroud)

但是,当运行这个蚂蚁目标时,我得到:

java.lang.RuntimeException: Couldn't find the class Can't find resource rulesets/basic.xml.  Make sure the resource is a valid file or URL or is on the CLASSPATH.
Run Code Online (Sandbox Code Playgroud)

basic.xml不应该是jar文件的一部分吗?我错过了什么?

java ant pmd

5
推荐指数
1
解决办法
5237
查看次数

无法在maven-pmd-plugin 5.0.2中使用自定义规则集

我希望maven-pmd-plugin包含我指定的规则集并排除一些规则(特别是UselessParentheses)

就像在文档中描述的那样,我将以下内容放在pmd.xml中,该pmd.xml是所有模块的父级:

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <rulesets>
            <ruleset>/home/ubuntu/ruleset.xml</ruleset>
          </rulesets>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
Run Code Online (Sandbox Code Playgroud)

并准备了一个自定义规则集,如下所示:

  <!-- We'll use the entire rulesets -->
  <rule ref="rulesets/java/basic.xml"/>
  <rule ref="rulesets/java/imports.xml"/>
  <rule ref="rulesets/java/codesize.xml"/>
  <rule ref="rulesets/java/design.xml"/>
  <rule ref="rulesets/java/strings.xml"/>
  <rule ref="rulesets/java/unusedcode.xml"/>

  <!-- We want everything from this except some -->
  <rule ref="rulesets/java/unnecessary.xml">
    <exclude name="UselessParentheses"/>
  </rule>
Run Code Online (Sandbox Code Playgroud)

作为主要部分.

然而,当我跑步时,我mvn clean jxr:jxr pmd:check在报告中有"UselessParentheses".而且,用-Xshow来运行它

[DEBUG] Preparing ruleset: java-basic
[DEBUG] Before: java-basic After: java-basic.xml
[DEBUG] The resource 'rulesets/java/basic.xml' was found as jar:file:/home/ubuntu/.m2/repository/net/sourceforge/pmd/pmd/5.0.2/pmd-5.0.2.jar!/rulesets/java/basic.xml.
[DEBUG] Preparing …
Run Code Online (Sandbox Code Playgroud)

customization pmd maven-plugin maven

5
推荐指数
1
解决办法
4464
查看次数

PMD中不可避免的DD异常

我遇到了一个特殊情况,我无法解决PMD中的DD异常现象.假设代码是:

BigDecimal amount = BigDecimal.ZERO;
for(int i=0;i<5;i++)
{
      amount = amount.add(i);
}
return amount;
Run Code Online (Sandbox Code Playgroud)

在通过PMD运行此代码时,它将在声明金额时显示DD异常.但是,如果我删除初始化,我将得到一个异常.这种情况如何通过PMD.任何人?

java performance pmd

5
推荐指数
1
解决办法
5942
查看次数

eclipse中的PMD不接受排除模式

我在eclipse 4.3.1/Kepler下使用PMD,我不能从违规检查中排除文件和文件夹.

我的文件夹结构

/any/path/to/the/workspace/myproject1
/any/path/to/the/workspace/myproject2
/any/path/to/the/workspace/myprojectWithPMDrulesFile/pmd-rules.xml
Run Code Online (Sandbox Code Playgroud)

现在,以下文件夹由testng生成

...../myproject1/test-output
...../myproject2/test-output
Run Code Online (Sandbox Code Playgroud)

现在我配置了以下规则文件:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     name="Xeno-PMD-rules"
     xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>PMD Plugin preferences rule set</description>
    <exclude-pattern>.*/test-output/*.*</exclude-pattern>
    <exclude-pattern>/.*/test-output/.*</exclude-pattern>
    <exclude-pattern>**/test-output/**</exclude-pattern>
    <exclude-pattern>./test-output/.</exclude-pattern>
    <rule ref="rulesets/java/android.xml/CallSuperFirst"/>
    ...
</ruleset>
Run Code Online (Sandbox Code Playgroud)

在我的情况下,我在jquery.js文件中有很多错误,它在测试输出中.

如何以递归方式排除特定文件夹及其中的所有文件?

如何设置模式在eclipse和ANT/maven下工作?

提示:似乎类似于:PMD排除不起作用

eclipse pmd

5
推荐指数
1
解决办法
5111
查看次数

Maven pmd插件 - 排除/禁用规则

我正在尝试使用maven-pmd-plugin(在多模块maven项目中)从pmd中排除某个规则.

做法:

使用excludeFromFailureFile http://maven.apache.org/plugins/maven-pmd-plugin/examples/violation-exclusions.html

理想情况下,我想要为整个产品(基于父包)排除此规则,但是,我正在测试特定类 - 即使这不起作用.

环境

Java 7,Maven 3.0.3

<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-pmd-plugin</artifactId>
       <version>3.1</version>
       <executions>
         <execution>
            <goals>
              <goal>check</goal>
            </goals>
            <configuration>
              <excludeFromFailureFile>exclude-pmd.properties</excludeFromFailureFile>
            </configuration>
          </execution>
          <execution>
             <goals>
                 <goal>cpd-check</goal>
             </goals>
          <!-- Added explicit execution Id to avoid the below problem -->
      <!-- 'build.pluginManagement.plugins.plugin[org.apache.maven.plugins:maven-pmd-plugin].executions.execution.id' must be unique but found duplicate execution with id default @ line 1423, column 36 -->
            <id>cpd-check</id>
          </execution>
   </executions>
Run Code Online (Sandbox Code Playgroud)

exclude-pmd.properties的内容

mycompany.project.classA=UselessParentheses
Run Code Online (Sandbox Code Playgroud)

pmd maven-3 maven

5
推荐指数
1
解决办法
6394
查看次数

在构建和报告部分之间共享Maven插件配置

我有一个Maven项目,在该项目中,我会在构建和报告部分中使用多个插件(pmd,checkstyle)pom.xml。前者用于实施一些约束,而后者用于站点报告。插件的这些调用大多共享共同的<configuration>元素,到目前为止,我发现的唯一解决方案是复制相应的XML片段。有什么办法可以避免这种重复?

pom.xml片段示例:

<project ...>
...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>${plugin.pmd.version}</version>
                <configuration>
                    <targetJdk>${target.java.version}</targetJdk>
                    <rulesets>
                        <ruleset>${project.basedir}/codecheck/pmd-rules.xml</ruleset>
                    </rulesets>
                    <excludeRoots>
                        <excludeRoot>${project.basedir}/target/generated-sources/protobuf/java</excludeRoot>
                    </excludeRoots>
                    <failOnViolation>${failOnStyleError}</failOnViolation>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <id>pmd-check</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                            <goal>cpd-check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
...
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>${plugin.pmd.version}</version>
                <configuration>
                    <targetJdk>${target.java.version}</targetJdk>
                    <rulesets>
                        <ruleset>${project.basedir}/codecheck/pmd-rules.xml</ruleset>
                    </rulesets>
                    <excludeRoots>
                        <excludeRoot>${project.basedir}/target/generated-sources/protobuf/java</excludeRoot>
                    </excludeRoots>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
...
Run Code Online (Sandbox Code Playgroud)

java pmd pom.xml maven-3 maven

5
推荐指数
1
解决办法
621
查看次数

忽略具有Test注释的方法的PMD规则

我将PMD用于包含MockMvc测试的Spring Boot项目.该类强制用户捕获常规Exception.

class MockMvc {
    public ResultActions perform(RequestBuilder requestBuilder) throws Exception {}
}
Run Code Online (Sandbox Code Playgroud)

使用会导致PMD错误 - SignatureDeclareThrowsException.我想抑制所有@Test方法的检查.因此,我尝试遵循Stackoverflow的答案,但配置更改无效.

<rule ref="rulesets/java/strictexception.xml/SignatureDeclareThrowsException" >
    <properties>
        <!-- Ignore @Test methods -->
        <property name="violationSuppressXPath" value="
        //ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation//Name[@Image='Test']" />
    </properties>
</rule>
Run Code Online (Sandbox Code Playgroud)

我怎么能实现它?


抽象语法树为测试方法显示以下子树.

> ClassOrInterfaceBodyDeclaration
  > Annotation
    > MarkerAnnotation
      > Name:Test
  > MethodDeclaration:(public)
    > ResultType:(void)
    ...
Run Code Online (Sandbox Code Playgroud)

java junit pmd abstract-syntax-tree

5
推荐指数
1
解决办法
907
查看次数