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.ExceptionConst;
23 import org.apache.bcel.util.ByteSequence;
24
25
26
27
28
29
30
31
32 public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower {
33
34
35
36
37 LDC() {
38 }
39
40 public LDC(final int index) {
41 super(org.apache.bcel.Const.LDC_W, index);
42 setSize();
43 }
44
45
46
47
48
49
50
51 @Override
52 public void accept(final Visitor v) {
53 v.visitStackProducer(this);
54 v.visitPushInstruction(this);
55 v.visitExceptionThrower(this);
56 v.visitTypedInstruction(this);
57 v.visitCPInstruction(this);
58 v.visitLDC(this);
59 }
60
61
62
63
64
65
66 @Override
67 public void dump(final DataOutputStream out) throws IOException {
68 out.writeByte(super.getOpcode());
69 if (super.getLength() == 2) {
70 out.writeByte(super.getIndex());
71 } else {
72 out.writeShort(super.getIndex());
73 }
74 }
75
76 @Override
77 public Class<?>[] getExceptions() {
78 return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_STRING_RESOLUTION);
79 }
80
81 @Override
82 public Type getType(final ConstantPoolGen cpg) {
83 switch (cpg.getConstantPool().getConstant(super.getIndex()).getTag()) {
84 case org.apache.bcel.Const.CONSTANT_String:
85 return Type.STRING;
86 case org.apache.bcel.Const.CONSTANT_Float:
87 return Type.FLOAT;
88 case org.apache.bcel.Const.CONSTANT_Integer:
89 return Type.INT;
90 case org.apache.bcel.Const.CONSTANT_Class:
91 return Type.CLASS;
92 case org.apache.bcel.Const.CONSTANT_Dynamic:
93 return Type.OBJECT;
94 default:
95 throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
96 }
97 }
98
99 public Object getValue(final ConstantPoolGen cpg) {
100 org.apache.bcel.classfile.Constant c = cpg.getConstantPool().getConstant(super.getIndex());
101 switch (c.getTag()) {
102 case org.apache.bcel.Const.CONSTANT_String:
103 final int i = ((org.apache.bcel.classfile.ConstantString) c).getStringIndex();
104 c = cpg.getConstantPool().getConstant(i);
105 return ((org.apache.bcel.classfile.ConstantUtf8) c).getBytes();
106 case org.apache.bcel.Const.CONSTANT_Float:
107 return Float.valueOf(((org.apache.bcel.classfile.ConstantFloat) c).getBytes());
108 case org.apache.bcel.Const.CONSTANT_Integer:
109 return Integer.valueOf(((org.apache.bcel.classfile.ConstantInteger) c).getBytes());
110 case org.apache.bcel.Const.CONSTANT_Class:
111 final int nameIndex = ((org.apache.bcel.classfile.ConstantClass) c).getNameIndex();
112 c = cpg.getConstantPool().getConstant(nameIndex);
113 return Type.getType(Type.internalTypeNameToSignature(((org.apache.bcel.classfile.ConstantUtf8) c).getBytes()));
114 case org.apache.bcel.Const.CONSTANT_Dynamic:
115
116 return c;
117 default:
118 throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
119 }
120 }
121
122
123
124
125 @Override
126 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
127 super.setLength(2);
128 super.setIndex(bytes.readUnsignedByte());
129 }
130
131
132
133
134 @Override
135 public final void setIndex(final int index) {
136 super.setIndex(index);
137 setSize();
138 }
139
140
141 protected final void setSize() {
142 if (super.getIndex() <= org.apache.bcel.Const.MAX_BYTE) {
143 super.setOpcode(org.apache.bcel.Const.LDC);
144 super.setLength(2);
145 } else {
146 super.setOpcode(org.apache.bcel.Const.LDC_W);
147 super.setLength(3);
148 }
149 }
150 }