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