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       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
44       */
45      @java.lang.Deprecated
46      protected int class_index; // TODO make private (has getter & setter)
47      // This field has the same meaning for all subclasses.
48  
49      /**
50       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
51       */
52      @java.lang.Deprecated
53      protected int name_and_type_index; // TODO make private (has getter & setter)
54  
55      /**
56       * Initialize instance from file data.
57       *
58       * @param tag Constant type tag
59       * @param file Input stream
60       * @throws IOException if an I/O error occurs.
61       */
62      ConstantCP(final byte tag, final DataInput file) throws IOException {
63          this(tag, file.readUnsignedShort(), file.readUnsignedShort());
64      }
65  
66      /**
67       * @param classIndex Reference to the class containing the field
68       * @param nameAndTypeIndex and the field signature
69       */
70      protected ConstantCP(final byte tag, final int classIndex, final int nameAndTypeIndex) {
71          super(tag);
72          this.class_index = classIndex;
73          this.name_and_type_index = nameAndTypeIndex;
74      }
75  
76      /**
77       * Initialize from another object.
78       *
79       * @param c Source to copy.
80       */
81      public ConstantCP(final ConstantCP c) {
82          this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
83      }
84  
85      /**
86       * Dump constant field reference to file stream in binary format.
87       *
88       * @param file Output file stream
89       * @throws IOException if an I/O error occurs.
90       */
91      @Override
92      public final void dump(final DataOutputStream file) throws IOException {
93          file.writeByte(super.getTag());
94          file.writeShort(class_index);
95          file.writeShort(name_and_type_index);
96      }
97  
98      /**
99       * @return Class this field belongs to.
100      */
101     public String getClass(final ConstantPool cp) {
102         return cp.constantToString(class_index, Const.CONSTANT_Class);
103     }
104 
105     /**
106      * @return Reference (index) to class this constant refers to.
107      */
108     public final int getClassIndex() {
109         return class_index;
110     }
111 
112     /**
113      * @return Reference (index) to signature of the field.
114      */
115     public final int getNameAndTypeIndex() {
116         return name_and_type_index;
117     }
118 
119     /**
120      * @param classIndex points to Constant_class
121      */
122     public final void setClassIndex(final int classIndex) {
123         this.class_index = classIndex;
124     }
125 
126     /**
127      * @param nameAndTypeIndex points to Constant_NameAndType
128      */
129     public final void setNameAndTypeIndex(final int nameAndTypeIndex) {
130         this.name_and_type_index = nameAndTypeIndex;
131     }
132 
133     /**
134      * @return String representation.
135      *
136      *         not final as ConstantInvokeDynamic needs to modify
137      */
138     @Override
139     public String toString() {
140         return super.toString() + "(class_index = " + class_index + ", name_and_type_index = " + name_and_type_index + ")";
141     }
142 }