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      @Override
12      public Object visit(ASTImportDeclaration node, Object data) {
13  
14  	if (node.isStatic()) {
15  	    return data;
16  	}
17  
18      String img = node.jjtGetChild(0).getImage();
19      if (img.startsWith("java.lang")) {
20  	if (img.startsWith("java.lang.ref") || img.startsWith("java.lang.reflect")
21  		|| img.startsWith("java.lang.annotation") || img.startsWith("java.lang.instrument")
22  		|| img.startsWith("java.lang.management") || img.startsWith("java.lang.Thread.")
23  		|| img.startsWith("java.lang.ProcessBuilder.")) {
24  	    return data;
25  	}
26  	addViolation(data, node);
27      }
28  	return data;
29      }
30  }