SonarQube的AvoidStaticFields规则?

时间:2016-06-08 18:59:41

标签: java sonarqube pmd

我们使用AvoidStaticFields自定义PMD规则:

    <rule
        name="AvoidStaticFields"
        language="java"
        externalInfoUrl=""
        message="Static fields that are not literal constants should be avoided."
        class="net.sourceforge.pmd.lang.rule.XPathRule">
        <description>
  Detects static fields that are not literal constants. Static fields are usually used to implement singleton pattern or caching, which is not a good practice. There are better ways to implement singleton pattern and caching.    
  </description>
        <priority>3</priority>
        <properties>
            <property name="xpath">
                <value>
<![CDATA[
//FieldDeclaration[@Static='true']
[count(descendant::Literal)=0]
[not(Type/ReferenceType/ClassOrInterfaceType[@Image='String'])]
]]>
    </value>
            </property>
        </properties>
        <example>
<![CDATA[
public class BadClass {
      final static int INT_CONSTANT = 0; // OK, constant
      final static String STRING_CONSTANT = "bl_id"; // OK, constant
      final static double DOUBLE_CONSTANT = 0.0; // OK, constant
      final static Integer INTEGER_CONSTANT = Integer.valueOf(0); // OK, constant

      final static Map MAP_CACHED = new HashMap(); // Bad, caching
      final static List LIST_CACHED = new ArrayList(); // Bad, caching

      private static final String[][] FIELDS_TO_PROPERTIES = { { STRING_CONSTANT, STRING_CONSTANT } }; // OK, array of constants
}
]]>
  </example>
    </rule>

我们正在迁移到SonarQube。有没有人对SonarQube有类似的规则?

0 个答案:

没有答案
相关问题