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(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 public Attribute copy(ConstantPool constant_pool) {
00066 ConstantValue c = (ConstantValue)clone();
00067 c.constant_pool = constant_pool;
00068 return c;
00069 }
00070
00071
00072
00073
00074
00075
00076 public final void dump(DataOutputStream file) throws IOException
00077 {
00078 super.dump(file);
00079 file.writeShort(constantvalue_index);
00080 }
00081
00082
00083
00084 public final int getConstantValueIndex() { return constantvalue_index; }
00085
00086
00087
00088 public final void setConstantValueIndex(int constantvalue_index) {
00089 this.constantvalue_index = constantvalue_index;
00090 }
00091
00092
00093
00094 public final String toString() throws InternalError
00095 {
00096 Constant c = constant_pool.getConstant(constantvalue_index);
00097
00098 String buf;
00099 int i;
00100
00101
00102 switch(c.getTag()) {
00103 case CONSTANT_Long: buf = "" + ((ConstantLong)c).getBytes(); break;
00104 case CONSTANT_Float: buf = "" + ((ConstantFloat)c).getBytes(); break;
00105 case CONSTANT_Double: buf = "" + ((ConstantDouble)c).getBytes(); break;
00106 case CONSTANT_Integer: buf = "" + ((ConstantInteger)c).getBytes(); break;
00107 case CONSTANT_String:
00108 i = ((ConstantString)c).getStringIndex();
00109 c = constant_pool.getConstant(i, CONSTANT_Utf8);
00110 buf = "\"" + ((ConstantUtf8)c).getBytes() + "\"";
00111 break;
00112 default: throw new InternalError("Type of ConstValue invalid: " + c);
00113 }
00114
00115 return buf;
00116 }
00117 }