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 net.sourceforge.pmd.lang.vm.ast.ASTBlock;
7   import net.sourceforge.pmd.lang.vm.ast.ASTForeachStatement;
8   import net.sourceforge.pmd.lang.vm.ast.ASTText;
9   import net.sourceforge.pmd.lang.vm.ast.AbstractVmNode;
10  import net.sourceforge.pmd.lang.vm.rule.AbstractVmRule;
11  
12  import org.apache.commons.lang3.StringUtils;
13  
14  public class EmptyForeachStmtRule extends AbstractVmRule {
15  
16      @Override
17      public Object visit(final ASTForeachStatement node, final Object data) {
18          final ASTBlock block = node.getFirstChildOfType(ASTBlock.class);
19          if (block.jjtGetNumChildren() == 0) {
20              addViolation(data, node);
21          }
22          else if (block.jjtGetNumChildren() == 1 && block.jjtGetChild(0) instanceof ASTText
23                  && StringUtils.isBlank(((AbstractVmNode)block.jjtGetChild(0)).getFirstToken().toString())) {
24              addViolation(data, node);
25          }
26          return super.visit(node, data);
27      }
28  
29  }