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