1 package com.sun.org.apache.bcel.internal.classfile; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import java.io.*; 59 60 69 public class ConstantPool implements Cloneable , Node { 70 private int constant_pool_count; 71 private Constant[] constant_pool; 72 73 76 public ConstantPool(Constant[] constant_pool) 77 { 78 setConstantPool(constant_pool); 79 } 80 81 88 ConstantPool(DataInputStream file) throws IOException, ClassFormatError 89 { 90 byte tag; 91 92 constant_pool_count = file.readUnsignedShort(); 93 constant_pool = new Constant[constant_pool_count]; 94 95 98 for(int i=1; i < constant_pool_count; i++) { 99 constant_pool[i] = Constant.readConstant(file); 100 101 108 tag = constant_pool[i].getTag(); 109 if((tag == Constants.CONSTANT_Double) || (tag == Constants.CONSTANT_Long)) 110 i++; 111 } 112 } 113 114 121 public void accept(Visitor v) { 122 v.visitConstantPool(this); 123 } 124 125 131 public String constantToString(Constant c) 132 throws ClassFormatError 133 { 134 String str; 135 int i; 136 byte tag = c.getTag(); 137 138 switch(tag) { 139 case Constants.CONSTANT_Class: 140 i = ((ConstantClass)c).getNameIndex(); 141 c = getConstant(i, Constants.CONSTANT_Utf8); 142 str = Utility.compactClassName(((ConstantUtf8)c).getBytes(), false); 143 break; 144 145 case Constants.CONSTANT_String: 146 i = ((ConstantString)c).getStringIndex(); 147 c = getConstant(i, Constants.CONSTANT_Utf8); 148 str = "\"" + escape(((ConstantUtf8)c).getBytes()) + "\""; 149 break; 150 151 case Constants.CONSTANT_Utf8: str = ((ConstantUtf8)c).getBytes(); break; 152 case Constants.CONSTANT_Double: str = "" + ((ConstantDouble)c).getBytes(); break; 153 case Constants.CONSTANT_Float: str = "" + ((ConstantFloat)c).getBytes(); break; 154 case Constants.CONSTANT_Long: str = "" + ((ConstantLong)c).getBytes(); break; 155 case Constants.CONSTANT_Integer: str = "" + ((ConstantInteger)c).getBytes(); break; 156 157 case Constants.CONSTANT_NameAndType: 158 str = (constantToString(((ConstantNameAndType)c).getNameIndex(), 159 Constants.CONSTANT_Utf8) + " " + 160 constantToString(((ConstantNameAndType)c).getSignatureIndex(), 161 Constants.CONSTANT_Utf8)); 162 break; 163 164 case Constants.CONSTANT_InterfaceMethodref: case Constants.CONSTANT_Methodref: 165 case Constants.CONSTANT_Fieldref: 166 str = (constantToString(((ConstantCP)c).getClassIndex(), 167 Constants.CONSTANT_Class) + "." + 168 constantToString(((ConstantCP)c).getNameAndTypeIndex(), 169 Constants.CONSTANT_NameAndType)); 170 break; 171 172 default: throw new RuntimeException ("Unknown constant type " + tag); 174 } 175 176 return str; 177 } 178 179 private static final String escape(String str) { 180 int len = str.length(); 181 StringBuffer buf = new StringBuffer (len + 5); 182 char[] ch = str.toCharArray(); 183 184 for(int i=0; i < len; i++) { 185 switch(ch[i]) { 186 case '\n' : buf.append("\\n"); break; 187 case '\r' : buf.append("\\r"); break; 188 case '\t' : buf.append("\\t"); break; 189 case '\b' : buf.append("\\b"); break; 190 case '"' : buf.append("\\\""); break; 191 default: buf.append(ch[i]); 192 } 193 } 194 195 return buf.toString(); 196 } 197 198 199 207 public String constantToString(int index, byte tag) 208 throws ClassFormatError 209 { 210 Constant c = getConstant(index, tag); 211 return constantToString(c); 212 } 213 214 220 public void dump(DataOutputStream file) throws IOException 221 { 222 file.writeShort(constant_pool_count); 223 224 for(int i=1; i < constant_pool_count; i++) 225 if(constant_pool[i] != null) 226 constant_pool[i].dump(file); 227 } 228 229 236 public Constant getConstant(int index) { 237 if (index >= constant_pool.length || index < 0) 238 throw new ClassFormatError ("Invalid constant pool reference: " + 239 index + ". Constant pool size is: " + 240 constant_pool.length); 241 return constant_pool[index]; 242 } 243 244 254 public Constant getConstant(int index, byte tag) 255 throws ClassFormatError 256 { 257 Constant c; 258 259 c = getConstant(index); 260 261 if(c == null) 262 throw new ClassFormatError ("Constant pool at index " + index + " is null."); 263 264 if(c.getTag() == tag) 265 return c; 266 else 267 throw new ClassFormatError ("Expected class `" + Constants.CONSTANT_NAMES[tag] + 268 "' at index " + index + " and got " + c); 269 } 270 271 275 public Constant[] getConstantPool() { return constant_pool; } 276 289 public String getConstantString(int index, byte tag) 290 throws ClassFormatError 291 { 292 Constant c; 293 int i; 294 String s; 295 296 c = getConstant(index, tag); 297 298 305 switch(tag) { 306 case Constants.CONSTANT_Class: i = ((ConstantClass)c).getNameIndex(); break; 307 case Constants.CONSTANT_String: i = ((ConstantString)c).getStringIndex(); break; 308 default: 309 throw new RuntimeException ("getConstantString called with illegal tag " + tag); 310 } 311 312 c = getConstant(i, Constants.CONSTANT_Utf8); 314 return ((ConstantUtf8)c).getBytes(); 315 } 316 319 public int getLength() 320 { 321 return constant_pool_count; 322 } 323 324 327 public void setConstant(int index, Constant constant) { 328 constant_pool[index] = constant; 329 } 330 331 334 public void setConstantPool(Constant[] constant_pool) { 335 this.constant_pool = constant_pool; 336 constant_pool_count = (constant_pool == null)? 0 : constant_pool.length; 337 } 338 341 public String toString() { 342 StringBuffer buf = new StringBuffer (); 343 344 for(int i=1; i < constant_pool_count; i++) 345 buf.append(i + ")" + constant_pool[i] + "\n"); 346 347 return buf.toString(); 348 } 349 350 353 public ConstantPool copy() { 354 ConstantPool c = null; 355 356 try { 357 c = (ConstantPool)clone(); 358 } catch(CloneNotSupportedException e) {} 359 360 c.constant_pool = new Constant[constant_pool_count]; 361 362 for(int i=1; i < constant_pool_count; i++) { 363 if(constant_pool[i] != null) 364 c.constant_pool[i] = constant_pool[i].copy(); 365 } 366 367 return c; 368 } 369 } 370 | Popular Tags |