    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="http://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 name_index;
    037
    038    /** The access flags */
    039    private int access_flags;
    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        name_index = input.readUnsignedShort();
    053        access_flags = input.readUnsignedShort();
    054    }
    055
    056    public int getNameIndex() {
    057        return name_index;
    058    }
    059
    060    public void setNameIndex(final int name_index) {
    061        this.name_index = name_index;
    062    }
    063
    064    /**
    065     * Returns the name of the parameter.
    066     */
    067    public String getParameterName(final ConstantPool constant_pool) {
    068        if (name_index == 0) {
    069            return null;
    070        }
    071        return ((ConstantUtf8) constant_pool.getConstant(name_index, Const.CONSTANT_Utf8)).getBytes();
    072       }
    073
    074    public int getAccessFlags() {
    075        return access_flags;
    076    }
    077
    078    public void setAccessFlags(final int access_flags) {
    079        this.access_flags = access_flags;
    080    }
    081
    082    public boolean isFinal() {
    083        return (access_flags & Const.ACC_FINAL) != 0;
    084    }
    085
    086    public boolean isSynthetic() {
    087        return (access_flags & Const.ACC_SYNTHETIC) != 0;
    088    }
    089
    090    public boolean isMandated() {
    091        return (access_flags & Const.ACC_MANDATED) != 0;
    092    }
    093
    094    /**
    095     * Dump object to file stream on binary format.
    096     *
    097     * @param file Output file stream
    098     * @throws IOException
    099     */
    100    public final void dump(final DataOutputStream file) throws IOException {
    101        file.writeShort(name_index);
    102        file.writeShort(access_flags);
    103    }
    104
    105    /**
    106     * @return deep copy of this object
    107     */
    108    public MethodParameter copy() {
    109        try {
    110            return (MethodParameter) clone();
    111        } catch (final CloneNotSupportedException e) {
    112            // TODO should this throw?
    113        }
    114        return null;
    115    }
    116}