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 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.bcel.classfile.ArrayElementValue;
25 import org.apache.bcel.classfile.ElementValue;
26
27
28
29
30 public class ArrayElementValueGen extends ElementValueGen {
31
32
33 private final List<ElementValueGen> evalues;
34
35
36
37
38
39 public ArrayElementValueGen(final ArrayElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
40 super(ARRAY, cpool);
41 evalues = new ArrayList<>();
42 final ElementValue[] in = value.getElementValuesArray();
43 for (final ElementValue element : in) {
44 evalues.add(ElementValueGen.copy(element, cpool, copyPoolEntries));
45 }
46 }
47
48 public ArrayElementValueGen(final ConstantPoolGen cp) {
49 super(ARRAY, cp);
50 evalues = new ArrayList<>();
51 }
52
53 public ArrayElementValueGen(final int type, final ElementValue[] datums, final ConstantPoolGen cpool) {
54 super(type, cpool);
55 if (type != ARRAY) {
56 throw new IllegalArgumentException("Only element values of type array can be built with this ctor - type specified: " + type);
57 }
58 this.evalues = new ArrayList<>();
59 for (final ElementValue datum : datums) {
60 evalues.add(ElementValueGen.copy(datum, cpool, true));
61 }
62 }
63
64 public void addElement(final ElementValueGen gen) {
65 evalues.add(gen);
66 }
67
68 @Override
69 public void dump(final DataOutputStream dos) throws IOException {
70 dos.writeByte(super.getElementValueType());
71 dos.writeShort(evalues.size());
72 for (final ElementValueGen element : evalues) {
73 element.dump(dos);
74 }
75 }
76
77
78
79
80 @Override
81 public ElementValue getElementValue() {
82 final ElementValue[] immutableData = new ElementValue[evalues.size()];
83 int i = 0;
84 for (final ElementValueGen element : evalues) {
85 immutableData[i++] = element.getElementValue();
86 }
87 return new ArrayElementValue(super.getElementValueType(), immutableData, getConstantPool().getConstantPool());
88 }
89
90 public List<ElementValueGen> getElementValues() {
91 return evalues;
92 }
93
94 public int getElementValuesSize() {
95 return evalues.size();
96 }
97
98 @Override
99 public String stringifyValue() {
100 final StringBuilder sb = new StringBuilder();
101 sb.append("[");
102 String comma = "";
103 for (final ElementValueGen element : evalues) {
104 sb.append(comma);
105 comma = ",";
106 sb.append(element.stringifyValue());
107 }
108 sb.append("]");
109 return sb.toString();
110 }
111 }