| 1 19 20 package edu.umd.cs.findbugs.ba.npe2; 21 22 import java.util.BitSet ; 23 24 import edu.umd.cs.findbugs.ba.DataflowAnalysisException; 25 import edu.umd.cs.findbugs.ba.vna.ValueNumber; 26 27 32 public class DefinitelyNullSet { 33 private BitSet contents; 34 private int numValueNumbers; 35 36 public DefinitelyNullSet(int numValueNumbers) { 37 this.contents = new BitSet (); 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 this.contents.and(other.contents); 110 } 111 112 public BitSet getAssignedNullLocationSet(ValueNumber vn) { 113 throw new UnsupportedOperationException (); 114 } 115 116 public void addAssignedNullLocation(int valueNumber, int locationNumber) { 117 } 119 120 public void clearAssignNullLocations(int valueNumber) { 121 } 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 143 @Override  144 public int hashCode() { 145 return contents.hashCode(); 146 } 147 148 151 @Override  152 public boolean equals(Object 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 164 @Override  165 public String toString() { 166 if (isTop()) { 167 return "[TOP]"; 168 } else if (isBottom()) { 169 return "[BOTTOM]"; 170 } else { 171 StringBuffer buf = new StringBuffer (); 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 |