KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > npe2 > DefinitelyNullSetAnalysis


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, 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.npe2;
21
22 import java.util.BitSet JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.bcel.Constants;
27 import org.apache.bcel.classfile.Method;
28 import org.apache.bcel.generic.InstructionHandle;
29
30 import edu.umd.cs.findbugs.ba.BasicBlock;
31 import edu.umd.cs.findbugs.ba.CFGBuilderException;
32 import edu.umd.cs.findbugs.ba.ClassContext;
33 import edu.umd.cs.findbugs.ba.CompactLocationNumbering;
34 import edu.umd.cs.findbugs.ba.Dataflow;
35 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
36 import edu.umd.cs.findbugs.ba.DataflowTestDriver;
37 import edu.umd.cs.findbugs.ba.DepthFirstSearch;
38 import edu.umd.cs.findbugs.ba.Edge;
39 import edu.umd.cs.findbugs.ba.EdgeTypes;
40 import edu.umd.cs.findbugs.ba.ForwardDataflowAnalysis;
41 import edu.umd.cs.findbugs.ba.Location;
42 import edu.umd.cs.findbugs.ba.npe.WillBeDereferencedInfo;
43 import edu.umd.cs.findbugs.ba.vna.ValueNumber;
44 import edu.umd.cs.findbugs.ba.vna.ValueNumberDataflow;
45 import edu.umd.cs.findbugs.ba.vna.ValueNumberFrame;
46
47 /**
48  * A simple null-pointer analysis that keeps track of which
49  * value numbers are definitely known to be null.
50  *
51  * @author David Hovemeyer
52  */

53 public class DefinitelyNullSetAnalysis extends ForwardDataflowAnalysis<DefinitelyNullSet> {
54     private ValueNumberDataflow vnaDataflow;
55     private CompactLocationNumbering compactLocationNumbering;
56     private Map JavaDoc<BasicBlock, Condition> conditionMap;
57     
58     private static final BitSet JavaDoc IFNULL_OPCODE_SET = new BitSet JavaDoc();
59     private static final BitSet JavaDoc IFACMP_OPCODE_SET = new BitSet JavaDoc();
60     private static final BitSet JavaDoc REFCMP_OPCODE_SET = new BitSet JavaDoc();
61     static {
62         IFNULL_OPCODE_SET.set(Constants.IFNULL);
63         IFNULL_OPCODE_SET.set(Constants.IFNONNULL);
64         
65         IFACMP_OPCODE_SET.set(Constants.IF_ACMPEQ);
66         IFACMP_OPCODE_SET.set(Constants.IF_ACMPNE);
67         
68         REFCMP_OPCODE_SET.or(IFNULL_OPCODE_SET);
69         REFCMP_OPCODE_SET.or(IFACMP_OPCODE_SET);
70     }
71     
72     /**
73      * Constructor.
74      *
75      * @param dfs DepthFirstSearch for the method
76      * @param vnaDataflow value number dataflow for the method
77      * @param compactLocationNumbering CompactLocationNumbering for the method
78      */

79     public DefinitelyNullSetAnalysis(
80             DepthFirstSearch dfs,
81             ValueNumberDataflow vnaDataflow,
82             CompactLocationNumbering compactLocationNumbering) {
83         super(dfs);
84         this.vnaDataflow = vnaDataflow;
85         this.compactLocationNumbering = compactLocationNumbering;
86         this.conditionMap = new HashMap JavaDoc<BasicBlock, Condition>();
87     }
88
89     /* (non-Javadoc)
90      * @see edu.umd.cs.findbugs.ba.AbstractDataflowAnalysis#isFactValid(java.lang.Object)
91      */

92     @Override JavaDoc
93     public boolean isFactValid(DefinitelyNullSet fact) {
94         return fact.isValid();
95     }
96
97     /* (non-Javadoc)
98      * @see edu.umd.cs.findbugs.ba.AbstractDataflowAnalysis#transferInstruction(org.apache.bcel.generic.InstructionHandle, edu.umd.cs.findbugs.ba.BasicBlock, java.lang.Object)
99      */

100     @Override JavaDoc
101     public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, DefinitelyNullSet fact) throws DataflowAnalysisException {
102         Location location = new Location(handle, basicBlock);
103         ValueNumberFrame vnaFrame = vnaDataflow.getFactAfterLocation(location);
104         
105         if (!vnaFrame.isValid()) {
106             fact.setTop();
107             return;
108         }
109         
110         // ACONST_NULL obviously produces a value that is DEFINITELY NULL.
111
// LDC produces values that are NOT NULL.
112
// NEW produces values that are NOT NULL.
113

114         short opcode = handle.getInstruction().getOpcode();
115         
116         if (opcode == Constants.ACONST_NULL) {
117             setTOS(vnaFrame, location, fact, NullnessValue.definitelyNullValue());
118         } else if (opcode == Constants.LDC || opcode == Constants.NEW) {
119             setTOS(vnaFrame, location, fact, NullnessValue.definitelyNotNullValue());
120         }
121         
122         // TODO: for method invocations, check return annotation
123

124         // Refresh condition/decision information
125
if (handle == basicBlock.getLastInstruction() && REFCMP_OPCODE_SET.get(opcode)) {
126             Condition condition = getCondition(basicBlock);
127             if (condition != null) {
128                 //System.out.println("handle: " + handle);
129
condition.refresh(vnaDataflow.getFactAtLocation(location), fact);
130             }
131         }
132     }
133     
134     /**
135      * Get the ConditionDecision providing information about the
136      * branch at the end of the given basic block.
137      *
138      * @param basicBlock
139      * @return the ConditionDecision, or null if the basic block
140      * does not end in a reference comparison
141      */

142     private Condition getCondition(BasicBlock basicBlock)
143             throws DataflowAnalysisException {
144         Condition condition = conditionMap.get(basicBlock);
145         if (condition == null) {
146             Location location = new Location(basicBlock.getLastInstruction(), basicBlock);
147             short opcode = basicBlock.getLastInstruction().getInstruction().getOpcode();
148             if (IFNULL_OPCODE_SET.get(opcode)) {
149                 condition = new IfNullCondition(location);
150             } else if (IFACMP_OPCODE_SET.get(opcode)) {
151                 //condition = new AcmpCondition(location);
152
return null;
153             } else {
154                 return null;
155             }
156             conditionMap.put(basicBlock, condition);
157         }
158         return condition;
159     }
160
161     /* (non-Javadoc)
162      * @see edu.umd.cs.findbugs.ba.BasicAbstractDataflowAnalysis#edgeTransfer(edu.umd.cs.findbugs.ba.Edge, java.lang.Object)
163      */

164     @Override JavaDoc
165     public void edgeTransfer(Edge edge, DefinitelyNullSet fact) throws DataflowAnalysisException {
166         if (!fact.isValid()) {
167             return;
168         }
169         
170         if (edge.getSource().isEmpty()) {
171             return;
172         }
173         
174         Condition condition = getCondition(edge.getSource());
175         if (condition == null) {
176             return;
177         }
178
179         Decision decision = condition.getDecision(edge);
180         if (!decision.isFeasible()) {
181             // This edge is not feasible.
182
// Set fact to TOP to avoid polluting dataflow information
183
// at a future control merge.
184
fact.setTop();
185             return;
186         }
187         
188         System.out.println("Setting " + condition.getValueNumber() + " to " + decision.getNullnessValue() + " on edge " + edge);
189         
190         changeNullnessOfValue(
191                 condition.getValueNumber(),
192                 condition.getLocation(),
193                 fact,
194                 decision.getNullnessValue());
195     }
196
197     /**
198      * Set the value on top of the stack as either null or unknown.
199      *
200      * @param vnaFrame ValueNumberFrame at Location
201      * @param location the Location
202      * @param fact DefinitelyNullSet to modify
203      * @param nullnessValue the nullness of the value number
204      * @throws DataflowAnalysisException
205      */

206     private void setTOS(ValueNumberFrame vnaFrame, Location location, DefinitelyNullSet fact, NullnessValue nullnessValue)
207             throws DataflowAnalysisException {
208         ValueNumber valueNumber = vnaFrame.getTopValue();
209         changeNullnessOfValue(valueNumber, location, fact, nullnessValue);
210     }
211
212     /**
213      * Change the nullness of a value number.
214      *
215      * @param valueNumber the ValueNumber
216      * @param location Location where information is gained
217      * @param fact the DefinitelyNullSet to modify
218      * @param nullnessValue the nullness of the value number
219      * @throws DataflowAnalysisException
220      */

221     private void changeNullnessOfValue(ValueNumber valueNumber, Location location, DefinitelyNullSet fact, NullnessValue nullnessValue) throws DataflowAnalysisException {
222 // fact.setValue(valueNumber, isNull);
223
// if (isNull) {
224
// fact.addAssignedNullLocation(valueNumber.getNumber(), compactLocationNumbering.getNumber(location));
225
// } else {
226
// fact.clearAssignNullLocations(valueNumber.getNumber());
227
// }
228

229         fact.setNullnessValue(valueNumber, nullnessValue);
230         
231         if (fact.getNulllessValue(valueNumber) != nullnessValue) {
232             throw new IllegalStateException JavaDoc();
233         }
234         
235         // TODO: set location where value becomes null or non-null
236
}
237
238     /* (non-Javadoc)
239      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#copy(java.lang.Object, java.lang.Object)
240      */

241     public void copy(DefinitelyNullSet source, DefinitelyNullSet dest) {
242         dest.makeSameAs(source);
243     }
244
245     /* (non-Javadoc)
246      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#createFact()
247      */

248     public DefinitelyNullSet createFact() {
249         // TODO: optionally create one with null locations
250
return new DefinitelyNullSet(vnaDataflow.getAnalysis().getNumValuesAllocated());
251     }
252
253     /* (non-Javadoc)
254      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#initEntryFact(java.lang.Object)
255      */

256     public void initEntryFact(DefinitelyNullSet result) throws DataflowAnalysisException {
257         // TODO: parameter annotations?
258
result.clear();
259     }
260
261     /* (non-Javadoc)
262      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#initResultFact(java.lang.Object)
263      */

264     public void initResultFact(DefinitelyNullSet result) {
265         result.setTop();
266     }
267
268     /* (non-Javadoc)
269      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#makeFactTop(java.lang.Object)
270      */

271     public void makeFactTop(DefinitelyNullSet fact) {
272         fact.setTop();
273     }
274     public boolean isTop(DefinitelyNullSet fact) {
275         return fact.isTop();
276     }
277     /* (non-Javadoc)
278      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#meetInto(java.lang.Object, edu.umd.cs.findbugs.ba.Edge, java.lang.Object)
279      */

280     public void meetInto(DefinitelyNullSet fact, Edge edge, DefinitelyNullSet result) throws DataflowAnalysisException {
281         // TODO: use edge information (ifnull, ifnonnull, etc.)
282

283         result.mergeWith(fact);
284     }
285
286     /* (non-Javadoc)
287      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#same(java.lang.Object, java.lang.Object)
288      */

289     public boolean same(DefinitelyNullSet fact1, DefinitelyNullSet fact2) {
290         return fact1.equals(fact2);
291     }
292     
293     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
294         if (args.length != 1) {
295             System.err.println("Usage: " + DefinitelyNullSetAnalysis.class.getName() + " <classfile>");
296             System.exit(1);
297         }
298         
299         DataflowTestDriver<DefinitelyNullSet, DefinitelyNullSetAnalysis> driver =
300             new DataflowTestDriver<DefinitelyNullSet, DefinitelyNullSetAnalysis>() {
301             /* (non-Javadoc)
302              * @see edu.umd.cs.findbugs.ba.DataflowTestDriver#createDataflow(edu.umd.cs.findbugs.ba.ClassContext, org.apache.bcel.classfile.Method)
303              */

304             @Override JavaDoc
305             public Dataflow<DefinitelyNullSet, DefinitelyNullSetAnalysis> createDataflow(ClassContext classContext, Method method) throws CFGBuilderException, DataflowAnalysisException {
306                 return classContext.getDefinitelyNullSetDataflow(method);
307             }
308         };
309         
310         driver.execute(args[0]);
311     }
312 }
313
Popular Tags