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   */
18  package org.apache.bcel.generic;
19  
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.bcel.classfile.ArrayElementValue;
26  import org.apache.bcel.classfile.ElementValue;
27  
28  /**
29   * @since 6.0
30   */
31  public class ArrayElementValueGen extends ElementValueGen
32  {
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      public ArrayElementValueGen(final ConstantPoolGen cp)
38      {
39          super(ARRAY, cp);
40          evalues = new ArrayList<>();
41      }
42  
43      public ArrayElementValueGen(final int type, final ElementValue[] datums,
44              final ConstantPoolGen cpool)
45      {
46          super(type, cpool);
47          if (type != ARRAY) {
48              throw new RuntimeException(
49                      "Only element values of type array can be built with this ctor - type specified: " + type);
50          }
51          this.evalues = new ArrayList<>();
52          for (final ElementValue datum : datums) {
53              evalues.add(ElementValueGen.copy(datum, cpool, true));
54          }
55      }
56  
57      /**
58       * Return immutable variant of this ArrayElementValueGen
59       */
60      @Override
61      public ElementValue getElementValue()
62      {
63          final ElementValue[] immutableData = new ElementValue[evalues.size()];
64          int i = 0;
65          for (final ElementValueGen element : evalues) {
66              immutableData[i++] = element.getElementValue();
67          }
68          return new ArrayElementValue(super.getElementValueType(),
69                  immutableData,
70                  getConstantPool().getConstantPool());
71      }
72  
73      /**
74       * @param value
75       * @param cpool
76       */
77      public ArrayElementValueGen(final ArrayElementValue value, final ConstantPoolGen cpool,
78              final boolean copyPoolEntries)
79      {
80          super(ARRAY, cpool);
81          evalues = new ArrayList<>();
82          final ElementValue[] in = value.getElementValuesArray();
83          for (final ElementValue element : in) {
84              evalues.add(ElementValueGen.copy(element, cpool, copyPoolEntries));
85          }
86      }
87  
88      @Override
89      public void dump(final DataOutputStream dos) throws IOException
90      {
91          dos.writeByte(super.getElementValueType()); // u1 type of value (ARRAY == '[')
92          dos.writeShort(evalues.size());
93          for (final ElementValueGen element : evalues) {
94              element.dump(dos);
95          }
96      }
97  
98      @Override
99      public String stringifyValue()
100     {
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 
113     public List<ElementValueGen> getElementValues()
114     {
115         return evalues;
116     }
117 
118     public int getElementValuesSize()
119     {
120         return evalues.size();
121     }
122 
123     public void addElement(final ElementValueGen gen)
124     {
125         evalues.add(gen);
126     }
127 }