1 4 package gnu.bytecode; 5 import java.io.*; 6 7 8 11 12 public class ConstantValueAttr extends Attribute 13 { 14 Object value; 15 int value_index; 16 17 public Object getValue(ConstantPool cpool) 18 { 19 if (value != null) 20 return value; 21 CpoolEntry entry = cpool.getPoolEntry(value_index); 22 switch (entry.getTag()) 23 { 24 case ConstantPool.STRING: 25 value = ((CpoolString) entry).getString().getString(); 26 break; 27 case ConstantPool.INTEGER: 28 value = new Integer (((CpoolValue1) entry).value); 29 break; 30 case ConstantPool.LONG: 31 value = new Long (((CpoolValue2) entry).value); 32 break; 33 case ConstantPool.FLOAT: 34 float f = Float.intBitsToFloat(((CpoolValue1) entry).value); 35 value = new Float (f); 36 break; 37 case ConstantPool.DOUBLE: 38 double d = Double.longBitsToDouble(((CpoolValue2) entry).value); 39 value = new Double (d); 40 break; 41 } 42 return value; 43 } 44 45 public ConstantValueAttr (Object value) 46 { 47 super("ConstantValue"); 48 this.value = value; 49 } 50 51 public ConstantValueAttr (int index) 52 { 53 super("ConstantValue"); 54 this.value_index = index; 55 } 56 57 public void assignConstants (ClassType cl) 58 { 59 super.assignConstants(cl); 60 if (value_index == 0) 61 { 62 ConstantPool cpool = cl.getConstants(); 63 CpoolEntry entry = null; 64 if (value instanceof String ) 65 entry = cpool.addString((String ) value); 66 else if (value instanceof Integer ) 67 entry = cpool.addInt(((Integer ) value).intValue()); 68 else if (value instanceof Long ) 69 entry = cpool.addLong(((Long ) value).longValue()); 70 else if (value instanceof Float ) 71 entry = cpool.addFloat(((Float ) value).floatValue()); 72 else if (value instanceof Long ) 73 entry = cpool.addDouble(((Double ) value).doubleValue()); 74 value_index = entry.getIndex(); 75 } 76 } 77 78 public final int getLength() { return 2; } 79 80 public void write (DataOutputStream dstr) throws java.io.IOException 81 { 82 dstr.writeShort(value_index); 83 } 84 85 public void print (ClassTypeWriter dst) 86 { 87 dst.print("Attribute \""); 88 dst.print(getName()); 89 dst.print("\", length:"); 90 dst.print(getLength()); 91 dst.print(", value: "); 92 if (value_index == 0) 93 { 94 Object value = getValue(dst.ctype.constants); 95 if (value instanceof String ) 96 dst.printQuotedString((String ) value); 97 else 98 dst.print(value); 99 } 100 else 101 { 102 if (dst.printConstants) 103 { 104 dst.print(value_index); 105 dst.print('='); 106 } 107 CpoolEntry entry = dst.ctype.constants.getPoolEntry(value_index); 108 entry.print(dst, 1); 109 } 110 dst.println(); 111 } 112 } 113 | Popular Tags |