jdk/src/java.base/share/classes/sun/invoke/util/Wrapper.java
author redestad
Tue, 11 Apr 2017 18:57:46 +0200
changeset 44591 b9bf065070fe
parent 44590 15a77e5b7612
permissions -rw-r--r--
8178387: Reduce memory churn when creating java.lang.invoke entities Reviewed-by: psandoz, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13423
diff changeset
     2
 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     4
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    10
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    15
 * accompanied this code).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    16
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    23
 * questions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    24
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    25
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 7668
diff changeset
    26
package sun.invoke.util;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    28
public enum Wrapper {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    29
    //        wrapperType      simple     primitiveType  simple     char  emptyArray     format
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    30
    BOOLEAN(  Boolean.class,   "Boolean", boolean.class, "boolean", 'Z', new boolean[0], Format.unsigned( 1)),
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
    31
    // These must be in the order defined for widening primitive conversions in JLS 5.1.2
33650
c99ead4a890b 8141678: sun.invoke.util.Wrapper eagerly initializes all integral type caches
redestad
parents: 32649
diff changeset
    32
    // Avoid boxing integral types here to defer initialization of internal caches
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    33
    BYTE   (     Byte.class,      "Byte",    byte.class,    "byte", 'B', new    byte[0], Format.signed(   8)),
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    34
    SHORT  (    Short.class,     "Short",   short.class,   "short", 'S', new   short[0], Format.signed(  16)),
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    35
    CHAR   (Character.class, "Character",    char.class,    "char", 'C', new    char[0], Format.unsigned(16)),
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    36
    INT    (  Integer.class,   "Integer",     int.class,     "int", 'I', new     int[0], Format.signed(  32)),
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    37
    LONG   (     Long.class,      "Long",    long.class,    "long", 'J', new    long[0], Format.signed(  64)),
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    38
    FLOAT  (    Float.class,     "Float",   float.class,   "float", 'F', new   float[0], Format.floating(32)),
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    39
    DOUBLE (   Double.class,    "Double",  double.class,  "double", 'D', new  double[0], Format.floating(64)),
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    40
    OBJECT (   Object.class,    "Object",  Object.class,  "Object", 'L', new  Object[0], Format.other(    1)),
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
    41
    // VOID must be the last type, since it is "assignable" from any other type:
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    42
    VOID   (     Void.class,      "Void",    void.class,    "void", 'V',           null, Format.other(    0)),
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    43
    ;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    44
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 33842
diff changeset
    45
    public static final int COUNT = 10;
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 33842
diff changeset
    46
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    47
    private final Class<?> wrapperType;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
    private final Class<?> primitiveType;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    49
    private final char     basicTypeChar;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    50
    private final Object   emptyArray;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    51
    private final int      format;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
    52
    private final String   wrapperSimpleName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
    53
    private final String   primitiveSimpleName;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    54
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    55
    private Wrapper(Class<?> wtype, String wtypeName, Class<?> ptype, String ptypeName, char tchar, Object emptyArray, int format) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    56
        this.wrapperType = wtype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    57
        this.primitiveType = ptype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    58
        this.basicTypeChar = tchar;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    59
        this.emptyArray = emptyArray;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    60
        this.format = format;
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    61
        this.wrapperSimpleName = wtypeName;
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 40256
diff changeset
    62
        this.primitiveSimpleName = ptypeName;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    63
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    64
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    65
    /** For debugging, give the details of this wrapper. */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    66
    public String detailString() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
    67
        return wrapperSimpleName+
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    68
                java.util.Arrays.asList(wrapperType, primitiveType,
33842
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
    69
                basicTypeChar, zero(),
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    70
                "0x"+Integer.toHexString(format));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    71
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    72
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 27196
diff changeset
    73
    private abstract static class Format {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    74
        static final int SLOT_SHIFT = 0, SIZE_SHIFT = 2, KIND_SHIFT = 12;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    75
        static final int
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    76
                SIGNED   = (-1) << KIND_SHIFT,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    77
                UNSIGNED = 0    << KIND_SHIFT,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    78
                FLOATING = 1    << KIND_SHIFT;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    79
        static final int
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    80
                SLOT_MASK = ((1<<(SIZE_SHIFT-SLOT_SHIFT))-1),
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    81
                SIZE_MASK = ((1<<(KIND_SHIFT-SIZE_SHIFT))-1);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    82
        static int format(int kind, int size, int slots) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    83
            assert(((kind >> KIND_SHIFT) << KIND_SHIFT) == kind);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    84
            assert((size & (size-1)) == 0); // power of two
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    85
            assert((kind == SIGNED)   ? (size > 0) :
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    86
                   (kind == UNSIGNED) ? (size > 0) :
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    87
                   (kind == FLOATING) ? (size == 32 || size == 64)  :
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    88
                   false);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    89
            assert((slots == 2) ? (size == 64) :
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    90
                   (slots == 1) ? (size <= 32) :
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    91
                   false);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    92
            return kind | (size << SIZE_SHIFT) | (slots << SLOT_SHIFT);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    93
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
    94
        static final int
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    95
                INT      = SIGNED   | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
    96
                SHORT    = SIGNED   | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    97
                BOOLEAN  = UNSIGNED | (1  << SIZE_SHIFT) | (1 << SLOT_SHIFT),
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
    98
                CHAR     = UNSIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    99
                FLOAT    = FLOATING | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   100
                VOID     = UNSIGNED | (0  << SIZE_SHIFT) | (0 << SLOT_SHIFT),
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   101
                NUM_MASK = (-1) << SIZE_SHIFT;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   102
        static int signed(int size)   { return format(SIGNED,   size, (size > 32 ? 2 : 1)); }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   103
        static int unsigned(int size) { return format(UNSIGNED, size, (size > 32 ? 2 : 1)); }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   104
        static int floating(int size) { return format(FLOATING, size, (size > 32 ? 2 : 1)); }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   105
        static int other(int slots)   { return slots << SLOT_SHIFT; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   106
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   107
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   108
    /// format queries:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   109
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   110
    /** How many bits are in the wrapped value?  Returns 0 for OBJECT or VOID. */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   111
    public int     bitWidth()      { return (format >> Format.SIZE_SHIFT) & Format.SIZE_MASK; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   112
    /** How many JVM stack slots occupied by the wrapped value?  Returns 0 for VOID. */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   113
    public int     stackSlots()    { return (format >> Format.SLOT_SHIFT) & Format.SLOT_MASK; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   114
    /** Does the wrapped value occupy a single JVM stack slot? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   115
    public boolean isSingleWord()  { return (format & (1 << Format.SLOT_SHIFT)) != 0; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   116
    /** Does the wrapped value occupy two JVM stack slots? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   117
    public boolean isDoubleWord()  { return (format & (2 << Format.SLOT_SHIFT)) != 0; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   118
    /** Is the wrapped type numeric (not void or object)? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   119
    public boolean isNumeric()     { return (format & Format.NUM_MASK) != 0; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   120
    /** Is the wrapped type a primitive other than float, double, or void? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   121
    public boolean isIntegral()    { return isNumeric() && format < Format.FLOAT; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   122
    /** Is the wrapped type one of int, boolean, byte, char, or short? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   123
    public boolean isSubwordOrInt() { return isIntegral() && isSingleWord(); }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   124
    /* Is the wrapped value a signed integral type (one of byte, short, int, or long)? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   125
    public boolean isSigned()      { return format < Format.VOID; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   126
    /* Is the wrapped value an unsigned integral type (one of boolean or char)? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   127
    public boolean isUnsigned()    { return format >= Format.BOOLEAN && format < Format.FLOAT; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   128
    /** Is the wrapped type either float or double? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   129
    public boolean isFloating()    { return format >= Format.FLOAT; }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   130
    /** Is the wrapped type either void or a reference? */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   131
    public boolean isOther()       { return (format & ~Format.SLOT_MASK) == 0; }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   132
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   133
    /** Does the JLS 5.1.2 allow a variable of this wrapper's
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   134
     *  primitive type to be assigned from a value of the given wrapper's primitive type?
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   135
     *  Cases:
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   136
     *  <ul>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   137
     *  <li>unboxing followed by widening primitive conversion
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   138
     *  <li>any type converted to {@code void} (i.e., dropping a method call's value)
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   139
     *  <li>boxing conversion followed by widening reference conversion to {@code Object}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   140
     *  </ul>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9730
diff changeset
   141
     *  These are the cases allowed by MethodHandle.asType.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   142
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   143
    public boolean isConvertibleFrom(Wrapper source) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   144
        if (this == source)  return true;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   145
        if (this.compareTo(source) < 0) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   146
            // At best, this is a narrowing conversion.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   147
            return false;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   148
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   149
        // All conversions are allowed in the enum order between floats and signed ints.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   150
        // First detect non-signed non-float types (boolean, char, Object, void).
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   151
        boolean floatOrSigned = (((this.format & source.format) & Format.SIGNED) != 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   152
        if (!floatOrSigned) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   153
            if (this.isOther())  return true;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   154
            // can convert char to int or wider, but nothing else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   155
            if (source.format == Format.CHAR)  return true;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   156
            // no other conversions are classified as widening
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   157
            return false;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   158
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   159
        // All signed and float conversions in the enum order are widening.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   160
        assert(this.isFloating() || this.isSigned());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   161
        assert(source.isFloating() || source.isSigned());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   162
        return true;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   163
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   164
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 33842
diff changeset
   165
    static {
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 33842
diff changeset
   166
        assert(checkConvertibleFrom());
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 33842
diff changeset
   167
        assert(COUNT == Wrapper.values().length);
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 33842
diff changeset
   168
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   169
    private static boolean checkConvertibleFrom() {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   170
        // Check the matrix for correct classification of widening conversions.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   171
        for (Wrapper w : values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   172
            assert(w.isConvertibleFrom(w));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   173
            assert(VOID.isConvertibleFrom(w));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   174
            if (w != VOID) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   175
                assert(OBJECT.isConvertibleFrom(w));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   176
                assert(!w.isConvertibleFrom(VOID));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   177
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   178
            // check relations with unsigned integral types:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   179
            if (w != CHAR) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   180
                assert(!CHAR.isConvertibleFrom(w));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   181
                if (!w.isConvertibleFrom(INT))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   182
                    assert(!w.isConvertibleFrom(CHAR));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   183
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   184
            if (w != BOOLEAN) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   185
                assert(!BOOLEAN.isConvertibleFrom(w));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   186
                if (w != VOID && w != OBJECT)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   187
                    assert(!w.isConvertibleFrom(BOOLEAN));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   188
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   189
            // check relations with signed integral types:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   190
            if (w.isSigned()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   191
                for (Wrapper x : values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   192
                    if (w == x)  continue;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   193
                    if (x.isFloating())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   194
                        assert(!w.isConvertibleFrom(x));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   195
                    else if (x.isSigned()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   196
                        if (w.compareTo(x) < 0)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   197
                            assert(!w.isConvertibleFrom(x));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   198
                        else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   199
                            assert(w.isConvertibleFrom(x));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   200
                    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   201
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   202
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   203
            // check relations with floating types:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   204
            if (w.isFloating()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   205
                for (Wrapper x : values()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   206
                    if (w == x)  continue;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   207
                    if (x.isSigned())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   208
                        assert(w.isConvertibleFrom(x));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   209
                    else if (x.isFloating()) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   210
                        if (w.compareTo(x) < 0)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   211
                            assert(!w.isConvertibleFrom(x));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   212
                        else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   213
                            assert(w.isConvertibleFrom(x));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   214
                    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   215
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   216
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   217
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   218
        return true;  // i.e., assert(true)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   219
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   220
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   221
    /** Produce a zero value for the given wrapper type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   222
     *  This will be a numeric zero for a number or character,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   223
     *  false for a boolean, and null for a reference or void.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   224
     *  The common thread is that this is what is contained
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   225
     *  in a default-initialized variable of the given primitive
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   226
     *  type.  (For void, it is what a reflective method returns
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   227
     *  instead of no value at all.)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   228
     */
33842
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   229
    public Object zero() {
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   230
        switch (this) {
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   231
            case BOOLEAN:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   232
                return Boolean.FALSE;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   233
            case INT:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   234
                return (Integer)0;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   235
            case BYTE:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   236
                return (Byte)(byte)0;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   237
            case CHAR:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   238
                return (Character)(char)0;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   239
            case SHORT:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   240
                return (Short)(short)0;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   241
            case LONG:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   242
                return (Long)(long)0;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   243
            case FLOAT:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   244
                return FLOAT_ZERO;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   245
            case DOUBLE:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   246
                return DOUBLE_ZERO;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   247
            case VOID:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   248
            case OBJECT:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   249
            default:
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   250
                return null;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   251
        }
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   252
    }
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   253
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   254
    private static final Object DOUBLE_ZERO = (Double)(double)0;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   255
    private static final Object FLOAT_ZERO = (Float)(float)0;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   256
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   257
    /** Produce a zero value for the given wrapper type T.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   258
     *  The optional argument must a type compatible with this wrapper.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   259
     *  Equivalent to {@code this.cast(this.zero(), type)}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   260
     */
33842
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   261
    public <T> T zero(Class<T> type) { return convert(zero(), type); }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   262
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   263
    /** Return the wrapper that wraps values of the given type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   264
     *  The type may be {@code Object}, meaning the {@code OBJECT} wrapper.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   265
     *  Otherwise, the type must be a primitive.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   266
     *  @throws IllegalArgumentException for unexpected types
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   267
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   268
    public static Wrapper forPrimitiveType(Class<?> type) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   269
        Wrapper w = findPrimitiveType(type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   270
        if (w != null)  return w;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   271
        if (type.isPrimitive())
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   272
            throw new InternalError(); // redo hash function
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   273
        throw newIllegalArgumentException("not primitive: "+type);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   274
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   275
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   276
    static Wrapper findPrimitiveType(Class<?> type) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   277
        Wrapper w = FROM_PRIM[hashPrim(type)];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   278
        if (w != null && w.primitiveType == type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   279
            return w;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   280
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   281
        return null;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   282
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   283
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   284
    /** Return the wrapper that wraps values into the given wrapper type.
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   285
     *  If it is {@code Object}, return {@code OBJECT}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   286
     *  Otherwise, it must be a wrapper type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   287
     *  The type must not be a primitive type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   288
     *  @throws IllegalArgumentException for unexpected types
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   289
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   290
    public static Wrapper forWrapperType(Class<?> type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   291
        Wrapper w = findWrapperType(type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   292
        if (w != null)  return w;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   293
        for (Wrapper x : values())
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   294
            if (x.wrapperType == type)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   295
                throw new InternalError(); // redo hash function
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   296
        throw newIllegalArgumentException("not wrapper: "+type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   297
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   298
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   299
    static Wrapper findWrapperType(Class<?> type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   300
        Wrapper w = FROM_WRAP[hashWrap(type)];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   301
        if (w != null && w.wrapperType == type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   302
            return w;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   303
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   304
        return null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   305
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   306
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   307
    /** Return the wrapper that corresponds to the given bytecode
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   308
     *  signature character.  Return {@code OBJECT} for the character 'L'.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   309
     *  @throws IllegalArgumentException for any non-signature character or {@code '['}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   310
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   311
    public static Wrapper forBasicType(char type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   312
        Wrapper w = FROM_CHAR[hashChar(type)];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   313
        if (w != null && w.basicTypeChar == type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   314
            return w;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   315
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   316
        for (Wrapper x : values())
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   317
            if (w.basicTypeChar == type)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   318
                throw new InternalError(); // redo hash function
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   319
        throw newIllegalArgumentException("not basic type char: "+type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   320
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   321
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   322
    /** Return the wrapper for the given type, if it is
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   323
     *  a primitive type, else return {@code OBJECT}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   324
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   325
    public static Wrapper forBasicType(Class<?> type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   326
        if (type.isPrimitive())
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   327
            return forPrimitiveType(type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   328
        return OBJECT;  // any reference, including wrappers or arrays
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   329
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   330
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   331
    // Note on perfect hashes:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   332
    //   for signature chars c, do (c + (c >> 1)) % 16
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   333
    //   for primitive type names n, do (n[0] + n[2]) % 16
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   334
    // The type name hash works for both primitive and wrapper names.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   335
    // You can add "java/lang/Object" to the primitive names.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   336
    // But you add the wrapper name Object, use (n[2] + (3*n[1])) % 16.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   337
    private static final Wrapper[] FROM_PRIM = new Wrapper[16];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   338
    private static final Wrapper[] FROM_WRAP = new Wrapper[16];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   339
    private static final Wrapper[] FROM_CHAR = new Wrapper[16];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   340
    private static int hashPrim(Class<?> x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   341
        String xn = x.getName();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   342
        if (xn.length() < 3)  return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   343
        return (xn.charAt(0) + xn.charAt(2)) % 16;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   344
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   345
    private static int hashWrap(Class<?> x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   346
        String xn = x.getName();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   347
        final int offset = 10; assert(offset == "java.lang.".length());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   348
        if (xn.length() < offset+3)  return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   349
        return (3*xn.charAt(offset+1) + xn.charAt(offset+2)) % 16;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   350
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   351
    private static int hashChar(char x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   352
        return (x + (x >> 1)) % 16;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   353
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   354
    static {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   355
        for (Wrapper w : values()) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   356
            int pi = hashPrim(w.primitiveType);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   357
            int wi = hashWrap(w.wrapperType);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   358
            int ci = hashChar(w.basicTypeChar);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   359
            assert(FROM_PRIM[pi] == null);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   360
            assert(FROM_WRAP[wi] == null);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   361
            assert(FROM_CHAR[ci] == null);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   362
            FROM_PRIM[pi] = w;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   363
            FROM_WRAP[wi] = w;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   364
            FROM_CHAR[ci] = w;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   365
        }
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 7668
diff changeset
   366
        //assert(jdk.sun.invoke.util.WrapperTest.test(false));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   367
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   368
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   369
    /** What is the primitive type wrapped by this wrapper? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   370
    public Class<?> primitiveType() { return primitiveType; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   371
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   372
    /** What is the wrapper type for this wrapper? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   373
    public Class<?> wrapperType() { return wrapperType; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   374
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   375
    /** What is the wrapper type for this wrapper?
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   376
     * Otherwise, the example type must be the wrapper type,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   377
     * or the corresponding primitive type.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   378
     * (For {@code OBJECT}, the example type can be any non-primitive,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   379
     * and is normalized to {@code Object.class}.)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   380
     * The resulting class type has the same type parameter.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   381
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   382
    public <T> Class<T> wrapperType(Class<T> exampleType) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   383
        if (exampleType == wrapperType) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   384
            return exampleType;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   385
        } else if (exampleType == primitiveType ||
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   386
                   wrapperType == Object.class ||
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   387
                   exampleType.isInterface()) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   388
            return forceType(wrapperType, exampleType);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   389
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   390
        throw newClassCastException(exampleType, primitiveType);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   391
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   392
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   393
    private static ClassCastException newClassCastException(Class<?> actual, Class<?> expected) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   394
        return new ClassCastException(actual + " is not compatible with " + expected);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   395
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   396
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   397
    /** If {@code type} is a primitive type, return the corresponding
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   398
     *  wrapper type, else return {@code type} unchanged.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   399
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   400
    public static <T> Class<T> asWrapperType(Class<T> type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   401
        if (type.isPrimitive()) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   402
            return forPrimitiveType(type).wrapperType(type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   403
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   404
        return type;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   405
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   406
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   407
    /** If {@code type} is a wrapper type, return the corresponding
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   408
     *  primitive type, else return {@code type} unchanged.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   409
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   410
    public static <T> Class<T> asPrimitiveType(Class<T> type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   411
        Wrapper w = findWrapperType(type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   412
        if (w != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   413
            return forceType(w.primitiveType(), type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   414
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   415
        return type;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   416
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   417
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   418
    /** Query:  Is the given type a wrapper, such as {@code Integer} or {@code Void}? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   419
    public static boolean isWrapperType(Class<?> type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   420
        return findWrapperType(type) != null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   421
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   422
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   423
    /** Query:  Is the given type a primitive, such as {@code int} or {@code void}? */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   424
    public static boolean isPrimitiveType(Class<?> type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   425
        return type.isPrimitive();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   426
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   427
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   428
    /** What is the bytecode signature character for this type?
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   429
     *  All non-primitives, including array types, report as 'L', the signature character for references.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   430
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   431
    public static char basicTypeChar(Class<?> type) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   432
        if (!type.isPrimitive())
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   433
            return 'L';
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   434
        else
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   435
            return forPrimitiveType(type).basicTypeChar();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   436
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   437
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   438
    /** What is the bytecode signature character for this wrapper's
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   439
     *  primitive type?
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   440
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   441
    public char basicTypeChar() { return basicTypeChar; }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   442
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   443
    /** What is the simple name of the wrapper type?
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   444
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   445
    public String wrapperSimpleName() { return wrapperSimpleName; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   446
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   447
    /** What is the simple name of the primitive type?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   448
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   449
    public String primitiveSimpleName() { return primitiveSimpleName; }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   450
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   451
//    /** Wrap a value in the given type, which may be either a primitive or wrapper type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   452
//     *  Performs standard primitive conversions, including truncation and float conversions.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   453
//     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   454
//    public static <T> T wrap(Object x, Class<T> type) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   455
//        return Wrapper.valueOf(type).cast(x, type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   456
//    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   457
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   458
    /** Cast a wrapped value to the given type, which may be either a primitive or wrapper type.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   459
     *  The given target type must be this wrapper's primitive or wrapper type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   460
     *  If this wrapper is OBJECT, the target type may also be an interface, perform no runtime check.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   461
     *  Performs standard primitive conversions, including truncation and float conversions.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   462
     *  The given type must be compatible with this wrapper.  That is, it must either
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   463
     *  be the wrapper type (or a subtype, in the case of {@code OBJECT}) or else
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   464
     *  it must be the wrapper's primitive type.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   465
     *  Primitive conversions are only performed if the given type is itself a primitive.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   466
     *  @throws ClassCastException if the given type is not compatible with this wrapper
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   467
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   468
    public <T> T cast(Object x, Class<T> type) {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   469
        return convert(x, type, true);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   470
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   471
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   472
    /** Convert a wrapped value to the given type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   473
     *  The given target type must be this wrapper's primitive or wrapper type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   474
     *  This is equivalent to {@link #cast}, except that it refuses to perform
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   475
     *  narrowing primitive conversions.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   476
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   477
    public <T> T convert(Object x, Class<T> type) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   478
        return convert(x, type, false);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   479
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   480
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   481
    private <T> T convert(Object x, Class<T> type, boolean isCast) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   482
        if (this == OBJECT) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   483
            // If the target wrapper is OBJECT, just do a reference cast.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   484
            // If the target type is an interface, perform no runtime check.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   485
            // (This loophole is safe, and is allowed by the JVM verifier.)
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   486
            // If the target type is a primitive, change it to a wrapper.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   487
            assert(!type.isPrimitive());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   488
            if (!type.isInterface())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   489
                type.cast(x);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   490
            @SuppressWarnings("unchecked")
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   491
            T result = (T) x;  // unchecked warning is expected here
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   492
            return result;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   493
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   494
        Class<T> wtype = wrapperType(type);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   495
        if (wtype.isInstance(x)) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   496
            return wtype.cast(x);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   497
        }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   498
        if (!isCast) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   499
            Class<?> sourceType = x.getClass();  // throw NPE if x is null
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   500
            Wrapper source = findWrapperType(sourceType);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   501
            if (source == null || !this.isConvertibleFrom(source)) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   502
                throw newClassCastException(wtype, sourceType);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   503
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   504
        } else if (x == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   505
            @SuppressWarnings("unchecked")
33842
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents: 33650
diff changeset
   506
            T z = (T) zero();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   507
            return z;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   508
        }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   509
        @SuppressWarnings("unchecked")
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   510
        T result = (T) wrap(x);  // unchecked warning is expected here
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   511
        assert (result == null ? Void.class : result.getClass()) == wtype;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 5506
diff changeset
   512
        return result;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   513
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   514
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   515
    /** Cast a reference type to another reference type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   516
     * If the target type is an interface, perform no runtime check.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   517
     * (This loophole is safe, and is allowed by the JVM verifier.)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   518
     * If the target type is a primitive, change it to a wrapper.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   519
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   520
    static <T> Class<T> forceType(Class<?> type, Class<T> exampleType) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   521
        assert(type == exampleType ||
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   522
               type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) ||
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   523
               exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) ||
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   524
               type == Object.class && !exampleType.isPrimitive());
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   525
        @SuppressWarnings("unchecked")
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   526
        Class<T> result = (Class<T>) type;  // unchecked warning is expected here
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   527
        return result;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   528
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   529
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   530
    /** Wrap a value in this wrapper's type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   531
     * Performs standard primitive conversions, including truncation and float conversions.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   532
     * Performs returns the unchanged reference for {@code OBJECT}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   533
     * Returns null for {@code VOID}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   534
     * Returns a zero value for a null input.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   535
     * @throws ClassCastException if this wrapper is numeric and the operand
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   536
     *                            is not a number, character, boolean, or null
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   537
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   538
    public Object wrap(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   539
        // do non-numeric wrappers first
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   540
        switch (basicTypeChar) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   541
            case 'L': return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   542
            case 'V': return null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   543
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   544
        Number xn = numberValue(x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   545
        switch (basicTypeChar) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   546
            case 'I': return Integer.valueOf(xn.intValue());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   547
            case 'J': return Long.valueOf(xn.longValue());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   548
            case 'F': return Float.valueOf(xn.floatValue());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   549
            case 'D': return Double.valueOf(xn.doubleValue());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   550
            case 'S': return Short.valueOf((short) xn.intValue());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   551
            case 'B': return Byte.valueOf((byte) xn.intValue());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   552
            case 'C': return Character.valueOf((char) xn.intValue());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   553
            case 'Z': return Boolean.valueOf(boolValue(xn.byteValue()));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   554
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   555
        throw new InternalError("bad wrapper");
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   556
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   557
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   558
    /** Wrap a value (an int or smaller value) in this wrapper's type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   559
     * Performs standard primitive conversions, including truncation and float conversions.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   560
     * Produces an {@code Integer} for {@code OBJECT}, although the exact type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   561
     * of the operand is not known.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   562
     * Returns null for {@code VOID}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   563
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   564
    public Object wrap(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   565
        if (basicTypeChar == 'L')  return (Integer)x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   566
        switch (basicTypeChar) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   567
            case 'L': throw newIllegalArgumentException("cannot wrap to object type");
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   568
            case 'V': return null;
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 9752
diff changeset
   569
            case 'I': return Integer.valueOf(x);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   570
            case 'J': return Long.valueOf(x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   571
            case 'F': return Float.valueOf(x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   572
            case 'D': return Double.valueOf(x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   573
            case 'S': return Short.valueOf((short) x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   574
            case 'B': return Byte.valueOf((byte) x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   575
            case 'C': return Character.valueOf((char) x);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   576
            case 'Z': return Boolean.valueOf(boolValue((byte) x));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   577
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   578
        throw new InternalError("bad wrapper");
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   579
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   580
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   581
    private static Number numberValue(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   582
        if (x instanceof Number)     return (Number)x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   583
        if (x instanceof Character)  return (int)(Character)x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   584
        if (x instanceof Boolean)    return (Boolean)x ? 1 : 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   585
        // Remaining allowed case of void:  Must be a null reference.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   586
        return (Number)x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   587
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   588
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   589
    // Parameter type of boolValue must be byte, because
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   590
    // MethodHandles.explicitCastArguments defines boolean
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   591
    // conversion as first converting to byte.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   592
    private static boolean boolValue(byte bits) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   593
        bits &= 1;  // simple 31-bit zero extension
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   594
        return (bits != 0);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   595
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   596
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   597
    private static RuntimeException newIllegalArgumentException(String message, Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   598
        return newIllegalArgumentException(message + x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   599
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   600
    private static RuntimeException newIllegalArgumentException(String message) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   601
        return new IllegalArgumentException(message);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   602
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   603
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   604
    // primitive array support
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   605
    public Object makeArray(int len) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   606
        return java.lang.reflect.Array.newInstance(primitiveType, len);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   607
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   608
    public Class<?> arrayType() {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   609
        return emptyArray.getClass();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   610
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   611
    public void copyArrayUnboxing(Object[] values, int vpos, Object a, int apos, int length) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   612
        if (a.getClass() != arrayType())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   613
            arrayType().cast(a);  // throw NPE or CCE if bad type
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   614
        for (int i = 0; i < length; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   615
            Object value = values[i+vpos];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   616
            value = convert(value, primitiveType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   617
            java.lang.reflect.Array.set(a, i+apos, value);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   618
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   619
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   620
    public void copyArrayBoxing(Object a, int apos, Object[] values, int vpos, int length) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   621
        if (a.getClass() != arrayType())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   622
            arrayType().cast(a);  // throw NPE or CCE if bad type
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   623
        for (int i = 0; i < length; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   624
            Object value = java.lang.reflect.Array.get(a, i+apos);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   625
            //Already done: value = convert(value, primitiveType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   626
            assert(value.getClass() == wrapperType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   627
            values[i+vpos] = value;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   628
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   629
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   630
}