KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > heap > FieldSetAnalysis


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.ba.heap;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.bcel.Constants;
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.InstructionHandle;
30
31 import edu.umd.cs.findbugs.ba.AnalysisContext;
32 import edu.umd.cs.findbugs.ba.BasicBlock;
33 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
34 import edu.umd.cs.findbugs.ba.DepthFirstSearch;
35 import edu.umd.cs.findbugs.ba.Edge;
36 import edu.umd.cs.findbugs.ba.ForwardDataflowAnalysis;
37 import edu.umd.cs.findbugs.ba.Hierarchy;
38 import edu.umd.cs.findbugs.ba.XField;
39
40 /**
41  * @author David Hovemeyer
42  */

43 public abstract class FieldSetAnalysis extends ForwardDataflowAnalysis<FieldSet> {
44     private ConstantPoolGen cpg;
45     
46     private Map JavaDoc<InstructionHandle, XField> instructionToFieldMap;
47     
48     public FieldSetAnalysis(DepthFirstSearch dfs, ConstantPoolGen cpg) {
49         super(dfs);
50         this.cpg = cpg;
51         this.instructionToFieldMap = new HashMap JavaDoc<InstructionHandle, XField>();
52     }
53     
54     public ConstantPoolGen getCPG() {
55         return cpg;
56     }
57     
58     public void makeFactTop(FieldSet fact) {
59         fact.setTop();
60     }
61     public boolean isTop(FieldSet fact) {
62         return fact.isTop();
63     }
64     public void initEntryFact(FieldSet result) throws DataflowAnalysisException {
65         result.clear();
66     }
67     
68     public void initResultFact(FieldSet result) {
69         makeFactTop(result);
70     }
71     
72     public void meetInto(FieldSet fact, Edge edge, FieldSet result) throws DataflowAnalysisException {
73         result.mergeWith(fact);
74     }
75     
76     public boolean same(FieldSet fact1, FieldSet fact2) {
77         return fact1.sameAs(fact2);
78     }
79     
80     public FieldSet createFact() {
81         return new FieldSet();
82     }
83     
84     //@Override
85
@Override JavaDoc
86          public boolean isFactValid(FieldSet fact) {
87         return fact.isValid();
88     }
89     
90     public void copy(FieldSet source, FieldSet dest) {
91         dest.copyFrom(source);
92     }
93     
94     @Override JavaDoc
95          public void transferInstruction(
96             InstructionHandle handle,
97             BasicBlock basicBlock,
98             FieldSet fact) throws DataflowAnalysisException {
99         if (!isFactValid(fact))
100             return;
101         
102         try {
103             handleInstruction(handle, basicBlock, fact);
104         } catch (ClassNotFoundException JavaDoc e) {
105             AnalysisContext.reportMissingClass(e);
106             fact.setBottom();
107         }
108     }
109     
110     private void handleInstruction(
111             InstructionHandle handle,
112             BasicBlock basicBlock,
113             FieldSet fact) throws DataflowAnalysisException, ClassNotFoundException JavaDoc {
114         Instruction ins = handle.getInstruction();
115         short opcode = ins.getOpcode();
116         XField field;
117         
118         switch (opcode) {
119         case Constants.GETFIELD:
120         case Constants.GETSTATIC:
121             field = lookupField(handle, (FieldInstruction) ins);
122             if (field != null) {
123                 sawLoad(fact, field);
124             }
125             break;
126         
127         case Constants.PUTFIELD:
128         case Constants.PUTSTATIC:
129             field = lookupField(handle, (FieldInstruction) ins);
130             if (field != null) {
131                 sawStore(fact, field);
132             }
133             break;
134         
135         case Constants.INVOKEINTERFACE:
136         case Constants.INVOKESPECIAL:
137         case Constants.INVOKESTATIC:
138         case Constants.INVOKEVIRTUAL:
139             // Assume that the called method assigns loads and stores all possible fields
140
fact.setBottom();
141             break;
142         }
143     }
144     
145     private XField lookupField(InstructionHandle handle, FieldInstruction fins) throws ClassNotFoundException JavaDoc {
146         XField field = instructionToFieldMap.get(handle);
147         if (field == null) {
148             field = Hierarchy.findXField(fins, getCPG());
149             instructionToFieldMap.put(handle, field);
150         }
151         return field;
152     }
153     
154     protected abstract void sawLoad(FieldSet fact, XField field);
155     protected abstract void sawStore(FieldSet fact, XField field);
156 }
157
Popular Tags