View Javadoc

    1   package net.sourceforge.pmd.lang.java.rule.unnecessary;
    2
    3   import net.sourceforge.pmd.lang.java.ast.ASTBlock;
    4   import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
    5   import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
    6   import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
    7   import net.sourceforge.pmd.lang.java.ast.ASTStatement;
    8   import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
    9
    10  public class UnnecessaryReturnRule extends AbstractJavaRule {
    11
    12      public Object visit(ASTMethodDeclaration node, Object data) {
    13
    14          if (node.getResultType().isVoid()) {
    15              super.visit(node, data);
    16          }
    17          return data;
    18      }
    19
    20      public Object visit(ASTReturnStatement node, Object data) {
    21          if (node.jjtGetParent() instanceof ASTStatement && node.getNthParent(2) instanceof ASTBlockStatement && node.getNthParent(3) instanceof ASTBlock
    22                  && node.getNthParent(4) instanceof ASTMethodDeclaration) {
    23              addViolation(data, node);
    24          }
    25          return data;
    26      }
    27
    28  }