1 17 package org.apache.bcel.classfile; 18 19 import java.io.DataInputStream ; 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import org.apache.bcel.Constants; 23 24 33 public final class ConstantValue extends Attribute { 34 35 private int constantvalue_index; 36 37 38 42 public ConstantValue(ConstantValue c) { 43 this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool()); 44 } 45 46 47 55 ConstantValue(int name_index, int length, DataInputStream file, ConstantPool constant_pool) 56 throws IOException { 57 this(name_index, length, file.readUnsignedShort(), constant_pool); 58 } 59 60 61 67 public ConstantValue(int name_index, int length, int constantvalue_index, 68 ConstantPool constant_pool) { 69 super(Constants.ATTR_CONSTANT_VALUE, name_index, length, constant_pool); 70 this.constantvalue_index = constantvalue_index; 71 } 72 73 74 81 public void accept( Visitor v ) { 82 v.visitConstantValue(this); 83 } 84 85 86 92 public final void dump( DataOutputStream file ) throws IOException { 93 super.dump(file); 94 file.writeShort(constantvalue_index); 95 } 96 97 98 101 public final int getConstantValueIndex() { 102 return constantvalue_index; 103 } 104 105 106 109 public final void setConstantValueIndex( int constantvalue_index ) { 110 this.constantvalue_index = constantvalue_index; 111 } 112 113 114 117 public final String toString() { 118 Constant c = constant_pool.getConstant(constantvalue_index); 119 String buf; 120 int i; 121 switch (c.getTag()) { 123 case Constants.CONSTANT_Long: 124 buf = "" + ((ConstantLong) c).getBytes(); 125 break; 126 case Constants.CONSTANT_Float: 127 buf = "" + ((ConstantFloat) c).getBytes(); 128 break; 129 case Constants.CONSTANT_Double: 130 buf = "" + ((ConstantDouble) c).getBytes(); 131 break; 132 case Constants.CONSTANT_Integer: 133 buf = "" + ((ConstantInteger) c).getBytes(); 134 break; 135 case Constants.CONSTANT_String: 136 i = ((ConstantString) c).getStringIndex(); 137 c = constant_pool.getConstant(i, Constants.CONSTANT_Utf8); 138 buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\""; 139 break; 140 default: 141 throw new IllegalStateException ("Type of ConstValue invalid: " + c); 142 } 143 return buf; 144 } 145 146 147 150 public Attribute copy( ConstantPool _constant_pool ) { 151 ConstantValue c = (ConstantValue) clone(); 152 c.constant_pool = _constant_pool; 153 return c; 154 } 155 } 156 | Popular Tags |