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