View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @since 6.0
33   */
34  public class ArrayElementValueGen extends ElementValueGen {
35      // J5TODO: Should we make this an array or a list? A list would be easier to
36      // modify ...
37      private final List<ElementValueGen> evalues;
38  
39      /**
40       * @param value
41       * @param cpool
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()); // u1 type of value (ARRAY == '[')
72          dos.writeShort(evalues.size());
73          for (final ElementValueGen element : evalues) {
74              element.dump(dos);
75          }
76      }
77  
78      /**
79       * Return immutable variant of this ArrayElementValueGen
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 }