    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.generic;
    019
    020import java.io.DataOutputStream;
    021import java.io.IOException;
    022import java.util.ArrayList;
    023import java.util.List;
    024
    025import org.apache.bcel.classfile.ArrayElementValue;
    026import org.apache.bcel.classfile.ElementValue;
    027
    028/**
    029 * @since 6.0
    030 */
    031public class ArrayElementValueGen extends ElementValueGen
    032{
    033    // J5TODO: Should we make this an array or a list? A list would be easier to
    034    // modify ...
    035    private final List<ElementValueGen> evalues;
    036
    037    public ArrayElementValueGen(final ConstantPoolGen cp)
    038    {
    039        super(ARRAY, cp);
    040        evalues = new ArrayList<>();
    041    }
    042
    043    public ArrayElementValueGen(final int type, final ElementValue[] datums,
    044            final ConstantPoolGen cpool)
    045    {
    046        super(type, cpool);
    047        if (type != ARRAY) {
    048            throw new IllegalArgumentException(
    049                    "Only element values of type array can be built with this ctor - type specified: " + type);
    050        }
    051        this.evalues = new ArrayList<>();
    052        for (final ElementValue datum : datums) {
    053            evalues.add(ElementValueGen.copy(datum, cpool, true));
    054        }
    055    }
    056
    057    /**
    058     * Return immutable variant of this ArrayElementValueGen
    059     */
    060    @Override
    061    public ElementValue getElementValue()
    062    {
    063        final ElementValue[] immutableData = new ElementValue[evalues.size()];
    064        int i = 0;
    065        for (final ElementValueGen element : evalues) {
    066            immutableData[i++] = element.getElementValue();
    067        }
    068        return new ArrayElementValue(super.getElementValueType(),
    069                immutableData,
    070                getConstantPool().getConstantPool());
    071    }
    072
    073    /**
    074     * @param value
    075     * @param cpool
    076     */
    077    public ArrayElementValueGen(final ArrayElementValue value, final ConstantPoolGen cpool,
    078            final boolean copyPoolEntries)
    079    {
    080        super(ARRAY, cpool);
    081        evalues = new ArrayList<>();
    082        final ElementValue[] in = value.getElementValuesArray();
    083        for (final ElementValue element : in) {
    084            evalues.add(ElementValueGen.copy(element, cpool, copyPoolEntries));
    085        }
    086    }
    087
    088    @Override
    089    public void dump(final DataOutputStream dos) throws IOException
    090    {
    091        dos.writeByte(super.getElementValueType()); // u1 type of value (ARRAY == '[')
    092        dos.writeShort(evalues.size());
    093        for (final ElementValueGen element : evalues) {
    094            element.dump(dos);
    095        }
    096    }
    097
    098    @Override
    099    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}