forked from phoedos/pmd
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:
@ -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
|
||||
|
@ -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>
|
||||
|
||||
|
||||
|
@ -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[
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
17
pmd/src/net/sourceforge/pmd/rules/imports/DontImportSun.java
Normal file
17
pmd/src/net/sourceforge/pmd/rules/imports/DontImportSun.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user