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  
24  import org.apache.bcel.classfile.ArrayElementValue;
25  import org.apache.bcel.classfile.ElementValue;
26  
27  /**
28   * @since 6.0
29   */
30  public class ArrayElementValueGen extends ElementValueGen {
31      // J5TODO: Should we make this an array or a list? A list would be easier to
32      // modify ...
33      private final List<ElementValueGen> evalues;
34  
35      /**
36       * @param value
37       * @param cpool
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()); // u1 type of value (ARRAY == '[')
71          dos.writeShort(evalues.size());
72          for (final ElementValueGen element : evalues) {
73              element.dump(dos);
74          }
75      }
76  
77      /**
78       * Return immutable variant of this ArrayElementValueGen
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 }