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