PMD-ATPCO-RuleSet_V7.xml 1

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="PMD-ATPCO-RuleSet_V7">
    <description>PMD Plugin preferences rule set</description>
    <rule class="net.sourceforge.pmd.rules.strings.AvoidDuplicateLiteralsRule" message="The String literal {0} appears {1} times in this file; the first occurrence is on line {2}" name="AvoidDuplicateLiterals">
        <description>
Code containing duplicate String literals can usually be improved by declaring the String as a constant field.
    </description>
        <example><![CDATA[

public class Foo {
 private void bar() {
    buz("Howdy");
    buz("Howdy");
    buz("Howdy");
    buz("Howdy");
 }
 private void buz(String x) {}
}

]]></example>
        <priority>1</priority>
        <properties>
            <property name="threshold" value="4"/>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strings.StringInstantiation" message="Avoid instantiating String objects; this is usually unnecessary." name="StringInstantiation">
        <description>
Avoid instantiating String objects; this is usually unnecessary.
    </description>
        <example><![CDATA[

public class Foo {
 private String bar = new String("bar"); // just do a String bar = "bar";
}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strings.StringToStringRule" message="Avoid calling toString() on String objects; this is unnecessary" name="StringToString">
        <description>
    Avoid calling toString() on String objects; this is unnecessary
    </description>
        <example><![CDATA[

public class Foo {
 private String baz() {
  String bar = "howdy";
  return bar.toString();
 }
}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strings.InefficientStringBuffering" message="Avoid concatenating nonliterals in a StringBuffer constructor or append()" name="InefficientStringBuffering">
        <description>
Avoid concatenating non literals in a StringBuffer constructor or append().
    </description>
        <example><![CDATA[

public class Foo {
 void bar() {
  // Avoid this
  StringBuffer sb=new StringBuffer("tmp = "+System.getProperty("java.io.tmpdir"));
  // use instead something like this
  StringBuffer sb = new StringBuffer("tmp = ");
  sb.append(System.getProperty("java.io.tmpdir"));
 }
}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strings.UnnecessaryCaseChange" message="Using equalsIgnoreCase() is cleaner than using toUpperCase/toLowerCase().equals()" name="UnnecessaryCaseChange">
        <description>
Using equalsIgnoreCase() is faster than using toUpperCase/toLowerCase().equals()
       </description>
        <example><![CDATA[
                
 public class Foo {
  public boolean bar(String buz) {
    // should be buz.equalsIgnoreCase("baz")
    return buz.toUpperCase().equals("baz");
    // another unnecessary toUpperCase()
    // return buz.toUpperCase().equalsIgnoreCase("baz");
  }
 }
                
       ]]></example>
        <priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strings.ConsecutiveLiteralAppends" message="StringBuffer.append is called {0} consecutive times with literal Strings. Use a single append with a single String." name="ConsecutiveLiteralAppends">
        <description>
Consecutively calling StringBuffer.append with String literals
    </description>
        <example><![CDATA[

public class Foo {
 private void bar() {
   StringBuffer buf = new StringBuffer();
   buf.append("Hello").append(" ").append("World"); //bad
   buf.append("Hello World");//good
 }
}

]]></example>
        <priority>1</priority>
        <properties>
            <property name="threshold" value="1"/>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strings.UseIndexOfChar" message="String.indexOf(char) is faster than String.indexOf(String)" name="UseIndexOfChar">
        <description>
Use String.indexOf(char) when checking for the index of a single character; it‘s faster.
    </description>
        <example><![CDATA[

public class Foo {
 void bar() {
  String s = "hello world";
  // avoid this
  if (s.indexOf("d") {}
  // instead do this
  if (s.indexOf(‘d‘) {}
 }
}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strings.InsufficientStringBufferDeclaration" message="StringBuffer constructor is initialized with size {0}, but has at least {1} characters appended" name="InsufficientStringBufferDeclaration">
        <description>
Failing to pre-size a StringBuffer properly could cause it to re-size many times
during runtime. This rule checks the characters that are actually passed into
StringBuffer.append(), but represents a best guess "worst case" scenario. An
empty StringBuffer constructor initializes the object to 16 characters. This default
is assumed if the length of the constructor can not be determined.
    </description>
        <example><![CDATA[

public class Foo {
    void bar() {
        StringBuffer bad = new StringBuffer();
        bad.append("This is a long string, will exceed the default 16 characters");//bad
        StringBuffer good = new StringBuffer(41);
        good.append("This is a long string, which is pre-sized");//good
    }
}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.imports.UnusedImportsRule" message="Avoid unused imports such as ‘‘{0}‘‘" name="UnusedImports">
        <description>
    Avoid unused import statements.
    </description>
        <example><![CDATA[

// this is bad
import java.io.File;
public class Foo {}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.UnusedPrivateFieldRule" message="Avoid unused private fields such as ‘‘{0}‘‘" name="UnusedPrivateField">
        <description>
Detects when a private field is declared and/or assigned a value, but not used.
    </description>
        <example><![CDATA[

public class Something {
  private static int FOO = 2; // Unused
  private int i = 5; // Unused
  private int j = 6;
  public int addOne() {
    return j++;
  }
}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.UnusedLocalVariableRule" message="Avoid unused local variables such as ‘‘{0}‘‘" name="UnusedLocalVariable">
        <description>
Detects when a local variable is declared and/or assigned, but not used.
    </description>
        <example><![CDATA[

public class Foo {
 public void doSomething() {
  int i = 5; // Unused
 }
}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.UnusedPrivateMethodRule" message="Avoid unused private methods such as ‘‘{0}‘‘" name="UnusedPrivateMethod">
        <description>
Unused Private Method detects when a private method is declared but is unused.
    </description>
        <example><![CDATA[

public class Something {
 private void foo() {} // unused
}

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.UnusedFormalParameterRule" message="Avoid unused {0} parameters such as ‘‘{1}‘‘" name="UnusedFormalParameter">
        <description>
Avoid passing parameters to methods or constructors and then not using those parameters.
    </description>
        <example><![CDATA[

public class Foo {
 private void bar(String howdy) {
  // howdy is not used
 }

]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strictexception.AvoidCatchingThrowable" message="A catch statement should never catch throwable since it includes errors" name="AvoidCatchingThrowable">
        <description>
This is dangerous because it casts too wide a net; it can catch things like OutOfMemoryError.
      </description>
        <example><![CDATA[
               
public class Foo {
 public void bar() {
  try {
   // do something
  } catch (Throwable th) {  //Should not catch throwable
   th.printStackTrace();
  }
 }
}
               
      ]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.strictexception.ExceptionSignatureDeclaration" message="A signature (constructor or method) shouldn‘t have Exception in throws declaration" name="SignatureDeclareThrowsException">
        <description>
It is unclear which exceptions that can be thrown from the methods.
It might be difficult to document and understand the vague interfaces.
Use either a class derived from RuntimeException or a checked exception.
      </description>
        <example><![CDATA[
               
public void methodThrowingException() throws Exception {
}
               
      ]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="Avoid catching NullPointerException; consider removing the cause of the NPE." name="AvoidCatchingNPE">
        <description>
      Code should never throw NPE under normal circumstances.  A catch block may hide the original error, causing other more subtle errors in its wake.
    </description>
        <example><![CDATA[ 
public class Foo {
 void bar() {
  try {
   // do something
   }  catch (NullPointerException npe) {
  }
 }
}

]]></example>
        <priority>1</priority>
        <properties>
            <property name="xpath">
                <value><![CDATA[
           
//CatchStatement/FormalParameter/Type
 /ReferenceType/ClassOrInterfaceType[@Image=‘NullPointerException‘]
 
        ]]></value>
            </property>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="Avoid throwing raw exception types" name="AvoidThrowingRawExceptionTypes">
        <description>
Avoid throwing certain exception types.  Rather than throw a raw RuntimeException, Throwable,
 Exception, or Error, use a subclassed exception or error instead.
    </description>
        <example><![CDATA[
     
public class Foo {
public void bar() throws Exception {
  throw new Exception();
 }
}

]]></example>
        <priority>1</priority>
        <properties>
            <property name="xpath">
                <value><![CDATA[
           
//AllocationExpression
 /ClassOrInterfaceType[
 @Image=‘Throwable‘ or
 @Image=‘Exception‘ or
 @Image=‘Error‘ or
 @Image=‘RuntimeException‘]
 
        ]]></value>
            </property>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="Consider replacing this Vector with the newer java.util.List" name="ReplaceVectorWithList">
        <description>
  Consider replacing this Vector with the newer java.util.List
  </description>
        <example><![CDATA[

public class Foo {
 void bar() {
    Vector v = new Vector();
 }
}

]]></example>
        <priority>1</priority>  <!-- Priority 5 to 1. Changed by ATP1AXR -->
        <properties>
            <property name="xpath">
                <value><![CDATA[

//Type/ReferenceType/ClassOrInterfaceType[@Image=‘Vector‘]
 
    ]]></value>
            </property>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="Consider replacing this Hashtable with the newer java.util.Map" name="ReplaceHashtableWithMap">
        <description>
      Consider replacing this Hashtable with the newer java.util.Map
      </description>
        <example><![CDATA[
   
    public class Foo {
     void bar() {
        Hashtable h = new Hashtable();
     }
    }
   
      ]]></example>
        <priority>1</priority>  <!-- Priority 5 to 1. Changed by ATP1AXR -->
        <properties>
            <property name="xpath">
                <value><![CDATA[
   
    //Type/ReferenceType/ClassOrInterfaceType[@Image=‘Hashtable‘]
    
        ]]></value>
            </property>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="Avoid using enum as an identifier; it‘s a reserved word in JDK 1.5" name="AvoidEnumAsIdentifier">
        <description>Finds all places ‘enum‘ is used as an identifier is used</description>
        <example><![CDATA[
 
    public class A {
        public  class foo {
            String enum = "foo";
        }
    }
 
      ]]></example>
        <priority>3</priority>
        <properties>
            <property name="xpath">
                <value><![CDATA[
                 
                    //VariableDeclaratorId[@Image=‘enum‘]
                 
              ]]></value>
            </property>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="Avoid using ‘while‘ statements without curly braces" name="WhileLoopsMustUseBraces">
        <description>
Avoid using ‘while‘ statements without using curly braces
      </description>
        <example><![CDATA[

public void doSomething() {
  while (true)
      x++;
}

]]></example>
        <priority>1</priority> <!-- Priority 5 to 1. Changed by ATP3RXS -->
        <properties>
            <property name="xpath">
                <value><![CDATA[

//WhileStatement[not(Statement/Block)]

]]></value>
            </property>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="Avoid using ‘for‘ statements without curly braces" name="ForLoopsMustUseBraces">
        <description>
Avoid using ‘for‘ statements without using curly braces
       </description>
        <example><![CDATA[

public void foo() {
 for (int i=0; i<42;i++)
   foo();
}

]]></example>
        <priority>1</priority>
        <properties>
            <property name="xpath">
                <value><![CDATA[
 
//ForStatement[not(Statement/Block)]
 
                 ]]></value>
            </property>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.SimplifyBooleanReturns" message="Avoid unnecessary if..then..else statements when returning a boolean" name="SimplifyBooleanReturns">
        <description>
Avoid unnecessary if..then..else statements when returning a boolean
    </description>
        <example><![CDATA[

public class Foo {
  private int bar =2;
  public boolean isBarEqualsTo(int x) {
    // this bit of code
    if (bar == x) {
     return true;
    } else {
     return false;
    }
    // can be replaced with a simple
    // return bar == x;
  }
}

]]></example>
        <priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="Switch statements should have a default label" name="SwitchStmtsShouldHaveDefault">
        <description>
Switch statements should have a default label.
    </description>
        <example><![CDATA[

public class Foo {
 public void bar() {
  int x = 2;
  switch (x) {
   case 2: int j = 8;
  }
 }
}

]]></example>
        <priority>1</priority>
        <properties>
            <property name="xpath">
                <value><![CDATA[
                 
//SwitchStatement[not(SwitchLabel[@Default=‘true‘])]
                 
              ]]></value>
            </property>
        </properties>
    </rule>
    <rule class="net.sourceforge.pmd.rules.ConstructorCallsOverridableMethod" message="Overridable {0} called during object construction" name="ConstructorCallsOverridableMethod">
        <description>
Calling overridable methods during construction poses a risk of invoking methods on an
incompletely constructed object.  This situation can be difficult to discern.
It may leave the sub-class unable to construct its superclass or forced to
replicate the construction process completely within itself, losing the ability to call
super().  If the default constructor contains a call to an overridable method,
the subclass may be completely uninstantiable.   Note that this includes method calls
throughout the control flow graph - i.e., if a constructor Foo() calls a private method
bar() that calls a public method buz(), there‘s a problem.
      </description>
        <example><![CDATA[
 
public class SeniorClass {
  public SeniorClass(){
      toString(); //may throw NullPointerException if overridden
  }
  public String toString(){
    return "IAmSeniorClass";
  }
}
public class JuniorClass extends SeniorClass {
  private String name;
  public JuniorClass(){
    super(); //Automatic call leads to NullPointerException
    name = "JuniorClass";
  }
  public String toString(){
    return name.toUpperCase();
  }
}
 
      ]]></example>
        <priority>1</priority>
        <properties/>
    </rule>
    <rule class="net.sourceforge.pmd.rules.XPathRule" message="This final field could be made static" name="FinalFieldCouldBeStatic">
        <description>
If a final field is assigned to a compile-time constant, it could be
made static, thus saving overhead in each object
      </description>
        <example><![CDATA[
 
public class Foo {
 public final int BAR = 42; // this could be static and save some space
}
 
      ]]></example>
        <priority>1</priority>
        <properties>
            <property name="xpath">
                <value><![CDATA[
                   
//FieldDeclaration
 [@Final=‘true‘ and @Static=‘false‘]
 [not (../../../../ClassOrInterfaceDeclaration[@Interface=‘true‘])]
   /VariableDeclarator/VariableInitializer/Expression
    /PrimaryExpression/PrimaryPrefix/Literal
                   
                ]]></value>
            </property>
        </properties>
    </rule>

时间: 2024-11-06 17:03:55

PMD-ATPCO-RuleSet_V7.xml 1的相关文章

1. PMD 使用,编译和自定义规则

一 PMD简介 PMD是一款代码静态检查工具,可以检查出很多代码中潜在的bug以及让人感到疑惑的代码,具体大家可以百度下. 二 PMD源代码下载 下载地址: https://github.com/pmd/pmd/tree/pmd/5.5.x 需要注意的是注意选择branch,一般选择最新的branch:然后可以用git clone下来,或者直接下载zip压缩包. 如下: 从上图也可以看到,pmd支持的语言有很多,java的检测那就是在pmd-java里面. 三 maven下载和环境变量配置 参考

jenkins 使用oclint 扫描 oc 代码

jenkins 环境的搭建,在这里不在赘述,下面我们写一写,如何接入oclint. OCLint是一个强大的静态代码分析工具,可以用来提高代码质量,查找潜在的bug,主要针对c,c++和Objective-c的静态分析.功能非常强大.项目地址:http://oclint.org/. 1.oclint 与 xcpretty的安装 推荐是用Homebrew 来安装,快速且节省精力,也可以选择源码安装或者release包来安装,不过需要配置环境变量的内容.使用Homebrew 安装时,需要先设置bre

iOS 持续集成

iOS 持续集成系列 - 开篇 前言 iOS 开发在经过这几年的野蛮生长之后,慢慢地趋于稳定.无论开发语言是 Objective-C 还是 Swift,工程类型是 Hybird 还是原生,开发思想是 OOP 还是函数式,随着项目逐渐变大都在面临相同的问题: 测试.发布等重复性工作占了很大一部分时间,回归成本越来越高.持续集成不可避免地被提上了日程. 本文主要阐述 iOS 下的持续集成,以目标.内容.流程.工具入手,希望可以为大家描绘一幅 iOS 持续集成的蓝图.这可能不是一篇可以让你 Step

代码静态解析PMD

在正式进入测试之前,进行一定的静态代码分析及code review对代码质量及系统提高是有帮助的,以上为数据证明 Pmd 它是一个基于静态规则集的Java源码分析器,它可以识别出潜在的如下问题:– 可能的bug——空的try/catch/finally/switch块.– 无用代码(Dead code):无用的本地变量,方法参数和私有方法.– 空的if/while语句.– 过度复杂的表达式——不必要的if语句,本来可以用while循环但是却用了for循环.– 可优化的代码:浪费性能的String

如何更好地利用Pmd、Findbugs和CheckStyle分析结果

这里列出了很多Java静态分析工具,每一种工具关注一个特定的能发挥自己特长的领域,我们可以列举一下: Pmd 它是一个基于静态规则集的Java源码分析器,它可以识别出潜在的如下问题:– 可能的bug--空的try/catch/finally/switch块.– 无用代码(Dead code):无用的本地变量,方法参数和私有方法.– 空的if/while语句.– 过度复杂的表达式--不必要的if语句,本来可以用while循环但是却用了for循环.– 可优化的代码:浪费性能的String/Strin

pmd静态代码分析

在正式进入测试之前,进行一定的静态代码分析及code review对代码质量及系统提高是有帮助的,以上为数据证明 Pmd 它是一个基于静态规则集的Java源码分析器,它可以识别出潜在的如下问题:– 可能的bug--空的try/catch/finally/switch块.– 无用代码(Dead code):无用的本地变量,方法参数和私有方法.– 空的if/while语句.– 过度复杂的表达式--不必要的if语句,本来可以用while循环但是却用了for循环.– 可优化的代码:浪费性能的String

findbugs、PMD、Checkstyle等CI插件的使用

前面介绍过Jenkins+Gerrit+Git搭建CI系统,其实在CI系统中有很多有用的插件,这些插件可以帮助开发人员发现代码中的bug或者不规范的地方.下面就介绍下CI相关的插件 FindBugs:FindBugs是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题.有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析 PMD:PMD是一款静态代码分析工具,它能够自动检测各种潜在缺陷以及不安全或未优化的代码.PMD 通过其内置的编码规则对 Ja

代码静态分析工具--PMD,Findbugs,CheckStyle

最近学习Mybatis的官方文档,看到了[项目文档]一节有很多内容没有见过,做个笔记,理解一下. PMD 扫描Java源代码,查找潜在的问题,如: 可能的bugs,如空的try/catch/finally/switch声明 死亡的代码,没有使用的本地变量,参数和私有方法 不合标准的代码,如String/StringBuffer用法 过于复杂的表达式,如不必要的if表达式 重复的代码,拷贝.粘贴的代码 FindBugs 它用来查找Java代码中存在的bug.它使用静态分析方法标识出Java程序中上

XML文件各种bean的创建

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c&q

pmd

在正式进入测试之前,进行一定的静态代码分析及code review对代码质量及系统提高是有帮助的,以上为数据证明 Pmd 它是一个基于静态规则集的Java源码分析器,它可以识别出潜在的如下问题:– 可能的bug——空的try/catch/finally/switch块.– 无用代码(Dead code):无用的本地变量,方法参数和私有方法.– 空的if/while语句.– 过度复杂的表达式——不必要的if语句,本来可以用while循环但是却用了for循环.– 可优化的代码:浪费性能的String