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_ARRAY = {};
042
043    private MethodParameter[] parameters = EMPTY_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        final int parameterCount = input.readUnsignedByte();
048        parameters = new MethodParameter[parameterCount];
049        for (int i = 0; i < parameterCount; i++) {
050            parameters[i] = new MethodParameter(input);
051        }
052    }
053
054    @Override
055    public void accept(final Visitor v) {
056        v.visitMethodParameters(this);
057    }
058
059    @Override
060    public Attribute copy(final ConstantPool constantPool) {
061        final MethodParameters c = (MethodParameters) clone();
062        c.parameters = new MethodParameter[parameters.length];
063        Arrays.setAll(c.parameters, i -> parameters[i].copy());
064        c.setConstantPool(constantPool);
065        return c;
066    }
067
068    /**
069     * Dump method parameters attribute to file stream in binary format.
070     *
071     * @param file Output file stream
072     * @throws IOException if an I/O error occurs.
073     */
074    @Override
075    public void dump(final DataOutputStream file) throws IOException {
076        super.dump(file);
077        file.writeByte(parameters.length);
078        for (final MethodParameter parameter : parameters) {
079            parameter.dump(file);
080        }
081    }
082
083    public MethodParameter[] getParameters() {
084        return parameters;
085    }
086
087    @Override
088    public Iterator<MethodParameter> iterator() {
089        return Stream.of(parameters).iterator();
090    }
091
092    public void setParameters(final MethodParameter[] parameters) {
093        this.parameters = parameters != null ? parameters : EMPTY_ARRAY;
094    }
095}