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.util.StringUtil;
    9
    10  public class ImportFromSamePackageRule extends AbstractJavaRule {
    11
    12      public Object visit(ASTImportDeclaration importDecl, Object data) {
    13          String packageName = importDecl.getScope().getEnclosingSourceFileScope().getPackageName();
    14
    15          if (packageName != null && packageName.equals(importDecl.getPackageName())) {
    16              addViolation(data, importDecl);
    17          }
    18
    19          // special case
    20          if (packageName == null && StringUtil.isEmpty(importDecl.getPackageName())) {
    21              addViolation(data, importDecl);
    22          }
    23          return data;
    24      }
    25  }