KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > npe > WillBeDereferencedAnalysis


1 /*
2  * Bytecode analysis framework
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.npe;
21
22 import org.apache.bcel.generic.InstructionHandle;
23 import org.apache.bcel.generic.MethodGen;
24
25 import edu.umd.cs.findbugs.SystemProperties;
26 import edu.umd.cs.findbugs.ba.AnalysisContext;
27 import edu.umd.cs.findbugs.ba.AnalysisFeatures;
28 import edu.umd.cs.findbugs.ba.BackwardDataflowAnalysis;
29 import edu.umd.cs.findbugs.ba.BasicBlock;
30 import edu.umd.cs.findbugs.ba.CFG;
31 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
32 import edu.umd.cs.findbugs.ba.DepthFirstSearch;
33 import edu.umd.cs.findbugs.ba.Edge;
34 import edu.umd.cs.findbugs.ba.EdgeTypes;
35 import edu.umd.cs.findbugs.ba.Location;
36 import edu.umd.cs.findbugs.ba.ReverseDepthFirstSearch;
37 import edu.umd.cs.findbugs.ba.type.TypeDataflow;
38 import edu.umd.cs.findbugs.ba.vna.ValueNumber;
39 import edu.umd.cs.findbugs.ba.vna.ValueNumberDataflow;
40 import edu.umd.cs.findbugs.ba.vna.ValueNumberFrame;
41
42 /**
43  * Dataflow analysis to look for parameters dereferenced unconditionally.
44  * Flow values are sets of parameters (indexed starting from 0) which are
45  * dereferenced on every path past the current location.
46  *
47  * @author David Hovemeyer
48  */

49 public @Deprecated JavaDoc class WillBeDereferencedAnalysis extends BackwardDataflowAnalysis<WillBeDereferencedInfo> {
50      
51
52     private static final boolean DEBUG = SystemProperties.getBoolean("npe.deref.debug");
53     
54     private final CFG cfg;
55     private final MethodGen methodGen;
56     //private final TypeDataflow typeDataflow;
57
private final ValueNumberDataflow vnaDataflow;
58     //private final int maxBit;
59

60
61     
62     public WillBeDereferencedAnalysis(
63             ReverseDepthFirstSearch rdfs,
64             DepthFirstSearch dfs,
65             CFG cfg,
66             MethodGen methodGen,
67             ValueNumberDataflow vnaDataflow,
68             TypeDataflow typeDataflow) {
69         super(rdfs, dfs);
70         this.cfg = cfg;
71         this.methodGen = methodGen;
72         //this.typeDataflow = typeDataflow;
73
this.vnaDataflow = vnaDataflow;
74         //this.maxBit = methodGen.getMaxLocals();
75

76     }
77
78     public void copy(WillBeDereferencedInfo source, WillBeDereferencedInfo dest) {
79         dest.copyFrom(source);
80     }
81     
82     public WillBeDereferencedInfo createFact() {
83         return new WillBeDereferencedInfo();
84     }
85     
86     public void initEntryFact(WillBeDereferencedInfo result) throws DataflowAnalysisException {
87         // At entry (really the CFG exit, since this is a backwards analysis)
88
// no dereferences have been seen
89
result.value.clear();
90         result.isTop = false;
91     }
92     
93     public void initResultFact(WillBeDereferencedInfo result) {
94         makeFactTop(result);
95     }
96     
97     public void makeFactTop(WillBeDereferencedInfo fact) {
98         fact.isTop = true;
99         fact.value.clear();
100     }
101
102     public boolean isTop(WillBeDereferencedInfo fact) {
103         return fact.isTop;
104     }
105     public void meetInto(WillBeDereferencedInfo fact, Edge edge, WillBeDereferencedInfo result) throws DataflowAnalysisException {
106         // Ignore implicit exceptions
107
if (AnalysisContext.currentAnalysisContext().getBoolProperty(AnalysisFeatures.ACCURATE_EXCEPTIONS)
108                 && edge.isExceptionEdge()
109                 && !edge.isFlagSet(EdgeTypes.EXPLICIT_EXCEPTIONS_FLAG)) {
110             return;
111         }
112         result.meet(fact);
113     }
114     
115     public boolean same(WillBeDereferencedInfo fact1, WillBeDereferencedInfo fact2) {
116         return fact1.equals(fact2);
117     }
118     
119     @Override JavaDoc
120     public boolean isFactValid(WillBeDereferencedInfo fact) {
121         return !fact.isTop;
122     }
123     
124     @Override JavaDoc
125          public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, WillBeDereferencedInfo fact)
126         throws DataflowAnalysisException {
127         
128
129         if (!isFactValid(fact))
130             throw new IllegalStateException JavaDoc();
131
132         
133         // See if this instruction has a null check.
134
if (handle != basicBlock.getFirstInstruction())
135             return;
136         BasicBlock fallThroughPredecessor = cfg.getPredecessorWithEdgeType(basicBlock, EdgeTypes.FALL_THROUGH_EDGE);
137         if (fallThroughPredecessor == null || !fallThroughPredecessor.isNullCheck())
138             return;
139
140         // Get value number of the checked value
141
ValueNumberFrame vnaFrame = vnaDataflow.getFactAtLocation(new Location(handle, basicBlock));
142         if (!vnaFrame.isValid()) {
143             // Probably dead code.
144
// Assume this location can't be reached.
145
makeFactTop(fact);
146             return;
147         }
148         ValueNumber instance = vnaFrame.getInstance(handle.getInstruction(), methodGen.getConstantPool());
149         if (DEBUG) {
150             System.out.println("[Null check of value " + instance.getNumber() + "]");
151         }
152         fact.value.add(instance);
153         
154         
155     }
156
157     
158 }
159
Popular Tags