6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
package com.sun.org.apache.bcel.internal.generic;
|
|
6 |
|
|
7 |
/* ====================================================================
|
|
8 |
* The Apache Software License, Version 1.1
|
|
9 |
*
|
|
10 |
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
|
11 |
* reserved.
|
|
12 |
*
|
|
13 |
* Redistribution and use in source and binary forms, with or without
|
|
14 |
* modification, are permitted provided that the following conditions
|
|
15 |
* are met:
|
|
16 |
*
|
|
17 |
* 1. Redistributions of source code must retain the above copyright
|
|
18 |
* notice, this list of conditions and the following disclaimer.
|
|
19 |
*
|
|
20 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
21 |
* notice, this list of conditions and the following disclaimer in
|
|
22 |
* the documentation and/or other materials provided with the
|
|
23 |
* distribution.
|
|
24 |
*
|
|
25 |
* 3. The end-user documentation included with the redistribution,
|
|
26 |
* if any, must include the following acknowledgment:
|
|
27 |
* "This product includes software developed by the
|
|
28 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
29 |
* Alternately, this acknowledgment may appear in the software itself,
|
|
30 |
* if and wherever such third-party acknowledgments normally appear.
|
|
31 |
*
|
|
32 |
* 4. The names "Apache" and "Apache Software Foundation" and
|
|
33 |
* "Apache BCEL" must not be used to endorse or promote products
|
|
34 |
* derived from this software without prior written permission. For
|
|
35 |
* written permission, please contact apache@apache.org.
|
|
36 |
*
|
|
37 |
* 5. Products derived from this software may not be called "Apache",
|
|
38 |
* "Apache BCEL", nor may "Apache" appear in their name, without
|
|
39 |
* prior written permission of the Apache Software Foundation.
|
|
40 |
*
|
|
41 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
42 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
43 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
44 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
45 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
46 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
47 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
48 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
49 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
50 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
51 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
52 |
* SUCH DAMAGE.
|
|
53 |
* ====================================================================
|
|
54 |
*
|
|
55 |
* This software consists of voluntary contributions made by many
|
|
56 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
57 |
* information on the Apache Software Foundation, please see
|
|
58 |
* <http://www.apache.org/>.
|
|
59 |
*/
|
|
60 |
|
|
61 |
import com.sun.org.apache.bcel.internal.Constants;
|
|
62 |
import com.sun.org.apache.bcel.internal.classfile.*;
|
|
63 |
import java.util.ArrayList;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Abstract super class for all possible java types, namely basic types
|
|
67 |
* such as int, object types like String and array types, e.g. int[]
|
|
68 |
*
|
|
69 |
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
|
|
70 |
*/
|
|
71 |
public abstract class Type implements java.io.Serializable {
|
|
72 |
protected byte type;
|
|
73 |
protected String signature; // signature for the type
|
|
74 |
|
|
75 |
/** Predefined constants
|
|
76 |
*/
|
|
77 |
public static final BasicType VOID = new BasicType(Constants.T_VOID);
|
|
78 |
public static final BasicType BOOLEAN = new BasicType(Constants.T_BOOLEAN);
|
|
79 |
public static final BasicType INT = new BasicType(Constants.T_INT);
|
|
80 |
public static final BasicType SHORT = new BasicType(Constants.T_SHORT);
|
|
81 |
public static final BasicType BYTE = new BasicType(Constants.T_BYTE);
|
|
82 |
public static final BasicType LONG = new BasicType(Constants.T_LONG);
|
|
83 |
public static final BasicType DOUBLE = new BasicType(Constants.T_DOUBLE);
|
|
84 |
public static final BasicType FLOAT = new BasicType(Constants.T_FLOAT);
|
|
85 |
public static final BasicType CHAR = new BasicType(Constants.T_CHAR);
|
|
86 |
public static final ObjectType OBJECT = new ObjectType("java.lang.Object");
|
|
87 |
public static final ObjectType STRING = new ObjectType("java.lang.String");
|
|
88 |
public static final ObjectType STRINGBUFFER = new ObjectType("java.lang.StringBuffer");
|
|
89 |
public static final ObjectType THROWABLE = new ObjectType("java.lang.Throwable");
|
|
90 |
public static final Type[] NO_ARGS = new Type[0];
|
|
91 |
public static final ReferenceType NULL = new ReferenceType(){};
|
|
92 |
public static final Type UNKNOWN = new Type(Constants.T_UNKNOWN,
|
|
93 |
"<unknown object>"){};
|
|
94 |
|
|
95 |
protected Type(byte t, String s) {
|
|
96 |
type = t;
|
|
97 |
signature = s;
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* @return signature for given type.
|
|
102 |
*/
|
|
103 |
public String getSignature() { return signature; }
|
|
104 |
|
|
105 |
/**
|
|
106 |
* @return type as defined in Constants
|
|
107 |
*/
|
|
108 |
public byte getType() { return type; }
|
|
109 |
|
|
110 |
/**
|
|
111 |
* @return stack size of this type (2 for long and double, 0 for void, 1 otherwise)
|
|
112 |
*/
|
|
113 |
public int getSize() {
|
|
114 |
switch(type) {
|
|
115 |
case Constants.T_DOUBLE:
|
|
116 |
case Constants.T_LONG: return 2;
|
|
117 |
case Constants.T_VOID: return 0;
|
|
118 |
default: return 1;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
/**
|
|
123 |
* @return Type string, e.g. `int[]'
|
|
124 |
*/
|
|
125 |
public String toString() {
|
|
126 |
return ((this.equals(Type.NULL) || (type >= Constants.T_UNKNOWN)))? signature :
|
|
127 |
Utility.signatureToString(signature, false);
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Convert type to Java method signature, e.g. int[] f(java.lang.String x)
|
|
132 |
* becomes (Ljava/lang/String;)[I
|
|
133 |
*
|
|
134 |
* @param return_type what the method returns
|
|
135 |
* @param arg_types what are the argument types
|
|
136 |
* @return method signature for given type(s).
|
|
137 |
*/
|
|
138 |
public static String getMethodSignature(Type return_type, Type[] arg_types) {
|
|
139 |
StringBuffer buf = new StringBuffer("(");
|
|
140 |
int length = (arg_types == null)? 0 : arg_types.length;
|
|
141 |
|
|
142 |
for(int i=0; i < length; i++)
|
|
143 |
buf.append(arg_types[i].getSignature());
|
|
144 |
|
|
145 |
buf.append(')');
|
|
146 |
buf.append(return_type.getSignature());
|
|
147 |
|
|
148 |
return buf.toString();
|
|
149 |
}
|
|
150 |
|
|
151 |
private static int consumed_chars=0; // Remember position in string, see getArgumentTypes
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Convert signature to a Type object.
|
|
155 |
* @param signature signature string such as Ljava/lang/String;
|
|
156 |
* @return type object
|
|
157 |
*/
|
|
158 |
public static final Type getType(String signature)
|
|
159 |
throws StringIndexOutOfBoundsException
|
|
160 |
{
|
|
161 |
byte type = Utility.typeOfSignature(signature);
|
|
162 |
|
|
163 |
if(type <= Constants.T_VOID) {
|
|
164 |
consumed_chars = 1;
|
|
165 |
return BasicType.getType(type);
|
|
166 |
} else if(type == Constants.T_ARRAY) {
|
|
167 |
int dim=0;
|
|
168 |
do { // Count dimensions
|
|
169 |
dim++;
|
|
170 |
} while(signature.charAt(dim) == '[');
|
|
171 |
|
|
172 |
// Recurse, but just once, if the signature is ok
|
|
173 |
Type t = getType(signature.substring(dim));
|
|
174 |
|
|
175 |
consumed_chars += dim; // update counter
|
|
176 |
|
|
177 |
return new ArrayType(t, dim);
|
|
178 |
} else { // type == T_REFERENCE
|
|
179 |
int index = signature.indexOf(';'); // Look for closing `;'
|
|
180 |
|
|
181 |
if(index < 0)
|
|
182 |
throw new ClassFormatException("Invalid signature: " + signature);
|
|
183 |
|
|
184 |
consumed_chars = index + 1; // "Lblabla;" `L' and `;' are removed
|
|
185 |
|
|
186 |
return new ObjectType(signature.substring(1, index).replace('/', '.'));
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Convert return value of a method (signature) to a Type object.
|
|
192 |
*
|
|
193 |
* @param signature signature string such as (Ljava/lang/String;)V
|
|
194 |
* @return return type
|
|
195 |
*/
|
|
196 |
public static Type getReturnType(String signature) {
|
|
197 |
try {
|
|
198 |
// Read return type after `)'
|
|
199 |
int index = signature.lastIndexOf(')') + 1;
|
|
200 |
return getType(signature.substring(index));
|
|
201 |
} catch(StringIndexOutOfBoundsException e) { // Should never occur
|
|
202 |
throw new ClassFormatException("Invalid method signature: " + signature);
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
/**
|
|
207 |
* Convert arguments of a method (signature) to an array of Type objects.
|
|
208 |
* @param signature signature string such as (Ljava/lang/String;)V
|
|
209 |
* @return array of argument types
|
|
210 |
*/
|
|
211 |
public static Type[] getArgumentTypes(String signature) {
|
|
212 |
ArrayList vec = new ArrayList();
|
|
213 |
int index;
|
|
214 |
Type[] types;
|
|
215 |
|
|
216 |
try { // Read all declarations between for `(' and `)'
|
|
217 |
if(signature.charAt(0) != '(')
|
|
218 |
throw new ClassFormatException("Invalid method signature: " + signature);
|
|
219 |
|
|
220 |
index = 1; // current string position
|
|
221 |
|
|
222 |
while(signature.charAt(index) != ')') {
|
|
223 |
vec.add(getType(signature.substring(index)));
|
|
224 |
index += consumed_chars; // update position
|
|
225 |
}
|
|
226 |
} catch(StringIndexOutOfBoundsException e) { // Should never occur
|
|
227 |
throw new ClassFormatException("Invalid method signature: " + signature);
|
|
228 |
}
|
|
229 |
|
|
230 |
types = new Type[vec.size()];
|
|
231 |
vec.toArray(types);
|
|
232 |
return types;
|
|
233 |
}
|
|
234 |
|
|
235 |
/** Convert runtime java.lang.Class to BCEL Type object.
|
|
236 |
* @param cl Java class
|
|
237 |
* @return corresponding Type object
|
|
238 |
*/
|
|
239 |
public static Type getType(java.lang.Class cl) {
|
|
240 |
if(cl == null) {
|
|
241 |
throw new IllegalArgumentException("Class must not be null");
|
|
242 |
}
|
|
243 |
|
|
244 |
/* That's an amzingly easy case, because getName() returns
|
|
245 |
* the signature. That's what we would have liked anyway.
|
|
246 |
*/
|
|
247 |
if(cl.isArray()) {
|
|
248 |
return getType(cl.getName());
|
|
249 |
} else if(cl.isPrimitive()) {
|
|
250 |
if(cl == Integer.TYPE) {
|
|
251 |
return INT;
|
|
252 |
} else if(cl == Void.TYPE) {
|
|
253 |
return VOID;
|
|
254 |
} else if(cl == Double.TYPE) {
|
|
255 |
return DOUBLE;
|
|
256 |
} else if(cl == Float.TYPE) {
|
|
257 |
return FLOAT;
|
|
258 |
} else if(cl == Boolean.TYPE) {
|
|
259 |
return BOOLEAN;
|
|
260 |
} else if(cl == Byte.TYPE) {
|
|
261 |
return BYTE;
|
|
262 |
} else if(cl == Short.TYPE) {
|
|
263 |
return SHORT;
|
|
264 |
} else if(cl == Byte.TYPE) {
|
|
265 |
return BYTE;
|
|
266 |
} else if(cl == Long.TYPE) {
|
|
267 |
return LONG;
|
|
268 |
} else if(cl == Character.TYPE) {
|
|
269 |
return CHAR;
|
|
270 |
} else {
|
|
271 |
throw new IllegalStateException("Ooops, what primitive type is " + cl);
|
|
272 |
}
|
|
273 |
} else { // "Real" class
|
|
274 |
return new ObjectType(cl.getName());
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
public static String getSignature(java.lang.reflect.Method meth) {
|
|
279 |
StringBuffer sb = new StringBuffer("(");
|
|
280 |
Class[] params = meth.getParameterTypes(); // avoid clone
|
|
281 |
|
|
282 |
for(int j = 0; j < params.length; j++) {
|
|
283 |
sb.append(getType(params[j]).getSignature());
|
|
284 |
}
|
|
285 |
|
|
286 |
sb.append(")");
|
|
287 |
sb.append(getType(meth.getReturnType()).getSignature());
|
|
288 |
return sb.toString();
|
|
289 |
}
|
|
290 |
}
|