View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.rules.imports;
5   
6   import net.sourceforge.pmd.AbstractRule;
7   import net.sourceforge.pmd.ast.ASTImportDeclaration;
8   
9   public class ImportFromSamePackageRule extends AbstractRule {
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  }