    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 */
    019
    020package org.apache.bcel.classfile;
    021
    022import java.io.DataInput;
    023import java.io.DataOutputStream;
    024import java.io.IOException;
    025import java.util.Arrays;
    026import java.util.Iterator;
    027import java.util.stream.Stream;
    028
    029import org.apache.bcel.Const;
    030
    031/**
    032 * This class represents a MethodParameters attribute.
    033 *
    034 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24"> The class File Format :
    035 *      The MethodParameters Attribute</a>
    036 * @since 6.0
    037 */
    038public class MethodParameters extends Attribute implements Iterable<MethodParameter> {
    039
    040    /**
    041     * Empty array.
    042     */
    043    private static final MethodParameter[] EMPTY_ARRAY = {};
    044
    045    private MethodParameter[] parameters = EMPTY_ARRAY;
    046
    047    MethodParameters(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
    048        super(Const.ATTR_METHOD_PARAMETERS, nameIndex, length, constantPool);
    049        final int parameterCount = input.readUnsignedByte();
    050        parameters = new MethodParameter[parameterCount];
    051        for (int i = 0; i < parameterCount; i++) {
    052            parameters[i] = new MethodParameter(input);
    053        }
    054    }
    055
    056    @Override
    057    public void accept(final Visitor v) {
    058        v.visitMethodParameters(this);
    059    }
    060
    061    @Override
    062    public Attribute copy(final ConstantPool constantPool) {
    063        final MethodParameters c = (MethodParameters) clone();
    064        c.parameters = new MethodParameter[parameters.length];
    065        Arrays.setAll(c.parameters, i -> parameters[i].copy());
    066        c.setConstantPool(constantPool);
    067        return c;
    068    }
    069
    070    /**
    071     * Dump method parameters attribute to file stream in binary format.
    072     *
    073     * @param file Output file stream
    074     * @throws IOException if an I/O error occurs.
    075     */
    076    @Override
    077    public void dump(final DataOutputStream file) throws IOException {
    078        super.dump(file);
    079        file.writeByte(parameters.length);
    080        for (final MethodParameter parameter : parameters) {
    081            parameter.dump(file);
    082        }
    083    }
    084
    085    public MethodParameter[] getParameters() {
    086        return parameters;
    087    }
    088
    089    @Override
    090    public Iterator<MethodParameter> iterator() {
    091        return Stream.of(parameters).iterator();
    092    }
    093
    094    public void setParameters(final MethodParameter[] parameters) {
    095        this.parameters = parameters != null ? parameters : EMPTY_ARRAY;
    096    }
    097}