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