KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-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
23 import edu.umd.cs.findbugs.*;
24 import org.apache.bcel.classfile.Method;
25
26 public class EqStringTest extends BytecodeScanningDetector implements StatelessDetector {
27     boolean constantOnTOS = false;
28     boolean callToInternSeen = false;
29     private BugReporter bugReporter;
30     // String stringOnTop;
31

32     public EqStringTest(BugReporter bugReporter) {
33         this.bugReporter = bugReporter;
34     }
35     
36
37
38     @Override JavaDoc
39          public void visit(Method obj) {
40         super.visit(obj);
41         constantOnTOS = false;
42         callToInternSeen = false;
43     }
44
45
46     @Override JavaDoc
47          public void sawOpcode(int seen) {
48
49         switch (seen) {
50         case LDC:
51             constantOnTOS = true;
52             // stringOnTop = stringConstant;
53
return;
54         case INVOKEVIRTUAL:
55             if (getRefConstantOperand().equals("java.lang.String.intern : ()Ljava.lang.String;")
56                     || getRefConstantOperand().equals("java.lang.String.equals : (Ljava.lang.Object;)Z"))
57                 callToInternSeen = true;
58             break;
59         case IF_ACMPEQ:
60         case IF_ACMPNE:
61             if (constantOnTOS && !callToInternSeen)
62                 bugReporter.reportBug(new BugInstance(this, "ES_COMPARING_STRINGS_WITH_EQ", NORMAL_PRIORITY)
63                         .addClassAndMethod(this)
64                         .addType("Ljava/lang/String;")
65                         .addSourceLine(this, getPC()));
66             break;
67         default:
68             break;
69         }
70         constantOnTOS = false;
71     }
72 }
73
Popular Tags