View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.basic;
5   
6   import java.io.InputStream;
7   
8   import net.sourceforge.pmd.lang.ast.Node;
9   import net.sourceforge.pmd.lang.java.ast.ASTExpression;
10  import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
11  import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
12  import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
13  import net.sourceforge.pmd.lang.java.ast.ASTType;
14  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
15  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
16  import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
17  import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
18  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
19  
20  public class CheckSkipResultRule extends AbstractJavaRule {
21  
22      @Override
23      public Object visit(ASTVariableDeclaratorId node, Object data) {
24          ASTType typeNode = node.getTypeNode();
25          if (typeNode == null || !TypeHelper.isA(typeNode, InputStream.class)) {
26              return data;
27          }
28          for (NameOccurrence occ : node.getUsages()) {
29              JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
30              NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
31              if (qualifier != null && "skip".equals(qualifier.getImage())) {
32                  Node loc = jocc.getLocation();
33                  if (loc != null) {
34                      ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
35                      while (exp != null) {
36                          if (exp.jjtGetParent() instanceof ASTStatementExpression) {
37                              // if exp is in a bare statement,
38                              // the returned value is not used
39                              addViolation(data, occ.getLocation());
40                              break;
41                          } else if (exp.jjtGetParent() instanceof ASTExpression
42                                  && exp.jjtGetParent().jjtGetParent() instanceof ASTPrimaryPrefix) {
43                              // if exp is enclosed in a pair of parenthesis
44                              // let's have a look at the enclosing expression
45                              // we'll see if it's in a bare statement
46                              exp = exp.getFirstParentOfType(ASTPrimaryExpression.class);
47                          } else {
48                              // if exp is neither in a bare statement
49                              // or between a pair of parentheses,
50                              // it's in some other kind of statement
51                              // or assignement so the returned value is used
52                              break;
53                          }
54                      }
55                  }
56              }
57          }
58          return data;
59      }
60  }