View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.vm.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.rule.AbstractRuleChainVisitor;
12  import net.sourceforge.pmd.lang.rule.XPathRule;
13  import net.sourceforge.pmd.lang.vm.ast.ASTprocess;
14  import net.sourceforge.pmd.lang.vm.ast.AbstractVmNode;
15  import net.sourceforge.pmd.lang.vm.ast.VmNode;
16  import net.sourceforge.pmd.lang.vm.ast.VmParserVisitor;
17  import net.sourceforge.pmd.lang.vm.ast.VmParserVisitorAdapter;
18  
19  public class VmRuleChainVisitor extends AbstractRuleChainVisitor {
20  
21      @Override
22      protected void indexNodes(final List<Node> nodes, final RuleContext ctx) {
23          final VmParserVisitor vmParserVisitor = new VmParserVisitorAdapter() {
24              // Perform a visitation of the AST to index nodes which need
25              // visiting by type
26              @Override
27              public Object visit(final VmNode node, final Object data) {
28                  indexNode(node);
29                  return super.visit(node, data);
30              }
31          };
32  
33          for (int i = 0; i < nodes.size(); i++) {
34              vmParserVisitor.visit((ASTprocess) nodes.get(i), ctx);
35          }
36      }
37  
38      @Override
39      protected void visit(final Rule rule, final Node node, final RuleContext ctx) {
40          // Rule better either be a VmParserVisitor, or a XPathRule
41          if (rule instanceof VmParserVisitor) {
42              ((AbstractVmNode) node).jjtAccept((VmParserVisitor) rule, ctx);
43          }
44          else {
45              ((XPathRule) rule).evaluate(node, ctx);
46          }
47      }
48  }