author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* This file is available under and governed by the GNU General Public |
|
28 |
* License version 2 only, as published by the Free Software Foundation. |
|
29 |
* However, the following notice accompanied the original version of this |
|
30 |
* file: |
|
31 |
* |
|
32 |
* ASM: a very small and fast Java bytecode manipulation framework |
|
33 |
* Copyright (c) 2000-2007 INRIA, France Telecom |
|
34 |
* All rights reserved. |
|
35 |
* |
|
36 |
* Redistribution and use in source and binary forms, with or without |
|
37 |
* modification, are permitted provided that the following conditions |
|
38 |
* are met: |
|
39 |
* 1. Redistributions of source code must retain the above copyright |
|
40 |
* notice, this list of conditions and the following disclaimer. |
|
41 |
* 2. Redistributions in binary form must reproduce the above copyright |
|
42 |
* notice, this list of conditions and the following disclaimer in the |
|
43 |
* documentation and/or other materials provided with the distribution. |
|
44 |
* 3. Neither the name of the copyright holders nor the names of its |
|
45 |
* contributors may be used to endorse or promote products derived from |
|
46 |
* this software without specific prior written permission. |
|
47 |
* |
|
48 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
49 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
50 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
51 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|
52 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
53 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
54 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
55 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
56 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
57 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
|
58 |
* THE POSSIBILITY OF SUCH DAMAGE. |
|
59 |
*/ |
|
60 |
package com.sun.xml.internal.ws.org.objectweb.asm; |
|
61 |
||
62 |
import java.lang.reflect.Constructor; |
|
63 |
import java.lang.reflect.Method; |
|
64 |
||
65 |
/** |
|
66 |
* A Java type. This class can be used to make it easier to manipulate type and |
|
67 |
* method descriptors. |
|
68 |
* |
|
69 |
* @author Eric Bruneton |
|
70 |
* @author Chris Nokleberg |
|
71 |
*/ |
|
72 |
public class Type { |
|
73 |
||
74 |
/** |
|
75 |
* The sort of the <tt>void</tt> type. See {@link #getSort getSort}. |
|
76 |
*/ |
|
77 |
public static final int VOID = 0; |
|
78 |
||
79 |
/** |
|
80 |
* The sort of the <tt>boolean</tt> type. See {@link #getSort getSort}. |
|
81 |
*/ |
|
82 |
public static final int BOOLEAN = 1; |
|
83 |
||
84 |
/** |
|
85 |
* The sort of the <tt>char</tt> type. See {@link #getSort getSort}. |
|
86 |
*/ |
|
87 |
public static final int CHAR = 2; |
|
88 |
||
89 |
/** |
|
90 |
* The sort of the <tt>byte</tt> type. See {@link #getSort getSort}. |
|
91 |
*/ |
|
92 |
public static final int BYTE = 3; |
|
93 |
||
94 |
/** |
|
95 |
* The sort of the <tt>short</tt> type. See {@link #getSort getSort}. |
|
96 |
*/ |
|
97 |
public static final int SHORT = 4; |
|
98 |
||
99 |
/** |
|
100 |
* The sort of the <tt>int</tt> type. See {@link #getSort getSort}. |
|
101 |
*/ |
|
102 |
public static final int INT = 5; |
|
103 |
||
104 |
/** |
|
105 |
* The sort of the <tt>float</tt> type. See {@link #getSort getSort}. |
|
106 |
*/ |
|
107 |
public static final int FLOAT = 6; |
|
108 |
||
109 |
/** |
|
110 |
* The sort of the <tt>long</tt> type. See {@link #getSort getSort}. |
|
111 |
*/ |
|
112 |
public static final int LONG = 7; |
|
113 |
||
114 |
/** |
|
115 |
* The sort of the <tt>double</tt> type. See {@link #getSort getSort}. |
|
116 |
*/ |
|
117 |
public static final int DOUBLE = 8; |
|
118 |
||
119 |
/** |
|
120 |
* The sort of array reference types. See {@link #getSort getSort}. |
|
121 |
*/ |
|
122 |
public static final int ARRAY = 9; |
|
123 |
||
124 |
/** |
|
125 |
* The sort of object reference type. See {@link #getSort getSort}. |
|
126 |
*/ |
|
127 |
public static final int OBJECT = 10; |
|
128 |
||
129 |
/** |
|
130 |
* The <tt>void</tt> type. |
|
131 |
*/ |
|
132 |
public static final Type VOID_TYPE = new Type(VOID); |
|
133 |
||
134 |
/** |
|
135 |
* The <tt>boolean</tt> type. |
|
136 |
*/ |
|
137 |
public static final Type BOOLEAN_TYPE = new Type(BOOLEAN); |
|
138 |
||
139 |
/** |
|
140 |
* The <tt>char</tt> type. |
|
141 |
*/ |
|
142 |
public static final Type CHAR_TYPE = new Type(CHAR); |
|
143 |
||
144 |
/** |
|
145 |
* The <tt>byte</tt> type. |
|
146 |
*/ |
|
147 |
public static final Type BYTE_TYPE = new Type(BYTE); |
|
148 |
||
149 |
/** |
|
150 |
* The <tt>short</tt> type. |
|
151 |
*/ |
|
152 |
public static final Type SHORT_TYPE = new Type(SHORT); |
|
153 |
||
154 |
/** |
|
155 |
* The <tt>int</tt> type. |
|
156 |
*/ |
|
157 |
public static final Type INT_TYPE = new Type(INT); |
|
158 |
||
159 |
/** |
|
160 |
* The <tt>float</tt> type. |
|
161 |
*/ |
|
162 |
public static final Type FLOAT_TYPE = new Type(FLOAT); |
|
163 |
||
164 |
/** |
|
165 |
* The <tt>long</tt> type. |
|
166 |
*/ |
|
167 |
public static final Type LONG_TYPE = new Type(LONG); |
|
168 |
||
169 |
/** |
|
170 |
* The <tt>double</tt> type. |
|
171 |
*/ |
|
172 |
public static final Type DOUBLE_TYPE = new Type(DOUBLE); |
|
173 |
||
174 |
// ------------------------------------------------------------------------ |
|
175 |
// Fields |
|
176 |
// ------------------------------------------------------------------------ |
|
177 |
||
178 |
/** |
|
179 |
* The sort of this Java type. |
|
180 |
*/ |
|
181 |
private final int sort; |
|
182 |
||
183 |
/** |
|
184 |
* A buffer containing the internal name of this Java type. This field is |
|
185 |
* only used for reference types. |
|
186 |
*/ |
|
187 |
private final char[] buf; |
|
188 |
||
189 |
/** |
|
190 |
* The offset of the internal name of this Java type in {@link #buf buf}. |
|
191 |
* This field is only used for reference types. |
|
192 |
*/ |
|
193 |
private final int off; |
|
194 |
||
195 |
/** |
|
196 |
* The length of the internal name of this Java type. This field is only |
|
197 |
* used for reference types. |
|
198 |
*/ |
|
199 |
private final int len; |
|
200 |
||
201 |
// ------------------------------------------------------------------------ |
|
202 |
// Constructors |
|
203 |
// ------------------------------------------------------------------------ |
|
204 |
||
205 |
/** |
|
206 |
* Constructs a primitive type. |
|
207 |
* |
|
208 |
* @param sort the sort of the primitive type to be constructed. |
|
209 |
*/ |
|
210 |
private Type(final int sort) { |
|
211 |
this(sort, null, 0, 1); |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Constructs a reference type. |
|
216 |
* |
|
217 |
* @param sort the sort of the reference type to be constructed. |
|
218 |
* @param buf a buffer containing the descriptor of the previous type. |
|
219 |
* @param off the offset of this descriptor in the previous buffer. |
|
220 |
* @param len the length of this descriptor. |
|
221 |
*/ |
|
222 |
private Type(final int sort, final char[] buf, final int off, final int len) |
|
223 |
{ |
|
224 |
this.sort = sort; |
|
225 |
this.buf = buf; |
|
226 |
this.off = off; |
|
227 |
this.len = len; |
|
228 |
} |
|
229 |
||
230 |
/** |
|
231 |
* Returns the Java type corresponding to the given type descriptor. |
|
232 |
* |
|
233 |
* @param typeDescriptor a type descriptor. |
|
234 |
* @return the Java type corresponding to the given type descriptor. |
|
235 |
*/ |
|
236 |
public static Type getType(final String typeDescriptor) { |
|
237 |
return getType(typeDescriptor.toCharArray(), 0); |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Returns the Java type corresponding to the given internal name. |
|
242 |
* |
|
243 |
* @param internalName an internal name. |
|
244 |
* @return the Java type corresponding to the given internal name. |
|
245 |
*/ |
|
246 |
public static Type getObjectType(final String internalName) { |
|
247 |
char[] buf = internalName.toCharArray(); |
|
248 |
return new Type(buf[0] == '[' ? ARRAY : OBJECT, buf, 0, buf.length); |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Returns the Java type corresponding to the given class. |
|
253 |
* |
|
254 |
* @param c a class. |
|
255 |
* @return the Java type corresponding to the given class. |
|
256 |
*/ |
|
257 |
public static Type getType(final Class c) { |
|
258 |
if (c.isPrimitive()) { |
|
259 |
if (c == Integer.TYPE) { |
|
260 |
return INT_TYPE; |
|
261 |
} else if (c == Void.TYPE) { |
|
262 |
return VOID_TYPE; |
|
263 |
} else if (c == Boolean.TYPE) { |
|
264 |
return BOOLEAN_TYPE; |
|
265 |
} else if (c == Byte.TYPE) { |
|
266 |
return BYTE_TYPE; |
|
267 |
} else if (c == Character.TYPE) { |
|
268 |
return CHAR_TYPE; |
|
269 |
} else if (c == Short.TYPE) { |
|
270 |
return SHORT_TYPE; |
|
271 |
} else if (c == Double.TYPE) { |
|
272 |
return DOUBLE_TYPE; |
|
273 |
} else if (c == Float.TYPE) { |
|
274 |
return FLOAT_TYPE; |
|
275 |
} else /* if (c == Long.TYPE) */{ |
|
276 |
return LONG_TYPE; |
|
277 |
} |
|
278 |
} else { |
|
279 |
return getType(getDescriptor(c)); |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Returns the Java types corresponding to the argument types of the given |
|
285 |
* method descriptor. |
|
286 |
* |
|
287 |
* @param methodDescriptor a method descriptor. |
|
288 |
* @return the Java types corresponding to the argument types of the given |
|
289 |
* method descriptor. |
|
290 |
*/ |
|
291 |
public static Type[] getArgumentTypes(final String methodDescriptor) { |
|
292 |
char[] buf = methodDescriptor.toCharArray(); |
|
293 |
int off = 1; |
|
294 |
int size = 0; |
|
295 |
while (true) { |
|
296 |
char car = buf[off++]; |
|
297 |
if (car == ')') { |
|
298 |
break; |
|
299 |
} else if (car == 'L') { |
|
300 |
while (buf[off++] != ';') { |
|
301 |
} |
|
302 |
++size; |
|
303 |
} else if (car != '[') { |
|
304 |
++size; |
|
305 |
} |
|
306 |
} |
|
307 |
Type[] args = new Type[size]; |
|
308 |
off = 1; |
|
309 |
size = 0; |
|
310 |
while (buf[off] != ')') { |
|
311 |
args[size] = getType(buf, off); |
|
312 |
off += args[size].len + (args[size].sort == OBJECT ? 2 : 0); |
|
313 |
size += 1; |
|
314 |
} |
|
315 |
return args; |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* Returns the Java types corresponding to the argument types of the given |
|
320 |
* method. |
|
321 |
* |
|
322 |
* @param method a method. |
|
323 |
* @return the Java types corresponding to the argument types of the given |
|
324 |
* method. |
|
325 |
*/ |
|
326 |
public static Type[] getArgumentTypes(final Method method) { |
|
327 |
Class[] classes = method.getParameterTypes(); |
|
328 |
Type[] types = new Type[classes.length]; |
|
329 |
for (int i = classes.length - 1; i >= 0; --i) { |
|
330 |
types[i] = getType(classes[i]); |
|
331 |
} |
|
332 |
return types; |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Returns the Java type corresponding to the return type of the given |
|
337 |
* method descriptor. |
|
338 |
* |
|
339 |
* @param methodDescriptor a method descriptor. |
|
340 |
* @return the Java type corresponding to the return type of the given |
|
341 |
* method descriptor. |
|
342 |
*/ |
|
343 |
public static Type getReturnType(final String methodDescriptor) { |
|
344 |
char[] buf = methodDescriptor.toCharArray(); |
|
345 |
return getType(buf, methodDescriptor.indexOf(')') + 1); |
|
346 |
} |
|
347 |
||
348 |
/** |
|
349 |
* Returns the Java type corresponding to the return type of the given |
|
350 |
* method. |
|
351 |
* |
|
352 |
* @param method a method. |
|
353 |
* @return the Java type corresponding to the return type of the given |
|
354 |
* method. |
|
355 |
*/ |
|
356 |
public static Type getReturnType(final Method method) { |
|
357 |
return getType(method.getReturnType()); |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Returns the Java type corresponding to the given type descriptor. |
|
362 |
* |
|
363 |
* @param buf a buffer containing a type descriptor. |
|
364 |
* @param off the offset of this descriptor in the previous buffer. |
|
365 |
* @return the Java type corresponding to the given type descriptor. |
|
366 |
*/ |
|
367 |
private static Type getType(final char[] buf, final int off) { |
|
368 |
int len; |
|
369 |
switch (buf[off]) { |
|
370 |
case 'V': |
|
371 |
return VOID_TYPE; |
|
372 |
case 'Z': |
|
373 |
return BOOLEAN_TYPE; |
|
374 |
case 'C': |
|
375 |
return CHAR_TYPE; |
|
376 |
case 'B': |
|
377 |
return BYTE_TYPE; |
|
378 |
case 'S': |
|
379 |
return SHORT_TYPE; |
|
380 |
case 'I': |
|
381 |
return INT_TYPE; |
|
382 |
case 'F': |
|
383 |
return FLOAT_TYPE; |
|
384 |
case 'J': |
|
385 |
return LONG_TYPE; |
|
386 |
case 'D': |
|
387 |
return DOUBLE_TYPE; |
|
388 |
case '[': |
|
389 |
len = 1; |
|
390 |
while (buf[off + len] == '[') { |
|
391 |
++len; |
|
392 |
} |
|
393 |
if (buf[off + len] == 'L') { |
|
394 |
++len; |
|
395 |
while (buf[off + len] != ';') { |
|
396 |
++len; |
|
397 |
} |
|
398 |
} |
|
399 |
return new Type(ARRAY, buf, off, len + 1); |
|
400 |
// case 'L': |
|
401 |
default: |
|
402 |
len = 1; |
|
403 |
while (buf[off + len] != ';') { |
|
404 |
++len; |
|
405 |
} |
|
406 |
return new Type(OBJECT, buf, off + 1, len - 1); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
// ------------------------------------------------------------------------ |
|
411 |
// Accessors |
|
412 |
// ------------------------------------------------------------------------ |
|
413 |
||
414 |
/** |
|
415 |
* Returns the sort of this Java type. |
|
416 |
* |
|
417 |
* @return {@link #VOID VOID}, {@link #BOOLEAN BOOLEAN}, |
|
418 |
* {@link #CHAR CHAR}, {@link #BYTE BYTE}, {@link #SHORT SHORT}, |
|
419 |
* {@link #INT INT}, {@link #FLOAT FLOAT}, {@link #LONG LONG}, |
|
420 |
* {@link #DOUBLE DOUBLE}, {@link #ARRAY ARRAY} or |
|
421 |
* {@link #OBJECT OBJECT}. |
|
422 |
*/ |
|
423 |
public int getSort() { |
|
424 |
return sort; |
|
425 |
} |
|
426 |
||
427 |
/** |
|
428 |
* Returns the number of dimensions of this array type. This method should |
|
429 |
* only be used for an array type. |
|
430 |
* |
|
431 |
* @return the number of dimensions of this array type. |
|
432 |
*/ |
|
433 |
public int getDimensions() { |
|
434 |
int i = 1; |
|
435 |
while (buf[off + i] == '[') { |
|
436 |
++i; |
|
437 |
} |
|
438 |
return i; |
|
439 |
} |
|
440 |
||
441 |
/** |
|
442 |
* Returns the type of the elements of this array type. This method should |
|
443 |
* only be used for an array type. |
|
444 |
* |
|
445 |
* @return Returns the type of the elements of this array type. |
|
446 |
*/ |
|
447 |
public Type getElementType() { |
|
448 |
return getType(buf, off + getDimensions()); |
|
449 |
} |
|
450 |
||
451 |
/** |
|
452 |
* Returns the name of the class corresponding to this type. |
|
453 |
* |
|
454 |
* @return the fully qualified name of the class corresponding to this type. |
|
455 |
*/ |
|
456 |
public String getClassName() { |
|
457 |
switch (sort) { |
|
458 |
case VOID: |
|
459 |
return "void"; |
|
460 |
case BOOLEAN: |
|
461 |
return "boolean"; |
|
462 |
case CHAR: |
|
463 |
return "char"; |
|
464 |
case BYTE: |
|
465 |
return "byte"; |
|
466 |
case SHORT: |
|
467 |
return "short"; |
|
468 |
case INT: |
|
469 |
return "int"; |
|
470 |
case FLOAT: |
|
471 |
return "float"; |
|
472 |
case LONG: |
|
473 |
return "long"; |
|
474 |
case DOUBLE: |
|
475 |
return "double"; |
|
476 |
case ARRAY: |
|
477 |
StringBuffer b = new StringBuffer(getElementType().getClassName()); |
|
478 |
for (int i = getDimensions(); i > 0; --i) { |
|
479 |
b.append("[]"); |
|
480 |
} |
|
481 |
return b.toString(); |
|
482 |
// case OBJECT: |
|
483 |
default: |
|
484 |
return new String(buf, off, len).replace('/', '.'); |
|
485 |
} |
|
486 |
} |
|
487 |
||
488 |
/** |
|
489 |
* Returns the internal name of the class corresponding to this object or |
|
490 |
* array type. The internal name of a class is its fully qualified name (as |
|
491 |
* returned by Class.getName(), where '.' are replaced by '/'. This method |
|
492 |
* should only be used for an object or array type. |
|
493 |
* |
|
494 |
* @return the internal name of the class corresponding to this object type. |
|
495 |
*/ |
|
496 |
public String getInternalName() { |
|
497 |
return new String(buf, off, len); |
|
498 |
} |
|
499 |
||
500 |
// ------------------------------------------------------------------------ |
|
501 |
// Conversion to type descriptors |
|
502 |
// ------------------------------------------------------------------------ |
|
503 |
||
504 |
/** |
|
505 |
* Returns the descriptor corresponding to this Java type. |
|
506 |
* |
|
507 |
* @return the descriptor corresponding to this Java type. |
|
508 |
*/ |
|
509 |
public String getDescriptor() { |
|
510 |
StringBuffer buf = new StringBuffer(); |
|
511 |
getDescriptor(buf); |
|
512 |
return buf.toString(); |
|
513 |
} |
|
514 |
||
515 |
/** |
|
516 |
* Returns the descriptor corresponding to the given argument and return |
|
517 |
* types. |
|
518 |
* |
|
519 |
* @param returnType the return type of the method. |
|
520 |
* @param argumentTypes the argument types of the method. |
|
521 |
* @return the descriptor corresponding to the given argument and return |
|
522 |
* types. |
|
523 |
*/ |
|
524 |
public static String getMethodDescriptor( |
|
525 |
final Type returnType, |
|
526 |
final Type[] argumentTypes) |
|
527 |
{ |
|
528 |
StringBuffer buf = new StringBuffer(); |
|
529 |
buf.append('('); |
|
530 |
for (int i = 0; i < argumentTypes.length; ++i) { |
|
531 |
argumentTypes[i].getDescriptor(buf); |
|
532 |
} |
|
533 |
buf.append(')'); |
|
534 |
returnType.getDescriptor(buf); |
|
535 |
return buf.toString(); |
|
536 |
} |
|
537 |
||
538 |
/** |
|
539 |
* Appends the descriptor corresponding to this Java type to the given |
|
540 |
* string buffer. |
|
541 |
* |
|
542 |
* @param buf the string buffer to which the descriptor must be appended. |
|
543 |
*/ |
|
544 |
private void getDescriptor(final StringBuffer buf) { |
|
545 |
switch (sort) { |
|
546 |
case VOID: |
|
547 |
buf.append('V'); |
|
548 |
return; |
|
549 |
case BOOLEAN: |
|
550 |
buf.append('Z'); |
|
551 |
return; |
|
552 |
case CHAR: |
|
553 |
buf.append('C'); |
|
554 |
return; |
|
555 |
case BYTE: |
|
556 |
buf.append('B'); |
|
557 |
return; |
|
558 |
case SHORT: |
|
559 |
buf.append('S'); |
|
560 |
return; |
|
561 |
case INT: |
|
562 |
buf.append('I'); |
|
563 |
return; |
|
564 |
case FLOAT: |
|
565 |
buf.append('F'); |
|
566 |
return; |
|
567 |
case LONG: |
|
568 |
buf.append('J'); |
|
569 |
return; |
|
570 |
case DOUBLE: |
|
571 |
buf.append('D'); |
|
572 |
return; |
|
573 |
case ARRAY: |
|
574 |
buf.append(this.buf, off, len); |
|
575 |
return; |
|
576 |
// case OBJECT: |
|
577 |
default: |
|
578 |
buf.append('L'); |
|
579 |
buf.append(this.buf, off, len); |
|
580 |
buf.append(';'); |
|
581 |
} |
|
582 |
} |
|
583 |
||
584 |
// ------------------------------------------------------------------------ |
|
585 |
// Direct conversion from classes to type descriptors, |
|
586 |
// without intermediate Type objects |
|
587 |
// ------------------------------------------------------------------------ |
|
588 |
||
589 |
/** |
|
590 |
* Returns the internal name of the given class. The internal name of a |
|
591 |
* class is its fully qualified name, as returned by Class.getName(), where |
|
592 |
* '.' are replaced by '/'. |
|
593 |
* |
|
594 |
* @param c an object or array class. |
|
595 |
* @return the internal name of the given class. |
|
596 |
*/ |
|
597 |
public static String getInternalName(final Class c) { |
|
598 |
return c.getName().replace('.', '/'); |
|
599 |
} |
|
600 |
||
601 |
/** |
|
602 |
* Returns the descriptor corresponding to the given Java type. |
|
603 |
* |
|
604 |
* @param c an object class, a primitive class or an array class. |
|
605 |
* @return the descriptor corresponding to the given class. |
|
606 |
*/ |
|
607 |
public static String getDescriptor(final Class c) { |
|
608 |
StringBuffer buf = new StringBuffer(); |
|
609 |
getDescriptor(buf, c); |
|
610 |
return buf.toString(); |
|
611 |
} |
|
612 |
||
613 |
/** |
|
614 |
* Returns the descriptor corresponding to the given constructor. |
|
615 |
* |
|
616 |
* @param c a {@link Constructor Constructor} object. |
|
617 |
* @return the descriptor of the given constructor. |
|
618 |
*/ |
|
619 |
public static String getConstructorDescriptor(final Constructor c) { |
|
620 |
Class[] parameters = c.getParameterTypes(); |
|
621 |
StringBuffer buf = new StringBuffer(); |
|
622 |
buf.append('('); |
|
623 |
for (int i = 0; i < parameters.length; ++i) { |
|
624 |
getDescriptor(buf, parameters[i]); |
|
625 |
} |
|
626 |
return buf.append(")V").toString(); |
|
627 |
} |
|
628 |
||
629 |
/** |
|
630 |
* Returns the descriptor corresponding to the given method. |
|
631 |
* |
|
632 |
* @param m a {@link Method Method} object. |
|
633 |
* @return the descriptor of the given method. |
|
634 |
*/ |
|
635 |
public static String getMethodDescriptor(final Method m) { |
|
636 |
Class[] parameters = m.getParameterTypes(); |
|
637 |
StringBuffer buf = new StringBuffer(); |
|
638 |
buf.append('('); |
|
639 |
for (int i = 0; i < parameters.length; ++i) { |
|
640 |
getDescriptor(buf, parameters[i]); |
|
641 |
} |
|
642 |
buf.append(')'); |
|
643 |
getDescriptor(buf, m.getReturnType()); |
|
644 |
return buf.toString(); |
|
645 |
} |
|
646 |
||
647 |
/** |
|
648 |
* Appends the descriptor of the given class to the given string buffer. |
|
649 |
* |
|
650 |
* @param buf the string buffer to which the descriptor must be appended. |
|
651 |
* @param c the class whose descriptor must be computed. |
|
652 |
*/ |
|
653 |
private static void getDescriptor(final StringBuffer buf, final Class c) { |
|
654 |
Class d = c; |
|
655 |
while (true) { |
|
656 |
if (d.isPrimitive()) { |
|
657 |
char car; |
|
658 |
if (d == Integer.TYPE) { |
|
659 |
car = 'I'; |
|
660 |
} else if (d == Void.TYPE) { |
|
661 |
car = 'V'; |
|
662 |
} else if (d == Boolean.TYPE) { |
|
663 |
car = 'Z'; |
|
664 |
} else if (d == Byte.TYPE) { |
|
665 |
car = 'B'; |
|
666 |
} else if (d == Character.TYPE) { |
|
667 |
car = 'C'; |
|
668 |
} else if (d == Short.TYPE) { |
|
669 |
car = 'S'; |
|
670 |
} else if (d == Double.TYPE) { |
|
671 |
car = 'D'; |
|
672 |
} else if (d == Float.TYPE) { |
|
673 |
car = 'F'; |
|
674 |
} else /* if (d == Long.TYPE) */{ |
|
675 |
car = 'J'; |
|
676 |
} |
|
677 |
buf.append(car); |
|
678 |
return; |
|
679 |
} else if (d.isArray()) { |
|
680 |
buf.append('['); |
|
681 |
d = d.getComponentType(); |
|
682 |
} else { |
|
683 |
buf.append('L'); |
|
684 |
String name = d.getName(); |
|
685 |
int len = name.length(); |
|
686 |
for (int i = 0; i < len; ++i) { |
|
687 |
char car = name.charAt(i); |
|
688 |
buf.append(car == '.' ? '/' : car); |
|
689 |
} |
|
690 |
buf.append(';'); |
|
691 |
return; |
|
692 |
} |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
// ------------------------------------------------------------------------ |
|
697 |
// Corresponding size and opcodes |
|
698 |
// ------------------------------------------------------------------------ |
|
699 |
||
700 |
/** |
|
701 |
* Returns the size of values of this type. |
|
702 |
* |
|
703 |
* @return the size of values of this type, i.e., 2 for <tt>long</tt> and |
|
704 |
* <tt>double</tt>, and 1 otherwise. |
|
705 |
*/ |
|
706 |
public int getSize() { |
|
707 |
return sort == LONG || sort == DOUBLE ? 2 : 1; |
|
708 |
} |
|
709 |
||
710 |
/** |
|
711 |
* Returns a JVM instruction opcode adapted to this Java type. |
|
712 |
* |
|
713 |
* @param opcode a JVM instruction opcode. This opcode must be one of ILOAD, |
|
714 |
* ISTORE, IALOAD, IASTORE, IADD, ISUB, IMUL, IDIV, IREM, INEG, ISHL, |
|
715 |
* ISHR, IUSHR, IAND, IOR, IXOR and IRETURN. |
|
716 |
* @return an opcode that is similar to the given opcode, but adapted to |
|
717 |
* this Java type. For example, if this type is <tt>float</tt> and |
|
718 |
* <tt>opcode</tt> is IRETURN, this method returns FRETURN. |
|
719 |
*/ |
|
720 |
public int getOpcode(final int opcode) { |
|
721 |
if (opcode == Opcodes.IALOAD || opcode == Opcodes.IASTORE) { |
|
722 |
switch (sort) { |
|
723 |
case BOOLEAN: |
|
724 |
case BYTE: |
|
725 |
return opcode + 5; |
|
726 |
case CHAR: |
|
727 |
return opcode + 6; |
|
728 |
case SHORT: |
|
729 |
return opcode + 7; |
|
730 |
case INT: |
|
731 |
return opcode; |
|
732 |
case FLOAT: |
|
733 |
return opcode + 2; |
|
734 |
case LONG: |
|
735 |
return opcode + 1; |
|
736 |
case DOUBLE: |
|
737 |
return opcode + 3; |
|
738 |
// case ARRAY: |
|
739 |
// case OBJECT: |
|
740 |
default: |
|
741 |
return opcode + 4; |
|
742 |
} |
|
743 |
} else { |
|
744 |
switch (sort) { |
|
745 |
case VOID: |
|
746 |
return opcode + 5; |
|
747 |
case BOOLEAN: |
|
748 |
case CHAR: |
|
749 |
case BYTE: |
|
750 |
case SHORT: |
|
751 |
case INT: |
|
752 |
return opcode; |
|
753 |
case FLOAT: |
|
754 |
return opcode + 2; |
|
755 |
case LONG: |
|
756 |
return opcode + 1; |
|
757 |
case DOUBLE: |
|
758 |
return opcode + 3; |
|
759 |
// case ARRAY: |
|
760 |
// case OBJECT: |
|
761 |
default: |
|
762 |
return opcode + 4; |
|
763 |
} |
|
764 |
} |
|
765 |
} |
|
766 |
||
767 |
// ------------------------------------------------------------------------ |
|
768 |
// Equals, hashCode and toString |
|
769 |
// ------------------------------------------------------------------------ |
|
770 |
||
771 |
/** |
|
772 |
* Tests if the given object is equal to this type. |
|
773 |
* |
|
774 |
* @param o the object to be compared to this type. |
|
775 |
* @return <tt>true</tt> if the given object is equal to this type. |
|
776 |
*/ |
|
777 |
public boolean equals(final Object o) { |
|
778 |
if (this == o) { |
|
779 |
return true; |
|
780 |
} |
|
781 |
if (!(o instanceof Type)) { |
|
782 |
return false; |
|
783 |
} |
|
784 |
Type t = (Type) o; |
|
785 |
if (sort != t.sort) { |
|
786 |
return false; |
|
787 |
} |
|
788 |
if (sort == OBJECT || sort == ARRAY) { |
|
789 |
if (len != t.len) { |
|
790 |
return false; |
|
791 |
} |
|
792 |
for (int i = off, j = t.off, end = i + len; i < end; i++, j++) { |
|
793 |
if (buf[i] != t.buf[j]) { |
|
794 |
return false; |
|
795 |
} |
|
796 |
} |
|
797 |
} |
|
798 |
return true; |
|
799 |
} |
|
800 |
||
801 |
/** |
|
802 |
* Returns a hash code value for this type. |
|
803 |
* |
|
804 |
* @return a hash code value for this type. |
|
805 |
*/ |
|
806 |
public int hashCode() { |
|
807 |
int hc = 13 * sort; |
|
808 |
if (sort == OBJECT || sort == ARRAY) { |
|
809 |
for (int i = off, end = i + len; i < end; i++) { |
|
810 |
hc = 17 * (hc + buf[i]); |
|
811 |
} |
|
812 |
} |
|
813 |
return hc; |
|
814 |
} |
|
815 |
||
816 |
/** |
|
817 |
* Returns a string representation of this type. |
|
818 |
* |
|
819 |
* @return the descriptor of this type. |
|
820 |
*/ |
|
821 |
public String toString() { |
|
822 |
return getDescriptor(); |
|
823 |
} |
|
824 |
} |