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