< prev index next >

src/java.base/share/classes/sun/invoke/util/Wrapper.java

Print this page
rev 52865 : [mq]: 8210031


  30     BOOLEAN(  Boolean.class,   "Boolean", boolean.class, "boolean", 'Z', new boolean[0], Format.unsigned( 1)),
  31     // These must be in the order defined for widening primitive conversions in JLS 5.1.2
  32     // Avoid boxing integral types here to defer initialization of internal caches
  33     BYTE   (     Byte.class,      "Byte",    byte.class,    "byte", 'B', new    byte[0], Format.signed(   8)),
  34     SHORT  (    Short.class,     "Short",   short.class,   "short", 'S', new   short[0], Format.signed(  16)),
  35     CHAR   (Character.class, "Character",    char.class,    "char", 'C', new    char[0], Format.unsigned(16)),
  36     INT    (  Integer.class,   "Integer",     int.class,     "int", 'I', new     int[0], Format.signed(  32)),
  37     LONG   (     Long.class,      "Long",    long.class,    "long", 'J', new    long[0], Format.signed(  64)),
  38     FLOAT  (    Float.class,     "Float",   float.class,   "float", 'F', new   float[0], Format.floating(32)),
  39     DOUBLE (   Double.class,    "Double",  double.class,  "double", 'D', new  double[0], Format.floating(64)),
  40     OBJECT (   Object.class,    "Object",  Object.class,  "Object", 'L', new  Object[0], Format.other(    1)),
  41     // VOID must be the last type, since it is "assignable" from any other type:
  42     VOID   (     Void.class,      "Void",    void.class,    "void", 'V',           null, Format.other(    0)),
  43     ;
  44 
  45     public static final int COUNT = 10;
  46 
  47     private final Class<?> wrapperType;
  48     private final Class<?> primitiveType;
  49     private final char     basicTypeChar;

  50     private final Object   emptyArray;
  51     private final int      format;
  52     private final String   wrapperSimpleName;
  53     private final String   primitiveSimpleName;
  54 
  55     private Wrapper(Class<?> wtype, String wtypeName, Class<?> ptype, String ptypeName, char tchar, Object emptyArray, int format) {
  56         this.wrapperType = wtype;
  57         this.primitiveType = ptype;
  58         this.basicTypeChar = tchar;

  59         this.emptyArray = emptyArray;
  60         this.format = format;
  61         this.wrapperSimpleName = wtypeName;
  62         this.primitiveSimpleName = ptypeName;
  63     }
  64 
  65     /** For debugging, give the details of this wrapper. */
  66     public String detailString() {
  67         return wrapperSimpleName+
  68                 java.util.Arrays.asList(wrapperType, primitiveType,
  69                 basicTypeChar, zero(),
  70                 "0x"+Integer.toHexString(format));
  71     }
  72 
  73     private abstract static class Format {
  74         static final int SLOT_SHIFT = 0, SIZE_SHIFT = 2, KIND_SHIFT = 12;
  75         static final int
  76                 SIGNED   = (-1) << KIND_SHIFT,
  77                 UNSIGNED = 0    << KIND_SHIFT,
  78                 FLOATING = 1    << KIND_SHIFT;


 442     /** Query:  Is the given type a primitive, such as {@code int} or {@code void}? */
 443     public static boolean isPrimitiveType(Class<?> type) {
 444         return type.isPrimitive();
 445     }
 446 
 447     /** What is the bytecode signature character for this type?
 448      *  All non-primitives, including array types, report as 'L', the signature character for references.
 449      */
 450     public static char basicTypeChar(Class<?> type) {
 451         if (!type.isPrimitive())
 452             return 'L';
 453         else
 454             return forPrimitiveType(type).basicTypeChar();
 455     }
 456 
 457     /** What is the bytecode signature character for this wrapper's
 458      *  primitive type?
 459      */
 460     public char basicTypeChar() { return basicTypeChar; }
 461 





 462     /** What is the simple name of the wrapper type?
 463      */
 464     public String wrapperSimpleName() { return wrapperSimpleName; }
 465 
 466     /** What is the simple name of the primitive type?
 467      */
 468     public String primitiveSimpleName() { return primitiveSimpleName; }
 469 
 470 //    /** Wrap a value in the given type, which may be either a primitive or wrapper type.
 471 //     *  Performs standard primitive conversions, including truncation and float conversions.
 472 //     */
 473 //    public static <T> T wrap(Object x, Class<T> type) {
 474 //        return Wrapper.valueOf(type).cast(x, type);
 475 //    }
 476 
 477     /** Cast a wrapped value to the given type, which may be either a primitive or wrapper type.
 478      *  The given target type must be this wrapper's primitive or wrapper type.
 479      *  If this wrapper is OBJECT, the target type may also be an interface, perform no runtime check.
 480      *  Performs standard primitive conversions, including truncation and float conversions.
 481      *  The given type must be compatible with this wrapper.  That is, it must either


 564         switch (basicTypeChar) {
 565             case 'I': return Integer.valueOf(xn.intValue());
 566             case 'J': return Long.valueOf(xn.longValue());
 567             case 'F': return Float.valueOf(xn.floatValue());
 568             case 'D': return Double.valueOf(xn.doubleValue());
 569             case 'S': return Short.valueOf((short) xn.intValue());
 570             case 'B': return Byte.valueOf((byte) xn.intValue());
 571             case 'C': return Character.valueOf((char) xn.intValue());
 572             case 'Z': return Boolean.valueOf(boolValue(xn.byteValue()));
 573         }
 574         throw new InternalError("bad wrapper");
 575     }
 576 
 577     /** Wrap a value (an int or smaller value) in this wrapper's type.
 578      * Performs standard primitive conversions, including truncation and float conversions.
 579      * Produces an {@code Integer} for {@code OBJECT}, although the exact type
 580      * of the operand is not known.
 581      * Returns null for {@code VOID}.
 582      */
 583     public Object wrap(int x) {
 584         if (basicTypeChar == 'L')  return (Integer)x;
 585         switch (basicTypeChar) {
 586             case 'L': throw newIllegalArgumentException("cannot wrap to object type");
 587             case 'V': return null;
 588             case 'I': return Integer.valueOf(x);
 589             case 'J': return Long.valueOf(x);
 590             case 'F': return Float.valueOf(x);
 591             case 'D': return Double.valueOf(x);
 592             case 'S': return Short.valueOf((short) x);
 593             case 'B': return Byte.valueOf((byte) x);
 594             case 'C': return Character.valueOf((char) x);
 595             case 'Z': return Boolean.valueOf(boolValue((byte) x));
 596         }
 597         throw new InternalError("bad wrapper");
 598     }
 599 
 600     private static Number numberValue(Object x) {
 601         if (x instanceof Number)     return (Number)x;
 602         if (x instanceof Character)  return (int)(Character)x;
 603         if (x instanceof Boolean)    return (Boolean)x ? 1 : 0;
 604         // Remaining allowed case of void:  Must be a null reference.
 605         return (Number)x;
 606     }




  30     BOOLEAN(  Boolean.class,   "Boolean", boolean.class, "boolean", 'Z', new boolean[0], Format.unsigned( 1)),
  31     // These must be in the order defined for widening primitive conversions in JLS 5.1.2
  32     // Avoid boxing integral types here to defer initialization of internal caches
  33     BYTE   (     Byte.class,      "Byte",    byte.class,    "byte", 'B', new    byte[0], Format.signed(   8)),
  34     SHORT  (    Short.class,     "Short",   short.class,   "short", 'S', new   short[0], Format.signed(  16)),
  35     CHAR   (Character.class, "Character",    char.class,    "char", 'C', new    char[0], Format.unsigned(16)),
  36     INT    (  Integer.class,   "Integer",     int.class,     "int", 'I', new     int[0], Format.signed(  32)),
  37     LONG   (     Long.class,      "Long",    long.class,    "long", 'J', new    long[0], Format.signed(  64)),
  38     FLOAT  (    Float.class,     "Float",   float.class,   "float", 'F', new   float[0], Format.floating(32)),
  39     DOUBLE (   Double.class,    "Double",  double.class,  "double", 'D', new  double[0], Format.floating(64)),
  40     OBJECT (   Object.class,    "Object",  Object.class,  "Object", 'L', new  Object[0], Format.other(    1)),
  41     // VOID must be the last type, since it is "assignable" from any other type:
  42     VOID   (     Void.class,      "Void",    void.class,    "void", 'V',           null, Format.other(    0)),
  43     ;
  44 
  45     public static final int COUNT = 10;
  46 
  47     private final Class<?> wrapperType;
  48     private final Class<?> primitiveType;
  49     private final char     basicTypeChar;
  50     private final String   basicTypeString;
  51     private final Object   emptyArray;
  52     private final int      format;
  53     private final String   wrapperSimpleName;
  54     private final String   primitiveSimpleName;
  55 
  56     private Wrapper(Class<?> wtype, String wtypeName, Class<?> ptype, String ptypeName, char tchar, Object emptyArray, int format) {
  57         this.wrapperType = wtype;
  58         this.primitiveType = ptype;
  59         this.basicTypeChar = tchar;
  60         this.basicTypeString = new String(new char[] {this.basicTypeChar});
  61         this.emptyArray = emptyArray;
  62         this.format = format;
  63         this.wrapperSimpleName = wtypeName;
  64         this.primitiveSimpleName = ptypeName;
  65     }
  66 
  67     /** For debugging, give the details of this wrapper. */
  68     public String detailString() {
  69         return wrapperSimpleName+
  70                 java.util.Arrays.asList(wrapperType, primitiveType,
  71                 basicTypeChar, zero(),
  72                 "0x"+Integer.toHexString(format));
  73     }
  74 
  75     private abstract static class Format {
  76         static final int SLOT_SHIFT = 0, SIZE_SHIFT = 2, KIND_SHIFT = 12;
  77         static final int
  78                 SIGNED   = (-1) << KIND_SHIFT,
  79                 UNSIGNED = 0    << KIND_SHIFT,
  80                 FLOATING = 1    << KIND_SHIFT;


 444     /** Query:  Is the given type a primitive, such as {@code int} or {@code void}? */
 445     public static boolean isPrimitiveType(Class<?> type) {
 446         return type.isPrimitive();
 447     }
 448 
 449     /** What is the bytecode signature character for this type?
 450      *  All non-primitives, including array types, report as 'L', the signature character for references.
 451      */
 452     public static char basicTypeChar(Class<?> type) {
 453         if (!type.isPrimitive())
 454             return 'L';
 455         else
 456             return forPrimitiveType(type).basicTypeChar();
 457     }
 458 
 459     /** What is the bytecode signature character for this wrapper's
 460      *  primitive type?
 461      */
 462     public char basicTypeChar() { return basicTypeChar; }
 463 
 464     /** What is the bytecode signature string for this wrapper's
 465      *  primitive type?
 466      */
 467     public String basicTypeString() { return basicTypeString; }
 468 
 469     /** What is the simple name of the wrapper type?
 470      */
 471     public String wrapperSimpleName() { return wrapperSimpleName; }
 472 
 473     /** What is the simple name of the primitive type?
 474      */
 475     public String primitiveSimpleName() { return primitiveSimpleName; }
 476 
 477 //    /** Wrap a value in the given type, which may be either a primitive or wrapper type.
 478 //     *  Performs standard primitive conversions, including truncation and float conversions.
 479 //     */
 480 //    public static <T> T wrap(Object x, Class<T> type) {
 481 //        return Wrapper.valueOf(type).cast(x, type);
 482 //    }
 483 
 484     /** Cast a wrapped value to the given type, which may be either a primitive or wrapper type.
 485      *  The given target type must be this wrapper's primitive or wrapper type.
 486      *  If this wrapper is OBJECT, the target type may also be an interface, perform no runtime check.
 487      *  Performs standard primitive conversions, including truncation and float conversions.
 488      *  The given type must be compatible with this wrapper.  That is, it must either


 571         switch (basicTypeChar) {
 572             case 'I': return Integer.valueOf(xn.intValue());
 573             case 'J': return Long.valueOf(xn.longValue());
 574             case 'F': return Float.valueOf(xn.floatValue());
 575             case 'D': return Double.valueOf(xn.doubleValue());
 576             case 'S': return Short.valueOf((short) xn.intValue());
 577             case 'B': return Byte.valueOf((byte) xn.intValue());
 578             case 'C': return Character.valueOf((char) xn.intValue());
 579             case 'Z': return Boolean.valueOf(boolValue(xn.byteValue()));
 580         }
 581         throw new InternalError("bad wrapper");
 582     }
 583 
 584     /** Wrap a value (an int or smaller value) in this wrapper's type.
 585      * Performs standard primitive conversions, including truncation and float conversions.
 586      * Produces an {@code Integer} for {@code OBJECT}, although the exact type
 587      * of the operand is not known.
 588      * Returns null for {@code VOID}.
 589      */
 590     public Object wrap(int x) {

 591         switch (basicTypeChar) {
 592             case 'L': return (Integer)x;
 593             case 'V': return null;
 594             case 'I': return Integer.valueOf(x);
 595             case 'J': return Long.valueOf(x);
 596             case 'F': return Float.valueOf(x);
 597             case 'D': return Double.valueOf(x);
 598             case 'S': return Short.valueOf((short) x);
 599             case 'B': return Byte.valueOf((byte) x);
 600             case 'C': return Character.valueOf((char) x);
 601             case 'Z': return Boolean.valueOf(boolValue((byte) x));
 602         }
 603         throw new InternalError("bad wrapper");
 604     }
 605 
 606     private static Number numberValue(Object x) {
 607         if (x instanceof Number)     return (Number)x;
 608         if (x instanceof Character)  return (int)(Character)x;
 609         if (x instanceof Boolean)    return (Boolean)x ? 1 : 0;
 610         // Remaining allowed case of void:  Must be a null reference.
 611         return (Number)x;
 612     }


< prev index next >