001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package com.sleepycat.asm;
030:
031: import java.lang.reflect.Method;
032:
033: /**
034: * A Java type. This class can be used to make it easier to manipulate type and
035: * method descriptors.
036: *
037: * @author Eric Bruneton
038: * @author Chris Nokleberg
039: */
040: public class Type {
041:
042: /**
043: * The sort of the <tt>void</tt> type. See {@link #getSort getSort}.
044: */
045: public final static int VOID = 0;
046:
047: /**
048: * The sort of the <tt>boolean</tt> type. See {@link #getSort getSort}.
049: */
050: public final static int BOOLEAN = 1;
051:
052: /**
053: * The sort of the <tt>char</tt> type. See {@link #getSort getSort}.
054: */
055: public final static int CHAR = 2;
056:
057: /**
058: * The sort of the <tt>byte</tt> type. See {@link #getSort getSort}.
059: */
060: public final static int BYTE = 3;
061:
062: /**
063: * The sort of the <tt>short</tt> type. See {@link #getSort getSort}.
064: */
065: public final static int SHORT = 4;
066:
067: /**
068: * The sort of the <tt>int</tt> type. See {@link #getSort getSort}.
069: */
070: public final static int INT = 5;
071:
072: /**
073: * The sort of the <tt>float</tt> type. See {@link #getSort getSort}.
074: */
075: public final static int FLOAT = 6;
076:
077: /**
078: * The sort of the <tt>long</tt> type. See {@link #getSort getSort}.
079: */
080: public final static int LONG = 7;
081:
082: /**
083: * The sort of the <tt>double</tt> type. See {@link #getSort getSort}.
084: */
085: public final static int DOUBLE = 8;
086:
087: /**
088: * The sort of array reference types. See {@link #getSort getSort}.
089: */
090: public final static int ARRAY = 9;
091:
092: /**
093: * The sort of object reference type. See {@link #getSort getSort}.
094: */
095: public final static int OBJECT = 10;
096:
097: /**
098: * The <tt>void</tt> type.
099: */
100: public final static Type VOID_TYPE = new Type(VOID);
101:
102: /**
103: * The <tt>boolean</tt> type.
104: */
105: public final static Type BOOLEAN_TYPE = new Type(BOOLEAN);
106:
107: /**
108: * The <tt>char</tt> type.
109: */
110: public final static Type CHAR_TYPE = new Type(CHAR);
111:
112: /**
113: * The <tt>byte</tt> type.
114: */
115: public final static Type BYTE_TYPE = new Type(BYTE);
116:
117: /**
118: * The <tt>short</tt> type.
119: */
120: public final static Type SHORT_TYPE = new Type(SHORT);
121:
122: /**
123: * The <tt>int</tt> type.
124: */
125: public final static Type INT_TYPE = new Type(INT);
126:
127: /**
128: * The <tt>float</tt> type.
129: */
130: public final static Type FLOAT_TYPE = new Type(FLOAT);
131:
132: /**
133: * The <tt>long</tt> type.
134: */
135: public final static Type LONG_TYPE = new Type(LONG);
136:
137: /**
138: * The <tt>double</tt> type.
139: */
140: public final static Type DOUBLE_TYPE = new Type(DOUBLE);
141:
142: // ------------------------------------------------------------------------
143: // Fields
144: // ------------------------------------------------------------------------
145:
146: /**
147: * The sort of this Java type.
148: */
149: private final int sort;
150:
151: /**
152: * A buffer containing the descriptor of this Java type. This field is only
153: * used for reference types.
154: */
155: private char[] buf;
156:
157: /**
158: * The offset of the descriptor of this Java type in {@link #buf buf}. This
159: * field is only used for reference types.
160: */
161: private int off;
162:
163: /**
164: * The length of the descriptor of this Java type.
165: */
166: private int len;
167:
168: // ------------------------------------------------------------------------
169: // Constructors
170: // ------------------------------------------------------------------------
171:
172: /**
173: * Constructs a primitive type.
174: *
175: * @param sort the sort of the primitive type to be constructed.
176: */
177: private Type(final int sort) {
178: this .sort = sort;
179: this .len = 1;
180: }
181:
182: /**
183: * Constructs a reference type.
184: *
185: * @param sort the sort of the reference type to be constructed.
186: * @param buf a buffer containing the descriptor of the previous type.
187: * @param off the offset of this descriptor in the previous buffer.
188: * @param len the length of this descriptor.
189: */
190: private Type(final int sort, final char[] buf, final int off,
191: final int len) {
192: this .sort = sort;
193: this .buf = buf;
194: this .off = off;
195: this .len = len;
196: }
197:
198: /**
199: * Returns the Java type corresponding to the given type descriptor.
200: *
201: * @param typeDescriptor a type descriptor.
202: * @return the Java type corresponding to the given type descriptor.
203: */
204: public static Type getType(final String typeDescriptor) {
205: return getType(typeDescriptor.toCharArray(), 0);
206: }
207:
208: /**
209: * Returns the Java type corresponding to the given class.
210: *
211: * @param c a class.
212: * @return the Java type corresponding to the given class.
213: */
214: public static Type getType(final Class c) {
215: if (c.isPrimitive()) {
216: if (c == Integer.TYPE) {
217: return INT_TYPE;
218: } else if (c == Void.TYPE) {
219: return VOID_TYPE;
220: } else if (c == Boolean.TYPE) {
221: return BOOLEAN_TYPE;
222: } else if (c == Byte.TYPE) {
223: return BYTE_TYPE;
224: } else if (c == Character.TYPE) {
225: return CHAR_TYPE;
226: } else if (c == Short.TYPE) {
227: return SHORT_TYPE;
228: } else if (c == Double.TYPE) {
229: return DOUBLE_TYPE;
230: } else if (c == Float.TYPE) {
231: return FLOAT_TYPE;
232: } else /* if (c == Long.TYPE) */{
233: return LONG_TYPE;
234: }
235: } else {
236: return getType(getDescriptor(c));
237: }
238: }
239:
240: /**
241: * Returns the Java types corresponding to the argument types of the given
242: * method descriptor.
243: *
244: * @param methodDescriptor a method descriptor.
245: * @return the Java types corresponding to the argument types of the given
246: * method descriptor.
247: */
248: public static Type[] getArgumentTypes(final String methodDescriptor) {
249: char[] buf = methodDescriptor.toCharArray();
250: int off = 1;
251: int size = 0;
252: while (true) {
253: char car = buf[off++];
254: if (car == ')') {
255: break;
256: } else if (car == 'L') {
257: while (buf[off++] != ';') {
258: }
259: ++size;
260: } else if (car != '[') {
261: ++size;
262: }
263: }
264: Type[] args = new Type[size];
265: off = 1;
266: size = 0;
267: while (buf[off] != ')') {
268: args[size] = getType(buf, off);
269: off += args[size].len;
270: size += 1;
271: }
272: return args;
273: }
274:
275: /**
276: * Returns the Java types corresponding to the argument types of the given
277: * method.
278: *
279: * @param method a method.
280: * @return the Java types corresponding to the argument types of the given
281: * method.
282: */
283: public static Type[] getArgumentTypes(final Method method) {
284: Class[] classes = method.getParameterTypes();
285: Type[] types = new Type[classes.length];
286: for (int i = classes.length - 1; i >= 0; --i) {
287: types[i] = getType(classes[i]);
288: }
289: return types;
290: }
291:
292: /**
293: * Returns the Java type corresponding to the return type of the given
294: * method descriptor.
295: *
296: * @param methodDescriptor a method descriptor.
297: * @return the Java type corresponding to the return type of the given
298: * method descriptor.
299: */
300: public static Type getReturnType(final String methodDescriptor) {
301: char[] buf = methodDescriptor.toCharArray();
302: return getType(buf, methodDescriptor.indexOf(')') + 1);
303: }
304:
305: /**
306: * Returns the Java type corresponding to the return type of the given
307: * method.
308: *
309: * @param method a method.
310: * @return the Java type corresponding to the return type of the given
311: * method.
312: */
313: public static Type getReturnType(final Method method) {
314: return getType(method.getReturnType());
315: }
316:
317: /**
318: * Returns the Java type corresponding to the given type descriptor.
319: *
320: * @param buf a buffer containing a type descriptor.
321: * @param off the offset of this descriptor in the previous buffer.
322: * @return the Java type corresponding to the given type descriptor.
323: */
324: private static Type getType(final char[] buf, final int off) {
325: int len;
326: switch (buf[off]) {
327: case 'V':
328: return VOID_TYPE;
329: case 'Z':
330: return BOOLEAN_TYPE;
331: case 'C':
332: return CHAR_TYPE;
333: case 'B':
334: return BYTE_TYPE;
335: case 'S':
336: return SHORT_TYPE;
337: case 'I':
338: return INT_TYPE;
339: case 'F':
340: return FLOAT_TYPE;
341: case 'J':
342: return LONG_TYPE;
343: case 'D':
344: return DOUBLE_TYPE;
345: case '[':
346: len = 1;
347: while (buf[off + len] == '[') {
348: ++len;
349: }
350: if (buf[off + len] == 'L') {
351: ++len;
352: while (buf[off + len] != ';') {
353: ++len;
354: }
355: }
356: return new Type(ARRAY, buf, off, len + 1);
357: // case 'L':
358: default:
359: len = 1;
360: while (buf[off + len] != ';') {
361: ++len;
362: }
363: return new Type(OBJECT, buf, off, len + 1);
364: }
365: }
366:
367: // ------------------------------------------------------------------------
368: // Accessors
369: // ------------------------------------------------------------------------
370:
371: /**
372: * Returns the sort of this Java type.
373: *
374: * @return {@link #VOID VOID}, {@link #BOOLEAN BOOLEAN},
375: * {@link #CHAR CHAR}, {@link #BYTE BYTE}, {@link #SHORT SHORT},
376: * {@link #INT INT}, {@link #FLOAT FLOAT}, {@link #LONG LONG},
377: * {@link #DOUBLE DOUBLE}, {@link #ARRAY ARRAY} or
378: * {@link #OBJECT OBJECT}.
379: */
380: public int getSort() {
381: return sort;
382: }
383:
384: /**
385: * Returns the number of dimensions of this array type. This method should
386: * only be used for an array type.
387: *
388: * @return the number of dimensions of this array type.
389: */
390: public int getDimensions() {
391: int i = 1;
392: while (buf[off + i] == '[') {
393: ++i;
394: }
395: return i;
396: }
397:
398: /**
399: * Returns the type of the elements of this array type. This method should
400: * only be used for an array type.
401: *
402: * @return Returns the type of the elements of this array type.
403: */
404: public Type getElementType() {
405: return getType(buf, off + getDimensions());
406: }
407:
408: /**
409: * Returns the name of the class corresponding to this type.
410: *
411: * @return the fully qualified name of the class corresponding to this type.
412: */
413: public String getClassName() {
414: switch (sort) {
415: case VOID:
416: return "void";
417: case BOOLEAN:
418: return "boolean";
419: case CHAR:
420: return "char";
421: case BYTE:
422: return "byte";
423: case SHORT:
424: return "short";
425: case INT:
426: return "int";
427: case FLOAT:
428: return "float";
429: case LONG:
430: return "long";
431: case DOUBLE:
432: return "double";
433: case ARRAY:
434: StringBuffer b = new StringBuffer(getElementType()
435: .getClassName());
436: for (int i = getDimensions(); i > 0; --i) {
437: b.append("[]");
438: }
439: return b.toString();
440: // case OBJECT:
441: default:
442: return new String(buf, off + 1, len - 2).replace('/', '.');
443: }
444: }
445:
446: /**
447: * Returns the internal name of the class corresponding to this object type.
448: * The internal name of a class is its fully qualified name, where '.' are
449: * replaced by '/'. This method should only be used for an object type.
450: *
451: * @return the internal name of the class corresponding to this object type.
452: */
453: public String getInternalName() {
454: return new String(buf, off + 1, len - 2);
455: }
456:
457: // ------------------------------------------------------------------------
458: // Conversion to type descriptors
459: // ------------------------------------------------------------------------
460:
461: /**
462: * Returns the descriptor corresponding to this Java type.
463: *
464: * @return the descriptor corresponding to this Java type.
465: */
466: public String getDescriptor() {
467: StringBuffer buf = new StringBuffer();
468: getDescriptor(buf);
469: return buf.toString();
470: }
471:
472: /**
473: * Returns the descriptor corresponding to the given argument and return
474: * types.
475: *
476: * @param returnType the return type of the method.
477: * @param argumentTypes the argument types of the method.
478: * @return the descriptor corresponding to the given argument and return
479: * types.
480: */
481: public static String getMethodDescriptor(final Type returnType,
482: final Type[] argumentTypes) {
483: StringBuffer buf = new StringBuffer();
484: buf.append('(');
485: for (int i = 0; i < argumentTypes.length; ++i) {
486: argumentTypes[i].getDescriptor(buf);
487: }
488: buf.append(')');
489: returnType.getDescriptor(buf);
490: return buf.toString();
491: }
492:
493: /**
494: * Appends the descriptor corresponding to this Java type to the given
495: * string buffer.
496: *
497: * @param buf the string buffer to which the descriptor must be appended.
498: */
499: private void getDescriptor(final StringBuffer buf) {
500: switch (sort) {
501: case VOID:
502: buf.append('V');
503: return;
504: case BOOLEAN:
505: buf.append('Z');
506: return;
507: case CHAR:
508: buf.append('C');
509: return;
510: case BYTE:
511: buf.append('B');
512: return;
513: case SHORT:
514: buf.append('S');
515: return;
516: case INT:
517: buf.append('I');
518: return;
519: case FLOAT:
520: buf.append('F');
521: return;
522: case LONG:
523: buf.append('J');
524: return;
525: case DOUBLE:
526: buf.append('D');
527: return;
528: // case ARRAY:
529: // case OBJECT:
530: default:
531: buf.append(this .buf, off, len);
532: }
533: }
534:
535: // ------------------------------------------------------------------------
536: // Direct conversion from classes to type descriptors,
537: // without intermediate Type objects
538: // ------------------------------------------------------------------------
539:
540: /**
541: * Returns the internal name of the given class. The internal name of a
542: * class is its fully qualified name, where '.' are replaced by '/'.
543: *
544: * @param c an object class.
545: * @return the internal name of the given class.
546: */
547: public static String getInternalName(final Class c) {
548: return c.getName().replace('.', '/');
549: }
550:
551: /**
552: * Returns the descriptor corresponding to the given Java type.
553: *
554: * @param c an object class, a primitive class or an array class.
555: * @return the descriptor corresponding to the given class.
556: */
557: public static String getDescriptor(final Class c) {
558: StringBuffer buf = new StringBuffer();
559: getDescriptor(buf, c);
560: return buf.toString();
561: }
562:
563: /**
564: * Returns the descriptor corresponding to the given method.
565: *
566: * @param m a {@link Method Method} object.
567: * @return the descriptor of the given method.
568: */
569: public static String getMethodDescriptor(final Method m) {
570: Class[] parameters = m.getParameterTypes();
571: StringBuffer buf = new StringBuffer();
572: buf.append('(');
573: for (int i = 0; i < parameters.length; ++i) {
574: getDescriptor(buf, parameters[i]);
575: }
576: buf.append(')');
577: getDescriptor(buf, m.getReturnType());
578: return buf.toString();
579: }
580:
581: /**
582: * Appends the descriptor of the given class to the given string buffer.
583: *
584: * @param buf the string buffer to which the descriptor must be appended.
585: * @param c the class whose descriptor must be computed.
586: */
587: private static void getDescriptor(final StringBuffer buf,
588: final Class c) {
589: Class d = c;
590: while (true) {
591: if (d.isPrimitive()) {
592: char car;
593: if (d == Integer.TYPE) {
594: car = 'I';
595: } else if (d == Void.TYPE) {
596: car = 'V';
597: } else if (d == Boolean.TYPE) {
598: car = 'Z';
599: } else if (d == Byte.TYPE) {
600: car = 'B';
601: } else if (d == Character.TYPE) {
602: car = 'C';
603: } else if (d == Short.TYPE) {
604: car = 'S';
605: } else if (d == Double.TYPE) {
606: car = 'D';
607: } else if (d == Float.TYPE) {
608: car = 'F';
609: } else /* if (d == Long.TYPE) */{
610: car = 'J';
611: }
612: buf.append(car);
613: return;
614: } else if (d.isArray()) {
615: buf.append('[');
616: d = d.getComponentType();
617: } else {
618: buf.append('L');
619: String name = d.getName();
620: int len = name.length();
621: for (int i = 0; i < len; ++i) {
622: char car = name.charAt(i);
623: buf.append(car == '.' ? '/' : car);
624: }
625: buf.append(';');
626: return;
627: }
628: }
629: }
630:
631: // ------------------------------------------------------------------------
632: // Corresponding size and opcodes
633: // ------------------------------------------------------------------------
634:
635: /**
636: * Returns the size of values of this type.
637: *
638: * @return the size of values of this type, i.e., 2 for <tt>long</tt> and
639: * <tt>double</tt>, and 1 otherwise.
640: */
641: public int getSize() {
642: return (sort == LONG || sort == DOUBLE ? 2 : 1);
643: }
644:
645: /**
646: * Returns a JVM instruction opcode adapted to this Java type.
647: *
648: * @param opcode a JVM instruction opcode. This opcode must be one of ILOAD,
649: * ISTORE, IALOAD, IASTORE, IADD, ISUB, IMUL, IDIV, IREM, INEG, ISHL,
650: * ISHR, IUSHR, IAND, IOR, IXOR and IRETURN.
651: * @return an opcode that is similar to the given opcode, but adapted to
652: * this Java type. For example, if this type is <tt>float</tt> and
653: * <tt>opcode</tt> is IRETURN, this method returns FRETURN.
654: */
655: public int getOpcode(final int opcode) {
656: if (opcode == Opcodes.IALOAD || opcode == Opcodes.IASTORE) {
657: switch (sort) {
658: case BOOLEAN:
659: case BYTE:
660: return opcode + 5;
661: case CHAR:
662: return opcode + 6;
663: case SHORT:
664: return opcode + 7;
665: case INT:
666: return opcode;
667: case FLOAT:
668: return opcode + 2;
669: case LONG:
670: return opcode + 1;
671: case DOUBLE:
672: return opcode + 3;
673: // case ARRAY:
674: // case OBJECT:
675: default:
676: return opcode + 4;
677: }
678: } else {
679: switch (sort) {
680: case VOID:
681: return opcode + 5;
682: case BOOLEAN:
683: case CHAR:
684: case BYTE:
685: case SHORT:
686: case INT:
687: return opcode;
688: case FLOAT:
689: return opcode + 2;
690: case LONG:
691: return opcode + 1;
692: case DOUBLE:
693: return opcode + 3;
694: // case ARRAY:
695: // case OBJECT:
696: default:
697: return opcode + 4;
698: }
699: }
700: }
701:
702: // ------------------------------------------------------------------------
703: // Equals, hashCode and toString
704: // ------------------------------------------------------------------------
705:
706: /**
707: * Tests if the given object is equal to this type.
708: *
709: * @param o the object to be compared to this type.
710: * @return <tt>true</tt> if the given object is equal to this type.
711: */
712: public boolean equals(final Object o) {
713: if (this == o) {
714: return true;
715: }
716: if (o == null || !(o instanceof Type)) {
717: return false;
718: }
719: Type t = (Type) o;
720: if (sort != t.sort) {
721: return false;
722: }
723: if (sort == Type.OBJECT || sort == Type.ARRAY) {
724: if (len != t.len) {
725: return false;
726: }
727: for (int i = off, j = t.off, end = i + len; i < end; i++, j++) {
728: if (buf[i] != t.buf[j]) {
729: return false;
730: }
731: }
732: }
733: return true;
734: }
735:
736: /**
737: * Returns a hash code value for this type.
738: *
739: * @return a hash code value for this type.
740: */
741: public int hashCode() {
742: int hc = 13 * sort;
743: if (sort == Type.OBJECT || sort == Type.ARRAY) {
744: for (int i = off, end = i + len; i < end; i++) {
745: hc = 17 * (hc + buf[i]);
746: }
747: }
748: return hc;
749: }
750:
751: /**
752: * Returns a string representation of this type.
753: *
754: * @return the descriptor of this type.
755: */
756: public String toString() {
757: return getDescriptor();
758: }
759: }
|