    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"> The class File Format :
    030 *      The MethodParameters Attribute</a>
    031 * @since 6.0
    032 */
    033public class MethodParameter implements Cloneable, Node {
    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 IOException if an I/O error occurs.
    049     * @throws ClassFormatException if a class is malformed or cannot be interpreted as a class file
    050     */
    051    MethodParameter(final DataInput input) throws IOException {
    052        nameIndex = input.readUnsignedShort();
    053        accessFlags = input.readUnsignedShort();
    054    }
    055
    056    @Override
    057    public void accept(final Visitor v) {
    058        v.visitMethodParameter(this);
    059    }
    060
    061    /**
    062     * @return deep copy of this object
    063     */
    064    public MethodParameter copy() {
    065        try {
    066            return (MethodParameter) clone();
    067        } catch (final CloneNotSupportedException e) {
    068            // TODO should this throw?
    069        }
    070        return null;
    071    }
    072
    073    /**
    074     * Dump object to file stream on binary format.
    075     *
    076     * @param file Output file stream
    077     * @throws IOException if an I/O error occurs.
    078     */
    079    public final void dump(final DataOutputStream file) throws IOException {
    080        file.writeShort(nameIndex);
    081        file.writeShort(accessFlags);
    082    }
    083
    084    public int getAccessFlags() {
    085        return accessFlags;
    086    }
    087
    088    public int getNameIndex() {
    089        return nameIndex;
    090    }
    091
    092    /**
    093     * Returns the name of the parameter.
    094     */
    095    public String getParameterName(final ConstantPool constantPool) {
    096        if (nameIndex == 0) {
    097            return null;
    098        }
    099        return constantPool.getConstantUtf8(nameIndex).getBytes();
    100    }
    101
    102    public boolean isFinal() {
    103        return (accessFlags & Const.ACC_FINAL) != 0;
    104    }
    105
    106    public boolean isMandated() {
    107        return (accessFlags & Const.ACC_MANDATED) != 0;
    108    }
    109
    110    public boolean isSynthetic() {
    111        return (accessFlags & Const.ACC_SYNTHETIC) != 0;
    112    }
    113
    114    public void setAccessFlags(final int accessFlags) {
    115        this.accessFlags = accessFlags;
    116    }
    117
    118    public void setNameIndex(final int nameIndex) {
    119        this.nameIndex = nameIndex;
    120    }
    121}