KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
25 import edu.umd.cs.findbugs.ba.vna.ValueNumber;
26
27 /**
28  * Set of values that is definitely known to be null.
29  *
30  * @author David Hovemeyer
31  */

32 public class DefinitelyNullSet /*extends BitSet*/ {
33     private BitSet JavaDoc contents;
34     private int numValueNumbers;
35     
36     public DefinitelyNullSet(int numValueNumbers) {
37         this.contents = new BitSet JavaDoc();
38         this.numValueNumbers = numValueNumbers;
39     }
40     
41     public NullnessValue getNulllessValue(ValueNumber valueNumber) {
42         return getNullnessValue(valueNumber.getNumber());
43     }
44     
45     private NullnessValue getNullnessValue(int vn) {
46         int flags = 0;
47         
48         int start = getStartIndex(vn);
49         for (int i = 0; i < NullnessValue.FLAGS_MAX; i++) {
50             if (contents.get(start + i)) {
51                 flags |= (1 << i);
52             }
53         }
54         
55         return NullnessValue.fromFlags(flags);
56     }
57
58     public void setNullnessValue(ValueNumber valueNumber, NullnessValue nullnessValue) {
59         int flags = nullnessValue.getFlags();
60
61         int start = getStartIndex(valueNumber.getNumber());
62         for (int i = 0; i < NullnessValue.FLAGS_MAX; i++) {
63             contents.set(start + i, (flags & (1 << i)) != 0);
64         }
65     }
66     
67     public void clear() {
68         contents.clear();
69     }
70
71     public void setTop() {
72         contents.clear();
73         contents.set(lastUsedBit());
74     }
75     
76     public boolean isTop() {
77         return contents.get(lastUsedBit());
78     }
79     
80     public void setBottom() {
81         contents.clear();
82         contents.set(lastUsedBit() + 1);
83     }
84     
85     public boolean isBottom() {
86         return contents.get(lastUsedBit() + 1);
87     }
88
89     public boolean isValid() {
90         return !(isTop() || isBottom());
91     }
92     
93     public void makeSameAs(DefinitelyNullSet other) {
94         contents.clear();
95         contents.or(other.contents);
96     }
97     
98     public void mergeWith(DefinitelyNullSet other) {
99         if (this.isBottom() || other.isTop()) {
100             return;
101         }
102         
103         if (this.isTop() || other.isBottom()) {
104             this.makeSameAs(other);
105             return;
106         }
107         
108         // Result is intersection of sets
109
this.contents.and(other.contents);
110     }
111     
112     public BitSet JavaDoc getAssignedNullLocationSet(ValueNumber vn) {
113         throw new UnsupportedOperationException JavaDoc();
114     }
115
116     public void addAssignedNullLocation(int valueNumber, int locationNumber) {
117         // Base class does not maintain this information.
118
}
119
120     public void clearAssignNullLocations(int valueNumber) {
121         // Base class does not maintain this information.
122
}
123
124     private int getStartIndex(int vn) {
125         return vn * (1 << NullnessValue.FLAGS_MAX);
126     }
127
128     private int lastUsedBit() {
129         return numValueNumbers * NullnessValue.FLAGS_MAX;
130     }
131     
132     private int topBit() {
133         return lastUsedBit();
134     }
135     
136     private int bottomBit() {
137         return lastUsedBit() + 1;
138     }
139     
140     /* (non-Javadoc)
141      * @see java.lang.Object#hashCode()
142      */

143     @Override JavaDoc
144     public int hashCode() {
145         return contents.hashCode();
146     }
147     
148     /* (non-Javadoc)
149      * @see java.lang.Object#equals(java.lang.Object)
150      */

151     @Override JavaDoc
152     public boolean equals(Object JavaDoc obj) {
153         if (obj == null || obj.getClass() != this.getClass()) {
154             return false;
155         }
156         
157         DefinitelyNullSet other = (DefinitelyNullSet) obj;
158         return this.contents.equals(other.contents);
159     }
160     
161     /* (non-Javadoc)
162      * @see java.lang.Object#toString()
163      */

164     @Override JavaDoc
165     public String JavaDoc toString() {
166         if (isTop()) {
167             return "[TOP]";
168         } else if (isBottom()) {
169             return "[BOTTOM]";
170         } else {
171             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
172             boolean first = true;
173             
174             buf.append("{");
175             
176             for (int i = 0; i < numValueNumbers; i++) {
177                 NullnessValue val = getNullnessValue(i);
178                 if (val.isDefinitelyNull() || val.isDefinitelyNotNull()) {
179                     if (first) {
180                         first = false;
181                     } else {
182                         buf.append(", ");
183                     }
184                     buf.append(i);
185                     buf.append("->");
186                     buf.append(val.toString());
187                 }
188             }
189             
190             buf.append("}");
191             
192             return buf.toString();
193         }
194     }
195 }
196
Popular Tags