00001 package de.fub.bytecode.classfile;
00002
00003 import de.fub.bytecode.Constants;
00004 import java.io.*;
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 public final class ConstantValue extends Attribute {
00016 private int constantvalue_index;
00017
00018
00019
00020
00021
00022
00023
00024 public ConstantValue(int name_index, int length,
00025 int constantvalue_index,
00026 ConstantPool constant_pool)
00027 {
00028 super(Constants.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
00029 this.constantvalue_index = constantvalue_index;
00030 }
00031
00032
00033
00034
00035
00036
00037
00038
00039 ConstantValue(int name_index, int length, DataInputStream file,
00040 ConstantPool constant_pool) throws IOException
00041 {
00042 this(name_index, length, (int)file.readUnsignedShort(), constant_pool);
00043 }
00044
00045
00046
00047
00048 public ConstantValue(ConstantValue c) {
00049 this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(),
00050 c.getConstantPool());
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 public void accept(Visitor v) {
00060 v.visitConstantValue(this);
00061 }
00062
00063
00064
00065 private static final String convertString(String label) {
00066 char[] ch = label.toCharArray();
00067 StringBuffer buf = new StringBuffer();
00068
00069 for(int i=0; i < ch.length; i++) {
00070 switch(ch[i]) {
00071 case '\n':
00072 buf.append("\\n"); break;
00073 case '\r':
00074 buf.append("\\r"); break;
00075 case '\"':
00076 buf.append("\\\""); break;
00077 case '\'':
00078 buf.append("\\'"); break;
00079 case '\\':
00080 buf.append("\\\\"); break;
00081 default:
00082 buf.append(ch[i]); break;
00083 }
00084 }
00085
00086 return buf.toString();
00087 }
00088
00089
00090
00091 public Attribute copy(ConstantPool constant_pool) {
00092 ConstantValue c = (ConstantValue)clone();
00093 c.constant_pool = constant_pool;
00094 return c;
00095 }
00096
00097
00098
00099
00100
00101
00102 public final void dump(DataOutputStream file) throws IOException
00103 {
00104 super.dump(file);
00105 file.writeShort(constantvalue_index);
00106 }
00107
00108
00109
00110 public final int getConstantValueIndex() { return constantvalue_index; }
00111
00112
00113
00114 public final void setConstantValueIndex(int constantvalue_index) {
00115 this.constantvalue_index = constantvalue_index;
00116 }
00117
00118
00119
00120 public final String toString() throws InternalError
00121 {
00122 Constant c = constant_pool.getConstant(constantvalue_index);
00123
00124 String buf;
00125 int i;
00126
00127
00128 switch(c.getTag()) {
00129 case Constants.CONSTANT_Long: buf = "" + ((ConstantLong)c).getBytes(); break;
00130 case Constants.CONSTANT_Float: buf = "" + ((ConstantFloat)c).getBytes(); break;
00131 case Constants.CONSTANT_Double: buf = "" + ((ConstantDouble)c).getBytes(); break;
00132 case Constants.CONSTANT_Integer: buf = "" + ((ConstantInteger)c).getBytes(); break;
00133 case Constants.CONSTANT_String:
00134 i = ((ConstantString)c).getStringIndex();
00135 c = constant_pool.getConstant(i, Constants.CONSTANT_Utf8);
00136 buf = "\"" + convertString(((ConstantUtf8)c).getBytes()) + "\"";
00137 break;
00138 default: throw new InternalError("Type of ConstValue invalid: " + c);
00139 }
00140
00141 return buf;
00142 }
00143 }