ImportFromSamePackageRule.java
01 /**
02  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03  */
04 package net.sourceforge.pmd.lang.java.rule.imports;
05 
06 import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
07 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
08 
09 public class ImportFromSamePackageRule extends AbstractJavaRule {
10 
11     public Object visit(ASTImportDeclaration importDecl, Object data) {
12         String packageName = importDecl.getScope().getEnclosingSourceFileScope().getPackageName();
13 
14         if (packageName != null && packageName.equals(importDecl.getPackageName())) {
15             addViolation(data, importDecl);
16         }
17 
18         // special case
19         if (packageName == null && importDecl.getPackageName().equals("")) {
20             addViolation(data, importDecl);
21         }
22         return data;
23     }
24 }