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.generic;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.stream.Collectors;
026
027import org.apache.bcel.classfile.ArrayElementValue;
028import org.apache.bcel.classfile.ElementValue;
029import org.apache.commons.lang3.stream.Streams;
030
031/**
032 * @since 6.0
033 */
034public class ArrayElementValueGen extends ElementValueGen {
035    // J5TODO: Should we make this an array or a list? A list would be easier to
036    // modify ...
037    private final List<ElementValueGen> evalues;
038
039    /**
040     * @param value
041     * @param cpool
042     */
043    public ArrayElementValueGen(final ArrayElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
044        super(ARRAY, cpool);
045        evalues = new ArrayList<>();
046        final ElementValue[] in = value.getElementValuesArray();
047        for (final ElementValue element : in) {
048            evalues.add(copy(element, cpool, copyPoolEntries));
049        }
050    }
051
052    public ArrayElementValueGen(final ConstantPoolGen cp) {
053        super(ARRAY, cp);
054        evalues = new ArrayList<>();
055    }
056
057    public ArrayElementValueGen(final int type, final ElementValue[] elementValues, final ConstantPoolGen cpool) {
058        super(type, cpool);
059        if (type != ARRAY) {
060            throw new IllegalArgumentException("Only element values of type array can be built with this ctor - type specified: " + type);
061        }
062        this.evalues = Streams.of(elementValues).map(e -> copy(e, cpool, true)).collect(Collectors.toList());
063    }
064
065    public void addElement(final ElementValueGen gen) {
066        evalues.add(gen);
067    }
068
069    @Override
070    public void dump(final DataOutputStream dos) throws IOException {
071        dos.writeByte(super.getElementValueType()); // u1 type of value (ARRAY == '[')
072        dos.writeShort(evalues.size());
073        for (final ElementValueGen element : evalues) {
074            element.dump(dos);
075        }
076    }
077
078    /**
079     * Return immutable variant of this ArrayElementValueGen
080     */
081    @Override
082    public ElementValue getElementValue() {
083        final ElementValue[] immutableData = new ElementValue[evalues.size()];
084        int i = 0;
085        for (final ElementValueGen element : evalues) {
086            immutableData[i++] = element.getElementValue();
087        }
088        return new ArrayElementValue(super.getElementValueType(), immutableData, getConstantPool().getConstantPool());
089    }
090
091    public List<ElementValueGen> getElementValues() {
092        return evalues;
093    }
094
095    public int getElementValuesSize() {
096        return evalues.size();
097    }
098
099    @Override
100    public String stringifyValue() {
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}