Source code

    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 {
    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    public void accept(final Visitor v) {
    057        v.visitMethodParameter(this);
    058    }
    059
    060    /**
    061     * @return deep copy of this object
    062     */
    063    public MethodParameter copy() {
    064        try {
    065            return (MethodParameter) clone();
    066        } catch (final CloneNotSupportedException e) {
    067            // TODO should this throw?
    068        }
    069        return null;
    070    }
    071
    072    /**
    073     * Dump object to file stream on binary format.
    074     *
    075     * @param file Output file stream
    076     * @throws IOException if an I/O error occurs.
    077     */
    078    public final void dump(final DataOutputStream file) throws IOException {
    079        file.writeShort(nameIndex);
    080        file.writeShort(accessFlags);
    081    }
    082
    083    public int getAccessFlags() {
    084        return accessFlags;
    085    }
    086
    087    public int getNameIndex() {
    088        return nameIndex;
    089    }
    090
    091    /**
    092     * Returns the name of the parameter.
    093     */
    094    public String getParameterName(final ConstantPool constantPool) {
    095        if (nameIndex == 0) {
    096            return null;
    097        }
    098        return constantPool.getConstantUtf8(nameIndex).getBytes();
    099    }
    100
    101    public boolean isFinal() {
    102        return (accessFlags & Const.ACC_FINAL) != 0;
    103    }
    104
    105    public boolean isMandated() {
    106        return (accessFlags & Const.ACC_MANDATED) != 0;
    107    }
    108
    109    public boolean isSynthetic() {
    110        return (accessFlags & Const.ACC_SYNTHETIC) != 0;
    111    }
    112
    113    public void setAccessFlags(final int accessFlags) {
    114        this.accessFlags = accessFlags;
    115    }
    116
    117    public void setNameIndex(final int nameIndex) {
    118        this.nameIndex = nameIndex;
    119    }
    120}