Performance Refactoring, XPath rules re-written as Java: DontImportJavaLang, DontImportSun

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4816 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2006-11-19 02:07:58 +00:00
parent b5aa7f3422
commit 477410f3eb
5 changed files with 51 additions and 30 deletions

View File

@ -46,6 +46,8 @@ Renderers use less memory when generating reports.
Performance Refactoring, XPath rules re-written as Java:
AssignmentInOperand
AvoidDollarSigns
DontImportJavaLang
DontImportSun
MoreThanOneLogger
SuspiciousHashcodeMethodName
UselessStringValueOf

View File

@ -182,23 +182,11 @@ public class Foo {
<rule name="DontImportSun"
message="Avoid importing anything from the 'sun.*' packages"
class="net.sourceforge.pmd.rules.XPathRule"
class="net.sourceforge.pmd.rules.imports.DontImportSun"
externalInfoUrl="http://pmd.sourceforge.net/rules/controversial.html#DontImportSun">
<description>
Avoid importing anything from the 'sun.*' packages. These packages are not portable and are likely to change.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ImportDeclaration
[starts-with(Name/@Image, 'sun.')]
[not(starts-with(Name/@Image, 'sun.misc.Signal'))]
]]>
</value>
</property>
</properties>
<priority>4</priority>
<example>
<![CDATA[
@ -381,6 +369,7 @@ public class Foo {
</example>
</rule>
<!--
<rule name="DataflowAnomalyAnalysis"
message="Found ''{0}''-anomaly for variable ''{1}'' (lines ''{2}''-''{3}'')."
class="net.sourceforge.pmd.dfa.DaaRule"
@ -412,6 +401,7 @@ public class Foo {
</rule>
-->
</ruleset>

View File

@ -29,27 +29,11 @@ public class Foo {}
<rule name="DontImportJavaLang"
message="Avoid importing anything from the package 'java.lang'"
class="net.sourceforge.pmd.rules.XPathRule"
class="net.sourceforge.pmd.rules.imports.DontImportJavaLang"
externalInfoUrl="http://pmd.sourceforge.net/rules/imports.html#DontImportJavaLang">
<description>
Avoid importing anything from the package 'java.lang'. These classes are automatically imported (JLS 7.5.3).
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ImportDeclaration
[@Static = 'false']
[starts-with(Name/@Image, 'java.lang')]
[not(starts-with(Name/@Image, 'java.lang.ref'))]
[not(starts-with(Name/@Image, 'java.lang.reflect'))]
[not(starts-with(Name/@Image, 'java.lang.annotation'))]
[not(starts-with(Name/@Image, 'java.lang.instrument'))]
[not(starts-with(Name/@Image, 'java.lang.management'))]
]]>
</value>
</property>
</properties>
<priority>4</priority>
<example>
<![CDATA[

View File

@ -0,0 +1,28 @@
package net.sourceforge.pmd.rules.imports;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTImportDeclaration;
import net.sourceforge.pmd.ast.SimpleNode;
public class DontImportJavaLang extends AbstractRule {
public Object visit(ASTImportDeclaration node, Object data) {
if (node.isStatic()) {
return data;
}
String img = ((SimpleNode) node.jjtGetChild(0)).getImage();
if (img.startsWith("java.lang")) {
if (img.startsWith("java.lang.ref")
|| img.startsWith("java.lang.reflect")
|| img.startsWith("java.lang.annotation")
|| img.startsWith("java.lang.instrument")
|| img.startsWith("java.lang.management")) {
return data;
}
addViolation(data, node);
}
return data;
}
}

View File

@ -0,0 +1,17 @@
package net.sourceforge.pmd.rules.imports;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTImportDeclaration;
import net.sourceforge.pmd.ast.SimpleNode;
public class DontImportSun extends AbstractRule {
public Object visit(ASTImportDeclaration node, Object data) {
String img = ((SimpleNode) node.jjtGetChild(0)).getImage();
if (img.startsWith("sun.") && !img.startsWith("sun.misc.Signal")) {
addViolation(data, node);
}
return data;
}
}