1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.classfile;
20
21 import java.io.DataInput;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24
25 import org.apache.bcel.Const;
26
27 /**
28 * This class is derived from the abstract {@link Constant} and represents a reference to a (external) class.
29 *
30 * @see Constant
31 */
32 public final class ConstantClass extends Constant implements ConstantObject {
33
34 private int nameIndex; // Identical to ConstantString except for the name
35
36 /**
37 * Initialize from another object.
38 *
39 * @param c Source to copy.
40 */
41 public ConstantClass(final ConstantClass c) {
42 this(c.getNameIndex());
43 }
44
45 /**
46 * Constructs an instance from file data.
47 *
48 * @param dataInput Input stream.
49 * @throws IOException if an I/O error occurs reading from the given {@code dataInput}.
50 */
51 ConstantClass(final DataInput dataInput) throws IOException {
52 this(dataInput.readUnsignedShort());
53 }
54
55 /**
56 * Constructs a ConstantClass.
57 *
58 * @param nameIndex Name index in constant pool. Should refer to a ConstantUtf8.
59 */
60 public ConstantClass(final int nameIndex) {
61 super(Const.CONSTANT_Class);
62 this.nameIndex = nameIndex;
63 }
64
65 /**
66 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
67 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
68 *
69 * @param v Visitor object.
70 */
71 @Override
72 public void accept(final Visitor v) {
73 v.visitConstantClass(this);
74 }
75
76 /**
77 * Dumps constant class to file stream in binary format.
78 *
79 * @param file Output file stream.
80 * @throws IOException if an I/O error occurs writing to the DataOutputStream.
81 */
82 @Override
83 public void dump(final DataOutputStream file) throws IOException {
84 file.writeByte(super.getTag());
85 file.writeShort(nameIndex);
86 }
87
88 /**
89 * Gets the dereferenced string.
90 *
91 * @param cp the constant pool.
92 * @return dereferenced string.
93 */
94 public String getBytes(final ConstantPool cp) {
95 return (String) getConstantValue(cp);
96 }
97
98 /**
99 * Gets the String object.
100 *
101 * @param cp the constant pool.
102 * @return String object.
103 */
104 @Override
105 public Object getConstantValue(final ConstantPool cp) {
106 return cp.getConstantUtf8(nameIndex).getBytes();
107 }
108
109 /**
110 * Gets the name index in constant pool of class name.
111 *
112 * @return Name index in constant pool of class name.
113 */
114 public int getNameIndex() {
115 return nameIndex;
116 }
117
118 /**
119 * Sets the name index in the constant pool.
120 *
121 * @param nameIndex the name index in the constant pool of this Constant Class.
122 */
123 public void setNameIndex(final int nameIndex) {
124 this.nameIndex = nameIndex;
125 }
126
127 /**
128 * @return String representation.
129 */
130 @Override
131 public String toString() {
132 return super.toString() + "(nameIndex = " + nameIndex + ")";
133 }
134 }