1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.generic;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.classfile.Constant;
25 import org.apache.bcel.classfile.ConstantClass;
26 import org.apache.bcel.classfile.ConstantPool;
27 import org.apache.bcel.classfile.Utility;
28 import org.apache.bcel.util.ByteSequence;
29
30
31
32
33
34
35
36
37 public abstract class CPInstruction extends Instruction implements TypedInstruction, IndexedInstruction {
38
39
40
41
42 @Deprecated
43 protected int index;
44
45
46
47
48 CPInstruction() {
49 }
50
51
52
53
54 protected CPInstruction(final short opcode, final int index) {
55 super(opcode, (short) 3);
56 setIndex(index);
57 }
58
59
60
61
62
63
64 @Override
65 public void dump(final DataOutputStream out) throws IOException {
66 out.writeByte(super.getOpcode());
67 out.writeShort(index);
68 }
69
70
71
72
73 @Override
74 public final int getIndex() {
75 return index;
76 }
77
78
79
80
81 @Override
82 public Type getType(final ConstantPoolGen cpg) {
83 final ConstantPool cp = cpg.getConstantPool();
84 String name = cp.getConstantString(index, org.apache.bcel.Const.CONSTANT_Class);
85 if (!name.startsWith("[")) {
86 name = "L" + name + ";";
87 }
88 return Type.getType(name);
89 }
90
91
92
93
94
95
96
97 @Override
98 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
99 setIndex(bytes.readUnsignedShort());
100 super.setLength(3);
101 }
102
103
104
105
106
107
108 @Override
109 public void setIndex(final int index) {
110 if (index < 0) {
111 throw new ClassGenException("Negative index value: " + index);
112 }
113 this.index = index;
114 }
115
116
117
118
119
120
121
122
123
124
125 @Override
126 public String toString(final boolean verbose) {
127 return super.toString(verbose) + " " + index;
128 }
129
130
131
132
133 @Override
134 public String toString(final ConstantPool cp) {
135 final Constant c = cp.getConstant(index);
136 String str = cp.constantToString(c);
137 if (c instanceof ConstantClass) {
138 str = Utility.packageToPath(str);
139 }
140 return org.apache.bcel.Const.getOpcodeName(super.getOpcode()) + " " + str;
141 }
142 }