KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.bcel.classfile.Method;
27 import org.apache.bcel.generic.Type;
28
29 import edu.umd.cs.findbugs.BugInstance;
30 import edu.umd.cs.findbugs.BugReporter;
31 import edu.umd.cs.findbugs.BytecodeScanningDetector;
32 import edu.umd.cs.findbugs.OpcodeStack;
33 import edu.umd.cs.findbugs.StatelessDetector;
34 import edu.umd.cs.findbugs.visitclass.PreorderVisitor;
35
36
37 public class BadResultSetAccess extends BytecodeScanningDetector {
38
39     private static final Set JavaDoc<String JavaDoc> dbFieldTypesSet = new HashSet JavaDoc<String JavaDoc>()
40     {
41         static final long serialVersionUID = -3510636899394546735L;
42         {
43         add("Array");
44         add("AsciiStream");
45         add("BigDecimal");
46         add("BinaryStream");
47         add("Blob");
48         add("Boolean");
49         add("Byte");
50         add("Bytes");
51         add("CharacterStream");
52         add("Clob");
53         add("Date");
54         add("Double");
55         add("Float");
56         add("Int");
57         add("Long");
58         add("Object");
59         add("Ref");
60         add("Short");
61         add("String");
62         add("Time");
63         add("Timestamp");
64         add("UnicodeStream");
65         add("URL");
66         }
67     };
68     
69     private OpcodeStack stack = new OpcodeStack();
70     private BugReporter bugReporter;
71     
72     public BadResultSetAccess(BugReporter bugReporter) {
73         this.bugReporter = bugReporter;
74     }
75     
76
77     @Override JavaDoc
78          public void visit(Method obj) {
79         stack.resetForMethodEntry(this);
80         super.visit(obj);
81     }
82
83     @Override JavaDoc
84          public void sawOpcode(int seen) {
85         stack.mergeJumps(this);
86         try {
87             if (seen == INVOKEINTERFACE) {
88                 String JavaDoc methodName = getNameConstantOperand();
89                 String JavaDoc clsConstant = getClassConstantOperand();
90                 if ((clsConstant.equals("java/sql/ResultSet") &&
91                         ((methodName.startsWith("get") && dbFieldTypesSet.contains(methodName.substring(3))) ||
92                          (methodName.startsWith("update") && dbFieldTypesSet.contains(methodName.substring(6)))))
93                 || ((clsConstant.equals("java/sql/PreparedStatement") &&
94                         ((methodName.startsWith("set") && dbFieldTypesSet.contains(methodName.substring(3))))))) {
95                     String JavaDoc signature = getSigConstantOperand();
96                     int numParms = PreorderVisitor.getNumberArguments(signature);
97                     if (stack.getStackDepth() >= numParms) {
98                         OpcodeStack.Item item = stack.getStackItem(numParms-1);
99                         
100
101                         if ("I".equals(item.getSignature()) && item.couldBeZero()) {
102                             bugReporter.reportBug(new BugInstance(this,
103                                     clsConstant.equals("java/sql/PreparedStatement") ? "SQL_BAD_PREPARED_STATEMENT_ACCESS" : "SQL_BAD_RESULTSET_ACCESS",
104                                             item.mustBeZero() ? HIGH_PRIORITY : NORMAL_PRIORITY)
105                                     .addClassAndMethod(this)
106                                     .addSourceLine(this));
107                         }
108                     }
109                 }
110             }
111         } finally {
112             stack.sawOpcode(this, seen);
113         }
114     }
115 }
116
117 // vim:ts=4
118
Popular Tags