    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 * Abstract super class for Fieldref, Methodref, InterfaceMethodref and InvokeDynamic constants.
    027 *
    028 * @see ConstantFieldref
    029 * @see ConstantMethodref
    030 * @see ConstantInterfaceMethodref
    031 * @see ConstantInvokeDynamic
    032 */
    033public abstract class ConstantCP extends Constant {
    034
    035    /**
    036     * References to the constants containing the class and the field signature
    037     */
    038    // Note that this field is used to store the
    039    // bootstrap_method_attr_index of a ConstantInvokeDynamic.
    040    /**
    041     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
    042     */
    043    @java.lang.Deprecated
    044    protected int class_index; // TODO make private (has getter & setter)
    045    // This field has the same meaning for all subclasses.
    046
    047    /**
    048     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
    049     */
    050    @java.lang.Deprecated
    051    protected int name_and_type_index; // TODO make private (has getter & setter)
    052
    053    /**
    054     * Initialize instance from file data.
    055     *
    056     * @param tag Constant type tag
    057     * @param file Input stream
    058     * @throws IOException if an I/O error occurs.
    059     */
    060    ConstantCP(final byte tag, final DataInput file) throws IOException {
    061        this(tag, file.readUnsignedShort(), file.readUnsignedShort());
    062    }
    063
    064    /**
    065     * @param classIndex Reference to the class containing the field
    066     * @param nameAndTypeIndex and the field signature
    067     */
    068    protected ConstantCP(final byte tag, final int classIndex, final int nameAndTypeIndex) {
    069        super(tag);
    070        this.class_index = classIndex;
    071        this.name_and_type_index = nameAndTypeIndex;
    072    }
    073
    074    /**
    075     * Initialize from another object.
    076     */
    077    public ConstantCP(final ConstantCP c) {
    078        this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
    079    }
    080
    081    /**
    082     * Dump constant field reference to file stream in binary format.
    083     *
    084     * @param file Output file stream
    085     * @throws IOException if an I/O error occurs.
    086     */
    087    @Override
    088    public final void dump(final DataOutputStream file) throws IOException {
    089        file.writeByte(super.getTag());
    090        file.writeShort(class_index);
    091        file.writeShort(name_and_type_index);
    092    }
    093
    094    /**
    095     * @return Class this field belongs to.
    096     */
    097    public String getClass(final ConstantPool cp) {
    098        return cp.constantToString(class_index, Const.CONSTANT_Class);
    099    }
    100
    101    /**
    102     * @return Reference (index) to class this constant refers to.
    103     */
    104    public final int getClassIndex() {
    105        return class_index;
    106    }
    107
    108    /**
    109     * @return Reference (index) to signature of the field.
    110     */
    111    public final int getNameAndTypeIndex() {
    112        return name_and_type_index;
    113    }
    114
    115    /**
    116     * @param classIndex points to Constant_class
    117     */
    118    public final void setClassIndex(final int classIndex) {
    119        this.class_index = classIndex;
    120    }
    121
    122    /**
    123     * @param nameAndTypeIndex points to Constant_NameAndType
    124     */
    125    public final void setNameAndTypeIndex(final int nameAndTypeIndex) {
    126        this.name_and_type_index = nameAndTypeIndex;
    127    }
    128
    129    /**
    130     * @return String representation.
    131     *
    132     *         not final as ConstantInvokeDynamic needs to modify
    133     */
    134    @Override
    135    public String toString() {
    136        return super.toString() + "(class_index = " + class_index + ", name_and_type_index = " + name_and_type_index + ")";
    137    }
    138}