KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
23
24 import org.apache.bcel.Constants;
25 import org.apache.bcel.classfile.Method;
26 import org.apache.bcel.generic.ConstantPoolGen;
27 import org.apache.bcel.generic.FieldInstruction;
28 import org.apache.bcel.generic.Instruction;
29 import org.apache.bcel.generic.ReferenceType;
30 import org.apache.bcel.generic.Type;
31
32 import edu.umd.cs.findbugs.BugReporter;
33 import edu.umd.cs.findbugs.Detector;
34 import edu.umd.cs.findbugs.TrainingDetector;
35 import edu.umd.cs.findbugs.ba.AnalysisContext;
36 import edu.umd.cs.findbugs.ba.CFG;
37 import edu.umd.cs.findbugs.ba.CFGBuilderException;
38 import edu.umd.cs.findbugs.ba.ClassContext;
39 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
40 import edu.umd.cs.findbugs.ba.Hierarchy;
41 import edu.umd.cs.findbugs.ba.Location;
42 import edu.umd.cs.findbugs.ba.XField;
43 import edu.umd.cs.findbugs.ba.type.FieldStoreType;
44 import edu.umd.cs.findbugs.ba.type.FieldStoreTypeDatabase;
45 import edu.umd.cs.findbugs.ba.type.TypeDataflow;
46 import edu.umd.cs.findbugs.ba.type.TypeFrame;
47
48 /**
49  * Build a database of reference types stored into fields.
50  * This can be used in the future to improve the precision
51  * of type analysis when values are loaded from fields.
52  *
53  * @author David Hovemeyer
54  */

55 public class TrainFieldStoreTypes implements Detector, TrainingDetector {
56     private BugReporter bugReporter;
57     private FieldStoreTypeDatabase database;
58     
59     public TrainFieldStoreTypes(BugReporter bugReporter) {
60         this.bugReporter = bugReporter;
61         this.database = new FieldStoreTypeDatabase();
62     }
63
64     public void visitClassContext(ClassContext classContext) {
65         Method[] methodList = classContext.getJavaClass().getMethods();
66         for (Method method : methodList) {
67             if (method.getCode() == null)
68                 continue;
69
70             try {
71                 analyzeMethod(classContext, method);
72             } catch (CFGBuilderException e) {
73                 bugReporter.logError("Error compting field store types", e);
74             } catch (DataflowAnalysisException e) {
75                 bugReporter.logError("Error compting field store types", e);
76             } catch (ClassNotFoundException JavaDoc e) {
77                 bugReporter.reportMissingClass(e);
78             }
79         }
80     }
81
82     private void analyzeMethod(ClassContext classContext, Method method)
83                 throws CFGBuilderException, DataflowAnalysisException, ClassNotFoundException JavaDoc {
84         CFG cfg = classContext.getCFG(method);
85         TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
86         ConstantPoolGen cpg = classContext.getConstantPoolGen();
87         
88         for (Iterator JavaDoc<Location> i = cfg.locationIterator(); i.hasNext();) {
89             Location location = i.next();
90             Instruction ins = location.getHandle().getInstruction();
91             short opcode = ins.getOpcode();
92             
93             // Field store instruction?
94
if (opcode != Constants.PUTFIELD && opcode != Constants.PUTSTATIC)
95                 continue;
96             
97             // Check if field type is a reference type
98
FieldInstruction fins = (FieldInstruction) ins;
99             Type fieldType = fins.getType(cpg);
100             if (!(fieldType instanceof ReferenceType))
101                 continue;
102             
103             // Find the exact field being stored into
104
XField xfield = Hierarchy.findXField(fins, cpg);
105             if (xfield == null)
106                 continue;
107             
108             // Skip public and protected fields, since it is reasonable to assume
109
// we won't see every store to those fields
110
if (xfield.isPublic() || xfield.isProtected())
111                 continue;
112             
113             // The top value on the stack is the one which will be stored
114
// into the field
115
TypeFrame frame = typeDataflow.getFactAtLocation(location);
116             if (!frame.isValid())
117                 continue;
118             Type storeType = frame.getTopValue();
119             if (!(storeType instanceof ReferenceType))
120                 continue;
121             
122             // Get or create the field store type set
123
FieldStoreType property = database.getProperty(xfield);
124             if (property == null) {
125                 property = new FieldStoreType();
126                 database.setProperty(xfield, property);
127             }
128             
129             // Add the store type to the set
130
property.addTypeSignature(storeType.getSignature());
131         }
132     }
133
134     public void report() {
135         AnalysisContext.currentAnalysisContext().storePropertyDatabase(
136                 database, FieldStoreTypeDatabase.DEFAULT_FILENAME, "store type database");
137     }
138
139 }
140
Popular Tags