ConstantCP.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;

  22. /**
  23.  * Abstract super class for Fieldref, Methodref, InterfaceMethodref and InvokeDynamic constants.
  24.  *
  25.  * @see ConstantFieldref
  26.  * @see ConstantMethodref
  27.  * @see ConstantInterfaceMethodref
  28.  * @see ConstantInvokeDynamic
  29.  */
  30. public abstract class ConstantCP extends Constant {

  31.     /**
  32.      * References to the constants containing the class and the field signature
  33.      */
  34.     // Note that this field is used to store the
  35.     // bootstrap_method_attr_index of a ConstantInvokeDynamic.
  36.     /**
  37.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  38.      */
  39.     @java.lang.Deprecated
  40.     protected int class_index; // TODO make private (has getter & setter)
  41.     // This field has the same meaning for all subclasses.

  42.     /**
  43.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  44.      */
  45.     @java.lang.Deprecated
  46.     protected int name_and_type_index; // TODO make private (has getter & setter)

  47.     /**
  48.      * Initialize instance from file data.
  49.      *
  50.      * @param tag Constant type tag
  51.      * @param file Input stream
  52.      * @throws IOException if an I/O error occurs.
  53.      */
  54.     ConstantCP(final byte tag, final DataInput file) throws IOException {
  55.         this(tag, file.readUnsignedShort(), file.readUnsignedShort());
  56.     }

  57.     /**
  58.      * @param classIndex Reference to the class containing the field
  59.      * @param nameAndTypeIndex and the field signature
  60.      */
  61.     protected ConstantCP(final byte tag, final int classIndex, final int nameAndTypeIndex) {
  62.         super(tag);
  63.         this.class_index = classIndex;
  64.         this.name_and_type_index = nameAndTypeIndex;
  65.     }

  66.     /**
  67.      * Initialize from another object.
  68.      *
  69.      * @param c Source to copy.
  70.      */
  71.     public ConstantCP(final ConstantCP c) {
  72.         this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
  73.     }

  74.     /**
  75.      * Dump constant field reference to file stream in binary format.
  76.      *
  77.      * @param file Output file stream
  78.      * @throws IOException if an I/O error occurs.
  79.      */
  80.     @Override
  81.     public final void dump(final DataOutputStream file) throws IOException {
  82.         file.writeByte(super.getTag());
  83.         file.writeShort(class_index);
  84.         file.writeShort(name_and_type_index);
  85.     }

  86.     /**
  87.      * @return Class this field belongs to.
  88.      */
  89.     public String getClass(final ConstantPool cp) {
  90.         return cp.constantToString(class_index, Const.CONSTANT_Class);
  91.     }

  92.     /**
  93.      * @return Reference (index) to class this constant refers to.
  94.      */
  95.     public final int getClassIndex() {
  96.         return class_index;
  97.     }

  98.     /**
  99.      * @return Reference (index) to signature of the field.
  100.      */
  101.     public final int getNameAndTypeIndex() {
  102.         return name_and_type_index;
  103.     }

  104.     /**
  105.      * @param classIndex points to Constant_class
  106.      */
  107.     public final void setClassIndex(final int classIndex) {
  108.         this.class_index = classIndex;
  109.     }

  110.     /**
  111.      * @param nameAndTypeIndex points to Constant_NameAndType
  112.      */
  113.     public final void setNameAndTypeIndex(final int nameAndTypeIndex) {
  114.         this.name_and_type_index = nameAndTypeIndex;
  115.     }

  116.     /**
  117.      * @return String representation.
  118.      *
  119.      *         not final as ConstantInvokeDynamic needs to modify
  120.      */
  121.     @Override
  122.     public String toString() {
  123.         return super.toString() + "(class_index = " + class_index + ", name_and_type_index = " + name_and_type_index + ")";
  124.     }
  125. }