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;
5   
6   import java.util.List;
7   
8   import net.sourceforge.pmd.Rule;
9   import net.sourceforge.pmd.RuleContext;
10  import net.sourceforge.pmd.lang.ast.Node;
11  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
12  import net.sourceforge.pmd.lang.java.ast.JavaNode;
13  import net.sourceforge.pmd.lang.java.ast.JavaParserVisitor;
14  import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter;
15  import net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor;
16  import net.sourceforge.pmd.lang.rule.XPathRule;
17  
18  public class JavaRuleChainVisitor extends AbstractRuleChainVisitor {
19  
20  	protected void indexNodes(List<Node> nodes, RuleContext ctx) {
21  		JavaParserVisitor javaParserVistor = new JavaParserVisitorAdapter() {
22  			// Perform a visitation of the AST to index nodes which need
23  			// visiting by type
24  			public Object visit(JavaNode node, Object data) {
25  				indexNode(node);
26  				return super.visit(node, data);
27  			}
28  		};
29  
30  		for (int i = 0; i < nodes.size(); i++) {
31  			javaParserVistor.visit((ASTCompilationUnit)nodes.get(i), ctx);
32  		}
33  	}
34  
35  	protected void visit(Rule rule, Node node, RuleContext ctx) {
36  		// Rule better either be a JavaParserVisitor, or a XPathRule
37  		if (rule instanceof XPathRule) {
38  			((XPathRule)rule).evaluate(node, ctx);
39  		} else {
40  			((JavaNode)node).jjtAccept((JavaParserVisitor)rule, ctx);
41  		}
42  	}
43  }