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 org.apache.bcel.Const;
20 import org.apache.bcel.classfile.ConstantCP;
21 import org.apache.bcel.classfile.ConstantNameAndType;
22 import org.apache.bcel.classfile.ConstantPool;
23 import org.apache.bcel.classfile.ConstantUtf8;
24 import org.apache.bcel.classfile.Utility;
25
26
27
28
29 public abstract class FieldOrMethod extends CPInstruction implements LoadClass {
30
31
32
33
34 FieldOrMethod() {
35
36 }
37
38
39
40
41 protected FieldOrMethod(final short opcode, final int index) {
42 super(opcode, index);
43 }
44
45
46
47
48
49
50
51
52 @Deprecated
53 public String getClassName(final ConstantPoolGen cpg) {
54 final ConstantPool cp = cpg.getConstantPool();
55 final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
56 final String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
57 if (className.startsWith("[")) {
58
59 return "java.lang.Object";
60 }
61 return Utility.pathToPackage(className);
62 }
63
64
65
66
67
68
69 @Deprecated
70 public ObjectType getClassType(final ConstantPoolGen cpg) {
71 return ObjectType.getInstance(getClassName(cpg));
72 }
73
74
75
76
77
78
79
80 @Override
81 public ObjectType getLoadClassType(final ConstantPoolGen cpg) {
82 final ReferenceType rt = getReferenceType(cpg);
83 if (rt instanceof ObjectType) {
84 return (ObjectType) rt;
85 }
86 if (rt instanceof ArrayType) {
87 return Type.OBJECT;
88 }
89 throw new ClassGenException(rt.getClass().getCanonicalName() + " " + rt.getSignature() + " does not represent an ObjectType");
90 }
91
92
93
94
95 public String getName(final ConstantPoolGen cpg) {
96 final ConstantPool cp = cpg.getConstantPool();
97 final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
98 final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
99 return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
100 }
101
102
103
104
105
106
107
108
109 public ReferenceType getReferenceType(final ConstantPoolGen cpg) {
110 final ConstantPool cp = cpg.getConstantPool();
111 final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
112 String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
113 if (className.startsWith("[")) {
114 return (ArrayType) Type.getType(className);
115 }
116 className = Utility.pathToPackage(className);
117 return ObjectType.getInstance(className);
118 }
119
120
121
122
123 public String getSignature(final ConstantPoolGen cpg) {
124 final ConstantPool cp = cpg.getConstantPool();
125 final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
126 final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
127 return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
128 }
129 }