    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
    024/**
    025 * Represents an array element value in an annotation.
    026 *
    027 * @since 6.0
    028 */
    029public class ArrayElementValue extends ElementValue {
    030    // For array types, this is the array
    031    private final ElementValue[] elementValues;
    032
    033    /**
    034     * Constructs an ArrayElementValue.
    035     *
    036     * @param type the type.
    037     * @param elementValues the element values.
    038     * @param cpool the constant pool.
    039     */
    040    public ArrayElementValue(final int type, final ElementValue[] elementValues, final ConstantPool cpool) {
    041        super(type, cpool);
    042        if (type != ARRAY) {
    043            throw new ClassFormatException("Only element values of type array can be built with this ctor - type specified: " + type);
    044        }
    045        this.elementValues = elementValues != null ? elementValues : EMPTY_ARRAY;
    046    }
    047
    048    @Override
    049    public void dump(final DataOutputStream dos) throws IOException {
    050        dos.writeByte(super.getType()); // u1 type of value (ARRAY == '[')
    051        dos.writeShort(elementValues.length);
    052        for (final ElementValue evalue : elementValues) {
    053            evalue.dump(dos);
    054        }
    055    }
    056
    057    /**
    058     * Gets the element values array.
    059     *
    060     * @return the element values array.
    061     */
    062    public ElementValue[] getElementValuesArray() {
    063        return elementValues;
    064    }
    065
    066    /**
    067     * Gets the element values array size.
    068     *
    069     * @return the element values array size.
    070     */
    071    public int getElementValuesArraySize() {
    072        return elementValues.length;
    073    }
    074
    075    @Override
    076    public String stringifyValue() {
    077        final StringBuilder sb = new StringBuilder();
    078        sb.append("[");
    079        for (int i = 0; i < elementValues.length; i++) {
    080            sb.append(elementValues[i].stringifyValue());
    081            if (i + 1 < elementValues.length) {
    082                sb.append(",");
    083            }
    084        }
    085        sb.append("]");
    086        return sb.toString();
    087    }
    088
    089    @Override
    090    public String toString() {
    091        final StringBuilder sb = new StringBuilder();
    092        sb.append("{");
    093        for (int i = 0; i < elementValues.length; i++) {
    094            sb.append(elementValues[i]);
    095            if (i + 1 < elementValues.length) {
    096                sb.append(",");
    097            }
    098        }
    099        sb.append("}");
    100        return sb.toString();
    101    }
    102}