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 * This class is derived from the abstract
26 * <A HREF="org.apache.bcel.classfile.Constant.html">Constant</A> class
27 * and represents a reference to the name and signature
28 * of a field or method.
29 *
30 * @version $Id: ConstantNameAndType.java 386056 2006-03-15 11:31:56Z tcurdt $
31 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
32 * @see Constant
33 */
34 public final class ConstantNameAndType extends Constant {
35
36 private int name_index;
37 private int signature_index;
38
39
40 /***
41 * Initialize from another object.
42 */
43 public ConstantNameAndType(ConstantNameAndType c) {
44 this(c.getNameIndex(), c.getSignatureIndex());
45 }
46
47
48 /***
49 * Initialize instance from file data.
50 *
51 * @param file Input stream
52 * @throws IOException
53 */
54 ConstantNameAndType(DataInputStream file) throws IOException {
55 this(file.readUnsignedShort(), file.readUnsignedShort());
56 }
57
58
59 /***
60 * @param name_index Name of field/method
61 * @param signature_index and its signature
62 */
63 public ConstantNameAndType(int name_index, int signature_index) {
64 super(Constants.CONSTANT_NameAndType);
65 this.name_index = name_index;
66 this.signature_index = signature_index;
67 }
68
69
70 /***
71 * Called by objects that are traversing the nodes of the tree implicitely
72 * defined by the contents of a Java class. I.e., the hierarchy of methods,
73 * fields, attributes, etc. spawns a tree of objects.
74 *
75 * @param v Visitor object
76 */
77 public void accept( Visitor v ) {
78 v.visitConstantNameAndType(this);
79 }
80
81
82 /***
83 * Dump name and signature index to file stream in binary format.
84 *
85 * @param file Output file stream
86 * @throws IOException
87 */
88 public final void dump( DataOutputStream file ) throws IOException {
89 file.writeByte(tag);
90 file.writeShort(name_index);
91 file.writeShort(signature_index);
92 }
93
94
95 /***
96 * @return Name index in constant pool of field/method name.
97 */
98 public final int getNameIndex() {
99 return name_index;
100 }
101
102
103 /*** @return name
104 */
105 public final String getName( ConstantPool cp ) {
106 return cp.constantToString(getNameIndex(), Constants.CONSTANT_Utf8);
107 }
108
109
110 /***
111 * @return Index in constant pool of field/method signature.
112 */
113 public final int getSignatureIndex() {
114 return signature_index;
115 }
116
117
118 /*** @return signature
119 */
120 public final String getSignature( ConstantPool cp ) {
121 return cp.constantToString(getSignatureIndex(), Constants.CONSTANT_Utf8);
122 }
123
124
125 /***
126 * @param name_index the name index of this constant
127 */
128 public final void setNameIndex( int name_index ) {
129 this.name_index = name_index;
130 }
131
132
133 /***
134 * @param signature_index the signature index in the constant pool of this type
135 */
136 public final void setSignatureIndex( int signature_index ) {
137 this.signature_index = signature_index;
138 }
139
140
141 /***
142 * @return String representation
143 */
144 public final String toString() {
145 return super.toString() + "(name_index = " + name_index + ", signature_index = "
146 + signature_index + ")";
147 }
148 }