KickJava   Java API By Example, From Geeks To Geeks.

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


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.visitclass.LVTHelper;
26 import org.apache.bcel.classfile.*;
27
28 /**
29  * Find occurrences of a instanceof b where it can be determined
30  * statically whether this is true or false. This may signal a misunderstanding
31  * of the inheritance hierarchy in use, and potential bugs.
32  *
33  * @author Dave Brosius
34  */

35 public class SuperfluousInstanceOf extends BytecodeScanningDetector implements StatelessDetector {
36
37     private static final int SEEN_NOTHING = 0;
38     private static final int SEEN_ALOAD = 1;
39     
40     private BugReporter bugReporter;
41     private LocalVariableTable varTable;
42     private int state;
43     private int register;
44
45     public SuperfluousInstanceOf(BugReporter bugReporter) {
46         this.bugReporter = bugReporter;
47     }
48     
49
50
51     @Override JavaDoc
52          public void visit(Method obj) {
53         state = SEEN_NOTHING;
54         varTable = obj.getLocalVariableTable();
55         if (varTable != null)
56             super.visit(obj);
57     }
58     
59     @Override JavaDoc
60          public void visit(Code obj) {
61         if (varTable != null)
62             super.visit(obj);
63     }
64     
65     
66     @Override JavaDoc
67          public void sawOpcode(int seen) {
68         switch (state) {
69             case SEEN_NOTHING:
70                 if (seen == ALOAD)
71                     register = getRegisterOperand();
72                 else if ((seen >= ALOAD_0) && (seen <= ALOAD_3))
73                     register = seen - ALOAD_0;
74                 else
75                     return;
76                 state = SEEN_ALOAD;
77             break;
78             
79             case SEEN_ALOAD:
80                 try {
81                     if (seen == INSTANCEOF) {
82                         LocalVariable lv = LVTHelper.getLocalVariableAtPC(varTable, register, getPC());
83                         if (lv != null) {
84                             String JavaDoc objSignature = lv.getSignature();
85                             if (objSignature.charAt(0) == 'L') {
86                                 objSignature = objSignature.substring(1, objSignature.length()-1).replace('/', '.');
87                                 String JavaDoc clsSignature = getDottedClassConstantOperand();
88                                 
89                                 if (clsSignature.charAt(0) != '[') {
90                                     if (org.apache.bcel.Repository.instanceOf( objSignature, clsSignature )) {
91                                         bugReporter.reportBug(new BugInstance(this, "SIO_SUPERFLUOUS_INSTANCEOF", LOW_PRIORITY)
92                                             .addClassAndMethod(this)
93                                             .addSourceLine(this));
94                                     }
95                                 }
96                             }
97                         }
98                     }
99                 } catch (ClassNotFoundException JavaDoc cnfe) {
100                     bugReporter.reportMissingClass(cnfe);
101                 }
102                 
103                 state = SEEN_NOTHING;
104             break;
105         }
106         
107     }
108 }
109
110 // vim:ts=4
111
Popular Tags