    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 * Entry of the parameters table.
    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 MethodParameter implements Cloneable {
    034
    035    /** Index of the CONSTANT_Utf8_info structure in the constant_pool table representing the name of the parameter */
    036    private int nameIndex;
    037
    038    /** The access flags */
    039    private int accessFlags;
    040
    041    public MethodParameter() {
    042    }
    043
    044    /**
    045     * Construct object from input stream.
    046     *
    047     * @param input Input stream
    048     * @throws java.io.IOException
    049     * @throws ClassFormatException
    050     */
    051    MethodParameter(final DataInput input) throws IOException {
    052        nameIndex = input.readUnsignedShort();
    053        accessFlags = input.readUnsignedShort();
    054    }
    055
    056    public int getNameIndex() {
    057        return nameIndex;
    058    }
    059
    060    public void setNameIndex(final int name_index) {
    061        this.nameIndex = name_index;
    062    }
    063
    064    /**
    065     * Returns the name of the parameter.
    066     */
    067    public String getParameterName(final ConstantPool constant_pool) {
    068        if (nameIndex == 0) {
    069            return null;
    070        }
    071        return ((ConstantUtf8) constant_pool.getConstant(nameIndex, Const.CONSTANT_Utf8)).getBytes();
    072       }
    073
    074    public int getAccessFlags() {
    075        return accessFlags;
    076    }
    077
    078    public void setAccessFlags(final int access_flags) {
    079        this.accessFlags = access_flags;
    080    }
    081
    082    public boolean isFinal() {
    083        return (accessFlags & Const.ACC_FINAL) != 0;
    084    }
    085
    086    public boolean isSynthetic() {
    087        return (accessFlags & Const.ACC_SYNTHETIC) != 0;
    088    }
    089
    090    public boolean isMandated() {
    091        return (accessFlags & Const.ACC_MANDATED) != 0;
    092    }
    093
    094    public void accept(final Visitor v) {
    095        v.visitMethodParameter(this);
    096    }
    097
    098    /**
    099     * Dump object to file stream on binary format.
    100     *
    101     * @param file Output file stream
    102     * @throws IOException
    103     */
    104    public final void dump(final DataOutputStream file) throws IOException {
    105        file.writeShort(nameIndex);
    106        file.writeShort(accessFlags);
    107    }
    108
    109    /**
    110     * @return deep copy of this object
    111     */
    112    public MethodParameter copy() {
    113        try {
    114            return (MethodParameter) clone();
    115        } catch (final CloneNotSupportedException e) {
    116            // TODO should this throw?
    117        }
    118        return null;
    119    }
    120}