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 final class ConstantValue extends Attribute { 70 private int constantvalue_index; 71 72 76 public ConstantValue(ConstantValue c) { 77 this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), 78 c.getConstantPool()); 79 } 80 81 89 ConstantValue(int name_index, int length, DataInputStream file, 90 ConstantPool constant_pool) throws IOException 91 { 92 this(name_index, length, (int)file.readUnsignedShort(), constant_pool); 93 } 94 95 101 public ConstantValue(int name_index, int length, 102 int constantvalue_index, 103 ConstantPool constant_pool) 104 { 105 super(Constants.ATTR_CONSTANT_VALUE, name_index, length, constant_pool); 106 this.constantvalue_index = constantvalue_index; 107 } 108 109 116 public void accept(Visitor v) { 117 v.visitConstantValue(this); 118 } 119 125 public final void dump(DataOutputStream file) throws IOException 126 { 127 super.dump(file); 128 file.writeShort(constantvalue_index); 129 } 130 133 public final int getConstantValueIndex() { return constantvalue_index; } 134 135 138 public final void setConstantValueIndex(int constantvalue_index) { 139 this.constantvalue_index = constantvalue_index; 140 } 141 142 145 public final String toString() throws InternalError 146 { 147 Constant c = constant_pool.getConstant(constantvalue_index); 148 149 String buf; 150 int i; 151 152 switch(c.getTag()) { 154 case Constants.CONSTANT_Long: buf = "" + ((ConstantLong)c).getBytes(); break; 155 case Constants.CONSTANT_Float: buf = "" + ((ConstantFloat)c).getBytes(); break; 156 case Constants.CONSTANT_Double: buf = "" + ((ConstantDouble)c).getBytes(); break; 157 case Constants.CONSTANT_Integer: buf = "" + ((ConstantInteger)c).getBytes(); break; 158 case Constants.CONSTANT_String: 159 i = ((ConstantString)c).getStringIndex(); 160 c = constant_pool.getConstant(i, Constants.CONSTANT_Utf8); 161 buf = "\"" + convertString(((ConstantUtf8)c).getBytes()) + "\""; 162 break; 163 default: throw new InternalError ("Type of ConstValue invalid: " + c); 164 } 165 166 return buf; 167 } 168 169 172 private static final String convertString(String label) { 173 char[] ch = label.toCharArray(); 174 StringBuffer buf = new StringBuffer (); 175 176 for(int i=0; i < ch.length; i++) { 177 switch(ch[i]) { 178 case '\n': 179 buf.append("\\n"); break; 180 case '\r': 181 buf.append("\\r"); break; 182 case '\"': 183 buf.append("\\\""); break; 184 case '\'': 185 buf.append("\\'"); break; 186 case '\\': 187 buf.append("\\\\"); break; 188 default: 189 buf.append(ch[i]); break; 190 } 191 } 192 193 return buf.toString(); 194 } 195 196 199 public Attribute copy(ConstantPool constant_pool) { 200 ConstantValue c = (ConstantValue)clone(); 201 c.constant_pool = constant_pool; 202 return c; 203 } 204 } 205 | Popular Tags |