KickJava   Java API By Example, From Geeks To Geeks.

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


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.SignatureUtil;
22 import alt.jiapi.reflect.Signature;
23 import alt.jiapi.file.ConstantPool;
24
25 /**
26  * This class represents a method invocation instruction.
27  *
28  * @author Mika Riekkinen
29  * @author Joni Suominen
30  * @version $Revision: 1.16 $ $Date: 2004/04/11 14:22:58 $
31  */

32 public class Invocation extends ReferencingInstruction {
33     public Invocation(byte[] bytes, ConstantPool cp) {
34         super(bytes, cp);
35     }
36
37
38     /**
39      * Get the name of the target method.
40      */

41     public String JavaDoc getMethodName() {
42         return super.getName();
43     }
44
45     /**
46      * Gets the return type.
47      */

48     public String JavaDoc getReturnType() {
49         Signature s = new Signature(getDescriptor());
50         return s.getReturnType();
51     }
52
53     /**
54      * Gets names of each parameter types.
55      */

56     public String JavaDoc[] getParameterTypes() {
57         Signature s = new Signature(getDescriptor());
58         return s.getParameters();
59     }
60
61
62    /**
63      * Get the stack consumption of this Invocation.
64      *
65      * @return Stack consumption
66      */

67     public short stackConsumption() {
68         short consumption = 0;//(short)getParameterTypes().length;
69

70         String JavaDoc[] paramTypes = getParameterTypes();
71         for (int i = 0; i < paramTypes.length; i++) {
72             if ("long".equals(paramTypes[i]) ||
73                 "double".equals(paramTypes[i])) {
74                 consumption += 2;
75             }
76             else {
77                 consumption++;
78             }
79         }
80
81         if (getOpcode() != Opcodes.INVOKESTATIC) {
82             consumption++; // 'this' parameter
83
}
84
85         return consumption;
86     }
87
88
89     /**
90      * Count stack usage of this invocation.
91      */

92     public short stackUsage() {
93         int usage = 0;
94         String JavaDoc[] paramTypes = getParameterTypes();
95         for (int i = 0; i < paramTypes.length; i++) {
96             if ("long".equals(paramTypes[i]) ||
97                 "double".equals(paramTypes[i])) {
98                 usage -= 2;
99             }
100             else {
101                 usage--;
102             }
103         }
104
105 // short usage = (short)-getParameterTypes().length;
106

107         if (getOpcode() != Opcodes.INVOKESTATIC) {
108             usage--; // 'this' parameter
109
}
110
111         
112         if (!"void".equals(getReturnType())) {
113             usage++;
114         }
115
116         if ("long".equals(getReturnType()) ||
117             "double".equals(getReturnType())) {
118             usage++;
119         }
120
121         return (short)usage;
122     }
123
124
125     public String JavaDoc toString() {
126         StringBuffer JavaDoc sb =
127             new StringBuffer JavaDoc(Opcodes.opcodeStrings[0xff & getOpcode()]);
128
129         byte[] bytes = getBytes();
130         for (int i = 1; i < bytes.length; i++) {
131             sb.append(" ");
132             sb.append(0xff & bytes[i]);
133         }
134
135         sb.append(" ; ");
136         sb.append(getClassName());
137         sb.append(".");
138         sb.append(getMethodName());
139         sb.append(getDescriptor());
140
141         return sb.toString();
142     }
143 }
144
145
Popular Tags