View Javadoc
1 /* 2 * @(#)ConstantPoolInfo.java 1.5 95/08/16 Chuck McManis 3 * 4 * Copyright (c) 1996 Chuck McManis, All Rights Reserved. 5 * 6 * Permission to use, copy, modify, and distribute this software 7 * and its documentation for NON-COMMERCIAL purposes and without 8 * fee is hereby granted provided that this copyright notice 9 * appears in all copies. 10 * 11 * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 12 * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 13 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 14 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE 15 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, 16 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. 17 */ 18 19 package ch.elca.dependency.core.classinfo; 20 21 import java.io.DataInputStream; 22 import java.io.DataOutputStream; 23 import java.io.IOException; 24 25 /*** 26 * This class defines an entry in the constant pool for a Java class. 27 * The class file is primarily composed of ConstantPool entries and 28 * manipulation is done by modifying those entries. 29 * 30 * @version 1.5, 16 Aug 1995 31 * @author Chuck McManis 32 * @see ch.elca.dependency.rawmodel.classinfo.Reader 33 */ 34 35 public class ConstantPoolInfo{ 36 int type; // type of this item 37 String name; // String for the type 38 ConstantPoolInfo arg1; // index to first argument 39 ConstantPoolInfo arg2; // index to second argument 40 short index1, index2; 41 String strValue; // ASCIZ String value 42 int intValue; 43 long longValue; 44 float floatValue; 45 double doubleValue; 46 47 public static final int CLASS = 7; 48 public static final int FIELDREF = 9; 49 public static final int METHODREF = 10; 50 public static final int STRING = 8; 51 public static final int INTEGER = 3; 52 public static final int FLOAT = 4; 53 public static final int LONG = 5; 54 public static final int DOUBLE = 6; 55 public static final int INTERFACE = 11; 56 public static final int NAMEANDTYPE = 12; 57 public static final int ASCIZ = 1; 58 public static final int UNICODE = 2; 59 60 61 /*** 62 * Construct a new ConstantPoolInfo object that is of type ASCIZ 63 */ 64 public ConstantPoolInfo(String value) { 65 index1 = -1; 66 index2 = -1; 67 arg1 = null; 68 arg2 = null; 69 type = ASCIZ; 70 strValue = value; 71 } 72 73 /*** 74 * Construct a new ConstantPoolInfo object that is of type INTEGER 75 */ 76 public ConstantPoolInfo(int value) { 77 index1 = -1; 78 index2 = -1; 79 arg1 = null; 80 arg2 = null; 81 type = INTEGER; 82 intValue = value; 83 } 84 85 /*** 86 * Construct a new ConstantPoolInfo object that is of type FLOAT 87 */ 88 public ConstantPoolInfo(float value) { 89 index1 = -1; 90 index2 = -1; 91 arg1 = null; 92 arg2 = null; 93 type = FLOAT; 94 floatValue = value; 95 } 96 97 /*** 98 * Construct a new ConstantPoolInfo object that is of type LONG 99 */ 100 public ConstantPoolInfo(long value) { 101 index1 = -1; 102 index2 = -1; 103 arg1 = null; 104 arg2 = null; 105 type = LONG; 106 longValue = value; 107 } 108 109 /*** 110 * Construct a new ConstantPoolInfo object that is of type DOUBLE 111 */ 112 public ConstantPoolInfo(double value) { 113 index1 = -1; 114 index2 = -1; 115 arg1 = null; 116 arg2 = null; 117 type = DOUBLE; 118 doubleValue = value; 119 } 120 121 /*** 122 * Generic constructor 123 */ 124 public ConstantPoolInfo() { 125 index1 = -1; 126 index2 = -1; 127 arg1 = null; 128 arg2 = null; 129 type = -1; 130 } 131 132 /*** 133 * return the type of this constant pool item. 134 */ 135 public int isType() { 136 return (type); 137 } 138 139 public boolean read(DataInputStream dis) 140 throws IOException { 141 142 int len; 143 char c; 144 145 type = dis.readByte(); 146 switch ( type ) { 147 case CLASS: 148 name = "Class"; 149 index1 = dis.readShort(); 150 index2 = -1; 151 break; 152 case FIELDREF: 153 name = "Field Reference"; 154 index1 = dis.readShort(); 155 index2 = dis.readShort(); 156 break; 157 case METHODREF: 158 name = "Method Reference"; 159 index1 = dis.readShort(); 160 index2 = dis.readShort(); 161 break; 162 case INTERFACE: 163 name = "Interface Method Reference"; 164 index1 = dis.readShort(); 165 index2 = dis.readShort(); 166 break; 167 case NAMEANDTYPE: 168 name = "Name and Type"; 169 index1 = dis.readShort(); 170 index2 = dis.readShort(); 171 break; 172 case STRING: 173 name = "String"; 174 index1 = dis.readShort(); 175 index2 = -1; 176 break; 177 case INTEGER: 178 name = "Integer"; 179 intValue = dis.readInt(); 180 break; 181 case FLOAT: 182 name = "Float"; 183 floatValue = dis.readFloat(); 184 break; 185 case LONG: 186 name = "Long"; 187 longValue = dis.readLong(); 188 break; 189 case DOUBLE: 190 name = "Double"; 191 doubleValue = dis.readDouble(); 192 break; 193 case ASCIZ: 194 case UNICODE: 195 if ( type == ASCIZ ) { 196 name = "ASCIZ"; 197 } 198 else{ 199 name = "UNICODE"; 200 } 201 202 StringBuffer xxBuf = new StringBuffer(); 203 204 len = dis.readShort(); 205 while ( len > 0 ) { 206 c = (char) (dis.readByte()); 207 xxBuf.append(c); 208 len--; 209 } 210 strValue = xxBuf.toString(); 211 break; 212 default: 213 System.out.println("Warning bad type."); 214 } 215 return (true); 216 } 217 218 public void write(DataOutputStream dos, ConstantPoolInfo pool[]) 219 throws IOException, Exception { 220 dos.write(type); 221 switch ( type ) { 222 case CLASS: 223 case STRING: 224 dos.writeShort(indexOf(arg1, pool)); 225 break; 226 case FIELDREF: 227 case METHODREF: 228 case INTERFACE: 229 case NAMEANDTYPE: 230 dos.writeShort(indexOf(arg1, pool)); 231 dos.writeShort(indexOf(arg2, pool)); 232 break; 233 case INTEGER: 234 dos.writeInt(intValue); 235 break; 236 case FLOAT: 237 dos.writeFloat(floatValue); 238 break; 239 case LONG: 240 dos.writeLong(longValue); 241 break; 242 case DOUBLE: 243 dos.writeDouble(doubleValue); 244 break; 245 case ASCIZ: 246 case UNICODE: 247 dos.writeShort(strValue.length()); 248 dos.writeBytes(strValue); 249 break; 250 default: 251 throw new Exception("ConstantPoolInfo::write() - bad type."); 252 } 253 } 254 255 public String toString() { 256 StringBuffer s; 257 258 if ( type == ASCIZ ) { 259 return(strValue); 260 } 261 262 if ( type == INTEGER ) { 263 return("= "+intValue); 264 } 265 266 if ( type == LONG ) { 267 return("= "+longValue); 268 } 269 270 if ( type == FLOAT ) { 271 return("= "+floatValue); 272 } 273 274 if ( type == DOUBLE ) { 275 return("= "+doubleValue); 276 } 277 278 s = new StringBuffer(); 279 s.append(name); 280 s.append(":"); 281 if ( arg1 != null ) 282 s.append(arg1.toString()); 283 else if ( index1 != -1 ) 284 s.append("I1["+index1+"], "); 285 if ( arg2 != null ) 286 s.append(arg2.toString()); 287 else if ( index2 != -1 ) 288 s.append("I2["+index2+"], "); 289 return (s.toString()); 290 } 291 292 public static short indexOf(ConstantPoolInfo item, 293 ConstantPoolInfo pool[]) 294 throws Exception { 295 for ( int i = 0 ; i < pool.length ; i++ ) { 296 if ( item == pool[i] ) 297 return (short) i; 298 } 299 throw new Exception("ConstantPoolInfo:: indexOf() - item not in pool."); 300 } 301 302 /*** 303 * Returns true if these constant pool items are identical. 304 */ 305 public boolean isEqual(ConstantPoolInfo cp) { 306 if ( cp == null ) 307 return false; 308 309 if ( cp.type != type ) 310 return (false); 311 switch ( cp.type ) { 312 case CLASS: 313 case STRING: 314 return (arg1 == cp.arg1); 315 case FIELDREF: 316 case METHODREF: 317 case INTERFACE: 318 case NAMEANDTYPE: 319 return ((arg1 == cp.arg1) && (arg2 == cp.arg2)); 320 case INTEGER: 321 return (cp.intValue == intValue); 322 case FLOAT: 323 return (cp.floatValue == floatValue); 324 case LONG: 325 return (cp.longValue == longValue); 326 case DOUBLE: 327 return (cp.doubleValue == doubleValue); 328 case ASCIZ: 329 case UNICODE: 330 return (cp.strValue.compareTo(strValue) == 0); 331 } 332 return (false); 333 } 334 335 /*** 336 * Returns the reference to the constant pool item that is 337 * already in pool, that matches this one. 338 */ 339 public ConstantPoolInfo inPool(ConstantPoolInfo pool[]) { 340 for ( int i = 1 ; i < pool.length ; i++ ) { 341 if ( isEqual(pool[i]) ) 342 return (pool[i]); 343 } 344 return null; 345 } 346 }

This page was automatically generated by Maven