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