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.classfile;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.util.Args;
25  
26  /**
27   * Represents an array element value in an annotation.
28   *
29   * @since 6.0
30   */
31  public class ArrayElementValue extends ElementValue {
32      // For array types, this is the array
33      private final ElementValue[] elementValues;
34  
35      /**
36       * Constructs an ArrayElementValue.
37       *
38       * @param type The type.
39       * @param elementValues The element values.
40       * @param cpool The constant pool.
41       */
42      public ArrayElementValue(final int type, final ElementValue[] elementValues, final ConstantPool cpool) {
43          super(type, cpool);
44          if (type != ARRAY) {
45              throw new ClassFormatException("Only element values of type array can be built with this ctor - type specified: " + type);
46          }
47          this.elementValues = elementValues != null ? elementValues : EMPTY_ARRAY;
48      }
49  
50      @Override
51      public void dump(final DataOutputStream dos) throws IOException {
52          dos.writeByte(super.getType()); // u1 type of value (ARRAY == '[')
53          dos.writeShort(Args.requireU2(elementValues.length, "elementValues.length"));
54          for (final ElementValue evalue : elementValues) {
55              evalue.dump(dos);
56          }
57      }
58  
59      /**
60       * Gets the element values array.
61       *
62       * @return The element values array.
63       */
64      public ElementValue[] getElementValuesArray() {
65          return elementValues;
66      }
67  
68      /**
69       * Gets the element values array size.
70       *
71       * @return The element values array size.
72       */
73      public int getElementValuesArraySize() {
74          return elementValues.length;
75      }
76  
77      @Override
78      public String stringifyValue() {
79          final StringBuilder sb = new StringBuilder();
80          sb.append("[");
81          for (int i = 0; i < elementValues.length; i++) {
82              sb.append(elementValues[i].stringifyValue());
83              if (i + 1 < elementValues.length) {
84                  sb.append(",");
85              }
86          }
87          sb.append("]");
88          return sb.toString();
89      }
90  
91      @Override
92      public String toString() {
93          final StringBuilder sb = new StringBuilder();
94          sb.append("{");
95          for (int i = 0; i < elementValues.length; i++) {
96              sb.append(elementValues[i]);
97              if (i + 1 < elementValues.length) {
98                  sb.append(",");
99              }
100         }
101         sb.append("}");
102         return sb.toString();
103     }
104 }