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