View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd;
5   
6   import java.util.List;
7   
8   import net.sourceforge.pmd.lang.DummyLanguageModule;
9   import net.sourceforge.pmd.lang.LanguageRegistry;
10  import net.sourceforge.pmd.lang.ast.Node;
11  import net.sourceforge.pmd.lang.rule.AbstractRule;
12  
13  /**
14   * Sample rule that detect any node with an image of "Foo". Used for testing.
15   */
16  public class FooRule extends AbstractRule {
17      public FooRule() {
18          setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
19      }
20  
21      public String getMessage() {
22          return "blah";
23      }
24  
25      public String getName() {
26          return "Foo";
27      }
28  
29      public String getRuleSetName() {
30          return "RuleSet";
31      }
32  
33      public String getDescription() {
34          return "desc";
35      }
36  
37      @Override
38      public void apply(List<? extends Node> nodes, RuleContext ctx) {
39          for (Node node : nodes) {
40              apply(node, ctx);
41          }
42      }
43  
44      protected void apply(Node node, RuleContext ctx) {
45          for (int i = 0; i < node.jjtGetNumChildren(); i++) {
46              apply(node.jjtGetChild(i), ctx);
47          }
48          if ("Foo".equals(node.getImage())) {
49              addViolation(ctx, node);
50          }
51      }
52  }