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