001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.classfile;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.util.Args;
025
026/**
027 * Represents an array element value in an annotation.
028 *
029 * @since 6.0
030 */
031public class ArrayElementValue extends ElementValue {
032    // For array types, this is the array
033    private final ElementValue[] elementValues;
034
035    /**
036     * Constructs an ArrayElementValue.
037     *
038     * @param type The type.
039     * @param elementValues The element values.
040     * @param cpool The constant pool.
041     */
042    public ArrayElementValue(final int type, final ElementValue[] elementValues, final ConstantPool cpool) {
043        super(type, cpool);
044        if (type != ARRAY) {
045            throw new ClassFormatException("Only element values of type array can be built with this ctor - type specified: " + type);
046        }
047        this.elementValues = elementValues != null ? elementValues : EMPTY_ARRAY;
048    }
049
050    @Override
051    public void dump(final DataOutputStream dos) throws IOException {
052        dos.writeByte(super.getType()); // u1 type of value (ARRAY == '[')
053        dos.writeShort(Args.requireU2(elementValues.length, "elementValues.length"));
054        for (final ElementValue evalue : elementValues) {
055            evalue.dump(dos);
056        }
057    }
058
059    /**
060     * Gets the element values array.
061     *
062     * @return The element values array.
063     */
064    public ElementValue[] getElementValuesArray() {
065        return elementValues;
066    }
067
068    /**
069     * Gets the element values array size.
070     *
071     * @return The element values array size.
072     */
073    public int getElementValuesArraySize() {
074        return elementValues.length;
075    }
076
077    @Override
078    public String stringifyValue() {
079        final StringBuilder sb = new StringBuilder();
080        sb.append("[");
081        for (int i = 0; i < elementValues.length; i++) {
082            sb.append(elementValues[i].stringifyValue());
083            if (i + 1 < elementValues.length) {
084                sb.append(",");
085            }
086        }
087        sb.append("]");
088        return sb.toString();
089    }
090
091    @Override
092    public String toString() {
093        final StringBuilder sb = new StringBuilder();
094        sb.append("{");
095        for (int i = 0; i < elementValues.length; i++) {
096            sb.append(elementValues[i]);
097            if (i + 1 < elementValues.length) {
098                sb.append(",");
099            }
100        }
101        sb.append("}");
102        return sb.toString();
103    }
104}