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 a (external) class.
28 *
29 * @version $Id: ConstantClass.java 386056 2006-03-15 11:31:56Z tcurdt $
30 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31 * @see Constant
32 */
33 public final class ConstantClass extends Constant implements ConstantObject {
34
35 private int name_index;
36
37
38 /***
39 * Initialize from another object.
40 */
41 public ConstantClass(ConstantClass c) {
42 this(c.getNameIndex());
43 }
44
45
46 /***
47 * Initialize instance from file data.
48 *
49 * @param file Input stream
50 * @throws IOException
51 */
52 ConstantClass(DataInputStream file) throws IOException {
53 this(file.readUnsignedShort());
54 }
55
56
57 /***
58 * @param name_index Name index in constant pool. Should refer to a
59 * ConstantUtf8.
60 */
61 public ConstantClass(int name_index) {
62 super(Constants.CONSTANT_Class);
63 this.name_index = name_index;
64 }
65
66
67 /***
68 * Called by objects that are traversing the nodes of the tree implicitely
69 * defined by the contents of a Java class. I.e., the hierarchy of methods,
70 * fields, attributes, etc. spawns a tree of objects.
71 *
72 * @param v Visitor object
73 */
74 public void accept( Visitor v ) {
75 v.visitConstantClass(this);
76 }
77
78
79 /***
80 * Dump constant class to file stream in binary format.
81 *
82 * @param file Output file stream
83 * @throws IOException
84 */
85 public final void dump( DataOutputStream file ) throws IOException {
86 file.writeByte(tag);
87 file.writeShort(name_index);
88 }
89
90
91 /***
92 * @return Name index in constant pool of class name.
93 */
94 public final int getNameIndex() {
95 return name_index;
96 }
97
98
99 /***
100 * @param name_index the name index in the constant pool of this Constant Class
101 */
102 public final void setNameIndex( int name_index ) {
103 this.name_index = name_index;
104 }
105
106
107 /*** @return String object
108 */
109 public Object getConstantValue( ConstantPool cp ) {
110 Constant c = cp.getConstant(name_index, Constants.CONSTANT_Utf8);
111 return ((ConstantUtf8) c).getBytes();
112 }
113
114
115 /*** @return dereferenced string
116 */
117 public String getBytes( ConstantPool cp ) {
118 return (String) getConstantValue(cp);
119 }
120
121
122 /***
123 * @return String representation.
124 */
125 public final String toString() {
126 return super.toString() + "(name_index = " + name_index + ")";
127 }
128 }