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     * @param nameIndex Name of field/method
059     * @param signatureIndex and its signature
060     */
061    public ConstantNameAndType(final int nameIndex, final int signatureIndex) {
062        super(Const.CONSTANT_NameAndType);
063        this.nameIndex = nameIndex;
064        this.signatureIndex = signatureIndex;
065    }
066
067    /**
068     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
069     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
070     *
071     * @param v Visitor object
072     */
073    @Override
074    public void accept(final Visitor v) {
075        v.visitConstantNameAndType(this);
076    }
077
078    /**
079     * Dump name and signature index to file stream in binary format.
080     *
081     * @param file Output file stream
082     * @throws IOException if an I/O error occurs.
083     */
084    @Override
085    public void dump(final DataOutputStream file) throws IOException {
086        file.writeByte(super.getTag());
087        file.writeShort(nameIndex);
088        file.writeShort(signatureIndex);
089    }
090
091    /**
092     * @return name
093     */
094    public String getName(final ConstantPool cp) {
095        return cp.constantToString(getNameIndex(), Const.CONSTANT_Utf8);
096    }
097
098    /**
099     * @return Name index in constant pool of field/method name.
100     */
101    public int getNameIndex() {
102        return nameIndex;
103    }
104
105    /**
106     * @return signature
107     */
108    public String getSignature(final ConstantPool cp) {
109        return cp.constantToString(getSignatureIndex(), Const.CONSTANT_Utf8);
110    }
111
112    /**
113     * @return Index in constant pool of field/method signature.
114     */
115    public int getSignatureIndex() {
116        return signatureIndex;
117    }
118
119    /**
120     * @param nameIndex the name index of this constant
121     */
122    public void setNameIndex(final int nameIndex) {
123        this.nameIndex = nameIndex;
124    }
125
126    /**
127     * @param signatureIndex the signature index in the constant pool of this type
128     */
129    public void setSignatureIndex(final int signatureIndex) {
130        this.signatureIndex = signatureIndex;
131    }
132
133    /**
134     * @return String representation
135     */
136    @Override
137    public String toString() {
138        return super.toString() + "(nameIndex = " + nameIndex + ", signatureIndex = " + signatureIndex + ")";
139    }
140}