refactored, new generalized importsPackage(packageName) function in superclass

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4612 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios
2006-10-10 04:51:43 +00:00
parent c7e7048c5b
commit 2e2c8b97af
2 changed files with 15 additions and 9 deletions

View File

@ -5,6 +5,7 @@ package net.sourceforge.pmd;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
import net.sourceforge.pmd.ast.ASTImportDeclaration;
import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
import net.sourceforge.pmd.ast.Node;
import net.sourceforge.pmd.ast.SimpleNode;
@ -246,4 +247,16 @@ public abstract class AbstractRule extends JavaParserVisitorAdapter implements R
public static boolean isQualifiedName(SimpleNode node) {
return node.getImage().indexOf('.') != -1;
}
public static boolean importsPackage(ASTCompilationUnit node, String packageName) {
List nodes = node.findChildrenOfType(ASTImportDeclaration.class);
for (Iterator i = nodes.iterator(); i.hasNext();) {
ASTImportDeclaration n = (ASTImportDeclaration) i.next();
if (n.getPackageName().startsWith(packageName)) {
return true;
}
}
return false;
}
}

View File

@ -125,16 +125,9 @@ public class CloseResource extends AbstractRule {
addViolation(data, id, clazz.getImage());
}
}
private boolean importsJavaSqlPackage(ASTCompilationUnit node) {
List nodes = node.findChildrenOfType(ASTImportDeclaration.class);
for (Iterator i = nodes.iterator(); i.hasNext();) {
ASTImportDeclaration n = (ASTImportDeclaration) i.next();
if (n.getPackageName().startsWith("java.sql")) {
return true;
}
}
return false;
return importsPackage(node, "java.sql");
}
}