View Javadoc

    1   package net.sourceforge.pmd.lang.java.rule.imports;
    2
    3   import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
    4   import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
    5
    6   public class DontImportJavaLangRule extends AbstractJavaRule {
    7
    8       private static final Package JAVA_LANG_PACKAGE = Package.getPackage("java.lang");
    9
    10      @Override
    11      public Object visit(ASTImportDeclaration node, Object data) {
    12
    13  	if (node.isStatic()) {
    14  	    return data;
    15  	}
    16
    17  	if (node.getPackage() != null) {
    18  	    if (node.getPackage().equals(JAVA_LANG_PACKAGE)) {
    19  		addViolation(data, node);
    20  	    }
    21  	} else {
    22  	    String img = node.jjtGetChild(0).getImage();
    23  	    if (img.startsWith("java.lang")) {
    24  		if (img.startsWith("java.lang.ref") || img.startsWith("java.lang.reflect")
    25  			|| img.startsWith("java.lang.annotation") || img.startsWith("java.lang.instrument")
    26  			|| img.startsWith("java.lang.management") || img.startsWith("java.lang.Thread.")
    27  			|| img.startsWith("java.lang.ProcessBuilder.")) {
    28  		    return data;
    29  		}
    30  		addViolation(data, node);
    31  	    }
    32  	}
    33  	return data;
    34      }
    35  }