KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > BadUseOfReturnValue


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2005, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.detect;
21
22 import org.apache.bcel.classfile.Code;
23
24 import edu.umd.cs.findbugs.BugInstance;
25 import edu.umd.cs.findbugs.BugReporter;
26 import edu.umd.cs.findbugs.BytecodeScanningDetector;
27 import edu.umd.cs.findbugs.StatelessDetector;
28
29 public class BadUseOfReturnValue extends BytecodeScanningDetector {
30
31     BugReporter bugReporter;
32
33     public BadUseOfReturnValue(BugReporter bugReporter) {
34         this.bugReporter = bugReporter;
35     }
36
37
38
39     boolean readLineOnTOS = false;
40     boolean stringIndexOfOnTOS= false;
41     @Override JavaDoc
42          public void visit(Code obj) {
43         stringIndexOfOnTOS= false;
44         readLineOnTOS = false;
45         super.visit(obj);
46     }
47
48
49     @Override JavaDoc
50          public void sawOpcode(int seen) {
51         if (seen == INVOKEVIRTUAL &&
52             getNameConstantOperand().equals("indexOf")
53             && getClassConstantOperand().equals("java/lang/String")
54             && getSigConstantOperand().equals("(Ljava/lang/String;)I"))
55            stringIndexOfOnTOS= true;
56         else if (stringIndexOfOnTOS) {
57             if (seen == IFLE || seen == IFGT)
58                    bugReporter.reportBug(new BugInstance(this, "RV_CHECK_FOR_POSITIVE_INDEXOF", LOW_PRIORITY)
59                                 .addClassAndMethod(this)
60                                 .addSourceLine(this));
61             stringIndexOfOnTOS = false;
62         }
63
64         if (seen == INVOKEVIRTUAL &&
65             getNameConstantOperand().equals("readLine")
66             && getSigConstantOperand().equals("()Ljava/lang/String;"))
67           readLineOnTOS = true;
68         else if (readLineOnTOS) {
69             if (seen == IFNULL || seen == IFNONNULL)
70                    bugReporter.reportBug(new BugInstance(this, "RV_DONT_JUST_NULL_CHECK_READLINE", NORMAL_PRIORITY)
71                                 .addClassAndMethod(this)
72                                 .addSourceLine(this));
73
74             readLineOnTOS = false;
75             }
76     }
77
78 }
79
Popular Tags