jdk/src/share/classes/sun/invoke/util/ValueConversions.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 9780 6fc3b49cfee4
child 11534 9949ffb8eb3a
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
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
/*
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
     2
 * Copyright (c) 2008, 2011, 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: 8821
diff changeset
    26
package sun.invoke.util;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    28
import java.lang.invoke.MethodHandle;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    29
import java.lang.invoke.MethodHandles;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    30
import java.lang.invoke.MethodHandles.Lookup;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    31
import java.lang.invoke.MethodType;
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    32
import java.security.AccessController;
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    33
import java.security.PrivilegedAction;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
    34
import java.util.ArrayList;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
    35
import java.util.Arrays;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    36
import java.util.Collections;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    37
import java.util.EnumMap;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
    38
import java.util.List;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    39
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    40
public class ValueConversions {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    41
    private static final Class<?> THIS_CLASS = ValueConversions.class;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    42
    // Do not adjust this except for special platforms:
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    43
    private static final int MAX_ARITY;
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    44
    static {
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    45
        final Object[] values = { 255 };
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    46
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    47
                public Void run() {
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    48
                    values[0] = Integer.getInteger(THIS_CLASS.getName()+".MAX_ARITY", 255);
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    49
                    return null;
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    50
                }
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    51
            });
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    52
        MAX_ARITY = (Integer) values[0];
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    53
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    54
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    55
    private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    56
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    57
    private static EnumMap<Wrapper, MethodHandle>[] newWrapperCaches(int n) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
    58
        @SuppressWarnings("unchecked")
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    59
        EnumMap<Wrapper, MethodHandle>[] caches
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    60
                = (EnumMap<Wrapper, MethodHandle>[]) new EnumMap[n];  // unchecked warning expected here
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    61
        for (int i = 0; i < n; i++)
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    62
            caches[i] = new EnumMap<>(Wrapper.class);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    63
        return caches;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    64
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    65
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    66
    /// Converting references to values.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    67
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    68
    // There are several levels of this unboxing conversions:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    69
    //   no conversions:  exactly Integer.valueOf, etc.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    70
    //   implicit conversions sanctioned by JLS 5.1.2, etc.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    71
    //   explicit conversions as allowed by explicitCastArguments
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    72
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    73
    static int unboxInteger(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    74
        if (x instanceof Integer)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    75
            return ((Integer) x).intValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    76
        return primitiveConversion(Wrapper.INT, x, cast).intValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    77
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    78
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    79
    static byte unboxByte(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    80
        if (x instanceof Byte)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    81
            return ((Byte) x).byteValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    82
        return primitiveConversion(Wrapper.BYTE, x, cast).byteValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    83
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    84
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    85
    static short unboxShort(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    86
        if (x instanceof Short)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    87
            return ((Short) x).shortValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    88
        return primitiveConversion(Wrapper.SHORT, x, cast).shortValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    89
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    90
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    91
    static boolean unboxBoolean(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    92
        if (x instanceof Boolean)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    93
            return ((Boolean) x).booleanValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    94
        return (primitiveConversion(Wrapper.BOOLEAN, x, cast).intValue() & 1) != 0;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    95
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    96
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    97
    static char unboxCharacter(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    98
        if (x instanceof Character)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    99
            return ((Character) x).charValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   100
        return (char) primitiveConversion(Wrapper.CHAR, x, cast).intValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   101
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   102
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   103
    static long unboxLong(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   104
        if (x instanceof Long)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   105
            return ((Long) x).longValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   106
        return primitiveConversion(Wrapper.LONG, x, cast).longValue();
2707
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
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   109
    static float unboxFloat(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   110
        if (x instanceof Float)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   111
            return ((Float) x).floatValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   112
        return primitiveConversion(Wrapper.FLOAT, x, cast).floatValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   113
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   114
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   115
    static double unboxDouble(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   116
        if (x instanceof Double)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   117
            return ((Double) x).doubleValue();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   118
        return primitiveConversion(Wrapper.DOUBLE, x, cast).doubleValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   119
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   120
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   121
    /// Converting references to "raw" values.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   122
    /// A raw primitive value is always an int or long.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   123
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   124
    static int unboxByteRaw(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   125
        return unboxByte(x, cast);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   126
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   127
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   128
    static int unboxShortRaw(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   129
        return unboxShort(x, cast);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   130
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   131
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   132
    static int unboxBooleanRaw(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   133
        return unboxBoolean(x, cast) ? 1 : 0;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   134
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   135
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   136
    static int unboxCharacterRaw(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   137
        return unboxCharacter(x, cast);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   138
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   139
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   140
    static int unboxFloatRaw(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   141
        return Float.floatToIntBits(unboxFloat(x, cast));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   142
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   143
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   144
    static long unboxDoubleRaw(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   145
        return Double.doubleToRawLongBits(unboxDouble(x, cast));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   146
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   147
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   148
    private static MethodType unboxType(Wrapper wrap, boolean raw) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   149
        return MethodType.methodType(rawWrapper(wrap, raw).primitiveType(), Object.class, boolean.class);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   150
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   151
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   152
    private static final EnumMap<Wrapper, MethodHandle>[]
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   153
            UNBOX_CONVERSIONS = newWrapperCaches(4);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   154
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   155
    private static MethodHandle unbox(Wrapper wrap, boolean raw, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   156
        EnumMap<Wrapper, MethodHandle> cache = UNBOX_CONVERSIONS[(cast?1:0)+(raw?2:0)];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   157
        MethodHandle mh = cache.get(wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   158
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   159
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   160
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   161
        // slow path
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   162
        switch (wrap) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   163
            case OBJECT:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   164
                mh = IDENTITY; break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   165
            case VOID:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   166
                mh = raw ? ALWAYS_ZERO : IGNORE; break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   167
            case INT: case LONG:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   168
                // these guys don't need separate raw channels
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   169
                if (raw)  mh = unbox(wrap, false, cast);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   170
                break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   171
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   172
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   173
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   174
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   175
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   176
        // look up the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   177
        String name = "unbox" + wrap.simpleName() + (raw ? "Raw" : "");
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   178
        MethodType type = unboxType(wrap, raw);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   179
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   180
            mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   181
        } catch (ReflectiveOperationException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   182
            mh = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   183
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   184
        if (mh != null) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   185
            mh = MethodHandles.insertArguments(mh, 1, cast);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   186
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   187
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   188
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   189
        throw new IllegalArgumentException("cannot find unbox adapter for " + wrap
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   190
                + (cast ? " (cast)" : "") + (raw ? " (raw)" : ""));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   191
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   192
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   193
    public static MethodHandle unboxCast(Wrapper type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   194
        return unbox(type, false, true);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   195
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   196
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   197
    public static MethodHandle unboxRaw(Wrapper type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   198
        return unbox(type, true, false);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   199
    }
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
    public static MethodHandle unbox(Class<?> type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   202
        return unbox(Wrapper.forPrimitiveType(type), false, false);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   203
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   204
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   205
    public static MethodHandle unboxCast(Class<?> type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   206
        return unbox(Wrapper.forPrimitiveType(type), false, true);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   207
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   208
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   209
    public static MethodHandle unboxRaw(Class<?> type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   210
        return unbox(Wrapper.forPrimitiveType(type), true, false);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   211
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   212
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   213
    static private final Integer ZERO_INT = 0, ONE_INT = 1;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   214
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   215
    /// Primitive conversions
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   216
    public static Number primitiveConversion(Wrapper wrap, Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   217
        // Maybe merge this code with Wrapper.convert/cast.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   218
        Number res = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   219
        if (x == null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   220
            if (!cast)  return null;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   221
            return ZERO_INT;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   222
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   223
        if (x instanceof Number) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   224
            res = (Number) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   225
        } else if (x instanceof Boolean) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   226
            res = ((boolean)x ? ONE_INT : ZERO_INT);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   227
        } else if (x instanceof Character) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   228
            res = (int)(char)x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   229
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   230
            // this will fail with the required ClassCastException:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   231
            res = (Number) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   232
        }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   233
        Wrapper xwrap = Wrapper.findWrapperType(x.getClass());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   234
        if (xwrap == null || !cast && !wrap.isConvertibleFrom(xwrap))
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   235
            // this will fail with the required ClassCastException:
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   236
            return (Number) wrap.wrapperType().cast(x);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   237
        return res;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   238
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   239
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   240
    /// Converting primitives to references
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   241
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   242
    static Integer boxInteger(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   243
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   244
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   245
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   246
    static Byte boxByte(byte x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   247
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   248
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   249
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   250
    static Short boxShort(short x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   251
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   252
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   253
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   254
    static Boolean boxBoolean(boolean x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   255
        return x;
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
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   258
    static Character boxCharacter(char x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   259
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   260
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   261
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   262
    static Long boxLong(long x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   263
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   264
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   265
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   266
    static Float boxFloat(float x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   267
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   268
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   269
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   270
    static Double boxDouble(double x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   271
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   272
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   273
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   274
    /// Converting raw primitives to references
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   275
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   276
    static Byte boxByteRaw(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   277
        return boxByte((byte)x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   278
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   279
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   280
    static Short boxShortRaw(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   281
        return boxShort((short)x);
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
    static Boolean boxBooleanRaw(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   285
        return boxBoolean(x != 0);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   286
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   287
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   288
    static Character boxCharacterRaw(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   289
        return boxCharacter((char)x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   290
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   291
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   292
    static Float boxFloatRaw(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   293
        return boxFloat(Float.intBitsToFloat(x));
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   294
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   295
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   296
    static Double boxDoubleRaw(long x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   297
        return boxDouble(Double.longBitsToDouble(x));
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
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   300
    // a raw void value is (arbitrarily) a garbage int
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   301
    static Void boxVoidRaw(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   302
        return null;
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
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   305
    private static MethodType boxType(Wrapper wrap, boolean raw) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   306
        // be exact, since return casts are hard to compose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   307
        Class<?> boxType = wrap.wrapperType();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   308
        return MethodType.methodType(boxType, rawWrapper(wrap, raw).primitiveType());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   309
    }
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
    private static Wrapper rawWrapper(Wrapper wrap, boolean raw) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   312
        if (raw)  return wrap.isDoubleWord() ? Wrapper.LONG : Wrapper.INT;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   313
        return wrap;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   314
    }
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
    private static final EnumMap<Wrapper, MethodHandle>[]
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   317
            BOX_CONVERSIONS = newWrapperCaches(4);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   318
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   319
    private static MethodHandle box(Wrapper wrap, boolean exact, boolean raw) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   320
        EnumMap<Wrapper, MethodHandle> cache = BOX_CONVERSIONS[(exact?1:0)+(raw?2:0)];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   321
        MethodHandle mh = cache.get(wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   322
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   323
            return mh;
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
        // slow path
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   326
        switch (wrap) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   327
            case OBJECT:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   328
                mh = IDENTITY; break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   329
            case VOID:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   330
                if (!raw)  mh = ZERO_OBJECT;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   331
                break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   332
            case INT: case LONG:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   333
                // these guys don't need separate raw channels
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   334
                if (raw)  mh = box(wrap, exact, false);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   335
                break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   336
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   337
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   338
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   339
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   340
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   341
        // look up the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   342
        String name = "box" + wrap.simpleName() + (raw ? "Raw" : "");
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   343
        MethodType type = boxType(wrap, raw);
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   344
        if (exact) {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   345
            try {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   346
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7668
diff changeset
   347
            } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   348
                mh = null;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   349
            }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   350
        } else {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   351
            mh = box(wrap, !exact, raw).asType(type.erase());
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   352
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   353
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   354
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   355
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   356
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   357
        throw new IllegalArgumentException("cannot find box adapter for "
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   358
                + wrap + (exact ? " (exact)" : "") + (raw ? " (raw)" : ""));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   359
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   360
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   361
    public static MethodHandle box(Class<?> type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   362
        boolean exact = false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   363
        // e.g., boxShort(short)Short if exact,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   364
        // e.g., boxShort(short)Object if !exact
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   365
        return box(Wrapper.forPrimitiveType(type), exact, false);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   366
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   367
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   368
    public static MethodHandle boxRaw(Class<?> type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   369
        boolean exact = false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   370
        // e.g., boxShortRaw(int)Short if exact
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   371
        // e.g., boxShortRaw(int)Object if !exact
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   372
        return box(Wrapper.forPrimitiveType(type), exact, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   373
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   374
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   375
    public static MethodHandle box(Wrapper type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   376
        boolean exact = false;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   377
        return box(type, exact, false);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   378
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   379
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   380
    public static MethodHandle boxRaw(Wrapper type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   381
        boolean exact = false;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   382
        return box(type, exact, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   383
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   384
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   385
    /// Kludges for when raw values get accidentally boxed.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   386
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   387
    static int unboxRawInteger(Object x) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   388
        if (x instanceof Integer)
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   389
            return (int) x;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   390
        else
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   391
            return (int) unboxLong(x, false);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   392
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   393
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   394
    static Integer reboxRawInteger(Object x) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   395
        if (x instanceof Integer)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   396
            return (Integer) x;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   397
        else
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   398
            return (int) unboxLong(x, false);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   399
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   400
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   401
    static Byte reboxRawByte(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   402
        if (x instanceof Byte)  return (Byte) x;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   403
        return boxByteRaw(unboxRawInteger(x));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   404
    }
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
    static Short reboxRawShort(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   407
        if (x instanceof Short)  return (Short) x;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   408
        return boxShortRaw(unboxRawInteger(x));
2707
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
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   411
    static Boolean reboxRawBoolean(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   412
        if (x instanceof Boolean)  return (Boolean) x;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   413
        return boxBooleanRaw(unboxRawInteger(x));
2707
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
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   416
    static Character reboxRawCharacter(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   417
        if (x instanceof Character)  return (Character) x;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   418
        return boxCharacterRaw(unboxRawInteger(x));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   419
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   420
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   421
    static Float reboxRawFloat(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   422
        if (x instanceof Float)  return (Float) x;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   423
        return boxFloatRaw(unboxRawInteger(x));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   424
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   425
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   426
    static Long reboxRawLong(Object x) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   427
        return (Long) x;  //never a rebox
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   428
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   429
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   430
    static Double reboxRawDouble(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   431
        if (x instanceof Double)  return (Double) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   432
        return boxDoubleRaw(unboxLong(x, true));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   433
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   434
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   435
    private static MethodType reboxType(Wrapper wrap) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   436
        Class<?> boxType = wrap.wrapperType();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   437
        return MethodType.methodType(boxType, Object.class);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   438
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   439
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   440
    private static final EnumMap<Wrapper, MethodHandle>[]
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   441
            REBOX_CONVERSIONS = newWrapperCaches(1);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   442
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   443
    /**
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   444
     * Because we normalize primitive types to reduce the number of signatures,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   445
     * primitives are sometimes manipulated under an "erased" type,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   446
     * either int (for types other than long/double) or long (for all types).
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   447
     * When the erased primitive value is then boxed into an Integer or Long,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   448
     * the final boxed primitive is sometimes required.  This transformation
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   449
     * is called a "rebox".  It takes an Integer or Long and produces some
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   450
     * other boxed value, typed (inexactly) as an Object
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   451
     */
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   452
    public static MethodHandle rebox(Wrapper wrap) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   453
        EnumMap<Wrapper, MethodHandle> cache = REBOX_CONVERSIONS[0];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   454
        MethodHandle mh = cache.get(wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   455
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   456
            return mh;
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
        // slow path
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   459
        switch (wrap) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   460
            case OBJECT:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   461
                mh = IDENTITY; break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   462
            case VOID:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   463
                throw new IllegalArgumentException("cannot rebox a void");
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   464
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   465
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   466
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   467
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   468
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   469
        // look up the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   470
        String name = "reboxRaw" + wrap.simpleName();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   471
        MethodType type = reboxType(wrap);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   472
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   473
            mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   474
            mh = mh.asType(IDENTITY.type());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   475
        } catch (ReflectiveOperationException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   476
            mh = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   477
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   478
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   479
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   480
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   481
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   482
        throw new IllegalArgumentException("cannot find rebox adapter for " + wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   483
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   484
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   485
    public static MethodHandle rebox(Class<?> type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   486
        return rebox(Wrapper.forPrimitiveType(type));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   487
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   488
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   489
    /// Width-changing conversions between int and long.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   490
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   491
    static long widenInt(int x) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   492
        return (long) x;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   493
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   494
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   495
    static Long widenBoxedInt(Integer x) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   496
        return (long)(int)x;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   497
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   498
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   499
    static int narrowLong(long x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   500
        return (int) x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   501
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   502
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   503
    static Integer narrowBoxedLong(Long x) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   504
        return (int)(long) x;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   505
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   506
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   507
    /// Constant functions
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   508
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   509
    static void ignore(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   510
        // no value to return; this is an unbox of null
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   511
        return;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   512
    }
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
    static void empty() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   515
        return;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   516
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   517
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   518
    static Object zeroObject() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   519
        return null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   520
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   521
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   522
    static int zeroInteger() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   523
        return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   524
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   525
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   526
    static long zeroLong() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   527
        return 0;
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
    static float zeroFloat() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   531
        return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   532
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   533
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   534
    static double zeroDouble() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   535
        return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   536
    }
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
    private static final EnumMap<Wrapper, MethodHandle>[]
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   539
            CONSTANT_FUNCTIONS = newWrapperCaches(2);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   540
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   541
    public static MethodHandle zeroConstantFunction(Wrapper wrap) {
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   542
        EnumMap<Wrapper, MethodHandle> cache = CONSTANT_FUNCTIONS[0];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   543
        MethodHandle mh = cache.get(wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   544
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   545
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   546
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   547
        // slow path
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   548
        MethodType type = MethodType.methodType(wrap.primitiveType());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   549
        switch (wrap) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   550
            case VOID:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   551
                mh = EMPTY;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   552
                break;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   553
            case OBJECT:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   554
            case INT: case LONG: case FLOAT: case DOUBLE:
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   555
                try {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   556
                    mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.simpleName(), type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7668
diff changeset
   557
                } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   558
                    mh = null;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   559
                }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   560
                break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   561
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   562
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   563
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   564
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   565
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   566
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   567
        // use the raw method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   568
        Wrapper rawWrap = wrap.rawPrimitive();
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   569
        if (mh == null && rawWrap != wrap) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   570
            mh = MethodHandles.explicitCastArguments(zeroConstantFunction(rawWrap), type);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   571
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   572
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   573
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   574
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   575
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   576
        throw new IllegalArgumentException("cannot find zero constant for " + wrap);
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
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   579
    /// Converting references to references.
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
    /**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   582
     * Value-killing function.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   583
     * @param x an arbitrary reference value
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   584
     * @return a null
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   585
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   586
    static Object alwaysNull(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   587
        return null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   588
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   589
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   590
    /**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   591
     * Value-killing function.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   592
     * @param x an arbitrary reference value
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   593
     * @return a zero
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   594
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   595
    static int alwaysZero(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   596
        return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   597
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   598
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
     * Identity function.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   601
     * @param x an arbitrary reference value
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   602
     * @return the same value x
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   603
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   604
    static <T> T identity(T x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   605
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   606
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   607
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   608
    /**
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   609
     * Identity function on ints.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   610
     * @param x an arbitrary int value
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   611
     * @return the same value x
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   612
     */
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   613
    static int identity(int x) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   614
        return x;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   615
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   616
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   617
    static byte identity(byte x) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   618
        return x;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   619
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   620
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   621
    static short identity(short x) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   622
        return x;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   623
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   624
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   625
    static boolean identity(boolean x) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   626
        return x;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   627
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   628
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   629
    static char identity(char x) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   630
        return x;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   631
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   632
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   633
    /**
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   634
     * Identity function on longs.
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   635
     * @param x an arbitrary long value
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   636
     * @return the same value x
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   637
     */
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   638
    static long identity(long x) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   639
        return x;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   640
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   641
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   642
    static float identity(float x) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   643
        return x;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   644
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   645
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   646
    static double identity(double x) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   647
        return x;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   648
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   649
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   650
    /**
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   651
     * Identity function, with reference cast.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   652
     * @param t an arbitrary reference type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   653
     * @param x an arbitrary reference value
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   654
     * @return the same value x
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   655
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   656
    static <T,U> T castReference(Class<? extends T> t, U x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   657
        return t.cast(x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   658
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   659
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   660
    private static final MethodHandle IDENTITY, IDENTITY_I, IDENTITY_J, CAST_REFERENCE, ALWAYS_NULL, ALWAYS_ZERO, ZERO_OBJECT, IGNORE, EMPTY, NEW_ARRAY;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   661
    static {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   662
        try {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   663
            MethodType idType = MethodType.genericMethodType(1);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   664
            MethodType castType = idType.insertParameterTypes(0, Class.class);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   665
            MethodType alwaysZeroType = idType.changeReturnType(int.class);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   666
            MethodType ignoreType = idType.changeReturnType(void.class);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   667
            MethodType zeroObjectType = MethodType.genericMethodType(0);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   668
            IDENTITY = IMPL_LOOKUP.findStatic(THIS_CLASS, "identity", idType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   669
            IDENTITY_I = IMPL_LOOKUP.findStatic(THIS_CLASS, "identity", MethodType.methodType(int.class, int.class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   670
            IDENTITY_J = IMPL_LOOKUP.findStatic(THIS_CLASS, "identity", MethodType.methodType(long.class, long.class));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   671
            //CAST_REFERENCE = IMPL_LOOKUP.findVirtual(Class.class, "cast", idType);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   672
            CAST_REFERENCE = IMPL_LOOKUP.findStatic(THIS_CLASS, "castReference", castType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   673
            ALWAYS_NULL = IMPL_LOOKUP.findStatic(THIS_CLASS, "alwaysNull", idType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   674
            ALWAYS_ZERO = IMPL_LOOKUP.findStatic(THIS_CLASS, "alwaysZero", alwaysZeroType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   675
            ZERO_OBJECT = IMPL_LOOKUP.findStatic(THIS_CLASS, "zeroObject", zeroObjectType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   676
            IGNORE = IMPL_LOOKUP.findStatic(THIS_CLASS, "ignore", ignoreType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   677
            EMPTY = IMPL_LOOKUP.findStatic(THIS_CLASS, "empty", ignoreType.dropParameterTypes(0, 1));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   678
            NEW_ARRAY = IMPL_LOOKUP.findStatic(THIS_CLASS, "newArray", MethodType.methodType(Object[].class, int.class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   679
        } catch (NoSuchMethodException | IllegalAccessException ex) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9780
diff changeset
   680
            throw new InternalError("uncaught exception", ex);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   681
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   682
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   683
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   684
    // Varargs methods need to be in a separately initialized class, to bootstrapping problems.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   685
    static class LazyStatics {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   686
        private static final MethodHandle COPY_AS_REFERENCE_ARRAY, COPY_AS_PRIMITIVE_ARRAY, MAKE_LIST;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   687
        static {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   688
            try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   689
                //MAKE_ARRAY = IMPL_LOOKUP.findStatic(THIS_CLASS, "makeArray", MethodType.methodType(Object[].class, Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   690
                COPY_AS_REFERENCE_ARRAY = IMPL_LOOKUP.findStatic(THIS_CLASS, "copyAsReferenceArray", MethodType.methodType(Object[].class, Class.class, Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   691
                COPY_AS_PRIMITIVE_ARRAY = IMPL_LOOKUP.findStatic(THIS_CLASS, "copyAsPrimitiveArray", MethodType.methodType(Object.class, Wrapper.class, Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   692
                MAKE_LIST = IMPL_LOOKUP.findStatic(THIS_CLASS, "makeList", MethodType.methodType(List.class, Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   693
            } catch (ReflectiveOperationException ex) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9780
diff changeset
   694
                throw new InternalError("uncaught exception", ex);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   695
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   696
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   697
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   698
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   699
    private static final EnumMap<Wrapper, MethodHandle>[] WRAPPER_CASTS
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   700
            = newWrapperCaches(2);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   701
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   702
    /** Return a method that casts its sole argument (an Object) to the given type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   703
     *  and returns it as the given type (if exact is true), or as plain Object (if erase is true).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   704
     */
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   705
    public static MethodHandle cast(Class<?> type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   706
        boolean exact = false;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   707
        if (type.isPrimitive())  throw new IllegalArgumentException("cannot cast primitive type "+type);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   708
        MethodHandle mh = null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   709
        Wrapper wrap = null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   710
        EnumMap<Wrapper, MethodHandle> cache = null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   711
        if (Wrapper.isWrapperType(type)) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   712
            wrap = Wrapper.forWrapperType(type);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   713
            cache = WRAPPER_CASTS[exact?1:0];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   714
            mh = cache.get(wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   715
            if (mh != null)  return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   716
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   717
        if (VerifyType.isNullReferenceConversion(Object.class, type))
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   718
            mh = IDENTITY;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   719
        else if (VerifyType.isNullType(type))
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   720
            mh = ALWAYS_NULL;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   721
        else
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   722
            mh = MethodHandles.insertArguments(CAST_REFERENCE, 0, type);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   723
        if (exact) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   724
            MethodType xmt = MethodType.methodType(type, Object.class);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   725
            mh = MethodHandles.explicitCastArguments(mh, xmt);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   726
            //mh = AdapterMethodHandle.makeRetypeRaw(IMPL_TOKEN, xmt, mh);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   727
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   728
        if (cache != null)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   729
            cache.put(wrap, mh);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   730
        return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   731
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   732
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   733
    public static MethodHandle identity() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   734
        return IDENTITY;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   735
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   736
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   737
    public static MethodHandle identity(Class<?> type) {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   738
        // This stuff has been moved into MethodHandles:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   739
        return MethodHandles.identity(type);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   740
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   741
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   742
    public static MethodHandle identity(Wrapper wrap) {
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   743
        EnumMap<Wrapper, MethodHandle> cache = CONSTANT_FUNCTIONS[1];
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   744
        MethodHandle mh = cache.get(wrap);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   745
        if (mh != null) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   746
            return mh;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   747
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   748
        // slow path
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   749
        MethodType type = MethodType.methodType(wrap.primitiveType());
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   750
        if (wrap != Wrapper.VOID)
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   751
            type = type.appendParameterTypes(wrap.primitiveType());
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   752
        try {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   753
            mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "identity", type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7668
diff changeset
   754
        } catch (ReflectiveOperationException ex) {
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   755
            mh = null;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   756
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   757
        if (mh == null && wrap == Wrapper.VOID) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   758
            mh = EMPTY;  // #(){} : #()void
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   759
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   760
        if (mh != null) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   761
            cache.put(wrap, mh);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   762
            return mh;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   763
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   764
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   765
        if (mh != null) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   766
            cache.put(wrap, mh);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   767
            return mh;
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   768
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   769
        throw new IllegalArgumentException("cannot find identity for " + wrap);
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   770
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   771
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   772
    /// Float/non-float conversions.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   773
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   774
    static float doubleToFloat(double x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   775
        return (float) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   776
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   777
    static double floatToDouble(float x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   778
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   779
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   780
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   781
    // narrow double to integral type
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   782
    static long doubleToLong(double x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   783
        return (long) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   784
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   785
    static int doubleToInt(double x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   786
        return (int) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   787
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   788
    static short doubleToShort(double x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   789
        return (short) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   790
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   791
    static char doubleToChar(double x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   792
        return (char) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   793
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   794
    static byte doubleToByte(double x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   795
        return (byte) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   796
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   797
    static boolean doubleToBoolean(double x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   798
        return toBoolean((byte) x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   799
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   800
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   801
    // narrow float to integral type
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   802
    static long floatToLong(float x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   803
        return (long) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   804
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   805
    static int floatToInt(float x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   806
        return (int) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   807
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   808
    static short floatToShort(float x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   809
        return (short) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   810
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   811
    static char floatToChar(float x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   812
        return (char) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   813
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   814
    static byte floatToByte(float x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   815
        return (byte) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   816
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   817
    static boolean floatToBoolean(float x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   818
        return toBoolean((byte) x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   819
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   820
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   821
    // widen integral type to double
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   822
    static double longToDouble(long x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   823
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   824
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   825
    static double intToDouble(int x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   826
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   827
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   828
    static double shortToDouble(short x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   829
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   830
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   831
    static double charToDouble(char x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   832
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   833
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   834
    static double byteToDouble(byte x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   835
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   836
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   837
    static double booleanToDouble(boolean x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   838
        return fromBoolean(x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   839
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   840
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   841
    // widen integral type to float
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   842
    static float longToFloat(long x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   843
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   844
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   845
    static float intToFloat(int x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   846
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   847
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   848
    static float shortToFloat(short x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   849
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   850
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   851
    static float charToFloat(char x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   852
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   853
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   854
    static float byteToFloat(byte x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   855
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   856
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   857
    static float booleanToFloat(boolean x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   858
        return fromBoolean(x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   859
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   860
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   861
    static boolean toBoolean(byte x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   862
        // see javadoc for MethodHandles.explicitCastArguments
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   863
        return ((x & 1) != 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   864
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   865
    static byte fromBoolean(boolean x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   866
        // see javadoc for MethodHandles.explicitCastArguments
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   867
        return (x ? (byte)1 : (byte)0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   868
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   869
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   870
    private static final EnumMap<Wrapper, MethodHandle>[]
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   871
            CONVERT_FLOAT_FUNCTIONS = newWrapperCaches(4);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   872
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   873
    static MethodHandle convertFloatFunction(Wrapper wrap, boolean toFloat, boolean doubleSize) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   874
        EnumMap<Wrapper, MethodHandle> cache = CONVERT_FLOAT_FUNCTIONS[(toFloat?1:0)+(doubleSize?2:0)];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   875
        MethodHandle mh = cache.get(wrap);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   876
        if (mh != null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   877
            return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   878
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   879
        // slow path
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   880
        Wrapper fwrap = (doubleSize ? Wrapper.DOUBLE : Wrapper.FLOAT);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   881
        Class<?> fix = wrap.primitiveType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   882
        Class<?> flt = (doubleSize ? double.class : float.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   883
        Class<?> src = toFloat ? fix : flt;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   884
        Class<?> dst = toFloat ? flt : fix;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   885
        if (src == dst)  return identity(wrap);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   886
        MethodType type = MethodType.methodType(dst, src);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   887
        switch (wrap) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   888
            case VOID:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   889
                mh = toFloat ? zeroConstantFunction(fwrap) : MethodHandles.dropArguments(EMPTY, 0, flt);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   890
                break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   891
            case OBJECT:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   892
                mh = toFloat ? unbox(flt) : box(flt);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   893
                break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   894
            default:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   895
                try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   896
                    mh = IMPL_LOOKUP.findStatic(THIS_CLASS, src.getSimpleName()+"To"+capitalize(dst.getSimpleName()), type);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   897
                } catch (ReflectiveOperationException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   898
                    mh = null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   899
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   900
                break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   901
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   902
        if (mh != null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   903
            assert(mh.type() == type) : mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   904
            cache.put(wrap, mh);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   905
            return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   906
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   907
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   908
        throw new IllegalArgumentException("cannot find float conversion constant for " +
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   909
                                           src.getSimpleName()+" -> "+dst.getSimpleName());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   910
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   911
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   912
    public static MethodHandle convertFromFloat(Class<?> fixType) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   913
        Wrapper wrap = Wrapper.forPrimitiveType(fixType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   914
        return convertFloatFunction(wrap, false, false);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   915
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   916
    public static MethodHandle convertFromDouble(Class<?> fixType) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   917
        Wrapper wrap = Wrapper.forPrimitiveType(fixType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   918
        return convertFloatFunction(wrap, false, true);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   919
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   920
    public static MethodHandle convertToFloat(Class<?> fixType) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   921
        Wrapper wrap = Wrapper.forPrimitiveType(fixType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   922
        return convertFloatFunction(wrap, true, false);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   923
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   924
    public static MethodHandle convertToDouble(Class<?> fixType) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   925
        Wrapper wrap = Wrapper.forPrimitiveType(fixType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   926
        return convertFloatFunction(wrap, true, true);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   927
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   928
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   929
    private static String capitalize(String x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   930
        return Character.toUpperCase(x.charAt(0))+x.substring(1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   931
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   932
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   933
    /// Collection of multiple arguments.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   934
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   935
    public static Object convertArrayElements(Class<?> arrayType, Object array) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   936
        Class<?> src = array.getClass().getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   937
        Class<?> dst = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   938
        if (src == null || dst == null)  throw new IllegalArgumentException("not array type");
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   939
        Wrapper sw = (src.isPrimitive() ? Wrapper.forPrimitiveType(src) : null);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   940
        Wrapper dw = (dst.isPrimitive() ? Wrapper.forPrimitiveType(dst) : null);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   941
        int length;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   942
        if (sw == null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   943
            Object[] a = (Object[]) array;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   944
            length = a.length;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   945
            if (dw == null)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   946
                return Arrays.copyOf(a, length, arrayType.asSubclass(Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   947
            Object res = dw.makeArray(length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   948
            dw.copyArrayUnboxing(a, 0, res, 0, length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   949
            return res;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   950
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   951
        length = java.lang.reflect.Array.getLength(array);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   952
        Object[] res;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   953
        if (dw == null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   954
            res = Arrays.copyOf(NO_ARGS_ARRAY, length, arrayType.asSubclass(Object[].class));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   955
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   956
            res = new Object[length];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   957
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   958
        sw.copyArrayBoxing(array, 0, res, 0, length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   959
        if (dw == null)  return res;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   960
        Object a = dw.makeArray(length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   961
        dw.copyArrayUnboxing(res, 0, a, 0, length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   962
        return a;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   963
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   964
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   965
    private static MethodHandle findCollector(String name, int nargs, Class<?> rtype, Class<?>... ptypes) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   966
        MethodType type = MethodType.genericMethodType(nargs)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   967
                .changeReturnType(rtype)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   968
                .insertParameterTypes(0, ptypes);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   969
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   970
            return IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   971
        } catch (ReflectiveOperationException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   972
            return null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   973
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   974
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   975
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   976
    private static final Object[] NO_ARGS_ARRAY = {};
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   977
    private static Object[] makeArray(Object... args) { return args; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   978
    private static Object[] array() { return NO_ARGS_ARRAY; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   979
    private static Object[] array(Object a0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   980
                { return makeArray(a0); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   981
    private static Object[] array(Object a0, Object a1)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   982
                { return makeArray(a0, a1); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   983
    private static Object[] array(Object a0, Object a1, Object a2)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   984
                { return makeArray(a0, a1, a2); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   985
    private static Object[] array(Object a0, Object a1, Object a2, Object a3)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   986
                { return makeArray(a0, a1, a2, a3); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   987
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   988
                                  Object a4)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   989
                { return makeArray(a0, a1, a2, a3, a4); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   990
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   991
                                  Object a4, Object a5)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   992
                { return makeArray(a0, a1, a2, a3, a4, a5); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   993
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   994
                                  Object a4, Object a5, Object a6)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   995
                { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   996
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   997
                                  Object a4, Object a5, Object a6, Object a7)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   998
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   999
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1000
                                  Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1001
                                  Object a8)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1002
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1003
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1004
                                  Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1005
                                  Object a8, Object a9)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1006
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1007
    private static MethodHandle[] makeArrays() {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1008
        ArrayList<MethodHandle> mhs = new ArrayList<>();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1009
        for (;;) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1010
            MethodHandle mh = findCollector("array", mhs.size(), Object[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1011
            if (mh == null)  break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1012
            mhs.add(mh);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1013
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1014
        assert(mhs.size() == 11);  // current number of methods
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1015
        return mhs.toArray(new MethodHandle[MAX_ARITY+1]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1016
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1017
    private static final MethodHandle[] ARRAYS = makeArrays();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1018
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1019
    // mh-fill versions of the above:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1020
    private static Object[] newArray(int len) { return new Object[len]; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1021
    private static void fillWithArguments(Object[] a, int pos, Object... args) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1022
        System.arraycopy(args, 0, a, pos, args.length);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1023
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1024
    // using Integer pos instead of int pos to avoid bootstrapping problems
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1025
    private static Object[] fillArray(Object[] a, Integer pos, Object a0)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1026
                { fillWithArguments(a, pos, a0); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1027
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1028
                { fillWithArguments(a, pos, a0, a1); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1029
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1, Object a2)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1030
                { fillWithArguments(a, pos, a0, a1, a2); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1031
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1, Object a2, Object a3)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1032
                { fillWithArguments(a, pos, a0, a1, a2, a3); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1033
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1, Object a2, Object a3,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1034
                                  Object a4)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1035
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1036
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1, Object a2, Object a3,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1037
                                  Object a4, Object a5)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1038
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1039
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1, Object a2, Object a3,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1040
                                  Object a4, Object a5, Object a6)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1041
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1042
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1, Object a2, Object a3,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1043
                                  Object a4, Object a5, Object a6, Object a7)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1044
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1045
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1, Object a2, Object a3,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1046
                                  Object a4, Object a5, Object a6, Object a7,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1047
                                  Object a8)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1048
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1049
    private static Object[] fillArray(Object[] a, Integer pos, Object a0, Object a1, Object a2, Object a3,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1050
                                  Object a4, Object a5, Object a6, Object a7,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1051
                                  Object a8, Object a9)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1052
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); return a; }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1053
    private static MethodHandle[] makeFillArrays() {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1054
        ArrayList<MethodHandle> mhs = new ArrayList<>();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1055
        mhs.add(null);  // there is no empty fill; at least a0 is required
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1056
        for (;;) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1057
            MethodHandle mh = findCollector("fillArray", mhs.size(), Object[].class, Object[].class, Integer.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1058
            if (mh == null)  break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1059
            mhs.add(mh);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1060
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1061
        assert(mhs.size() == 11);  // current number of methods
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1062
        return mhs.toArray(new MethodHandle[0]);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1063
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1064
    private static final MethodHandle[] FILL_ARRAYS = makeFillArrays();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1065
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1066
    private static Object[] copyAsReferenceArray(Class<? extends Object[]> arrayType, Object... a) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1067
        return Arrays.copyOf(a, a.length, arrayType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1068
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1069
    private static Object copyAsPrimitiveArray(Wrapper w, Object... boxes) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1070
        Object a = w.makeArray(boxes.length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1071
        w.copyArrayUnboxing(boxes, 0, a, 0, boxes.length);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1072
        return a;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1073
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1074
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1075
    /** Return a method handle that takes the indicated number of Object
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1076
     *  arguments and returns an Object array of them, as if for varargs.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1077
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1078
    public static MethodHandle varargsArray(int nargs) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1079
        MethodHandle mh = ARRAYS[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1080
        if (mh != null)  return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1081
        mh = findCollector("array", nargs, Object[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1082
        if (mh != null)  return ARRAYS[nargs] = mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1083
        MethodHandle producer = filler(0);  // identity function produces result
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1084
        return ARRAYS[nargs] = buildVarargsArray(producer, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1085
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1086
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1087
    private static MethodHandle buildVarargsArray(MethodHandle producer, int nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1088
        // Build up the result mh as a sequence of fills like this:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1089
        //   producer(fill(fill(fill(newArray(23),0,x1..x10),10,x11..x20),20,x21..x23))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1090
        // The various fill(_,10*I,___*[J]) are reusable.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1091
        MethodHandle filler = filler(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1092
        MethodHandle mh = producer;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1093
        mh = MethodHandles.dropArguments(mh, 1, filler.type().parameterList());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1094
        mh = MethodHandles.foldArguments(mh, filler);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1095
        mh = MethodHandles.foldArguments(mh, buildNewArray(nargs));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1096
        return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1097
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1098
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1099
    private static MethodHandle buildNewArray(int nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1100
        return MethodHandles.insertArguments(NEW_ARRAY, 0, (int) nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1101
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1102
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1103
    private static final MethodHandle[] FILLERS = new MethodHandle[MAX_ARITY+1];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1104
    // filler(N).invoke(a, arg0..arg[N-1]) fills a[0]..a[N-1]
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1105
    private static MethodHandle filler(int nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1106
        MethodHandle filler = FILLERS[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1107
        if (filler != null)  return filler;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1108
        return FILLERS[nargs] = buildFiller(nargs);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1109
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1110
    private static MethodHandle buildFiller(int nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1111
        if (nargs == 0)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1112
            return MethodHandles.identity(Object[].class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1113
        final int CHUNK = (FILL_ARRAYS.length - 1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1114
        int rightLen = nargs % CHUNK;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1115
        int leftLen = nargs - rightLen;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1116
        if (rightLen == 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1117
            leftLen = nargs - (rightLen = CHUNK);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1118
            if (FILLERS[leftLen] == null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1119
                // build some precursors from left to right
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1120
                for (int j = 0; j < leftLen; j += CHUNK)  filler(j);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1121
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1122
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1123
        MethodHandle leftFill = filler(leftLen);  // recursive fill
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1124
        MethodHandle rightFill = FILL_ARRAYS[rightLen];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1125
        rightFill = MethodHandles.insertArguments(rightFill, 1, (int) leftLen);  // [leftLen..nargs-1]
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1126
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1127
        // Combine the two fills: right(left(newArray(nargs), x1..x20), x21..x23)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1128
        MethodHandle mh = filler(0);  // identity function produces result
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1129
        mh = MethodHandles.dropArguments(mh, 1, rightFill.type().parameterList());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1130
        mh = MethodHandles.foldArguments(mh, rightFill);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1131
        if (leftLen > 0) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1132
            mh = MethodHandles.dropArguments(mh, 1, leftFill.type().parameterList());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1133
            mh = MethodHandles.foldArguments(mh, leftFill);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1134
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1135
        return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1136
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1137
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1138
    // Type-polymorphic version of varargs maker.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1139
    private static final ClassValue<MethodHandle[]> TYPED_COLLECTORS
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1140
        = new ClassValue<MethodHandle[]>() {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1141
            protected MethodHandle[] computeValue(Class<?> type) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1142
                return new MethodHandle[256];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1143
            }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1144
    };
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1145
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1146
    /** Return a method handle that takes the indicated number of
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1147
     *  typed arguments and returns an array of them.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1148
     *  The type argument is the array type.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1149
     */
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1150
    public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1151
        Class<?> elemType = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1152
        if (elemType == null)  throw new IllegalArgumentException("not an array: "+arrayType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1153
        // FIXME: Need more special casing and caching here.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1154
        if (elemType == Object.class)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1155
            return varargsArray(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1156
        // other cases:  primitive arrays, subtypes of Object[]
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1157
        MethodHandle cache[] = TYPED_COLLECTORS.get(elemType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1158
        MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1159
        if (mh != null)  return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1160
        MethodHandle producer = buildArrayProducer(arrayType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1161
        mh = buildVarargsArray(producer, nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1162
        mh = mh.asType(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1163
        cache[nargs] = mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1164
        return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1165
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1166
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1167
    private static MethodHandle buildArrayProducer(Class<?> arrayType) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1168
        Class<?> elemType = arrayType.getComponentType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1169
        if (elemType.isPrimitive())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1170
            return LazyStatics.COPY_AS_PRIMITIVE_ARRAY.bindTo(Wrapper.forPrimitiveType(elemType));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1171
        else
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1172
            return LazyStatics.COPY_AS_REFERENCE_ARRAY.bindTo(arrayType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1173
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1174
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1175
    // List version of varargs maker.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1176
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1177
    private static final List<Object> NO_ARGS_LIST = Arrays.asList(NO_ARGS_ARRAY);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1178
    private static List<Object> makeList(Object... args) { return Arrays.asList(args); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1179
    private static List<Object> list() { return NO_ARGS_LIST; }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1180
    private static List<Object> list(Object a0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1181
                { return makeList(a0); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1182
    private static List<Object> list(Object a0, Object a1)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1183
                { return makeList(a0, a1); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1184
    private static List<Object> list(Object a0, Object a1, Object a2)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1185
                { return makeList(a0, a1, a2); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1186
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1187
                { return makeList(a0, a1, a2, a3); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1188
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1189
                                     Object a4)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1190
                { return makeList(a0, a1, a2, a3, a4); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1191
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1192
                                     Object a4, Object a5)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1193
                { return makeList(a0, a1, a2, a3, a4, a5); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1194
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1195
                                     Object a4, Object a5, Object a6)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1196
                { return makeList(a0, a1, a2, a3, a4, a5, a6); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1197
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1198
                                     Object a4, Object a5, Object a6, Object a7)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1199
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1200
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1201
                                     Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1202
                                     Object a8)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1203
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1204
    private static List<Object> list(Object a0, Object a1, Object a2, Object a3,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1205
                                     Object a4, Object a5, Object a6, Object a7,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1206
                                     Object a8, Object a9)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1207
                { return makeList(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1208
    private static MethodHandle[] makeLists() {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1209
        ArrayList<MethodHandle> mhs = new ArrayList<>();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1210
        for (;;) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1211
            MethodHandle mh = findCollector("list", mhs.size(), List.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1212
            if (mh == null)  break;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1213
            mhs.add(mh);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1214
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1215
        assert(mhs.size() == 11);  // current number of methods
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1216
        return mhs.toArray(new MethodHandle[MAX_ARITY+1]);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1217
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1218
    private static final MethodHandle[] LISTS = makeLists();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1219
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1220
    /** Return a method handle that takes the indicated number of Object
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1221
     *  arguments and returns a List.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1222
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1223
    public static MethodHandle varargsList(int nargs) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1224
        MethodHandle mh = LISTS[nargs];
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1225
        if (mh != null)  return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1226
        mh = findCollector("list", nargs, List.class);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1227
        if (mh != null)  return LISTS[nargs] = mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1228
        return LISTS[nargs] = buildVarargsList(nargs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1229
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1230
    private static MethodHandle buildVarargsList(int nargs) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
  1231
        return MethodHandles.filterReturnValue(varargsArray(nargs), LazyStatics.MAKE_LIST);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
  1232
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1233
}