1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.classfile;
20
21 import java.io.DataInput;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24
25 import org.apache.bcel.Const;
26 import org.apache.bcel.util.Args;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public final class ConstantValue extends Attribute {
42
43 private int constantValueIndex;
44
45
46
47
48
49
50
51 public ConstantValue(final ConstantValue c) {
52 this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
53 }
54
55
56
57
58
59
60
61
62
63
64 ConstantValue(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
65 this(nameIndex, length, input.readUnsignedShort(), constantPool);
66 }
67
68
69
70
71
72
73
74 public ConstantValue(final int nameIndex, final int length, final int constantValueIndex, final ConstantPool constantPool) {
75 super(Const.ATTR_CONSTANT_VALUE, nameIndex, Args.require(length, 2, "ConstantValue attribute length"), constantPool);
76 this.constantValueIndex = constantValueIndex;
77 }
78
79
80
81
82
83
84
85 @Override
86 public void accept(final Visitor v) {
87 v.visitConstantValue(this);
88 }
89
90
91
92
93 @Override
94 public Attribute copy(final ConstantPool constantPool) {
95 final ConstantValue c = (ConstantValue) clone();
96 c.setConstantPool(constantPool);
97 return c;
98 }
99
100
101
102
103
104
105
106 @Override
107 public void dump(final DataOutputStream file) throws IOException {
108 super.dump(file);
109 file.writeShort(constantValueIndex);
110 }
111
112
113
114
115 public int getConstantValueIndex() {
116 return constantValueIndex;
117 }
118
119
120
121
122 public void setConstantValueIndex(final int constantValueIndex) {
123 this.constantValueIndex = constantValueIndex;
124 }
125
126
127
128
129 @Override
130 public String toString() {
131 Constant c = super.getConstantPool().getConstant(constantValueIndex);
132 final String buf;
133 final int i;
134
135 switch (c.getTag()) {
136 case Const.CONSTANT_Long:
137 buf = String.valueOf(((ConstantLong) c).getBytes());
138 break;
139 case Const.CONSTANT_Float:
140 buf = String.valueOf(((ConstantFloat) c).getBytes());
141 break;
142 case Const.CONSTANT_Double:
143 buf = String.valueOf(((ConstantDouble) c).getBytes());
144 break;
145 case Const.CONSTANT_Integer:
146 buf = String.valueOf(((ConstantInteger) c).getBytes());
147 break;
148 case Const.CONSTANT_String:
149 i = ((ConstantString) c).getStringIndex();
150 c = super.getConstantPool().getConstantUtf8(i);
151 buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
152 break;
153 default:
154 throw new IllegalStateException("Type of ConstValue invalid: " + c);
155 }
156 return buf;
157 }
158 }