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   
9   public class DontImportJavaLangRule extends AbstractJavaRule {
10  
11      private static final Package JAVA_LANG_PACKAGE = Package.getPackage("java.lang");
12  
13      @Override
14      public Object visit(ASTImportDeclaration node, Object data) {
15  
16  	if (node.isStatic()) {
17  	    return data;
18  	}
19  
20  	if (node.getPackage() != null) {
21  	    if (node.getPackage().equals(JAVA_LANG_PACKAGE)) {
22  		addViolation(data, node);
23  	    }
24  	} else {
25  	    String img = node.jjtGetChild(0).getImage();
26  	    if (img.startsWith("java.lang")) {
27  		if (img.startsWith("java.lang.ref") || img.startsWith("java.lang.reflect")
28  			|| img.startsWith("java.lang.annotation") || img.startsWith("java.lang.instrument")
29  			|| img.startsWith("java.lang.management") || img.startsWith("java.lang.Thread.")
30  			|| img.startsWith("java.lang.ProcessBuilder.")) {
31  		    return data;
32  		}
33  		addViolation(data, node);
34  	    }
35  	}
36  	return data;
37      }
38  }