1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.classfile;
18
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import org.apache.bcel.Constants;
23
24 /***
25 * Abstract super class for Fieldref and Methodref constants.
26 *
27 * @version $Id: ConstantCP.java 386056 2006-03-15 11:31:56Z tcurdt $
28 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
29 * @see ConstantFieldref
30 * @see ConstantMethodref
31 * @see ConstantInterfaceMethodref
32 */
33 public abstract class ConstantCP extends Constant {
34
35 /*** References to the constants containing the class and the field signature
36 */
37 protected int class_index, name_and_type_index;
38
39
40 /***
41 * Initialize from another object.
42 */
43 public ConstantCP(ConstantCP c) {
44 this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
45 }
46
47
48 /***
49 * Initialize instance from file data.
50 *
51 * @param tag Constant type tag
52 * @param file Input stream
53 * @throws IOException
54 */
55 ConstantCP(byte tag, DataInputStream file) throws IOException {
56 this(tag, file.readUnsignedShort(), file.readUnsignedShort());
57 }
58
59
60 /***
61 * @param class_index Reference to the class containing the field
62 * @param name_and_type_index and the field signature
63 */
64 protected ConstantCP(byte tag, int class_index, int name_and_type_index) {
65 super(tag);
66 this.class_index = class_index;
67 this.name_and_type_index = name_and_type_index;
68 }
69
70
71 /***
72 * Dump constant field reference to file stream in binary format.
73 *
74 * @param file Output file stream
75 * @throws IOException
76 */
77 public final void dump( DataOutputStream file ) throws IOException {
78 file.writeByte(tag);
79 file.writeShort(class_index);
80 file.writeShort(name_and_type_index);
81 }
82
83
84 /***
85 * @return Reference (index) to class this field or method belongs to.
86 */
87 public final int getClassIndex() {
88 return class_index;
89 }
90
91
92 /***
93 * @return Reference (index) to signature of the field.
94 */
95 public final int getNameAndTypeIndex() {
96 return name_and_type_index;
97 }
98
99
100 /***
101 * @param class_index points to Constant_class
102 */
103 public final void setClassIndex( int class_index ) {
104 this.class_index = class_index;
105 }
106
107
108 /***
109 * @return Class this field belongs to.
110 */
111 public String getClass( ConstantPool cp ) {
112 return cp.constantToString(class_index, Constants.CONSTANT_Class);
113 }
114
115
116 /***
117 * @param name_and_type_index points to Constant_NameAndType
118 */
119 public final void setNameAndTypeIndex( int name_and_type_index ) {
120 this.name_and_type_index = name_and_type_index;
121 }
122
123
124 /***
125 * @return String representation.
126 */
127 public final String toString() {
128 return super.toString() + "(class_index = " + class_index + ", name_and_type_index = "
129 + name_and_type_index + ")";
130 }
131 }