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.ASTElseIfStatement;
8   import net.sourceforge.pmd.lang.vm.ast.ASTElseStatement;
9   import net.sourceforge.pmd.lang.vm.ast.ASTIfStatement;
10  import net.sourceforge.pmd.lang.vm.ast.ASTText;
11  import net.sourceforge.pmd.lang.vm.ast.AbstractVmNode;
12  import net.sourceforge.pmd.lang.vm.rule.AbstractVmRule;
13  
14  import org.apache.commons.lang3.StringUtils;
15  
16  public class EmptyIfStmtRule extends AbstractVmRule {
17      @Override
18      public Object visit(final ASTIfStatement node, final Object data) {
19          handleIf(node, data);
20          return super.visit(node, data);
21      }
22  
23      @Override
24      public Object visit(final ASTElseIfStatement node, final Object data) {
25          handleIf(node, data);
26          return super.visit(node, data);
27      }
28  
29      @Override
30      public Object visit(final ASTElseStatement node, final Object data) {
31          handleIf(node, data);
32          return super.visit(node, data);
33      }
34  
35      private void handleIf(final AbstractVmNode node, final Object data) {
36          final ASTBlock block = node.getFirstChildOfType(ASTBlock.class);
37          if (block.jjtGetNumChildren() == 0) {
38              addViolation(data, node);
39          }
40          else if (block.jjtGetNumChildren() == 1 && block.jjtGetChild(0) instanceof ASTText
41                  && StringUtils.isBlank(((AbstractVmNode)block.jjtGetChild(0)).getFirstToken().toString())) {
42              addViolation(data, node);
43          }
44      }
45  }