KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > instruction > ReferencingInstruction


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.reflect.instruction;
20
21 import alt.jiapi.reflect.Instruction;
22 import alt.jiapi.file.ConstantPool;
23 import alt.jiapi.file.ConstantPool.NameAndTypeInfo;
24
25 /**
26  * This class represents an Instruction, that uses Object
27  * references.
28  *
29  * @author Mika Riekkinen
30  * @author Joni Suominen
31  * @version $Revision: 1.17 $ $Date: 2005/06/09 16:48:28 $
32  */

33 public class ReferencingInstruction extends CPInstruction {
34     public ReferencingInstruction(byte[] bytes, ConstantPool cp) {
35         super(bytes, cp);
36     }
37
38
39     /**
40      * Gets name of the referenced type. This method inspects
41      * instruction directly to determine type of the referenced Object.
42      *
43      * @return name of the referenced type.
44      */

45     public String JavaDoc getReferencedTypeName() {
46         return getClassName();
47     }
48
49
50     /**
51      * Get a name of the referenced class's type.
52      */

53     public String JavaDoc getClassName() {
54         return getInternalName().replace('/', '.');
55     }
56
57
58     /**
59      * Get an internal name of referenced type
60      */

61     public String JavaDoc getInternalName() {
62         ConstantPool.Entry entry = cp.get(getIndex());
63
64         if (entry instanceof ConstantPool.MethodRefInfo) {
65             ConstantPool.MethodRefInfo mri = (ConstantPool.MethodRefInfo)entry;
66             return mri.getClassInfo().getName();
67         }
68         else if (entry instanceof ConstantPool.FieldRefInfo) {
69             ConstantPool.FieldRefInfo fri = (ConstantPool.FieldRefInfo)entry;
70             return fri.getClassInfo().getName();
71         }
72         else if (entry instanceof ConstantPool.InterfaceMethodRefInfo) {
73             ConstantPool.InterfaceMethodRefInfo fri = (ConstantPool.InterfaceMethodRefInfo)entry;
74             return fri.getClassInfo().getName();
75     }
76     else {
77         System.out.println("Don't know what to do with " + entry);
78     }
79         
80         return null;
81     }
82
83     /**
84      * Get a name of the reference (e.g. method name or field name).
85      */

86     public String JavaDoc getName() {
87         ConstantPool.Entry entry = cp.get(getIndex());
88         NameAndTypeInfo nti = null;
89
90         if (entry instanceof ConstantPool.MethodRefInfo) {
91             ConstantPool.MethodRefInfo mri = (ConstantPool.MethodRefInfo)entry;
92             nti = mri.getNameAndTypeInfo();
93         }
94         else if (entry instanceof ConstantPool.FieldRefInfo) {
95             ConstantPool.FieldRefInfo fri = (ConstantPool.FieldRefInfo)entry;
96             nti = fri.getNameAndTypeInfo();
97         }
98         else if (entry instanceof ConstantPool.InterfaceMethodRefInfo) {
99             ConstantPool.InterfaceMethodRefInfo fri = (ConstantPool.InterfaceMethodRefInfo)entry;
100             nti = fri.getNameAndTypeInfo();
101     }
102     else {
103         System.out.println("Don't know what to do with " + entry);
104     }
105
106         return nti.getName();
107     }
108
109
110     /**
111      * Get a signature of the reference (e.g. method's signature or
112      * field's signature).
113      */

114     public String JavaDoc getDescriptor() {
115         ConstantPool.Entry entry = cp.get(getIndex());
116         NameAndTypeInfo nti = null;
117
118         if (entry instanceof ConstantPool.MethodRefInfo) {
119             ConstantPool.MethodRefInfo mri = (ConstantPool.MethodRefInfo)entry;
120             nti = mri.getNameAndTypeInfo();
121         }
122         else if (entry instanceof ConstantPool.FieldRefInfo) {
123             ConstantPool.FieldRefInfo fri = (ConstantPool.FieldRefInfo)entry;
124             nti = fri.getNameAndTypeInfo();
125         }
126         else if (entry instanceof ConstantPool.InterfaceMethodRefInfo) {
127             ConstantPool.InterfaceMethodRefInfo fri =
128                 (ConstantPool.InterfaceMethodRefInfo)entry;
129             nti = fri.getNameAndTypeInfo();
130         }
131         else {
132             System.out.println(cp);
133             System.out.println("Failed to get (interface)Method/Field ref-info from index " + getIndex() + ": " + entry);
134             System.out.println("Instruction: " +
135                                Opcodes.opcodeStrings[getOpcode()&0xff]);
136         }
137         return nti.getDescriptor();
138     }
139
140     // Add following method, if we decide to provide getDeclaringClass()
141
// on alt.jiapi.reflect.instruction.(Abstract)Instruction
142
/*
143      * Gets name of the referenced type. This method inspects
144      * instruction directly to determine type of the referenced Object.
145      * Furthermore, it uses <code>alt.jiapi.reflect.Loader</code>
146      * to load this type.
147      *
148      * @exception ClassNotFoundException is thrown, if corresponding
149      * JiapiClass could not be loaded
150      * @return JiapiClass representing referenced type.
151      */

152     
153 // public JiapiClass getReferencedType() throws ClassNotFoundException;
154

155 // private short getIndex() {
156
// byte[] bytes = getBytes();
157

158 // short index = 0;
159
// if (bytes.length == 2) {
160
// index = bytes[1];
161
// }
162
// else {
163
// short index = (short)(bytes[1] << 8);
164
// index |= bytes[2];
165
// }
166

167 // return (short)(0xff & index);
168
// }
169
}
170
Popular Tags