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