ali-other.xml 4.5 KB
Newer Older
骏烈 已提交
1 2 3 4 5
<?xml version="1.0"?>

<ruleset name="AlibabaJavaOthers" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
C
v2.0.0  
caikang.ck 已提交
6
    <description>AlibabaJavaOthers</description>
骏烈 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    <rule name="AvoidPatternCompileInMethodRule" language="java"
        message="java.other.AvoidPatternCompileInMethodRule.rule.msg"
        class="com.alibaba.p3c.pmd.lang.java.rule.other.AvoidPatternCompileInMethodRule">
        <description>java.other.AvoidPatternCompileInMethodRule.rule.desc</description>
        <priority>1</priority>
        <example>
<![CDATA[
    public class XxxClass {
        // Use precompile
        private static Pattern NUMBER_PATTERN = Pattern.compile("[0-9]+");
        public Pattern getNumberPattern() {
            // Avoid use Pattern.compile in method body.
            Pattern localPattern = Pattern.compile("[0-9]+");
            return localPattern;
        }
    }
]]>
      </example>
    </rule>

    <rule name="AvoidApacheBeanUtilsCopyRule" language="java"
        message="java.other.AvoidApacheBeanUtilsCopyRule.rule.msg"
        class="com.alibaba.p3c.pmd.lang.java.rule.other.AvoidApacheBeanUtilsCopyRule">
        <description>java.other.AvoidApacheBeanUtilsCopyRule.rule.desc</description>
        <priority>1</priority>
        <example>
<![CDATA[
    TestObject a = new TestObject();
    TestObject b = new TestObject();
    a.setX(b.getX());
    a.setY(b.getY());
]]>
      </example>
    </rule>

    <rule name="AvoidNewDateGetTimeRule" language="java"
        message="java.other.AvoidNewDateGetTimeRule.rule.msg"
        class="com.alibaba.p3c.pmd.lang.java.rule.other.AvoidNewDateGetTimeRule">
        <description>java.other.AvoidNewDateGetTimeRule.rule.desc</description>
        <priority>1</priority>
        <example>
<![CDATA[
    public class TimeMillisDemo {
        public static void main(String args[]) {
            // Positive example:
            long a = System.currentTimeMillis();
            // Negative example:
            long b = new Date().getTime();
C
v2.0.0  
caikang.ck 已提交
56

骏烈 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            System.out.println(a);
            System.out.println(b);
        }
    }
]]>
      </example>
    </rule>

    <rule name="AvoidMissUseOfMathRandomRule" language="java"
          message="java.other.AvoidMissUseOfMathRandomRule.rule.msg"
          class="com.alibaba.p3c.pmd.lang.java.rule.other.AvoidMissUseOfMathRandomRule">
        <priority>3</priority>
        <example>
<![CDATA[
Negative example:
    Long randomLong =(long) (Math.random() * 10);
]]>
        </example>
        <example>
<![CDATA[
Positive example:
    Long randomLong = new Random().nextLong();
]]>
        </example>
    </rule>

骏烈 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    <rule name="MethodTooLongRule" language="java"
          message="java.other.MethodTooLongRule.rule.msg"
          class="com.alibaba.p3c.pmd.lang.java.rule.other.MethodTooLongRule">
        <description>java.other.MethodTooLongRule.rule.desc</description>
        <priority>3</priority>
    </rule>

    <rule name="UseRightCaseForDateFormatRule" language="java"
          message="java.other.UseRightCaseForDateFormatRule.rule.msg"
          class="com.alibaba.p3c.pmd.lang.java.rule.other.UseRightCaseForDateFormatRule">
        <description>java.other.UseRightCaseForDateFormatRule.rule.desc</description>
        <priority>2</priority>
        <example>
            <![CDATA[
Negative example:
C
caikang.ck 已提交
98
    SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
骏烈 已提交
99 100 101 102 103
 ]]>
        </example>
        <example>
            <![CDATA[
Positive example:
C
caikang.ck 已提交
104
        SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
C
v2.0.0  
caikang.ck 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
]]>
        </example>
    </rule>

    <rule name="AvoidDoubleOrFloatEqualCompareRule" language="java"
          message="java.other.AvoidDoubleOrFloatEqualCompareRule.rule.msg"
          class="com.alibaba.p3c.pmd.lang.java.rule.other.AvoidDoubleOrFloatEqualCompareRule">
        <description>java.other.AvoidDoubleOrFloatEqualCompareRule.rule.desc</description>
        <priority>2</priority>
        <example>
            <![CDATA[
Negative example:
        float g = 0.7f-0.6f;
        float h = 0.8f-0.7f;
        if (g == h) {
            System.out.println("true");
        }
 ]]>
        </example>
        <example>
            <![CDATA[
Positive example:
        double dis = 1e-6;
        double d1 = 0.0000001d;
        double d2 = 0d;
        System.out.println(Math.abs(d1 - d2) < dis);
骏烈 已提交
131 132 133
]]>
        </example>
    </rule>
骏烈 已提交
134
</ruleset>