    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;
    023
    024import org.apache.bcel.Const;
    025
    026/**
    027 * This class represents a MethodParameters attribute.
    028 *
    029 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24">
    030 * The class File Format : The MethodParameters Attribute</a>
    031 * @since 6.0
    032 */
    033public class MethodParameters extends Attribute {
    034
    035    private MethodParameter[] parameters = new MethodParameter[0];
    036
    037    MethodParameters(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
    038        super(Const.ATTR_METHOD_PARAMETERS, name_index, length, constant_pool);
    039
    040        final int parameters_count = input.readUnsignedByte();
    041        parameters = new MethodParameter[parameters_count];
    042        for (int i = 0; i < parameters_count; i++) {
    043            parameters[i] = new MethodParameter(input);
    044        }
    045    }
    046
    047    public MethodParameter[] getParameters() {
    048        return parameters;
    049    }
    050
    051    public void setParameters(final MethodParameter[] parameters) {
    052        this.parameters = parameters;
    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 _constant_pool) {
    062        final MethodParameters c = (MethodParameters) clone();
    063        c.parameters = new MethodParameter[parameters.length];
    064
    065        for (int i = 0; i < parameters.length; i++) {
    066            c.parameters[i] = parameters[i].copy();
    067        }
    068        c.setConstantPool(_constant_pool);
    069        return c;
    070    }
    071
    072    /**
    073     * Dump method parameters attribute to file stream in binary format.
    074     *
    075     * @param file Output file stream
    076     * @throws IOException
    077     */
    078    @Override
    079       public void dump(final DataOutputStream file) throws IOException {
    080           super.dump(file);
    081           file.writeByte(parameters.length);
    082        for (final MethodParameter parameter : parameters) {
    083            parameter.dump(file);
    084        }
    085    }
    086}