jdk/src/share/classes/java/lang/invoke/MethodHandleImpl.java
author alanb
Wed, 12 Feb 2014 14:23:52 +0000
changeset 22960 436141944957
parent 21649 1aca56f8780c
child 23038 bb8b3b23af3a
permissions -rw-r--r--
8034780: Remove used imports Reviewed-by: sundar, chegar, lancea, dfuchs
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
/*
16030
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
     2
 * Copyright (c) 2008, 2013, 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 java.lang.invoke;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
    28
import java.security.AccessController;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
    29
import java.security.PrivilegedAction;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    30
import java.util.ArrayList;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    31
import java.util.Arrays;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    32
import java.util.HashMap;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    33
import sun.invoke.empty.Empty;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    34
import sun.invoke.util.ValueConversions;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
    35
import sun.invoke.util.VerifyType;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    36
import sun.invoke.util.Wrapper;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
    37
import sun.reflect.CallerSensitive;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
    38
import sun.reflect.Reflection;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    39
import static java.lang.invoke.LambdaForm.*;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    40
import static java.lang.invoke.MethodHandleStatics.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    41
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    42
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    43
/**
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    44
 * Trusted implementation code for MethodHandle.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    45
 * @author jrose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    46
 */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    47
/*non-public*/ abstract class MethodHandleImpl {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
    /// Factory methods to create method handles:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    49
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    50
    static void initStatics() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    51
        // Trigger selected static initializations.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    52
        MemberName.Factory.INSTANCE.getClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    53
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    54
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    55
    static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    56
        if (!arrayClass.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    57
            throw newIllegalArgumentException("not an array: "+arrayClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    58
        MethodHandle accessor = ArrayAccessor.getAccessor(arrayClass, isSetter);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    59
        MethodType srcType = accessor.type().erase();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    60
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    61
        Name[] names = arguments(1, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    62
        Name[] args  = Arrays.copyOfRange(names, 1, 1 + srcType.parameterCount());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    63
        names[names.length - 1] = new Name(accessor.asType(srcType), (Object[]) args);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    64
        LambdaForm form = new LambdaForm("getElement", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
    65
        MethodHandle mh = SimpleMethodHandle.make(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    66
        if (ArrayAccessor.needCast(arrayClass)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    67
            mh = mh.bindTo(arrayClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    68
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    69
        mh = mh.asType(ArrayAccessor.correctType(arrayClass, isSetter));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    70
        return mh;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    71
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    72
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    73
    static final class ArrayAccessor {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    74
        /// Support for array element access
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    75
        static final HashMap<Class<?>, MethodHandle> GETTER_CACHE = new HashMap<>();  // TODO use it
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    76
        static final HashMap<Class<?>, MethodHandle> SETTER_CACHE = new HashMap<>();  // TODO use it
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    77
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    78
        static int     getElementI(int[]     a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    79
        static long    getElementJ(long[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    80
        static float   getElementF(float[]   a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    81
        static double  getElementD(double[]  a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    82
        static boolean getElementZ(boolean[] a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    83
        static byte    getElementB(byte[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    84
        static short   getElementS(short[]   a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    85
        static char    getElementC(char[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    86
        static Object  getElementL(Object[]  a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    87
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    88
        static void    setElementI(int[]     a, int i, int     x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    89
        static void    setElementJ(long[]    a, int i, long    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    90
        static void    setElementF(float[]   a, int i, float   x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    91
        static void    setElementD(double[]  a, int i, double  x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    92
        static void    setElementZ(boolean[] a, int i, boolean x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    93
        static void    setElementB(byte[]    a, int i, byte    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    94
        static void    setElementS(short[]   a, int i, short   x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    95
        static void    setElementC(char[]    a, int i, char    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    96
        static void    setElementL(Object[]  a, int i, Object  x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    97
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    98
        static Object  getElementL(Class<?> arrayClass, Object[] a, int i)           { arrayClass.cast(a); return a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    99
        static void    setElementL(Class<?> arrayClass, Object[] a, int i, Object x) { arrayClass.cast(a); a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   100
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   101
        // Weakly typed wrappers of Object[] accessors:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   102
        static Object  getElementL(Object    a, int i)            { return getElementL((Object[])a, i); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   103
        static void    setElementL(Object    a, int i, Object  x) {        setElementL((Object[]) a, i, x); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   104
        static Object  getElementL(Object   arrayClass, Object a, int i)             { return getElementL((Class<?>) arrayClass, (Object[])a, i); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   105
        static void    setElementL(Object   arrayClass, Object a, int i, Object x)   {        setElementL((Class<?>) arrayClass, (Object[])a, i, x); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   106
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   107
        static boolean needCast(Class<?> arrayClass) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   108
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   109
            return !elemClass.isPrimitive() && elemClass != Object.class;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   110
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   111
        static String name(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   112
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   113
            if (elemClass == null)  throw new IllegalArgumentException();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   114
            return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   115
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   116
        static final boolean USE_WEAKLY_TYPED_ARRAY_ACCESSORS = false;  // FIXME: decide
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   117
        static MethodType type(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   118
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   119
            Class<?> arrayArgClass = arrayClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   120
            if (!elemClass.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   121
                arrayArgClass = Object[].class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   122
                if (USE_WEAKLY_TYPED_ARRAY_ACCESSORS)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   123
                    arrayArgClass = Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   124
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   125
            if (!needCast(arrayClass)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   126
                return !isSetter ?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   127
                    MethodType.methodType(elemClass,  arrayArgClass, int.class) :
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   128
                    MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   129
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   130
                Class<?> classArgClass = Class.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   131
                if (USE_WEAKLY_TYPED_ARRAY_ACCESSORS)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   132
                    classArgClass = Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   133
                return !isSetter ?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   134
                    MethodType.methodType(Object.class, classArgClass, arrayArgClass, int.class) :
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   135
                    MethodType.methodType(void.class,   classArgClass, arrayArgClass, int.class, Object.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   136
            }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   137
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   138
        static MethodType correctType(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   139
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   140
            return !isSetter ?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   141
                    MethodType.methodType(elemClass,  arrayClass, int.class) :
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   142
                    MethodType.methodType(void.class, arrayClass, int.class, elemClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   143
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   144
        static MethodHandle getAccessor(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   145
            String     name = name(arrayClass, isSetter);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   146
            MethodType type = type(arrayClass, isSetter);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   147
            try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   148
                return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   149
            } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   150
                throw uncaughtException(ex);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   151
            }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   152
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   153
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   154
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   155
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   156
     * Create a JVM-level adapter method handle to conform the given method
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   157
     * handle to the similar newType, using only pairwise argument conversions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   158
     * For each argument, convert incoming argument to the exact type needed.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   159
     * The argument conversions allowed are casting, boxing and unboxing,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   160
     * integral widening or narrowing, and floating point widening or narrowing.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   161
     * @param srcType required call type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   162
     * @param target original method handle
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   163
     * @param level which strength of conversion is allowed
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   164
     * @return an adapter to the original handle with the desired new type,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   165
     *          or the original target if the types are already identical
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   166
     *          or null if the adaptation cannot be made
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   167
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   168
    static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType, int level) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   169
        assert(level >= 0 && level <= 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   170
        MethodType dstType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   171
        assert(dstType.parameterCount() == target.type().parameterCount());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   172
        if (srcType == dstType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   173
            return target;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   174
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   175
        // Calculate extra arguments (temporaries) required in the names array.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   176
        // FIXME: Use an ArrayList<Name>.  Some arguments require more than one conversion step.
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   177
        final int INARG_COUNT = srcType.parameterCount();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   178
        int conversions = 0;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   179
        boolean[] needConv = new boolean[1+INARG_COUNT];
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   180
        for (int i = 0; i <= INARG_COUNT; i++) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   181
            Class<?> src = (i == INARG_COUNT) ? dstType.returnType() : srcType.parameterType(i);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   182
            Class<?> dst = (i == INARG_COUNT) ? srcType.returnType() : dstType.parameterType(i);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   183
            if (!VerifyType.isNullConversion(src, dst) ||
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   184
                level <= 1 && dst.isInterface() && !dst.isAssignableFrom(src)) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   185
                needConv[i] = true;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   186
                conversions++;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   187
            }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   188
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   189
        boolean retConv = needConv[INARG_COUNT];
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   190
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   191
        final int IN_MH         = 0;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   192
        final int INARG_BASE    = 1;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   193
        final int INARG_LIMIT   = INARG_BASE + INARG_COUNT;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   194
        final int NAME_LIMIT    = INARG_LIMIT + conversions + 1;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   195
        final int RETURN_CONV   = (!retConv ? -1         : NAME_LIMIT - 1);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   196
        final int OUT_CALL      = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   197
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   198
        // Now build a LambdaForm.
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   199
        MethodType lambdaType = srcType.basicType().invokerType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   200
        Name[] names = arguments(NAME_LIMIT - INARG_LIMIT, lambdaType);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   201
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   202
        // Collect the arguments to the outgoing call, maybe with conversions:
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   203
        final int OUTARG_BASE = 0;  // target MH is Name.function, name Name.arguments[0]
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   204
        Object[] outArgs = new Object[OUTARG_BASE + INARG_COUNT];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   205
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   206
        int nameCursor = INARG_LIMIT;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   207
        for (int i = 0; i < INARG_COUNT; i++) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   208
            Class<?> src = srcType.parameterType(i);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   209
            Class<?> dst = dstType.parameterType(i);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   210
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   211
            if (!needConv[i]) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   212
                // do nothing: difference is trivial
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   213
                outArgs[OUTARG_BASE + i] = names[INARG_BASE + i];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   214
                continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   215
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   216
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   217
            // Tricky case analysis follows.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   218
            MethodHandle fn = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   219
            if (src.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   220
                if (dst.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   221
                    fn = ValueConversions.convertPrimitive(src, dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   222
                } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   223
                    Wrapper w = Wrapper.forPrimitiveType(src);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   224
                    MethodHandle boxMethod = ValueConversions.box(w);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   225
                    if (dst == w.wrapperType())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   226
                        fn = boxMethod;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   227
                    else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   228
                        fn = boxMethod.asType(MethodType.methodType(dst, src));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   229
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   230
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   231
                if (dst.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   232
                    // Caller has boxed a primitive.  Unbox it for the target.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   233
                    Wrapper w = Wrapper.forPrimitiveType(dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   234
                    if (level == 0 || VerifyType.isNullConversion(src, w.wrapperType())) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   235
                        fn = ValueConversions.unbox(dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   236
                    } else if (src == Object.class || !Wrapper.isWrapperType(src)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   237
                        // Examples:  Object->int, Number->int, Comparable->int; Byte->int, Character->int
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   238
                        // must include additional conversions
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   239
                        // src must be examined at runtime, to detect Byte, Character, etc.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   240
                        MethodHandle unboxMethod = (level == 1
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   241
                                                    ? ValueConversions.unbox(dst)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   242
                                                    : ValueConversions.unboxCast(dst));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   243
                        fn = unboxMethod;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   244
                    } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   245
                        // Example: Byte->int
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   246
                        // Do this by reformulating the problem to Byte->byte.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   247
                        Class<?> srcPrim = Wrapper.forWrapperType(src).primitiveType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   248
                        MethodHandle unbox = ValueConversions.unbox(srcPrim);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   249
                        // Compose the two conversions.  FIXME:  should make two Names for this job
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   250
                        fn = unbox.asType(MethodType.methodType(dst, src));
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
   251
                    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   252
                } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   253
                    // Simple reference conversion.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   254
                    // Note:  Do not check for a class hierarchy relation
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   255
                    // between src and dst.  In all cases a 'null' argument
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   256
                    // will pass the cast conversion.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   257
                    fn = ValueConversions.cast(dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   258
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   259
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   260
            Name conv = new Name(fn, names[INARG_BASE + i]);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   261
            assert(names[nameCursor] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   262
            names[nameCursor++] = conv;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   263
            assert(outArgs[OUTARG_BASE + i] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   264
            outArgs[OUTARG_BASE + i] = conv;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   265
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   266
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   267
        // Build argument array for the call.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   268
        assert(nameCursor == OUT_CALL);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   269
        names[OUT_CALL] = new Name(target, outArgs);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   270
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   271
        if (RETURN_CONV < 0) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   272
            assert(OUT_CALL == names.length-1);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   273
        } else {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   274
            Class<?> needReturn = srcType.returnType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   275
            Class<?> haveReturn = dstType.returnType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   276
            MethodHandle fn;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   277
            Object[] arg = { names[OUT_CALL] };
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   278
            if (haveReturn == void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   279
                // synthesize a zero value for the given void
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   280
                Object zero = Wrapper.forBasicType(needReturn).zero();
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   281
                fn = MethodHandles.constant(needReturn, zero);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   282
                arg = new Object[0];  // don't pass names[OUT_CALL] to conversion
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   283
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   284
                MethodHandle identity = MethodHandles.identity(needReturn);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   285
                MethodType needConversion = identity.type().changeParameterType(0, haveReturn);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   286
                fn = makePairwiseConvert(identity, needConversion, level);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   287
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   288
            assert(names[RETURN_CONV] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   289
            names[RETURN_CONV] = new Name(fn, arg);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   290
            assert(RETURN_CONV == names.length-1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   291
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   292
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   293
        LambdaForm form = new LambdaForm("convert", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   294
        return SimpleMethodHandle.make(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   295
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   296
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   297
    static MethodHandle makeReferenceIdentity(Class<?> refType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   298
        MethodType lambdaType = MethodType.genericMethodType(1).invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   299
        Name[] names = arguments(1, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   300
        names[names.length - 1] = new Name(ValueConversions.identity(), names[1]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   301
        LambdaForm form = new LambdaForm("identity", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   302
        return SimpleMethodHandle.make(MethodType.methodType(refType, refType), form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   303
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   304
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   305
    static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   306
        MethodType type = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   307
        int last = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   308
        if (type.parameterType(last) != arrayType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   309
            target = target.asType(type.changeParameterType(last, arrayType));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   310
        target = target.asFixedArity();  // make sure this attribute is turned off
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   311
        return new AsVarargsCollector(target, target.type(), arrayType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   312
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   313
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   314
    static class AsVarargsCollector extends MethodHandle {
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   315
        private final MethodHandle target;
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   316
        private final Class<?> arrayType;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   317
        private /*@Stable*/ MethodHandle asCollectorCache;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   318
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   319
        AsVarargsCollector(MethodHandle target, MethodType type, Class<?> arrayType) {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   320
            super(type, reinvokerForm(target));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   321
            this.target = target;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   322
            this.arrayType = arrayType;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   323
            this.asCollectorCache = target.asCollector(arrayType, 0);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   324
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   325
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   326
        @Override MethodHandle reinvokerTarget() { return target; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   327
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   328
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   329
        public boolean isVarargsCollector() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   330
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   331
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   332
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   333
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   334
        public MethodHandle asFixedArity() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   335
            return target;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   336
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   337
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   338
        @Override
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   339
        public MethodHandle asTypeUncached(MethodType newType) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   340
            MethodType type = this.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   341
            int collectArg = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   342
            int newArity = newType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   343
            if (newArity == collectArg+1 &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   344
                type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   345
                // if arity and trailing parameter are compatible, do normal thing
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   346
                return asTypeCache = asFixedArity().asType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   347
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   348
            // check cache
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   349
            MethodHandle acc = asCollectorCache;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   350
            if (acc != null && acc.type().parameterCount() == newArity)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   351
                return asTypeCache = acc.asType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   352
            // build and cache a collector
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   353
            int arrayLength = newArity - collectArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   354
            MethodHandle collector;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   355
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   356
                collector = asFixedArity().asCollector(arrayType, arrayLength);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   357
                assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   358
            } catch (IllegalArgumentException ex) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   359
                throw new WrongMethodTypeException("cannot build collector", ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   360
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   361
            asCollectorCache = collector;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   362
            return asTypeCache = collector.asType(newType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   363
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   364
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   365
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   366
        MethodHandle setVarargs(MemberName member) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   367
            if (member.isVarargs())  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   368
            return asFixedArity();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   369
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   370
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   371
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   372
        MethodHandle viewAsType(MethodType newType) {
16030
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   373
            if (newType.lastParameterType() != type().lastParameterType())
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   374
                throw new InternalError();
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   375
            MethodHandle newTarget = asFixedArity().viewAsType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   376
            // put back the varargs bit:
16030
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   377
            return new AsVarargsCollector(newTarget, newType, arrayType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   378
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   379
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   380
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   381
        MemberName internalMemberName() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   382
            return asFixedArity().internalMemberName();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   383
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   384
        @Override
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   385
        Class<?> internalCallerClass() {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   386
            return asFixedArity().internalCallerClass();
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   387
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   388
16030
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   389
        /*non-public*/
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   390
        @Override
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   391
        boolean isInvokeSpecial() {
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   392
            return asFixedArity().isInvokeSpecial();
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   393
        }
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   394
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   395
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   396
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   397
        MethodHandle bindArgument(int pos, char basicType, Object value) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   398
            return asFixedArity().bindArgument(pos, basicType, value);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   399
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   400
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   401
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   402
        MethodHandle bindReceiver(Object receiver) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   403
            return asFixedArity().bindReceiver(receiver);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   404
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   405
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   406
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   407
        MethodHandle dropArguments(MethodType srcType, int pos, int drops) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   408
            return asFixedArity().dropArguments(srcType, pos, drops);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   409
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   410
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   411
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   412
        MethodHandle permuteArguments(MethodType newType, int[] reorder) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   413
            return asFixedArity().permuteArguments(newType, reorder);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   414
        }
2707
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
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   417
    /** Factory method:  Spread selected argument. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   418
    static MethodHandle makeSpreadArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   419
                                            Class<?> spreadArgType, int spreadArgPos, int spreadArgCount) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   420
        MethodType targetType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   421
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   422
        for (int i = 0; i < spreadArgCount; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   423
            Class<?> arg = VerifyType.spreadArgElementType(spreadArgType, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   424
            if (arg == null)  arg = Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   425
            targetType = targetType.changeParameterType(spreadArgPos + i, arg);
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2707
diff changeset
   426
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   427
        target = target.asType(targetType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   428
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   429
        MethodType srcType = targetType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   430
                .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   431
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   432
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   433
        Name[] names = arguments(spreadArgCount + 2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   434
        int nameCursor = lambdaType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   435
        int[] indexes = new int[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   436
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   437
        for (int i = 0, argIndex = 1; i < targetType.parameterCount() + 1; i++, argIndex++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   438
            Class<?> src = lambdaType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   439
            if (i == spreadArgPos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   440
                // Spread the array.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   441
                MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   442
                Name array = names[argIndex];
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   443
                names[nameCursor++] = new Name(Lazy.NF_checkSpreadArgument, array, spreadArgCount);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   444
                for (int j = 0; j < spreadArgCount; i++, j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   445
                    indexes[i] = nameCursor;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   446
                    names[nameCursor++] = new Name(aload, array, j);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   447
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   448
            } else if (i < indexes.length) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   449
                indexes[i] = argIndex;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   450
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   451
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   452
        assert(nameCursor == names.length-1);  // leave room for the final call
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   453
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   454
        // Build argument array for the call.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   455
        Name[] targetArgs = new Name[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   456
        for (int i = 0; i < targetType.parameterCount(); i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   457
            int idx = indexes[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   458
            targetArgs[i] = names[idx];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   459
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   460
        names[names.length - 1] = new Name(target, (Object[]) targetArgs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   461
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   462
        LambdaForm form = new LambdaForm("spread", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   463
        return SimpleMethodHandle.make(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   464
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   465
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   466
    static void checkSpreadArgument(Object av, int n) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   467
        if (av == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   468
            if (n == 0)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   469
        } else if (av instanceof Object[]) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   470
            int len = ((Object[])av).length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   471
            if (len == n)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   472
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   473
            int len = java.lang.reflect.Array.getLength(av);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   474
            if (len == n)  return;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   475
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   476
        // fall through to error:
20530
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20527
diff changeset
   477
        throw newIllegalArgumentException("array is not of length "+n);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   478
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   479
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   480
    /**
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   481
     * Pre-initialized NamedFunctions for bootstrapping purposes.
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   482
     * Factored in an inner class to delay initialization until first usage.
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   483
     */
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   484
    private static class Lazy {
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   485
        static final NamedFunction NF_checkSpreadArgument;
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   486
        static {
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   487
            try {
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   488
                NF_checkSpreadArgument = new NamedFunction(MethodHandleImpl.class
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   489
                        .getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   490
                NF_checkSpreadArgument.resolve();
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   491
            } catch (ReflectiveOperationException ex) {
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   492
                throw newInternalError(ex);
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   493
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   494
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   495
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   496
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   497
    /** Factory method:  Collect or filter selected argument(s). */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   498
    static MethodHandle makeCollectArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   499
                MethodHandle collector, int collectArgPos, boolean retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   500
        MethodType targetType = target.type();          // (a..., c, [b...])=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   501
        MethodType collectorType = collector.type();    // (b...)=>c
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   502
        int collectArgCount = collectorType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   503
        Class<?> collectValType = collectorType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   504
        int collectValCount = (collectValType == void.class ? 0 : 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   505
        MethodType srcType = targetType                 // (a..., [b...])=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   506
                .dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   507
        if (!retainOriginalArgs) {                      // (a..., b...)=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   508
            srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList());
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   509
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   510
        // in  arglist: [0: ...keep1 | cpos: collect...  | cpos+cacount: keep2... ]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   511
        // out arglist: [0: ...keep1 | cpos: collectVal? | cpos+cvcount: keep2... ]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   512
        // out(retain): [0: ...keep1 | cpos: cV? coll... | cpos+cvc+cac: keep2... ]
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   513
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   514
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   515
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   516
        Name[] names = arguments(2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   517
        final int collectNamePos = names.length - 2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   518
        final int targetNamePos  = names.length - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   519
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   520
        Name[] collectorArgs = Arrays.copyOfRange(names, 1 + collectArgPos, 1 + collectArgPos + collectArgCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   521
        names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   522
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   523
        // Build argument array for the target.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   524
        // Incoming LF args to copy are: [ (mh) headArgs collectArgs tailArgs ].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   525
        // Output argument array is [ headArgs (collectVal)? (collectArgs)? tailArgs ].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   526
        Name[] targetArgs = new Name[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   527
        int inputArgPos  = 1;  // incoming LF args to copy to target
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   528
        int targetArgPos = 0;  // fill pointer for targetArgs
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   529
        int chunk = collectArgPos;  // |headArgs|
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   530
        System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   531
        inputArgPos  += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   532
        targetArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   533
        if (collectValType != void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   534
            targetArgs[targetArgPos++] = names[collectNamePos];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   535
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   536
        chunk = collectArgCount;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   537
        if (retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   538
            System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   539
            targetArgPos += chunk;   // optionally pass on the collected chunk
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   540
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   541
        inputArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   542
        chunk = targetArgs.length - targetArgPos;  // all the rest
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   543
        System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   544
        assert(inputArgPos + chunk == collectNamePos);  // use of rest of input args also
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   545
        names[targetNamePos] = new Name(target, (Object[]) targetArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   546
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   547
        LambdaForm form = new LambdaForm("collect", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   548
        return SimpleMethodHandle.make(srcType, form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   549
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   550
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   551
    static
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   552
    MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   553
        return testResult ? target : fallback;
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   554
    }
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   555
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   556
    static MethodHandle SELECT_ALTERNATIVE;
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   557
    static MethodHandle selectAlternative() {
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   558
        if (SELECT_ALTERNATIVE != null)  return SELECT_ALTERNATIVE;
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   559
        try {
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   560
            SELECT_ALTERNATIVE
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   561
            = IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   562
                    MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   563
        } catch (ReflectiveOperationException ex) {
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   564
            throw new RuntimeException(ex);
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   565
        }
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   566
        return SELECT_ALTERNATIVE;
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   567
    }
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   568
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   569
    static
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   570
    MethodHandle makeGuardWithTest(MethodHandle test,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   571
                                   MethodHandle target,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   572
                                   MethodHandle fallback) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   573
        MethodType basicType = target.type().basicType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   574
        MethodHandle invokeBasic = MethodHandles.basicInvoker(basicType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   575
        int arity = basicType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   576
        int extraNames = 3;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   577
        MethodType lambdaType = basicType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   578
        Name[] names = arguments(extraNames, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   579
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   580
        Object[] testArgs   = Arrays.copyOfRange(names, 1, 1 + arity, Object[].class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   581
        Object[] targetArgs = Arrays.copyOfRange(names, 0, 1 + arity, Object[].class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   582
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   583
        // call test
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   584
        names[arity + 1] = new Name(test, testArgs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   585
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   586
        // call selectAlternative
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   587
        Object[] selectArgs = { names[arity + 1], target, fallback };
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   588
        names[arity + 2] = new Name(MethodHandleImpl.selectAlternative(), selectArgs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   589
        targetArgs[0] = names[arity + 2];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   590
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   591
        // call target or fallback
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   592
        names[arity + 3] = new Name(new NamedFunction(invokeBasic), targetArgs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   593
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   594
        LambdaForm form = new LambdaForm("guard", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   595
        return SimpleMethodHandle.make(target.type(), form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   596
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   597
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   598
    private static class GuardWithCatch {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   599
        private final MethodHandle target;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   600
        private final Class<? extends Throwable> exType;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   601
        private final MethodHandle catcher;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   602
        // FIXME: Build the control flow out of foldArguments.
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   603
        GuardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   604
            this.target = target;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   605
            this.exType = exType;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   606
            this.catcher = catcher;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   607
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   608
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   609
        private Object invoke_V(Object... av) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   610
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   611
                return target.invokeExact(av);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   612
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   613
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   614
                return catcher.invokeExact(t, av);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   615
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   616
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   617
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   618
        private Object invoke_L0() throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   619
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   620
                return target.invokeExact();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   621
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   622
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   623
                return catcher.invokeExact(t);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   624
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   625
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   626
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   627
        private Object invoke_L1(Object a0) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   628
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   629
                return target.invokeExact(a0);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   630
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   631
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   632
                return catcher.invokeExact(t, a0);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   633
            }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   634
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   635
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   636
        private Object invoke_L2(Object a0, Object a1) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   637
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   638
                return target.invokeExact(a0, a1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   639
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   640
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   641
                return catcher.invokeExact(t, a0, a1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   642
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   643
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   644
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   645
        private Object invoke_L3(Object a0, Object a1, Object a2) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   646
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   647
                return target.invokeExact(a0, a1, a2);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   648
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   649
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   650
                return catcher.invokeExact(t, a0, a1, a2);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   651
            }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   652
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   653
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   654
        private Object invoke_L4(Object a0, Object a1, Object a2, Object a3) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   655
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   656
                return target.invokeExact(a0, a1, a2, a3);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   657
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   658
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   659
                return catcher.invokeExact(t, a0, a1, a2, a3);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   660
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   661
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   662
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   663
        private Object invoke_L5(Object a0, Object a1, Object a2, Object a3, Object a4) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   664
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   665
                return target.invokeExact(a0, a1, a2, a3, a4);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   666
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   667
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   668
                return catcher.invokeExact(t, a0, a1, a2, a3, a4);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   669
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   670
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   671
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   672
        private Object invoke_L6(Object a0, Object a1, Object a2, Object a3, Object a4, Object a5) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   673
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   674
                return target.invokeExact(a0, a1, a2, a3, a4, a5);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   675
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   676
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   677
                return catcher.invokeExact(t, a0, a1, a2, a3, a4, a5);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   678
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   679
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   680
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   681
        private Object invoke_L7(Object a0, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   682
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   683
                return target.invokeExact(a0, a1, a2, a3, a4, a5, a6);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   684
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   685
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   686
                return catcher.invokeExact(t, a0, a1, a2, a3, a4, a5, a6);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   687
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   688
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   689
        @LambdaForm.Hidden
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   690
        private Object invoke_L8(Object a0, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) throws Throwable {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   691
            try {
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   692
                return target.invokeExact(a0, a1, a2, a3, a4, a5, a6, a7);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   693
            } catch (Throwable t) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   694
                if (!exType.isInstance(t))  throw t;
7556
4a5711d43948 6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents: 7555
diff changeset
   695
                return catcher.invokeExact(t, a0, a1, a2, a3, a4, a5, a6, a7);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   696
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   697
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   698
        static MethodHandle[] makeInvokes() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   699
            ArrayList<MethodHandle> invokes = new ArrayList<>();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   700
            MethodHandles.Lookup lookup = IMPL_LOOKUP;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   701
            for (;;) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   702
                int nargs = invokes.size();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   703
                String name = "invoke_L"+nargs;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   704
                MethodHandle invoke = null;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   705
                try {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   706
                    invoke = lookup.findVirtual(GuardWithCatch.class, name, MethodType.genericMethodType(nargs));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   707
                } catch (ReflectiveOperationException ex) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   708
                }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   709
                if (invoke == null)  break;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   710
                invokes.add(invoke);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   711
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   712
            assert(invokes.size() == 9);  // current number of methods
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   713
            return invokes.toArray(new MethodHandle[0]);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   714
        };
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   715
        static final MethodHandle[] INVOKES = makeInvokes();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   716
        // For testing use this:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   717
        //static final MethodHandle[] INVOKES = Arrays.copyOf(makeInvokes(), 2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   718
        static final MethodHandle VARARGS_INVOKE;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   719
        static {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   720
            try {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   721
                VARARGS_INVOKE = IMPL_LOOKUP.findVirtual(GuardWithCatch.class, "invoke_V", MethodType.genericMethodType(0, true));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   722
            } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   723
                throw uncaughtException(ex);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   724
            }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   725
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   726
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   727
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   728
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   729
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   730
    MethodHandle makeGuardWithCatch(MethodHandle target,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   731
                                    Class<? extends Throwable> exType,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   732
                                    MethodHandle catcher) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   733
        MethodType type = target.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   734
        MethodType ctype = catcher.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   735
        int nargs = type.parameterCount();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   736
        if (nargs < GuardWithCatch.INVOKES.length) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   737
            MethodType gtype = type.generic();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   738
            MethodType gcatchType = gtype.insertParameterTypes(0, Throwable.class);
9859
47e26ad535c4 7052202: JSR 292: Crash in sun.invoke.util.ValueConversions.fillArray
jrose
parents: 9780
diff changeset
   739
            // Note: convertArguments(...2) avoids interface casts present in convertArguments(...0)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   740
            MethodHandle gtarget = makePairwiseConvert(target, gtype, 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   741
            MethodHandle gcatcher = makePairwiseConvert(catcher, gcatchType, 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   742
            GuardWithCatch gguard = new GuardWithCatch(gtarget, exType, gcatcher);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   743
            if (gtarget == null || gcatcher == null)  throw new InternalError();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   744
            MethodHandle ginvoker = GuardWithCatch.INVOKES[nargs].bindReceiver(gguard);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   745
            return makePairwiseConvert(ginvoker, type, 2);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   746
        } else {
21649
1aca56f8780c 8027823: catchException combinator fails with 9 argument target
vlivanov
parents: 20534
diff changeset
   747
            target = target.asType(type.changeReturnType(Object.class));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   748
            MethodHandle gtarget = makeSpreadArguments(target, Object[].class, 0, nargs);
21649
1aca56f8780c 8027823: catchException combinator fails with 9 argument target
vlivanov
parents: 20534
diff changeset
   749
            MethodType catcherType = ctype.changeParameterType(0, Throwable.class)
1aca56f8780c 8027823: catchException combinator fails with 9 argument target
vlivanov
parents: 20534
diff changeset
   750
                                          .changeReturnType(Object.class);
1aca56f8780c 8027823: catchException combinator fails with 9 argument target
vlivanov
parents: 20534
diff changeset
   751
            catcher = catcher.asType(catcherType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   752
            MethodHandle gcatcher = makeSpreadArguments(catcher, Object[].class, 1, nargs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   753
            GuardWithCatch gguard = new GuardWithCatch(gtarget, exType, gcatcher);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   754
            if (gtarget == null || gcatcher == null)  throw new InternalError();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   755
            MethodHandle ginvoker = GuardWithCatch.VARARGS_INVOKE.bindReceiver(gguard);
18769
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents: 16906
diff changeset
   756
            MethodHandle gcollect = makeCollectArguments(ginvoker, ValueConversions.varargsArray(nargs), 0, false);
53b3406abedf 8019184: MethodHandles.catchException() fails when methods have 8 args + varargs
twisti
parents: 16906
diff changeset
   757
            return makePairwiseConvert(gcollect, type, 2);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   758
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   759
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   760
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   761
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   762
    MethodHandle throwException(MethodType type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   763
        assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   764
        int arity = type.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   765
        if (arity > 1) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   766
            return throwException(type.dropParameterTypes(1, arity)).dropArguments(type, 1, arity-1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   767
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   768
        return makePairwiseConvert(throwException(), type, 2);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   769
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   770
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   771
    static MethodHandle THROW_EXCEPTION;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   772
    static MethodHandle throwException() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   773
        MethodHandle mh = THROW_EXCEPTION;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   774
        if (mh != null)  return mh;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   775
        try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   776
            mh
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   777
            = IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "throwException",
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   778
                    MethodType.methodType(Empty.class, Throwable.class));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   779
        } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   780
            throw new RuntimeException(ex);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   781
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   782
        THROW_EXCEPTION = mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   783
        return mh;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   784
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   785
    static <T extends Throwable> Empty throwException(T t) throws T { throw t; }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   786
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   787
    static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   788
    static MethodHandle fakeMethodHandleInvoke(MemberName method) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   789
        int idx;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   790
        assert(method.isMethodHandleInvoke());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   791
        switch (method.getName()) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   792
        case "invoke":       idx = 0; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   793
        case "invokeExact":  idx = 1; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   794
        default:             throw new InternalError(method.getName());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   795
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   796
        MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   797
        if (mh != null)  return mh;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   798
        MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   799
                                                MethodHandle.class, Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   800
        mh = throwException(type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   801
        mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   802
        if (!method.getInvocationType().equals(mh.type()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   803
            throw new InternalError(method.toString());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   804
        mh = mh.withInternalMemberName(method);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   805
        mh = mh.asVarargsCollector(Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   806
        assert(method.isVarargs());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   807
        FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   808
        return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   809
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   810
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   811
    /**
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   812
     * Create an alias for the method handle which, when called,
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   813
     * appears to be called from the same class loader and protection domain
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   814
     * as hostClass.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   815
     * This is an expensive no-op unless the method which is called
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   816
     * is sensitive to its caller.  A small number of system methods
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   817
     * are in this category, including Class.forName and Method.invoke.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   818
     */
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   819
    static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   820
    MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   821
        return BindCaller.bindCaller(mh, hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   822
    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   823
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   824
    // Put the whole mess into its own nested class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   825
    // That way we can lazily load the code and set up the constants.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   826
    private static class BindCaller {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   827
        static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   828
        MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   829
            // Do not use this function to inject calls into system classes.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   830
            if (hostClass == null
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   831
                ||    (hostClass.isArray() ||
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   832
                       hostClass.isPrimitive() ||
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   833
                       hostClass.getName().startsWith("java.") ||
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   834
                       hostClass.getName().startsWith("sun."))) {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   835
                throw new InternalError();  // does not happen, and should not anyway
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   836
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   837
            // For simplicity, convert mh to a varargs-like method.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   838
            MethodHandle vamh = prepareForInvoker(mh);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   839
            // Cache the result of makeInjectedInvoker once per argument class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   840
            MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   841
            return restoreToType(bccInvoker.bindTo(vamh), mh.type(), mh.internalMemberName(), hostClass);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   842
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   843
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   844
        private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   845
            Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   846
            if (hostClass.getClassLoader() != bcc.getClassLoader())
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   847
                throw new InternalError(hostClass.getName()+" (CL)");
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   848
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   849
                if (hostClass.getProtectionDomain() != bcc.getProtectionDomain())
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   850
                    throw new InternalError(hostClass.getName()+" (PD)");
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   851
            } catch (SecurityException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   852
                // Self-check was blocked by security manager.  This is OK.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   853
                // In fact the whole try body could be turned into an assertion.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   854
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   855
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   856
                MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   857
                init.invokeExact();  // force initialization of the class
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   858
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   859
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   860
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   861
            MethodHandle bccInvoker;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   862
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   863
                MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   864
                bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   865
            } catch (ReflectiveOperationException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   866
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   867
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   868
            // Test the invoker, to ensure that it really injects into the right place.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   869
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   870
                MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   871
                Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc});
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   872
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   873
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   874
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   875
            return bccInvoker;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   876
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   877
        private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   878
            @Override protected MethodHandle computeValue(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   879
                return makeInjectedInvoker(hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   880
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   881
        };
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   882
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   883
        // Adapt mh so that it can be called directly from an injected invoker:
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   884
        private static MethodHandle prepareForInvoker(MethodHandle mh) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   885
            mh = mh.asFixedArity();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   886
            MethodType mt = mh.type();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   887
            int arity = mt.parameterCount();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   888
            MethodHandle vamh = mh.asType(mt.generic());
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   889
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   890
            vamh = vamh.asSpreader(Object[].class, arity);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   891
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   892
            return vamh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   893
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   894
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   895
        // Undo the adapter effect of prepareForInvoker:
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   896
        private static MethodHandle restoreToType(MethodHandle vamh, MethodType type,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   897
                                                  MemberName member,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   898
                                                  Class<?> hostClass) {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   899
            MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   900
            mh = mh.asType(type);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   901
            mh = new WrappedMember(mh, type, member, hostClass);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   902
            return mh;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   903
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   904
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   905
        private static final MethodHandle MH_checkCallerClass;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   906
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   907
            final Class<?> THIS_CLASS = BindCaller.class;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   908
            assert(checkCallerClass(THIS_CLASS, THIS_CLASS));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   909
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   910
                MH_checkCallerClass = IMPL_LOOKUP
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   911
                    .findStatic(THIS_CLASS, "checkCallerClass",
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   912
                                MethodType.methodType(boolean.class, Class.class, Class.class));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   913
                assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   914
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   915
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   916
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   917
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   918
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   919
        @CallerSensitive
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   920
        private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   921
            // This method is called via MH_checkCallerClass and so it's
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   922
            // correct to ask for the immediate caller here.
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   923
            Class<?> actual = Reflection.getCallerClass();
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   924
            if (actual != expected && actual != expected2)
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   925
                throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   926
                                        +(expected == expected2 ? "" : ", or else "+expected2.getName()));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   927
            return true;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   928
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   929
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   930
        private static final byte[] T_BYTES;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   931
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   932
            final Object[] values = {null};
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   933
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   934
                    public Void run() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   935
                        try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   936
                            Class<T> tClass = T.class;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   937
                            String tName = tClass.getName();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   938
                            String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   939
                            java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   940
                            int len = uconn.getContentLength();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   941
                            byte[] bytes = new byte[len];
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   942
                            try (java.io.InputStream str = uconn.getInputStream()) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   943
                                int nr = str.read(bytes);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   944
                                if (nr != len)  throw new java.io.IOException(tResource);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   945
                            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   946
                            values[0] = bytes;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   947
                        } catch (java.io.IOException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   948
                            throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   949
                        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   950
                        return null;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   951
                    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   952
                });
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   953
            T_BYTES = (byte[]) values[0];
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   954
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   955
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   956
        // The following class is used as a template for Unsafe.defineAnonymousClass:
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   957
        private static class T {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   958
            static void init() { }  // side effect: initializes this class
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   959
            static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   960
                return vamh.invokeExact(args);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   961
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   962
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   963
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   964
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   965
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   966
    /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   967
    static class WrappedMember extends MethodHandle {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   968
        private final MethodHandle target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   969
        private final MemberName member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   970
        private final Class<?> callerClass;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   971
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   972
        private WrappedMember(MethodHandle target, MethodType type, MemberName member, Class<?> callerClass) {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   973
            super(type, reinvokerForm(target));
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   974
            this.target = target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   975
            this.member = member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   976
            this.callerClass = callerClass;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   977
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   978
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   979
        @Override
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   980
        MethodHandle reinvokerTarget() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   981
            return target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   982
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   983
        @Override
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   984
        public MethodHandle asTypeUncached(MethodType newType) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   985
            // This MH is an alias for target, except for the MemberName
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   986
            // Drop the MemberName if there is any conversion.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   987
            return asTypeCache = target.asType(newType);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   988
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   989
        @Override
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   990
        MemberName internalMemberName() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   991
            return member;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   992
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   993
        @Override
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   994
        Class<?> internalCallerClass() {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   995
            return callerClass;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   996
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   997
        @Override
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   998
        boolean isInvokeSpecial() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   999
            return target.isInvokeSpecial();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1000
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1001
        @Override
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1002
        MethodHandle viewAsType(MethodType newType) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1003
            return new WrappedMember(target, newType, member, callerClass);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1004
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1005
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1006
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1007
    static MethodHandle makeWrappedMember(MethodHandle target, MemberName member) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1008
        if (member.equals(target.internalMemberName()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1009
            return target;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1010
        return new WrappedMember(target, target.type(), member, null);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1011
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1012
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1013
}