Source code

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