    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} and represents a reference to the name and signature of a
    028 * field or method.
    029 *
    030 * @see Constant
    031 */
    032public final class ConstantNameAndType extends Constant {
    033
    034    private int nameIndex; // Name of field/method
    035    private int signatureIndex; // and its signature.
    036
    037    /**
    038     * Initialize from another object.
    039     */
    040    public ConstantNameAndType(final ConstantNameAndType c) {
    041        this(c.getNameIndex(), c.getSignatureIndex());
    042    }
    043
    044    /**
    045     * Initialize instance from file data.
    046     *
    047     * @param file Input stream
    048     * @throws IOException if an I/O error occurs.
    049     */
    050    ConstantNameAndType(final DataInput file) throws IOException {
    051        this(file.readUnsignedShort(), file.readUnsignedShort());
    052    }
    053
    054    /**
    055     * @param nameIndex Name of field/method
    056     * @param signatureIndex and its signature
    057     */
    058    public ConstantNameAndType(final int nameIndex, final int signatureIndex) {
    059        super(Const.CONSTANT_NameAndType);
    060        this.nameIndex = nameIndex;
    061        this.signatureIndex = signatureIndex;
    062    }
    063
    064    /**
    065     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    066     * I.e., the hierarchy of methods, 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.visitConstantNameAndType(this);
    073    }
    074
    075    /**
    076     * Dump name and signature index to file stream in binary format.
    077     *
    078     * @param file Output file stream
    079     * @throws IOException if an I/O error occurs.
    080     */
    081    @Override
    082    public void dump(final DataOutputStream file) throws IOException {
    083        file.writeByte(super.getTag());
    084        file.writeShort(nameIndex);
    085        file.writeShort(signatureIndex);
    086    }
    087
    088    /**
    089     * @return name
    090     */
    091    public String getName(final ConstantPool cp) {
    092        return cp.constantToString(getNameIndex(), Const.CONSTANT_Utf8);
    093    }
    094
    095    /**
    096     * @return Name index in constant pool of field/method name.
    097     */
    098    public int getNameIndex() {
    099        return nameIndex;
    100    }
    101
    102    /**
    103     * @return signature
    104     */
    105    public String getSignature(final ConstantPool cp) {
    106        return cp.constantToString(getSignatureIndex(), Const.CONSTANT_Utf8);
    107    }
    108
    109    /**
    110     * @return Index in constant pool of field/method signature.
    111     */
    112    public int getSignatureIndex() {
    113        return signatureIndex;
    114    }
    115
    116    /**
    117     * @param nameIndex the name index of this constant
    118     */
    119    public void setNameIndex(final int nameIndex) {
    120        this.nameIndex = nameIndex;
    121    }
    122
    123    /**
    124     * @param signatureIndex the signature index in the constant pool of this type
    125     */
    126    public void setSignatureIndex(final int signatureIndex) {
    127        this.signatureIndex = signatureIndex;
    128    }
    129
    130    /**
    131     * @return String representation
    132     */
    133    @Override
    134    public String toString() {
    135        return super.toString() + "(nameIndex = " + nameIndex + ", signatureIndex = " + signatureIndex + ")";
    136    }
    137}