1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.generic;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.classfile.ConstantUtf8;
25 import org.apache.bcel.classfile.ElementValue;
26 import org.apache.bcel.classfile.ElementValuePair;
27
28
29
30
31 public class ElementValuePairGen {
32 private final int nameIdx;
33
34 private final ElementValueGen value;
35
36 private final ConstantPoolGen constantPoolGen;
37
38 public ElementValuePairGen(final ElementValuePair nvp, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
39 this.constantPoolGen = cpool;
40
41
42
43
44
45
46
47
48 if (copyPoolEntries) {
49 nameIdx = cpool.addUtf8(nvp.getNameString());
50 } else {
51 nameIdx = nvp.getNameIndex();
52 }
53 value = ElementValueGen.copy(nvp.getValue(), cpool, copyPoolEntries);
54 }
55
56 protected ElementValuePairGen(final int idx, final ElementValueGen value, final ConstantPoolGen cpool) {
57 this.nameIdx = idx;
58 this.value = value;
59 this.constantPoolGen = cpool;
60 }
61
62 public ElementValuePairGen(final String name, final ElementValueGen value, final ConstantPoolGen cpool) {
63 this.nameIdx = cpool.addUtf8(name);
64 this.value = value;
65 this.constantPoolGen = cpool;
66 }
67
68 protected void dump(final DataOutputStream dos) throws IOException {
69 dos.writeShort(nameIdx);
70 value.dump(dos);
71 }
72
73
74
75
76 public ElementValuePair getElementNameValuePair() {
77 final ElementValue immutableValue = value.getElementValue();
78 return new ElementValuePair(nameIdx, immutableValue, constantPoolGen.getConstantPool());
79 }
80
81 public int getNameIndex() {
82 return nameIdx;
83 }
84
85 public final String getNameString() {
86
87 return ((ConstantUtf8) constantPoolGen.getConstant(nameIdx)).getBytes();
88 }
89
90 public final ElementValueGen getValue() {
91 return value;
92 }
93
94 @Override
95 public String toString() {
96 return "ElementValuePair:[" + getNameString() + "=" + value.stringifyValue() + "]";
97 }
98 }