Tweaked ShortVariableRule to allow for short catch block names; thanks to Astro Jetson Jr for the code

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1879 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-04-30 13:29:38 +00:00
parent af1dc32cd1
commit cd5ba51fc2
2 changed files with 47 additions and 36 deletions

View File

@ -6,45 +6,11 @@ import net.sourceforge.pmd.rules.XPathRule;
public class ShortVariableRuleTest extends SimpleAggregatorTst {
private static final String TEST1 =
"public class ShortVariableParam {" + CPD.EOL +
" public static void main(String a[]) { // a should trigger it." + CPD.EOL +
" }" + CPD.EOL +
"}";
private static final String TEST2 =
"public class ShortVariableNone {" + CPD.EOL +
" public static void main(String args[]) {" + CPD.EOL +
" int bugleDeWump = -1;" + CPD.EOL +
" }" + CPD.EOL +
"}";
private static final String TEST3 =
"public class ShortVariableLocal {" + CPD.EOL +
"" + CPD.EOL +
" public static void main(String args[]) {" + CPD.EOL +
" int ab = -1; " + CPD.EOL +
" // Should trigger ShortVariable rule." + CPD.EOL +
" }" + CPD.EOL +
"}";
private static final String TEST4 =
"public class ShortVariableFor {" + CPD.EOL +
" public static void main(String args[]) {" + CPD.EOL +
" for (int i = 0; i < 10; i++) { } // Should NOT!! trigger." + CPD.EOL +
" }" + CPD.EOL +
"}";
private static final String TEST5 =
"public class ShortVariableField {" + CPD.EOL +
" private int qx; // Should cause a problem." + CPD.EOL +
"}";
private Rule rule;
public void setUp() {
rule = new XPathRule();
rule.addProperty("xpath", "//VariableDeclaratorId[string-length(@Image) < 3][not(ancestor::ForInit)]");
rule.addProperty("xpath", "//VariableDeclaratorId[string-length(@Image) < 3][not(ancestor::ForInit)][not((ancestor::FormalParameter) and (ancestor::TryStatement))]");
}
public void testAll() {
@ -54,6 +20,49 @@ public class ShortVariableRuleTest extends SimpleAggregatorTst {
new TestDescriptor(TEST3, "local", 1, rule),
new TestDescriptor(TEST4, "for", 0, rule),
new TestDescriptor(TEST5, "field", 1, rule),
new TestDescriptor(TEST6, "catch(Exception e) is OK", 0, rule),
});
}
private static final String TEST1 =
"public class Foo {" + CPD.EOL +
" public static void main(String a[]) { // a should trigger it." + CPD.EOL +
" }" + CPD.EOL +
"}";
private static final String TEST2 =
"public class Foo {" + CPD.EOL +
" public static void main(String args[]) {" + CPD.EOL +
" int bugleDeWump = -1;" + CPD.EOL +
" }" + CPD.EOL +
"}";
private static final String TEST3 =
"public class Foo {" + CPD.EOL +
"" + CPD.EOL +
" public static void main(String args[]) {" + CPD.EOL +
" int ab = -1; " + CPD.EOL +
" // Should trigger ShortVariable rule." + CPD.EOL +
" }" + CPD.EOL +
"}";
private static final String TEST4 =
"public class Foo {" + CPD.EOL +
" public static void main(String args[]) {" + CPD.EOL +
" for (int i = 0; i < 10; i++) { } // Should NOT!! trigger." + CPD.EOL +
" }" + CPD.EOL +
"}";
private static final String TEST5 =
"public class Foo {" + CPD.EOL +
" private int qx; // Should cause a problem." + CPD.EOL +
"}";
private static final String TEST6 =
"public class Foo {" + CPD.EOL +
" private void bar() {" + CPD.EOL +
" try {} catch (Exception e) {}" + CPD.EOL +
" }" + CPD.EOL +
"}";
}

View File

@ -16,7 +16,9 @@ Detects when a field, local or parameter has a short name.
<property name="xpath">
<value>
<![CDATA[
//VariableDeclaratorId[string-length(@Image) < 3][not(ancestor::ForInit)]
//VariableDeclaratorId[string-length(@Image) < 3]
[not(ancestor::ForInit)]
[not((ancestor::FormalParameter) and (ancestor::TryStatement))]
]]>
</value>
</property>