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.basic;
5   
6   import java.util.HashSet;
7   import java.util.List;
8   import java.util.Set;
9   
10  import net.sourceforge.pmd.lang.vm.ast.ASTDirective;
11  import net.sourceforge.pmd.lang.vm.ast.ASTReference;
12  import net.sourceforge.pmd.lang.vm.ast.ASTSetDirective;
13  import net.sourceforge.pmd.lang.vm.rule.AbstractVmRule;
14  
15  public class AvoidReassigningParametersRule extends AbstractVmRule {
16  
17      @Override
18      public Object visit(final ASTDirective node, final Object data) {
19          if ("macro".equals(node.getDirectiveName())) {
20              final Set<String> paramNames = new HashSet<>();
21              final List<ASTReference> params = node.findChildrenOfType(ASTReference.class);
22              for (final ASTReference param : params) {
23                  paramNames.add(param.getFirstToken().toString());
24              }
25              final List<ASTSetDirective> assignments = node.findDescendantsOfType(ASTSetDirective.class);
26              for (final ASTSetDirective assignment : assignments) {
27                  final ASTReference ref = assignment.getFirstChildOfType(ASTReference.class);
28                  if (ref != null && paramNames.contains(ref.getFirstToken().toString())) {
29                      addViolation(data, node, ref.getFirstToken().toString());
30                  }
31              }
32          }
33          return super.visit(node, data);
34      }
35  }