1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import java.util.BitSet ; 23 24 51 public class BlockType extends BitSet { 52 55 private static final long serialVersionUID = 1L; 56 public static final boolean CATCH = false; 57 public static final boolean FINALLY = true; 58 59 private boolean isValid; 60 private boolean isTop; 61 private int depth; 62 63 67 BlockType() { 68 } 69 70 75 public boolean isValid() { 76 return isValid; 77 } 78 79 83 public int getDepth() { 84 if (!isValid) throw new IllegalStateException (); 85 return depth; 86 } 87 88 91 public boolean getTopValue() { 92 if (depth == 0) throw new IllegalStateException (); 93 return get(depth - 1); 94 } 95 96 100 public boolean isNormal() { 101 if (!isValid) throw new IllegalStateException (); 102 return getDepth() == 0; 103 } 104 105 108 public void setNormal() { 109 this.isValid = true; 110 this.depth = 0; 111 } 112 113 116 public boolean isTop() { 117 return !isValid && isTop; 118 } 119 120 123 public void setTop() { 124 this.isValid = false; 125 this.isTop = true; 126 } 127 128 131 public boolean isBottom() { 132 return !isValid && !isTop; 133 } 134 135 138 public void setBottom() { 139 this.isValid = false; 140 this.isTop = false; 141 } 142 143 148 public void copyFrom(BlockType other) { 149 this.isValid = other.isValid; 150 this.isTop = other.isTop; 151 if (isValid) { 152 this.depth = other.depth; 153 this.clear(); 154 this.or(other); 155 } 156 } 157 158 165 public boolean sameAs(BlockType other) { 166 if (!this.isValid) { 167 return !other.isValid 168 && (this.isTop == other.isTop); 169 } else { 170 if (!other.isValid) 171 return false; 172 else { 173 if (this.depth != other.depth) 175 return false; 176 177 for (int i = 0; i < this.depth; ++i) { 179 if (this.get(i) != other.get(i)) 180 return false; 181 } 182 183 return true; 184 } 185 } 186 } 187 188 193 public void mergeWith(BlockType other) { 194 if (this.isTop() || other.isBottom()) { 195 copyFrom(other); 196 } else if (isValid()) { 197 int pfxLen = Math.min(this.depth, other.depth); 199 int commonLen; 200 for (commonLen = 0; commonLen < pfxLen; ++commonLen) { 201 if (this.get(commonLen) != other.get(commonLen)) 202 break; 203 } 204 this.depth = commonLen; 205 } 206 } 207 208 211 public void pushCatch() { 212 push(CATCH); 213 } 214 215 218 public void pushFinally() { 219 push(FINALLY); 220 } 221 222 @Override 223 public String toString() { 224 if (isTop()) 225 return "<top>"; 226 else if (isBottom()) 227 return "<bottom>"; 228 else { 229 StringBuffer buf = new StringBuffer (); 230 buf.append("N"); 231 for (int i = 0; i < depth; ++i) { 232 buf.append(get(i) == CATCH ? "C" : "F"); 233 } 234 return buf.toString(); 235 } 236 } 237 238 private void push(boolean value) { 239 set(depth, value); 240 ++depth; 241 } 242 } 243 244 | Popular Tags |