View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.imports;
5   
6   import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
7   import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
8   import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope;
9   import net.sourceforge.pmd.util.StringUtil;
10  
11  public class ImportFromSamePackageRule extends AbstractJavaRule {
12  
13      public Object visit(ASTImportDeclaration importDecl, Object data) {
14          String packageName = importDecl.getScope().getEnclosingScope(SourceFileScope.class).getPackageName();
15  
16          if (packageName != null && packageName.equals(importDecl.getPackageName())) {
17              addViolation(data, importDecl);
18          }
19  
20          // special case
21          if (packageName == null && StringUtil.isEmpty(importDecl.getPackageName())) {
22              addViolation(data, importDecl);
23          }
24          return data;
25      }
26  }