View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataInput;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  
25  import org.apache.bcel.Const;
26  
27  /**
28   * Abstract super class for Fieldref, Methodref, InterfaceMethodref and InvokeDynamic constants.
29   *
30   * @see ConstantFieldref
31   * @see ConstantMethodref
32   * @see ConstantInterfaceMethodref
33   * @see ConstantInvokeDynamic
34   */
35  public abstract class ConstantCP extends Constant {
36  
37      /**
38       * References to the constants containing the class and the field signature
39       */
40      // Note that this field is used to store the
41      // bootstrap_method_attr_index of a ConstantInvokeDynamic.
42  
43      /**
44       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter.
45       */
46      @java.lang.Deprecated
47      protected int class_index; // TODO make private (has getter & setter)
48      // This field has the same meaning for all subclasses.
49  
50      /**
51       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter.
52       */
53      @java.lang.Deprecated
54      protected int name_and_type_index; // TODO make private (has getter & setter)
55  
56      /**
57       * Initialize instance from file data.
58       *
59       * @param tag Constant type tag.
60       * @param file Input stream.
61       * @throws IOException if an I/O error occurs.
62       */
63      ConstantCP(final byte tag, final DataInput file) throws IOException {
64          this(tag, file.readUnsignedShort(), file.readUnsignedShort());
65      }
66  
67      /**
68       * Constructs a ConstantCP.
69       *
70       * @param tag the constant type tag.
71       * @param classIndex Reference to the class containing the field.
72       * @param nameAndTypeIndex and the field signature.
73       */
74      protected ConstantCP(final byte tag, final int classIndex, final int nameAndTypeIndex) {
75          super(tag);
76          this.class_index = classIndex;
77          this.name_and_type_index = nameAndTypeIndex;
78      }
79  
80      /**
81       * Initialize from another object.
82       *
83       * @param c Source to copy.
84       */
85      public ConstantCP(final ConstantCP c) {
86          this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
87      }
88  
89      /**
90       * Dumps constant field reference to file stream in binary format.
91       *
92       * @param file Output file stream.
93       * @throws IOException if an I/O error occurs.
94       */
95      @Override
96      public final void dump(final DataOutputStream file) throws IOException {
97          file.writeByte(super.getTag());
98          file.writeShort(class_index);
99          file.writeShort(name_and_type_index);
100     }
101 
102     /**
103      * Gets the class this field belongs to.
104      *
105      * @param cp the constant pool.
106      * @return Class this field belongs to.
107      */
108     public String getClass(final ConstantPool cp) {
109         return cp.constantToString(class_index, Const.CONSTANT_Class);
110     }
111 
112     /**
113      * Gets the reference (index) to class this constant refers to.
114      *
115      * @return Reference (index) to class this constant refers to.
116      */
117     public final int getClassIndex() {
118         return class_index;
119     }
120 
121     /**
122      * Gets the reference (index) to signature of the field.
123      *
124      * @return Reference (index) to signature of the field.
125      */
126     public final int getNameAndTypeIndex() {
127         return name_and_type_index;
128     }
129 
130     /**
131      * Sets the class index.
132      *
133      * @param classIndex points to Constant_class.
134      */
135     public final void setClassIndex(final int classIndex) {
136         this.class_index = classIndex;
137     }
138 
139     /**
140      * Sets the name and type index.
141      *
142      * @param nameAndTypeIndex points to Constant_NameAndType.
143      */
144     public final void setNameAndTypeIndex(final int nameAndTypeIndex) {
145         this.name_and_type_index = nameAndTypeIndex;
146     }
147 
148     /**
149      * @return String representation.
150      *
151      *         not final as ConstantInvokeDynamic needs to modify
152      */
153     @Override
154     public String toString() {
155         return super.toString() + "(class_index = " + class_index + ", name_and_type_index = " + name_and_type_index + ")";
156     }
157 }