    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    public ConstantMethodType(final int descriptorIndex) {
    057        super(Const.CONSTANT_MethodType);
    058        this.descriptorIndex = descriptorIndex;
    059    }
    060
    061    /**
    062     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
    063     * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    064     *
    065     * @param v Visitor object
    066     */
    067    @Override
    068    public void accept(final Visitor v) {
    069        v.visitConstantMethodType(this);
    070    }
    071
    072    /**
    073     * Dump name and signature index to file stream in binary format.
    074     *
    075     * @param file Output file stream
    076     * @throws IOException if an I/O error occurs.
    077     */
    078    @Override
    079    public void dump(final DataOutputStream file) throws IOException {
    080        file.writeByte(super.getTag());
    081        file.writeShort(descriptorIndex);
    082    }
    083
    084    public int getDescriptorIndex() {
    085        return descriptorIndex;
    086    }
    087
    088    public void setDescriptorIndex(final int descriptorIndex) {
    089        this.descriptorIndex = descriptorIndex;
    090    }
    091
    092    /**
    093     * @return String representation
    094     */
    095    @Override
    096    public String toString() {
    097        return super.toString() + "(descriptorIndex = " + descriptorIndex + ")";
    098    }
    099}