1 25 package classycle.classfile; 26 27 import java.io.DataInputStream ; 28 import java.io.IOException ; 29 30 36 public abstract class Constant { 37 private static final int MAGIC = 0xcafebabe; 38 private static final int CONSTANT_CLASS = 7, 39 CONSTANT_FIELDREF = 9, 40 CONSTANT_METHODREF = 10, 41 CONSTANT_INTERFACE_METHODREF = 11, 42 CONSTANT_STRING = 8, 43 CONSTANT_INTEGER = 3, 44 CONSTANT_FLOAT = 4, 45 CONSTANT_LONG = 5, 46 CONSTANT_DOUBLE = 6, 47 CONSTANT_NAME_AND_TYPE = 12, 48 CONSTANT_UTF8 = 1; 49 50 56 public static Constant[] extractConstantPool(DataInputStream stream) 57 throws IOException { 58 Constant[] pool = null; 59 if (stream.readInt() == MAGIC) { 60 stream.readUnsignedShort(); 61 stream.readUnsignedShort(); 62 pool = new Constant[stream.readUnsignedShort()]; 63 for (int i = 1; i < pool.length; ) { 64 boolean skipIndex = false; 65 Constant c = null; 66 int type = stream.readUnsignedByte(); 67 switch (type) { 68 case CONSTANT_CLASS: 69 c = new ClassConstant(pool, stream.readUnsignedShort()); 70 break; 71 case CONSTANT_FIELDREF: 72 c = new FieldRefConstant(pool, stream.readUnsignedShort(), 73 stream.readUnsignedShort()); 74 break; 75 case CONSTANT_METHODREF: 76 c = new MethodRefConstant(pool, stream.readUnsignedShort(), 77 stream.readUnsignedShort()); 78 break; 79 case CONSTANT_INTERFACE_METHODREF: 80 c = new InterfaceMethodRefConstant(pool, 81 stream.readUnsignedShort(), 82 stream.readUnsignedShort()); 83 break; 84 case CONSTANT_STRING: 85 c = new StringConstant(pool, stream.readUnsignedShort()); 86 break; 87 case CONSTANT_INTEGER: 88 c = new IntConstant(pool, stream.readInt()); 89 break; 90 case CONSTANT_FLOAT: 91 c = new FloatConstant(pool, stream.readFloat()); 92 break; 93 case CONSTANT_LONG: 94 c = new LongConstant(pool, stream.readLong()); 95 skipIndex = true; 96 break; 97 case CONSTANT_DOUBLE: 98 c = new DoubleConstant(pool, stream.readDouble()); 99 skipIndex = true; 100 break; 101 case CONSTANT_NAME_AND_TYPE: 102 c = new NameAndTypeConstant(pool, stream.readUnsignedShort(), 103 stream.readUnsignedShort()); 104 break; 105 case CONSTANT_UTF8: 106 c = new UTF8Constant(pool, stream.readUTF()); 107 break; 108 } 109 pool[i] = c; 110 i += skipIndex ? 2 : 1; } 112 return pool; 113 } 114 throw new IOException ("Not a class file: Magic number missing."); 115 } 116 117 private Constant[] _pool; 118 119 123 public Constant(Constant[] pool) { 124 _pool = pool; 125 } 126 127 131 public Constant getConstant(int index) { 132 return _pool[index]; 133 } 134 } | Popular Tags |