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 * This class is derived from the abstract {@link Constant}
028 * 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    /**
039     * Initialize from another object.
040     */
041    public ConstantMethodType(final ConstantMethodType c) {
042        this(c.getDescriptorIndex());
043    }
044
045
046    /**
047     * Initialize instance from file data.
048     *
049     * @param file Input stream
050     * @throws IOException
051     */
052    ConstantMethodType(final DataInput file) throws IOException {
053        this(file.readUnsignedShort());
054    }
055
056
057    public ConstantMethodType(final int descriptor_index) {
058        super(Const.CONSTANT_MethodType);
059        this.descriptorIndex = descriptor_index;
060    }
061
062
063    /**
064     * Called by objects that are traversing the nodes of the tree implicitly
065     * defined by the contents of a Java class. I.e., the hierarchy of methods,
066     * fields, attributes, etc. spawns a tree of objects.
067     *
068     * @param v Visitor object
069     */
070    @Override
071    public void accept( final Visitor v ) {
072        v.visitConstantMethodType(this);
073    }
074
075
076    /**
077     * Dump name and signature index to file stream in binary format.
078     *
079     * @param file Output file stream
080     * @throws IOException
081     */
082    @Override
083    public void dump( final DataOutputStream file ) throws IOException {
084        file.writeByte(super.getTag());
085        file.writeShort(descriptorIndex);
086    }
087
088
089    public int getDescriptorIndex() {
090        return descriptorIndex;
091    }
092
093
094    public void setDescriptorIndex(final int descriptor_index) {
095        this.descriptorIndex = descriptor_index;
096    }
097
098
099    /**
100     * @return String representation
101     */
102    @Override
103    public String toString() {
104        return super.toString() + "(descriptorIndex = " + descriptorIndex + ")";
105    }
106}