1 26 package net.sf.javaguard.classfile; 27 28 import java.util.Iterator ; 29 import java.util.Vector ; 30 31 32 38 public class ConstantPool { 39 40 private ClassFile classFile; 41 42 private Vector poolVector; 43 44 45 46 47 51 public ConstantPool(ClassFile classFile, CpInfo[] cpInfo) { 52 setClassFile(classFile); 53 int length = cpInfo.length; 54 Vector vec = new Vector (length); 55 vec.setSize(length); 56 for (int i = 0; i < length; i++) { 57 vec.setElementAt(cpInfo[i], i); 58 } 59 setPool(vec); 60 } 61 62 63 64 65 68 public Iterator iterator() { 69 return getPool().iterator(); 70 } 71 72 73 76 public int length() { 77 return getPool().size(); 78 } 79 80 81 82 83 88 public CpInfo getCpEntry(int index) 89 throws ArrayIndexOutOfBoundsException { 90 if (index < getPool().size()) { 91 return (CpInfo) getPool().elementAt(index); 92 } 93 throw new ArrayIndexOutOfBoundsException ("Constant Pool index out of range."); 94 } 95 96 97 98 99 102 public void updateRefCount() { 103 walkPool(new PoolAction() { 105 public void defaultAction(CpInfo cpInfo) { 106 cpInfo.resetRefCount(); 107 } 108 }); 109 110 getClassFile().markUtf8Refs(this); 112 113 getClassFile().markNTRefs(this); 115 116 walkPool(new PoolAction() { 118 public void utf8Action(Utf8CpInfo cpInfo) { 119 if (0 == cpInfo.getRefCount()) { 120 cpInfo.clearString(); 121 } 122 } 123 }); 124 } 125 126 127 128 129 133 public void incRefCount(int index) 134 throws ArrayIndexOutOfBoundsException { 135 if (index < getPool().size()) { 136 CpInfo cpInfo = (CpInfo) getPool().elementAt(index); 137 if (null != cpInfo) { 138 cpInfo.incRefCount(); 139 } else { 140 throw new IllegalStateException ("Illegal access to a Constant Pool element."); 141 } 142 } else { 143 throw new ArrayIndexOutOfBoundsException ("Constant Pool index out of range."); 144 } 145 } 146 147 148 149 150 155 public void decRefCount(int index) 156 throws ArrayIndexOutOfBoundsException { 157 if (index < getPool().size()) { 158 CpInfo cpInfo = (CpInfo) getPool().elementAt(index); 159 if (null != cpInfo) { 160 cpInfo.decRefCount(); 161 } else { 162 throw new IllegalStateException ("Illegal access to a Constant Pool element."); 163 } 164 } else { 165 throw new ArrayIndexOutOfBoundsException ("Constant Pool index out of range."); 166 } 167 } 168 169 170 171 172 177 public int remapUtf8To(String newString, int oldIndex) { 178 decRefCount(oldIndex); 179 return addUtf8Entry(newString); 180 } 181 182 183 184 185 189 public int addEntry(CpInfo entry) { 190 int oldLength = getPool().size(); 191 getPool().setSize(oldLength + 1); 192 getPool().setElementAt(entry, oldLength); 193 return oldLength; 194 } 195 196 197 198 199 203 private int addUtf8Entry(String str) { 204 for (int i = 0; i < getPool().size(); i++) { 207 Object obj = getPool().elementAt(i); 208 if (obj instanceof Utf8CpInfo) { 209 Utf8CpInfo entry = (Utf8CpInfo) obj; 210 if (entry.getString().equals(str)) { 211 entry.incRefCount(); 212 return i; 213 } 214 } 215 } 216 217 for (int i = 0; i < getPool().size(); i++) { 219 Object obj = getPool().elementAt(i); 220 if (obj instanceof Utf8CpInfo) { 221 Utf8CpInfo entry = (Utf8CpInfo) obj; 222 if (0 == entry.getRefCount()) { 223 entry.setString(str); 224 entry.incRefCount(); 225 return i; 226 } 227 } 228 } 229 230 return addEntry(new Utf8CpInfo(str)); 232 } 233 234 235 236 237 240 private void walkPool(PoolAction pa) { 241 for (int i=0; i<getPool().size(); i++) { 242 Object obj = getPool().elementAt(i); 243 if (null != obj) { 244 if (obj instanceof Utf8CpInfo) { 245 pa.utf8Action((Utf8CpInfo) obj); 246 } else if (obj instanceof CpInfo) { 247 pa.defaultAction((CpInfo) obj); 248 } 249 } 250 } 251 } 252 253 254 255 256 260 private void setClassFile(ClassFile classFile) { 261 this.classFile = classFile; 262 } 263 264 265 269 private ClassFile getClassFile() { 270 return this.classFile; 271 } 272 273 274 275 276 280 private void setPool(Vector vec) { 281 this.poolVector = vec; 282 } 283 284 285 289 private Vector getPool() { 290 return poolVector; 291 } 292 } 293 | Popular Tags |