    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.bcel.classfile;
    020
    021import java.io.DataInput;
    022import java.io.DataOutputStream;
    023import java.io.IOException;
    024
    025import org.apache.bcel.Const;
    026
    027/**
    028 * This class is derived from the abstract {@link Constant} and represents a reference to a method type.
    029 *
    030 * @see Constant
    031 * @since 6.0
    032 */
    033public final class ConstantMethodType extends Constant {
    034
    035    private int descriptorIndex;
    036
    037    /**
    038     * Initialize from another object.
    039     *
    040     * @param c Source to copy.
    041     */
    042    public ConstantMethodType(final ConstantMethodType c) {
    043        this(c.getDescriptorIndex());
    044    }
    045
    046    /**
    047     * Initialize instance from file data.
    048     *
    049     * @param file Input stream.
    050     * @throws IOException if an I/O error occurs.
    051     */
    052    ConstantMethodType(final DataInput file) throws IOException {
    053        this(file.readUnsignedShort());
    054    }
    055
    056    /**
    057     * Constructs a ConstantMethodType.
    058     *
    059     * @param descriptorIndex Index to the method descriptor.
    060     */
    061    public ConstantMethodType(final int descriptorIndex) {
    062        super(Const.CONSTANT_MethodType);
    063        this.descriptorIndex = descriptorIndex;
    064    }
    065
    066    /**
    067     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
    068     * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    069     *
    070     * @param v Visitor object.
    071     */
    072    @Override
    073    public void accept(final Visitor v) {
    074        v.visitConstantMethodType(this);
    075    }
    076
    077    /**
    078     * Dumps name and signature index to file stream in binary format.
    079     *
    080     * @param file Output file stream.
    081     * @throws IOException if an I/O error occurs.
    082     */
    083    @Override
    084    public void dump(final DataOutputStream file) throws IOException {
    085        file.writeByte(super.getTag());
    086        file.writeShort(descriptorIndex);
    087    }
    088
    089    /**
    090     * Gets the descriptor index.
    091     *
    092     * @return the descriptor index.
    093     */
    094    public int getDescriptorIndex() {
    095        return descriptorIndex;
    096    }
    097
    098    /**
    099     * Sets the descriptor index.
    100     *
    101     * @param descriptorIndex the descriptor index.
    102     */
    103    public void setDescriptorIndex(final int descriptorIndex) {
    104        this.descriptorIndex = descriptorIndex;
    105    }
    106
    107    /**
    108     * @return String representation.
    109     */
    110    @Override
    111    public String toString() {
    112        return super.toString() + "(descriptorIndex = " + descriptorIndex + ")";
    113    }
    114}