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.classfile;
019
020import java.io.DataInput;
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.util.Arrays;
024import java.util.Iterator;
025import java.util.stream.Stream;
026
027import org.apache.bcel.Const;
028
029/**
030 * This class represents a MethodParameters attribute.
031 *
032 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24"> The class File Format :
033 *      The MethodParameters Attribute</a>
034 * @since 6.0
035 */
036public class MethodParameters extends Attribute implements Iterable<MethodParameter> {
037
038    /**
039     * Empty array.
040     */
041    private static final MethodParameter[] EMPTY_METHOD_PARAMETER_ARRAY = {};
042
043    private MethodParameter[] parameters = EMPTY_METHOD_PARAMETER_ARRAY;
044
045    MethodParameters(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
046        super(Const.ATTR_METHOD_PARAMETERS, nameIndex, length, constantPool);
047
048        final int parameterCount = input.readUnsignedByte();
049        parameters = new MethodParameter[parameterCount];
050        for (int i = 0; i < parameterCount; i++) {
051            parameters[i] = new MethodParameter(input);
052        }
053    }
054
055    @Override
056    public void accept(final Visitor v) {
057        v.visitMethodParameters(this);
058    }
059
060    @Override
061    public Attribute copy(final ConstantPool constantPool) {
062        final MethodParameters c = (MethodParameters) clone();
063        c.parameters = new MethodParameter[parameters.length];
064
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;
096    }
097}