PMD-ATPCO-RuleSet_V7.xml 4

<!-- New rules Jan 2010-->
 <rule name="NPathComplexity"
      since="3.9"
      message="The method {0}() has an NPath complexity of {1}"
      class="net.sourceforge.pmd.rules.design.NpathComplexity"
      externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#NPathComplexity">
   <description>
   The NPath complexity of a method is the number of acyclic execution paths through that method.
   A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
   </description>
     <priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
   <properties>
    <property name="minimum" description="The npath reporting threshold" value="200"/>
   </properties>
    <example>
 <![CDATA[
 public class Foo {
  void bar() {
   // lots of complicated code
  }
 }
 ]]>
    </example>
</rule>

<rule name="ExcessiveMethodLength"
    since="0.6"
       message="Avoid really long methods."
       class="net.sourceforge.pmd.rules.design.LongMethodRule"
       externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#ExcessiveMethodLength">
   <description>
Violations of this rule usually indicate that the method is doing
too much.  Try to reduce the method size by creating helper methods and removing any copy/pasted code.
   </description>
     <priority>2</priority> <!-- Priority 3 to 2. Changed by ATP3RXS -->
   <properties>
    <property name="minimum" description="The method size reporting threshold" value="60"/>
   </properties>
   <example>
<![CDATA[
public class Foo {
 public void doSomething() {
  System.out.println("Hello world!");
  System.out.println("Hello world!");
  // 98 copies omitted for brevity.
 }
}
]]>
   </example>

</rule>
 
<rule name="ExcessiveClassLength"
    since="0.6"
       message="Avoid really long classes."
       class="net.sourceforge.pmd.rules.design.LongClassRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#ExcessiveClassLength">
   <description>
Long Class files are indications that the class may be trying to
do too much.  Try to break it down, and reduce the size to something
manageable.
   </description>
     <priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
       <properties>
        <property name="minimum" description="The class size reporting threshold"  value="1000"/>
       </properties>
   <example>
<![CDATA[
public class Foo {
  public void bar() {
    // 1000 lines of code
  }
}
]]>
   </example>
</rule>
 
<rule  name="CyclomaticComplexity"
       since="1.03"
        message = "The {0} ‘‘{1}‘‘ has a Cyclomatic Complexity of {2}."
        class="net.sourceforge.pmd.rules.CyclomaticComplexity"
        externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#CyclomaticComplexity">
   <description>
     <![CDATA[
Complexity is determined by the number of decision points in a method plus one for the
method entry.  The decision points are ‘if‘, ‘while‘, ‘for‘, and ‘case labels‘.  Generally,
1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity,
 and 11+ is very high complexity.
  ]]>
   </description>
   <priority>2</priority>  <!-- Priority 1 to 2. Changed by ATP3RXS -->
   <properties>
      <property name="reportLevel" description="The Cyclomatic Complexity reporting threshold"  value="10"/>
      <property name="showClassesComplexity"
         description="Indicate if class average violation should be added to the report"
         value="true"/>
      <property name="showMethodsComplexity"
         description="Indicate if class average violation should be added to the report"
         value="true"/>

</properties>
   <example>
<![CDATA[
// Cyclomatic Complexity = 12
public class Foo {
1   public void example()  {
2       if (a == b)  {
3           if (a1 == b1) {
                fiddle();
4           } else if a2 == b2) {
                fiddle();
            }  else {
                fiddle();
            }
5       } else if (c == d) {
6           while (c == d) {
                fiddle();
            }
7        } else if (e == f) {
8           for (int n = 0; n < h; n++) {
                fiddle();
            }
        } else{
            switch (z) {
9               case 1:
                    fiddle();
                    break;
10              case 2:
                    fiddle();
                    break;
11              case 3:
                    fiddle();
                    break;
12              default:
                    fiddle();
                    break;
            }
        }
    }
}
]]>
   </example>
</rule>
<rule name="NcssMethodCount" message="The method {0}() has an NCSS line count of {1}"
   since="3.9"
   class="net.sourceforge.pmd.rules.codesize.NcssMethodCount"
   externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#NcssMethodCount">
    <description>
This rule uses the NCSS (Non Commenting Source Statements) algorithm to determine the number of lines
of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm,
lines of code that are split are counted as one.
    </description>
    <priority>1</priority>
    <properties>
        <property name="minimum" description="The method NCSS count reporting threshold" value="100"/>
    </properties>
   <example>
<![CDATA[
public class Foo extends Bar {
 public int methd() {
     super.methd();

//this method only has 1 NCSS lines
      return 1;
 }
}
]]>
   </example>
   </rule>

<rule name="AvoidReassigningParameters"
       since="1.0"
        message="Avoid reassigning parameters such as ‘‘{0}‘‘"
        class="net.sourceforge.pmd.rules.AvoidReassigningParameters"
          externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#AvoidReassigningParameters">
    <description>
Reassigning values to parameters is a questionable practice.  Use a temporary local variable instead.
    </description>
        <priority>2</priority>
    <example>
<![CDATA[
public class Foo {
 private void foo(String bar) {
  bar = "something else";
 }
}
]]>
    </example>
  </rule>

<rule name="EqualsNull"
       since="1.9"
            message="Avoid using equals() to compare against null"
            class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#EqualsNull">
        <description>
Inexperienced programmers sometimes confuse comparison concepts
and use equals() to compare to null.
        </description>
        <priority>1</priority>
        <properties>
            <property name="xpath">
                <value>
    <![CDATA[
//PrimaryExpression
 [
PrimaryPrefix/Name[ends-with(@Image, ‘equals‘)]
or
PrimarySuffix[ends-with(@Image, ‘equals‘)]
]
[PrimarySuffix/Arguments/ArgumentList[count(Expression)=1]
  /Expression/PrimaryExpression/PrimaryPrefix
   /Literal/NullLiteral]
    ]]>
                </value>
            </property>
         </properties>
    <example>
       <![CDATA[
class Bar {
   void foo() {
       String x = "foo";
       if (x.equals(null)) { // bad!
        doSomething();
       }
   }
}
    ]]>
        </example>
        </rule>
<rule name="InstantiationToGetClass"
       since="2.0"
          message="Avoid instantiating an object just to call getClass() on it; use the .class public member instead"
          class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#InstantiationToGetClass">
      <description>
Avoid instantiating an object just to call getClass() on it; use the .class public member instead.
      </description>
      <priority>2</priority> <!-- Priority 4 to 2. Changed by ATP3RXS -->
        <properties>
          <property name="xpath">
            <value>
                <![CDATA[
//PrimarySuffix
 [@Image=‘getClass‘]
 [parent::PrimaryExpression
  [PrimaryPrefix/AllocationExpression]
  [count(PrimarySuffix) = 2]
 ]
     ]]>
            </value>
          </property>
        </properties>
        <example>
    <![CDATA[
public class Foo {
 // Replace this
 Class c = new String().getClass();
 // with this:
 Class c = String.class;
}
    ]]>
        </example>
      </rule>
<rule name="MissingBreakInSwitch"
       since="3.0"
          message="A switch statement does not contain a break"
          class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#MissingBreakInSwitch">
      <description>
A switch statement without an enclosed break statement may be a bug.
      </description>
      <priority>2</priority>
      <properties>
          <property name="xpath">
              <value>
    <![CDATA[
//SwitchStatement
[count(.//BreakStatement)=0]
[count(SwitchLabel) > 0]
[count(BlockStatement/Statement/ReturnStatement)
 + count(BlockStatement/Statement/ThrowStatement)
     < count (SwitchLabel)]
    ]]>
              </value>
          </property>
      </properties>
      <example>
<![CDATA[
public class Foo {
 public void bar(int status) {
  switch(status) {
   case CANCELLED:
    doCancelled();
    // break; hm, should this be commented out?
   case NEW:
    doNew();
   case REMOVED:
    doRemoved();
   }
 }
}
]]>
      </example>
    </rule>
<rule name="AbstractClassWithoutAbstractMethod"
       since="3.0"
          message="This abstract class does not have any abstract methods"
          class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#AbstractClassWithoutAbstractMethod">
      <description>
The abstract class does not contain any abstract methods. An abstract class suggests
an incomplete implementation, which is to be completed by subclasses implementing the
abstract methods. If the class is intended to be used as a base class only (not to be instantiated
direcly) a protected constructor can be provided prevent direct instantiation.
      </description>
      <priority>3</priority>
      <properties>
          <property name="xpath">
              <value><![CDATA[
//ClassOrInterfaceDeclaration
 [@Abstract=‘true‘
  and count( .//MethodDeclaration[@Abstract=‘true‘] )=0 ]
  [count(ImplementsList)=0]
  [count(.//ExtendsList)=0]
              ]]>
              </value>
          </property>
      </properties>
      <example>
<![CDATA[
public abstract class Foo {
 void int method1() { ... }
 void int method2() { ... }
 // consider using abstract methods or removing
 // the abstract modifier and adding protected constructors
}
]]>
      </example>
    </rule>

<rule name="AvoidConstantsInterface"
      since="3.5"
      message="An Interface should be used only to model a behaviour; consider converting this to a class."
      class="net.sourceforge.pmd.rules.XPathRule"
      externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#AvoidConstantsInterface">
      <description>
     An interface should be used only to model a behaviour of a
    class: using an interface as a container of constants is a poor usage pattern.
      </description>
      <priority>2</priority>
      <properties>
        <property name="xpath">
        <value>
    <![CDATA[
//ClassOrInterfaceDeclaration[@Interface="true"]
    [
     count(.//MethodDeclaration)=0
     and
     count(.//FieldDeclaration)>0
    ]
    ]]>
        </value>
        </property>
      </properties>
      <example>
    <![CDATA[
    public interface ConstantsInterface {
     public static final int CONSTANT1=0;
     public static final String CONSTANT2="1";
    }
    ]]>
      </example>
    </rule>
  <rule name="AbstractClassWithoutAnyMethod"
          since="4.2"
          class="net.sourceforge.pmd.rules.XPathRule"
          message="No abstract method which means that the  keyword is most likely used to prevent instantiation. use a private or protected constructor instead."
          externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#AbstractClassWithoutAnyMethod">
         <description>
             <![CDATA[
    If the abstract class does not provides any methods, it may be just a data container that is not to be instantiated. In this case, it‘s probably
    better to use a private or a protected constructor in order to prevent instantiation than make the class misleadingly abstract.
             ]]>
         </description>
         <priority>2</priority> <!-- Priority 1 to 2. Changed by ATP3RXS -->
         <properties>
             <property name="xpath">
                 <value>
                     <![CDATA[
 //ClassOrInterfaceDeclaration[
  (@Abstract = ‘true‘)
  and
  (count(//MethodDeclaration) + count(//ConstructorDeclaration) = 0)
 ]
                     ]]>
                 </value>
             </property>
         </properties>
         <example>
             <![CDATA[
 public class abstract Example {
  String field;
  int otherField;
 }
             ]]>
         </example>
     </rule>
<rule  name="MisplacedNullCheck"
           since="3.5"
           message="The null check here is misplaced; if the variable is null there‘ll be a NullPointerException"
           class="net.sourceforge.pmd.rules.XPathRule"
           externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#MisplacedNullCheck">
      <description>
    The null check here is misplaced. if the variable is null you‘ll get a NullPointerException.
    Either the check is useless (the variable will never be "null") or it‘s incorrect.
      </description>
      <priority>3</priority>
      <properties>
        <property name="xpath">
        <value>
    <![CDATA[
//Expression
    /*[self::ConditionalOrExpression or self::ConditionalAndExpression]
     /descendant::PrimaryExpression/PrimaryPrefix
      /Name[starts-with(@Image,
      concat(ancestor::PrimaryExpression/following-sibling::EqualityExpression
       [./PrimaryExpression/PrimaryPrefix/Literal/NullLiteral]
     /PrimaryExpression/PrimaryPrefix
      /Name[count(../../PrimarySuffix)=0]/@Image,"."))
    ]
    ]]>
        </value>
        </property>
      </properties>
      <example>
    <![CDATA[
public class Foo {
 void bar() {
  if (a.equals(baz) && a != null) {}
 }
}
    ]]>
      </example>
      <example><![CDATA[
public class Foo {
 void bar() {
  if (a.equals(baz) || a == null) {}
 }
}
   ]]></example>
    </rule>

<rule name="AvoidThrowingNullPointerException"
       since="1.8"
        message="Avoid throwing null pointer exceptions."
        class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#AvoidThrowingNullPointerException">
      <description>
Avoid throwing a NullPointerException - it‘s confusing because most people will assume that the
virtual machine threw it. Consider using an IllegalArgumentException instead; this will be
clearly seen as a programmer-initiated exception.
      </description>
      <priority>1</priority>
      <properties>
        <property name="xpath">
          <value>
              <![CDATA[
//AllocationExpression/ClassOrInterfaceType[@Image=‘NullPointerException‘]
   ]]>
          </value>
        </property>
      </properties>
      <example>
        <![CDATA[
public class Foo {
 void bar() {
  throw new NullPointerException();
 }
}
  ]]>
      </example>
    </rule>

<rule name="AvoidRethrowingException"
    since="3.8"
    message="A catch statement that catches an exception only to rethrow it should be avoided."
    class="net.sourceforge.pmd.rules.XPathRule"
    externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#AvoidRethrowingException">
    <description>
     Catch blocks that merely rethrow a caught exception only add to code size and runtime complexity.
    </description>
    <priority>1</priority> <!-- Priority 3 to 1. Changed by ATP3RXS -->
    <properties>
        <property name="xpath">
            <value>
                <![CDATA[
//CatchStatement[FormalParameter
 /VariableDeclaratorId/@Image = Block/BlockStatement/Statement
 /ThrowStatement/Expression/PrimaryExpression[count(PrimarySuffix)=0]/PrimaryPrefix/Name/@Image
 and count(Block/BlockStatement/Statement) =1]
 ]]>
            </value>
        </property>
    </properties>
    <example>  <![CDATA[
  public class Foo {
   void bar() {
    try {
    // do something
    }  catch (SomeException se) {
       throw se;
    }
   }
  }
  ]]>
    </example>
  </rule>

<rule
    name="DoNotExtendJavaLangError"
    since="4.0"
    message="Exceptions should not extend java.lang.Error"
    class="net.sourceforge.pmd.rules.XPathRule"
    externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#DoNotExtendJavaLangError">
    <description>
      <![CDATA[
        Errors are system exceptions. Do not extend them.
      ]]>
    </description>
    <priority>1</priority>  <!-- Priority 3 to 1. Changed by ATP1AXR -->
    <properties>
      <property name="xpath">
        <value>
          <![CDATA[
//ClassOrInterfaceDeclaration/ExtendsList/ClassOrInterfaceType
  [@Image="Error" or @Image="java.lang.Error"]
          ]]>
        </value>
      </property>
    </properties>
    <example><![CDATA[
        public class Foo extends Error { }
    ]]></example>
  </rule>

<rule name="DoNotThrowExceptionInFinally"
        since="4.2"
      message="A throw statement in a finally block makes the control flow hard to understand."
      class="net.sourceforge.pmd.rules.XPathRule"
      externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#DoNotThrowExceptionInFinally">
     <description>
   <![CDATA[
         Throwing exception in a finally block is confusing. It may mask exception or a defect of the code,
         it also render code cleanup uninstable.
   Note: This is a PMD implementation of the Lint4j rule "A throw in a finally block"
   ]]>
  </description>
     <priority>1</priority>  <!-- Priority 4 to 1. Changed by ATP1AXR -->
     <properties>
   <property name="xpath">
     <value>
       <![CDATA[
//FinallyStatement[descendant::ThrowStatement]
             ]]>
     </value>
   </property>
  </properties>
     <example>
      <![CDATA[
    public class Foo
    {
     public void bar()
     {
      try {
       // Here do some stuff
      }
      catch( Exception e) {
       // Handling the issue
      }
      finally
      {
       // is this really a good idea ?
       throw new Exception();
      }
     }
    }
      ]]>
     </example>
    </rule>
 <rule name="UseStringBufferLength"
          since="3.4"
          message="This is an inefficient use of StringBuffer.toString; call StringBuffer.length instead."
          class="net.sourceforge.pmd.rules.strings.UseStringBufferLength"
          externalInfoUrl="http://pmd.sourceforge.net/rules/strings.html#UseStringBufferLength">
      <description>
 Use StringBuffer.length() to determine StringBuffer length rather than using StringBuffer.toString().equals("")
          or StringBuffer.toString().length() ==.
      </description>
      <priority>2</priority> <!-- Priority 3 to 2. Changed by ATP3RXS -->
      <example>
  <![CDATA[
public class Foo {
 void bar() {
  StringBuffer sb = new StringBuffer();
  // this is bad
  if(sb.toString().equals("")) {}
  // this is good
  if(sb.length() == 0) {}
 }
}

]]>
      </example>
    </rule>

<rule name="UseStringBufferForStringAppends"
       since="3.1"
          message="Prefer StringBuffer over += for concatenating strings"
          class="net.sourceforge.pmd.rules.optimization.UseStringBufferForStringAppends"
          externalInfoUrl="http://pmd.sourceforge.net/rules/optimizations.html#UseStringBufferForStringAppends">
           <description>
Finds usages of += for appending strings.
           </description>
            <priority>3</priority>
           <example>
      <![CDATA[
public class Foo {
 void bar() {
  String a;
  a = "foo";
  a += " bar";
  // better would be:
  // StringBuffer a = new StringBuffer("foo");
  // a.append(" bar);
 }
}
      ]]>
           </example>
        </rule>
  
  
<!-- END new rules Jan 2010-->
</ruleset>

时间: 2024-10-07 04:14:17

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

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