View Javadoc
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   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * Abstract super class for Fieldref, Methodref, InterfaceMethodref and
28   *                          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      /** References to the constants containing the class and the field signature
38       */
39      // Note that this field is used to store the
40      // bootstrap_method_attr_index of a ConstantInvokeDynamic.
41      /**
42       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
43       */
44      @java.lang.Deprecated
45      protected int class_index; // TODO make private (has getter & setter)
46      // This field has the same meaning for all subclasses.
47  
48      /**
49       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
50       */
51      @java.lang.Deprecated
52      protected int name_and_type_index; // TODO make private (has getter & setter)
53  
54  
55      /**
56       * Initialize from another object.
57       */
58      public ConstantCP/ConstantCP.html#ConstantCP">ConstantCP(final ConstantCP c) {
59          this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
60      }
61  
62  
63      /**
64       * Initialize instance from file data.
65       *
66       * @param tag  Constant type tag
67       * @param file Input stream
68       * @throws IOException
69       */
70      ConstantCP(final byte tag, final DataInput file) throws IOException {
71          this(tag, file.readUnsignedShort(), file.readUnsignedShort());
72      }
73  
74  
75      /**
76       * @param class_index Reference to the class containing the field
77       * @param name_and_type_index and the field signature
78       */
79      protected ConstantCP(final byte tag, final int class_index, final int name_and_type_index) {
80          super(tag);
81          this.class_index = class_index;
82          this.name_and_type_index = name_and_type_index;
83      }
84  
85  
86      /**
87       * Dump constant field reference to file stream in binary format.
88       *
89       * @param file Output file stream
90       * @throws IOException
91       */
92      @Override
93      public final void dump( final DataOutputStream file ) throws IOException {
94          file.writeByte(super.getTag());
95          file.writeShort(class_index);
96          file.writeShort(name_and_type_index);
97      }
98  
99  
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 }