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.classfile.ConstantCP;
20 import org.apache.bcel.classfile.ConstantNameAndType;
21 import org.apache.bcel.classfile.ConstantPool;
22 import org.apache.bcel.classfile.ConstantUtf8;
23
24 /***
25 * Super class for InvokeInstruction and FieldInstruction, since they have
26 * some methods in common!
27 *
28 * @version $Id: FieldOrMethod.java 386056 2006-03-15 11:31:56Z tcurdt $
29 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
30 */
31 public abstract class FieldOrMethod extends CPInstruction implements LoadClass {
32
33 /***
34 * Empty constructor needed for the Class.newInstance() statement in
35 * Instruction.readInstruction(). Not to be used otherwise.
36 */
37 FieldOrMethod() {
38 }
39
40
41 /***
42 * @param index to constant pool
43 */
44 protected FieldOrMethod(short opcode, int index) {
45 super(opcode, index);
46 }
47
48
49 /*** @return signature of referenced method/field.
50 */
51 public String getSignature( ConstantPoolGen cpg ) {
52 ConstantPool cp = cpg.getConstantPool();
53 ConstantCP cmr = (ConstantCP) cp.getConstant(index);
54 ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
55 return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
56 }
57
58
59 /*** @return name of referenced method/field.
60 */
61 public String getName( ConstantPoolGen cpg ) {
62 ConstantPool cp = cpg.getConstantPool();
63 ConstantCP cmr = (ConstantCP) cp.getConstant(index);
64 ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
65 return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
66 }
67
68
69 /*** @return name of the referenced class/interface
70 * @deprecated If the instruction references an array class,
71 * this method will return "java.lang.Object".
72 * For code generated by Java 1.5, this answer is
73 * sometimes wrong (e.g., if the "clone()" method is
74 * called on an array). A better idea is to use
75 * the getReferenceType() method, which correctly distinguishes
76 * between class types and array types.
77 */
78 public String getClassName( ConstantPoolGen cpg ) {
79 ConstantPool cp = cpg.getConstantPool();
80 ConstantCP cmr = (ConstantCP) cp.getConstant(index);
81 String className = cp.getConstantString(cmr.getClassIndex(),
82 org.apache.bcel.Constants.CONSTANT_Class);
83 if (className.startsWith("[")) {
84
85 return "java.lang.Object";
86 }
87 return className.replace('/', '.');
88 }
89
90
91 /*** @return type of the referenced class/interface
92 * @deprecated If the instruction references an array class,
93 * the ObjectType returned will be invalid. Use
94 * getReferenceType() instead.
95 */
96 public ObjectType getClassType( ConstantPoolGen cpg ) {
97 return new ObjectType(getClassName(cpg));
98 }
99
100
101 /***
102 * Return the reference type representing the class, interface,
103 * or array class referenced by the instruction.
104 * @param cpg the ConstantPoolGen used to create the instruction
105 * @return an ObjectType (if the referenced class type is a class
106 * or interface), or an ArrayType (if the referenced class
107 * type is an array class)
108 */
109 public ReferenceType getReferenceType( ConstantPoolGen cpg ) {
110 ConstantPool cp = cpg.getConstantPool();
111 ConstantCP cmr = (ConstantCP) cp.getConstant(index);
112 String className = cp.getConstantString(cmr.getClassIndex(),
113 org.apache.bcel.Constants.CONSTANT_Class);
114 if (className.startsWith("[")) {
115 return (ArrayType) Type.getType(className);
116 } else {
117 className = className.replace('/', '.');
118 return new ObjectType(className);
119 }
120 }
121
122
123 /*** @return type of the referenced class/interface
124 */
125 public ObjectType getLoadClassType( ConstantPoolGen cpg ) {
126 return getClassType(cpg);
127 }
128 }