KickJava   Java API By Example, From Geeks To Geeks.

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


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

20
21 package edu.umd.cs.findbugs.detect;
22
23
24 import edu.umd.cs.findbugs.*;
25 import edu.umd.cs.findbugs.ba.AnalysisContext;
26 import org.apache.bcel.classfile.*;
27
28 public class UseObjectEquals extends BytecodeScanningDetector implements StatelessDetector {
29     private BugReporter bugReporter;
30     private OpcodeStack stack = new OpcodeStack();
31
32     public UseObjectEquals(BugReporter bugReporter) {
33         this.bugReporter = bugReporter;
34     }
35
36
37
38     @Override JavaDoc
39          public void visit(Method obj) {
40         super.visit(obj);
41         stack.resetForMethodEntry(this);
42     }
43         
44     @Override JavaDoc
45          public void sawOpcode(int seen) {
46         stack.mergeJumps(this);
47         if ((seen == INVOKEVIRTUAL)
48         && getNameConstantOperand().equals("equals")
49         && getSigConstantOperand().equals("(Ljava/lang/Object;)Z")) {
50             
51             if (stack.getStackDepth() > 1) {
52                 OpcodeStack.Item item1 = stack.getStackItem(1);
53                 
54                     try {
55                         JavaClass cls = item1.getJavaClass();
56
57                         if ((cls != null) && cls.isFinal()) {
58                             if (item1.getSignature().equals("Ljava/lang/Class;"))
59                                 return;
60                             String JavaDoc methodClassName = getClassConstantOperand();
61                             if (methodClassName.equals("java/lang/Object")) {
62                                 if (!AnalysisContext.currentAnalysisContext().isApplicationClass(cls))
63                                     return;
64
65                                 bugReporter.reportBug(new BugInstance("UOE_USE_OBJECT_EQUALS", LOW_PRIORITY)
66                                     .addClassAndMethod(this)
67                                     .addSourceLine(this));
68                             }
69                         }
70                     } catch (ClassNotFoundException JavaDoc cnfe) {
71                         //cnfe.printStackTrace();
72
bugReporter.reportMissingClass(cnfe);
73                     }
74                 }
75         }
76
77         stack.sawOpcode(this, seen);
78     }
79 }
80
81 // vim:ts=4
82
Popular Tags