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