View Javadoc

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