jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 29094 a4fd2b5e49f8
child 33874 46651fd30c0b
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
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
/*
24367
705490680527 8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents: 23898
diff changeset
     2
 * Copyright (c) 2008, 2014, 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
7053
da0e2dc092d3 6953246: JSR 292 should support SAM conversion
jrose
parents: 7052
diff changeset
    28
import java.lang.reflect.*;
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
    29
import java.util.BitSet;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
    30
import java.util.List;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
    31
import java.util.Arrays;
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
    32
import java.util.Objects;
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
    33
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    34
import sun.invoke.util.ValueConversions;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    35
import sun.invoke.util.VerifyAccess;
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: 16898
diff changeset
    37
import sun.reflect.CallerSensitive;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    38
import sun.reflect.Reflection;
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
    39
import sun.reflect.misc.ReflectUtil;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
    40
import sun.security.util.SecurityConstants;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 24367
diff changeset
    41
import java.lang.invoke.LambdaForm.BasicType;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 24367
diff changeset
    42
import static java.lang.invoke.LambdaForm.BasicType.*;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    43
import static java.lang.invoke.MethodHandleStatics.*;
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
    44
import static java.lang.invoke.MethodHandleImpl.Intrinsic;
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
    45
import static java.lang.invoke.MethodHandleNatives.Constants.*;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
    46
import java.util.concurrent.ConcurrentHashMap;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    47
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
/**
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    49
 * This class consists exclusively of static methods that operate on or return
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    50
 * method handles. They fall into several categories:
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    51
 * <ul>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
    52
 * <li>Lookup methods which help create method handles for methods and fields.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
    53
 * <li>Combinator methods, which combine or transform pre-existing method handles into new ones.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
    54
 * <li>Other factory methods to create method handles that emulate other common JVM operations or control flow patterns.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    55
 * </ul>
24367
705490680527 8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents: 23898
diff changeset
    56
 *
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    57
 * @author John Rose, JSR 292 EG
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
    58
 * @since 1.7
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    59
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    60
public class MethodHandles {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    61
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    62
    private MethodHandles() { }  // do not instantiate
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    63
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
    64
    private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    65
    static { MethodHandleImpl.initStatics(); }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    66
    // See IMPL_LOOKUP below.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    67
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    68
    //// Method handle creation from ordinary methods.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    69
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    70
    /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    71
     * Returns a {@link Lookup lookup object} with
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    72
     * full capabilities to emulate all supported bytecode behaviors of the caller.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    73
     * These capabilities include <a href="MethodHandles.Lookup.html#privacc">private access</a> to the caller.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    74
     * Factory methods on the lookup object can create
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    75
     * <a href="MethodHandleInfo.html#directmh">direct method handles</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    76
     * for any member that the caller has access to via bytecodes,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    77
     * including protected and private fields and methods.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    78
     * This lookup object is a <em>capability</em> which may be delegated to trusted agents.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    79
     * Do not store it in place where untrusted code can access it.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    80
     * <p>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    81
     * This method is caller sensitive, which means that it may return different
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    82
     * values to different callers.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    83
     * <p>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    84
     * For any given caller class {@code C}, the lookup object returned by this call
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    85
     * has equivalent capabilities to any lookup object
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    86
     * supplied by the JVM to the bootstrap method of an
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    87
     * <a href="package-summary.html#indyinsn">invokedynamic instruction</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    88
     * executing in the same caller class {@code C}.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    89
     * @return a lookup object for the caller of this method, with private access
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    90
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
    91
    @CallerSensitive
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    92
    public static Lookup lookup() {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
    93
        return new Lookup(Reflection.getCallerClass());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    94
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    95
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    96
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    97
     * Returns a {@link Lookup lookup object} which is trusted minimally.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    98
     * It can only be used to create method handles to
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    99
     * publicly accessible fields and methods.
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   100
     * <p>
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   101
     * As a matter of pure convention, the {@linkplain Lookup#lookupClass lookup class}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   102
     * of this lookup object will be {@link java.lang.Object}.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   103
     *
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   104
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   105
     * <em>Discussion:</em>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   106
     * The lookup class can be changed to any other class {@code C} using an expression of the form
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   107
     * {@link Lookup#in publicLookup().in(C.class)}.
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   108
     * Since all classes have equal access to public names,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   109
     * such a change would confer no new access rights.
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   110
     * A public lookup object is always subject to
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   111
     * <a href="MethodHandles.Lookup.html#secmgr">security manager checks</a>.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   112
     * Also, it cannot access
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   113
     * <a href="MethodHandles.Lookup.html#callsens">caller sensitive methods</a>.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   114
     * @return a lookup object which is trusted minimally
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   115
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   116
    public static Lookup publicLookup() {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   117
        return Lookup.PUBLIC_LOOKUP;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   118
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   119
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   120
    /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   121
     * Performs an unchecked "crack" of a
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   122
     * <a href="MethodHandleInfo.html#directmh">direct method handle</a>.
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   123
     * The result is as if the user had obtained a lookup object capable enough
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   124
     * to crack the target method handle, called
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   125
     * {@link java.lang.invoke.MethodHandles.Lookup#revealDirect Lookup.revealDirect}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   126
     * on the target to obtain its symbolic reference, and then called
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   127
     * {@link java.lang.invoke.MethodHandleInfo#reflectAs MethodHandleInfo.reflectAs}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   128
     * to resolve the symbolic reference to a member.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   129
     * <p>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   130
     * If there is a security manager, its {@code checkPermission} method
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   131
     * is called with a {@code ReflectPermission("suppressAccessChecks")} permission.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   132
     * @param <T> the desired type of the result, either {@link Member} or a subtype
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   133
     * @param target a direct method handle to crack into symbolic reference components
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   134
     * @param expected a class object representing the desired result type {@code T}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   135
     * @return a reference to the method, constructor, or field object
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   136
     * @exception SecurityException if the caller is not privileged to call {@code setAccessible}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   137
     * @exception NullPointerException if either argument is {@code null}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   138
     * @exception IllegalArgumentException if the target is not a direct method handle
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   139
     * @exception ClassCastException if the member is not of the expected type
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   140
     * @since 1.8
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   141
     */
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   142
    public static <T extends Member> T
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   143
    reflectAs(Class<T> expected, MethodHandle target) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   144
        SecurityManager smgr = System.getSecurityManager();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   145
        if (smgr != null)  smgr.checkPermission(ACCESS_PERMISSION);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   146
        Lookup lookup = Lookup.IMPL_LOOKUP;  // use maximally privileged lookup
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   147
        return lookup.revealDirect(target).reflectAs(expected, lookup);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   148
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   149
    // Copied from AccessibleObject, as used by Method.setAccessible, etc.:
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
   150
    private static final java.security.Permission ACCESS_PERMISSION =
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   151
        new ReflectPermission("suppressAccessChecks");
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   152
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   153
    /**
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   154
     * A <em>lookup object</em> is a factory for creating method handles,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   155
     * when the creation requires access checking.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   156
     * Method handles do not perform
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   157
     * access checks when they are called, but rather when they are created.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   158
     * Therefore, method handle access
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   159
     * restrictions must be enforced when a method handle is created.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   160
     * The caller class against which those restrictions are enforced
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   161
     * is known as the {@linkplain #lookupClass lookup class}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   162
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   163
     * A lookup class which needs to create method handles will call
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   164
     * {@link MethodHandles#lookup MethodHandles.lookup} to create a factory for itself.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   165
     * When the {@code Lookup} factory object is created, the identity of the lookup class is
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   166
     * determined, and securely stored in the {@code Lookup} object.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   167
     * The lookup class (or its delegates) may then use factory methods
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   168
     * on the {@code Lookup} object to create method handles for access-checked members.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   169
     * This includes all methods, constructors, and fields which are allowed to the lookup class,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   170
     * even private ones.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   171
     *
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   172
     * <h1><a name="lookups"></a>Lookup Factory Methods</h1>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   173
     * The factory methods on a {@code Lookup} object correspond to all major
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   174
     * use cases for methods, constructors, and fields.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   175
     * Each method handle created by a factory method is the functional
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   176
     * equivalent of a particular <em>bytecode behavior</em>.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   177
     * (Bytecode behaviors are described in section 5.4.3.5 of the Java Virtual Machine Specification.)
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   178
     * Here is a summary of the correspondence between these factory methods and
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   179
     * the behavior the resulting method handles:
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   180
     * <table border=1 cellpadding=5 summary="lookup method behaviors">
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   181
     * <tr>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   182
     *     <th><a name="equiv"></a>lookup expression</th>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   183
     *     <th>member</th>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   184
     *     <th>bytecode behavior</th>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   185
     * </tr>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   186
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   187
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   188
     *     <td>{@code FT f;}</td><td>{@code (T) this.f;}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   189
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   190
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   191
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   192
     *     <td>{@code static}<br>{@code FT f;}</td><td>{@code (T) C.f;}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   193
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   194
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   195
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   196
     *     <td>{@code FT f;}</td><td>{@code this.f = x;}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   197
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   198
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   199
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findStaticSetter lookup.findStaticSetter(C.class,"f",FT.class)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   200
     *     <td>{@code static}<br>{@code FT f;}</td><td>{@code C.f = arg;}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   201
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   202
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   203
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findVirtual lookup.findVirtual(C.class,"m",MT)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   204
     *     <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   205
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   206
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   207
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findStatic lookup.findStatic(C.class,"m",MT)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   208
     *     <td>{@code static}<br>{@code T m(A*);}</td><td>{@code (T) C.m(arg*);}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   209
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   210
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   211
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findSpecial lookup.findSpecial(C.class,"m",MT,this.class)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   212
     *     <td>{@code T m(A*);}</td><td>{@code (T) super.m(arg*);}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   213
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   214
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   215
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findConstructor lookup.findConstructor(C.class,MT)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   216
     *     <td>{@code C(A*);}</td><td>{@code new C(arg*);}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   217
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   218
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   219
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectGetter lookup.unreflectGetter(aField)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   220
     *     <td>({@code static})?<br>{@code FT f;}</td><td>{@code (FT) aField.get(thisOrNull);}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   221
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   222
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   223
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectSetter lookup.unreflectSetter(aField)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   224
     *     <td>({@code static})?<br>{@code FT f;}</td><td>{@code aField.set(thisOrNull, arg);}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   225
     * </tr>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   226
     * <tr>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   227
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   228
     *     <td>({@code static})?<br>{@code T m(A*);}</td><td>{@code (T) aMethod.invoke(thisOrNull, arg*);}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   229
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   230
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   231
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor lookup.unreflectConstructor(aConstructor)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   232
     *     <td>{@code C(A*);}</td><td>{@code (C) aConstructor.newInstance(arg*);}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   233
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   234
     * <tr>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   235
     *     <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   236
     *     <td>({@code static})?<br>{@code T m(A*);}</td><td>{@code (T) aMethod.invoke(thisOrNull, arg*);}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   237
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   238
     * </table>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   239
     *
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   240
     * Here, the type {@code C} is the class or interface being searched for a member,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   241
     * documented as a parameter named {@code refc} in the lookup methods.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   242
     * The method type {@code MT} is composed from the return type {@code T}
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   243
     * and the sequence of argument types {@code A*}.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   244
     * The constructor also has a sequence of argument types {@code A*} and
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   245
     * is deemed to return the newly-created object of type {@code C}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   246
     * Both {@code MT} and the field type {@code FT} are documented as a parameter named {@code type}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   247
     * The formal parameter {@code this} stands for the self-reference of type {@code C};
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   248
     * if it is present, it is always the leading argument to the method handle invocation.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   249
     * (In the case of some {@code protected} members, {@code this} may be
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   250
     * restricted in type to the lookup class; see below.)
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   251
     * The name {@code arg} stands for all the other method handle arguments.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   252
     * In the code examples for the Core Reflection API, the name {@code thisOrNull}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   253
     * stands for a null reference if the accessed method or field is static,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   254
     * and {@code this} otherwise.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   255
     * The names {@code aMethod}, {@code aField}, and {@code aConstructor} stand
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   256
     * for reflective objects corresponding to the given members.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   257
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   258
     * In cases where the given member is of variable arity (i.e., a method or constructor)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   259
     * the returned method handle will also be of {@linkplain MethodHandle#asVarargsCollector variable arity}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   260
     * In all other cases, the returned method handle will be of fixed arity.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   261
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   262
     * <em>Discussion:</em>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   263
     * The equivalence between looked-up method handles and underlying
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   264
     * class members and bytecode behaviors
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   265
     * can break down in a few ways:
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   266
     * <ul style="font-size:smaller;">
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   267
     * <li>If {@code C} is not symbolically accessible from the lookup class's loader,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   268
     * the lookup can still succeed, even when there is no equivalent
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   269
     * Java expression or bytecoded constant.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   270
     * <li>Likewise, if {@code T} or {@code MT}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   271
     * is not symbolically accessible from the lookup class's loader,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   272
     * the lookup can still succeed.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   273
     * For example, lookups for {@code MethodHandle.invokeExact} and
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
   274
     * {@code MethodHandle.invoke} will always succeed, regardless of requested type.
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   275
     * <li>If there is a security manager installed, it can forbid the lookup
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   276
     * on various grounds (<a href="MethodHandles.Lookup.html#secmgr">see below</a>).
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   277
     * By contrast, the {@code ldc} instruction on a {@code CONSTANT_MethodHandle}
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   278
     * constant is not subject to security manager checks.
20529
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
   279
     * <li>If the looked-up method has a
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
   280
     * <a href="MethodHandle.html#maxarity">very large arity</a>,
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
   281
     * the method handle creation may fail, due to the method handle
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
   282
     * type having too many parameters.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   283
     * </ul>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   284
     *
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   285
     * <h1><a name="access"></a>Access checking</h1>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   286
     * Access checks are applied in the factory methods of {@code Lookup},
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   287
     * when a method handle is created.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   288
     * This is a key difference from the Core Reflection API, since
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   289
     * {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   290
     * performs access checking against every caller, on every call.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   291
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   292
     * All access checks start from a {@code Lookup} object, which
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   293
     * compares its recorded lookup class against all requests to
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   294
     * create method handles.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   295
     * A single {@code Lookup} object can be used to create any number
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   296
     * of access-checked method handles, all checked against a single
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   297
     * lookup class.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   298
     * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   299
     * A {@code Lookup} object can be shared with other trusted code,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   300
     * such as a metaobject protocol.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   301
     * A shared {@code Lookup} object delegates the capability
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   302
     * to create method handles on private members of the lookup class.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   303
     * Even if privileged code uses the {@code Lookup} object,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   304
     * the access checking is confined to the privileges of the
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   305
     * original lookup class.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   306
     * <p>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   307
     * A lookup can fail, because
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   308
     * the containing class is not accessible to the lookup class, or
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   309
     * because the desired class member is missing, or because the
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   310
     * desired class member is not accessible to the lookup class, or
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   311
     * because the lookup object is not trusted enough to access the member.
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   312
     * In any of these cases, a {@code ReflectiveOperationException} will be
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   313
     * thrown from the attempted lookup.  The exact class will be one of
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   314
     * the following:
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   315
     * <ul>
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   316
     * <li>NoSuchMethodException &mdash; if a method is requested but does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   317
     * <li>NoSuchFieldException &mdash; if a field is requested but does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   318
     * <li>IllegalAccessException &mdash; if the member exists but an access check fails
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   319
     * </ul>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   320
     * <p>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   321
     * In general, the conditions under which a method handle may be
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   322
     * looked up for a method {@code M} are no more restrictive than the conditions
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   323
     * under which the lookup class could have compiled, verified, and resolved a call to {@code M}.
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   324
     * Where the JVM would raise exceptions like {@code NoSuchMethodError},
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   325
     * a method handle lookup will generally raise a corresponding
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   326
     * checked exception, such as {@code NoSuchMethodException}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   327
     * And the effect of invoking the method handle resulting from the lookup
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   328
     * is <a href="MethodHandles.Lookup.html#equiv">exactly equivalent</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   329
     * to executing the compiled, verified, and resolved call to {@code M}.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   330
     * The same point is true of fields and constructors.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   331
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   332
     * <em>Discussion:</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   333
     * Access checks only apply to named and reflected methods,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   334
     * constructors, and fields.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   335
     * Other method handle creation methods, such as
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   336
     * {@link MethodHandle#asType MethodHandle.asType},
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   337
     * do not require any access checks, and are used
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   338
     * independently of any {@code Lookup} object.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   339
     * <p>
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   340
     * If the desired member is {@code protected}, the usual JVM rules apply,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   341
     * including the requirement that the lookup class must be either be in the
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   342
     * same package as the desired member, or must inherit that member.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   343
     * (See the Java Virtual Machine Specification, sections 4.9.2, 5.4.3.5, and 6.4.)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   344
     * In addition, if the desired member is a non-static field or method
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   345
     * in a different package, the resulting method handle may only be applied
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   346
     * to objects of the lookup class or one of its subclasses.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   347
     * This requirement is enforced by narrowing the type of the leading
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   348
     * {@code this} parameter from {@code C}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   349
     * (which will necessarily be a superclass of the lookup class)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   350
     * to the lookup class itself.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   351
     * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   352
     * The JVM imposes a similar requirement on {@code invokespecial} instruction,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   353
     * that the receiver argument must match both the resolved method <em>and</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   354
     * the current class.  Again, this requirement is enforced by narrowing the
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   355
     * type of the leading parameter to the resulting method handle.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   356
     * (See the Java Virtual Machine Specification, section 4.10.1.9.)
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   357
     * <p>
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   358
     * The JVM represents constructors and static initializer blocks as internal methods
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   359
     * with special names ({@code "<init>"} and {@code "<clinit>"}).
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   360
     * The internal syntax of invocation instructions allows them to refer to such internal
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   361
     * methods as if they were normal methods, but the JVM bytecode verifier rejects them.
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   362
     * A lookup of such an internal method will produce a {@code NoSuchMethodException}.
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   363
     * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   364
     * In some cases, access between nested classes is obtained by the Java compiler by creating
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   365
     * an wrapper method to access a private method of another class
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   366
     * in the same top-level declaration.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   367
     * For example, a nested class {@code C.D}
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   368
     * can access private members within other related classes such as
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   369
     * {@code C}, {@code C.D.E}, or {@code C.B},
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   370
     * but the Java compiler may need to generate wrapper methods in
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   371
     * those related classes.  In such cases, a {@code Lookup} object on
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   372
     * {@code C.E} would be unable to those private members.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   373
     * A workaround for this limitation is the {@link Lookup#in Lookup.in} method,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   374
     * which can transform a lookup on {@code C.E} into one on any of those other
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   375
     * classes, without special elevation of privilege.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   376
     * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   377
     * The accesses permitted to a given lookup object may be limited,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   378
     * according to its set of {@link #lookupModes lookupModes},
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   379
     * to a subset of members normally accessible to the lookup class.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   380
     * For example, the {@link MethodHandles#publicLookup publicLookup}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   381
     * method produces a lookup object which is only allowed to access
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   382
     * public members in public classes.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   383
     * The caller sensitive method {@link MethodHandles#lookup lookup}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   384
     * produces a lookup object with full capabilities relative to
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   385
     * its caller class, to emulate all supported bytecode behaviors.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   386
     * Also, the {@link Lookup#in Lookup.in} method may produce a lookup object
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   387
     * with fewer access modes than the original lookup object.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   388
     *
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   389
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   390
     * <a name="privacc"></a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   391
     * <em>Discussion of private access:</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   392
     * We say that a lookup has <em>private access</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   393
     * if its {@linkplain #lookupModes lookup modes}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   394
     * include the possibility of accessing {@code private} members.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   395
     * As documented in the relevant methods elsewhere,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   396
     * only lookups with private access possess the following capabilities:
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   397
     * <ul style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   398
     * <li>access private fields, methods, and constructors of the lookup class
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   399
     * <li>create method handles which invoke <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a> methods,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   400
     *     such as {@code Class.forName}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   401
     * <li>create method handles which {@link Lookup#findSpecial emulate invokespecial} instructions
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   402
     * <li>avoid <a href="MethodHandles.Lookup.html#secmgr">package access checks</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   403
     *     for classes accessible to the lookup class
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   404
     * <li>create {@link Lookup#in delegated lookup objects} which have private access to other classes
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   405
     *     within the same package member
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   406
     * </ul>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   407
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   408
     * Each of these permissions is a consequence of the fact that a lookup object
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   409
     * with private access can be securely traced back to an originating class,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   410
     * whose <a href="MethodHandles.Lookup.html#equiv">bytecode behaviors</a> and Java language access permissions
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   411
     * can be reliably determined and emulated by method handles.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   412
     *
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   413
     * <h1><a name="secmgr"></a>Security manager interactions</h1>
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   414
     * Although bytecode instructions can only refer to classes in
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   415
     * a related class loader, this API can search for methods in any
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   416
     * class, as long as a reference to its {@code Class} object is
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   417
     * available.  Such cross-loader references are also possible with the
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   418
     * Core Reflection API, and are impossible to bytecode instructions
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   419
     * such as {@code invokestatic} or {@code getfield}.
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   420
     * There is a {@linkplain java.lang.SecurityManager security manager API}
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   421
     * to allow applications to check such cross-loader references.
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   422
     * These checks apply to both the {@code MethodHandles.Lookup} API
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   423
     * and the Core Reflection API
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   424
     * (as found on {@link java.lang.Class Class}).
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   425
     * <p>
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   426
     * If a security manager is present, member lookups are subject to
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   427
     * additional checks.
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
   428
     * From one to three calls are made to the security manager.
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   429
     * Any of these calls can refuse access by throwing a
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   430
     * {@link java.lang.SecurityException SecurityException}.
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   431
     * Define {@code smgr} as the security manager,
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
   432
     * {@code lookc} as the lookup class of the current lookup object,
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   433
     * {@code refc} as the containing class in which the member
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   434
     * is being sought, and {@code defc} as the class in which the
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   435
     * member is actually defined.
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
   436
     * The value {@code lookc} is defined as <em>not present</em>
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
   437
     * if the current lookup object does not have
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   438
     * <a href="MethodHandles.Lookup.html#privacc">private access</a>.
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   439
     * The calls are made according to the following rules:
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   440
     * <ul>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   441
     * <li><b>Step 1:</b>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   442
     *     If {@code lookc} is not present, or if its class loader is not
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   443
     *     the same as or an ancestor of the class loader of {@code refc},
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   444
     *     then {@link SecurityManager#checkPackageAccess
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   445
     *     smgr.checkPackageAccess(refcPkg)} is called,
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   446
     *     where {@code refcPkg} is the package of {@code refc}.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   447
     * <li><b>Step 2:</b>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   448
     *     If the retrieved member is not public and
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
   449
     *     {@code lookc} is not present, then
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
   450
     *     {@link SecurityManager#checkPermission smgr.checkPermission}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
   451
     *     with {@code RuntimePermission("accessDeclaredMembers")} is called.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   452
     * <li><b>Step 3:</b>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   453
     *     If the retrieved member is not public,
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   454
     *     and if {@code lookc} is not present,
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
   455
     *     and if {@code defc} and {@code refc} are different,
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   456
     *     then {@link SecurityManager#checkPackageAccess
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   457
     *     smgr.checkPackageAccess(defcPkg)} is called,
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   458
     *     where {@code defcPkg} is the package of {@code defc}.
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   459
     * </ul>
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   460
     * Security checks are performed after other access checks have passed.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   461
     * Therefore, the above rules presuppose a member that is public,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   462
     * or else that is being accessed from a lookup class that has
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   463
     * rights to access the member.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   464
     *
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   465
     * <h1><a name="callsens"></a>Caller sensitive methods</h1>
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   466
     * A small number of Java methods have a special property called caller sensitivity.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   467
     * A <em>caller-sensitive</em> method can behave differently depending on the
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   468
     * identity of its immediate caller.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   469
     * <p>
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   470
     * If a method handle for a caller-sensitive method is requested,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   471
     * the general rules for <a href="MethodHandles.Lookup.html#equiv">bytecode behaviors</a> apply,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   472
     * but they take account of the lookup class in a special way.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   473
     * The resulting method handle behaves as if it were called
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   474
     * from an instruction contained in the lookup class,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   475
     * so that the caller-sensitive method detects the lookup class.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   476
     * (By contrast, the invoker of the method handle is disregarded.)
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   477
     * Thus, in the case of caller-sensitive methods,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   478
     * different lookup classes may give rise to
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   479
     * differently behaving method handles.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   480
     * <p>
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   481
     * In cases where the lookup object is
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   482
     * {@link MethodHandles#publicLookup() publicLookup()},
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   483
     * or some other lookup object without
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   484
     * <a href="MethodHandles.Lookup.html#privacc">private access</a>,
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   485
     * the lookup class is disregarded.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   486
     * In such cases, no caller-sensitive method handle can be created,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   487
     * access is forbidden, and the lookup fails with an
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   488
     * {@code IllegalAccessException}.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   489
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   490
     * <em>Discussion:</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   491
     * For example, the caller-sensitive method
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   492
     * {@link java.lang.Class#forName(String) Class.forName(x)}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   493
     * can return varying classes or throw varying exceptions,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   494
     * depending on the class loader of the class that calls it.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   495
     * A public lookup of {@code Class.forName} will fail, because
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   496
     * there is no reasonable way to determine its bytecode behavior.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   497
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   498
     * If an application caches method handles for broad sharing,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   499
     * it should use {@code publicLookup()} to create them.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   500
     * If there is a lookup of {@code Class.forName}, it will fail,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   501
     * and the application must take appropriate action in that case.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   502
     * It may be that a later lookup, perhaps during the invocation of a
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   503
     * bootstrap method, can incorporate the specific identity
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   504
     * of the caller, making the method accessible.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   505
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   506
     * The function {@code MethodHandles.lookup} is caller sensitive
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   507
     * so that there can be a secure foundation for lookups.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   508
     * Nearly all other methods in the JSR 292 API rely on lookup
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   509
     * objects to check access requests.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   510
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   511
    public static final
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   512
    class Lookup {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   513
        /** The class on behalf of whom the lookup is being performed. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   514
        private final Class<?> lookupClass;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   515
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   516
        /** The allowed sorts of members which may be looked up (PUBLIC, etc.). */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   517
        private final int allowedModes;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   518
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   519
        /** A single-bit mask representing {@code public} access,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   520
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   521
         *  The value, {@code 0x01}, happens to be the same as the value of the
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   522
         *  {@code public} {@linkplain java.lang.reflect.Modifier#PUBLIC modifier bit}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   523
         */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   524
        public static final int PUBLIC = Modifier.PUBLIC;
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   525
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   526
        /** A single-bit mask representing {@code private} access,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   527
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   528
         *  The value, {@code 0x02}, happens to be the same as the value of the
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   529
         *  {@code private} {@linkplain java.lang.reflect.Modifier#PRIVATE modifier bit}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   530
         */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   531
        public static final int PRIVATE = Modifier.PRIVATE;
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   532
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   533
        /** A single-bit mask representing {@code protected} access,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   534
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   535
         *  The value, {@code 0x04}, happens to be the same as the value of the
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   536
         *  {@code protected} {@linkplain java.lang.reflect.Modifier#PROTECTED modifier bit}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   537
         */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   538
        public static final int PROTECTED = Modifier.PROTECTED;
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   539
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   540
        /** A single-bit mask representing {@code package} access (default access),
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   541
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   542
         *  The value is {@code 0x08}, which does not correspond meaningfully to
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   543
         *  any particular {@linkplain java.lang.reflect.Modifier modifier bit}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   544
         */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   545
        public static final int PACKAGE = Modifier.STATIC;
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   546
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   547
        private static final int ALL_MODES = (PUBLIC | PRIVATE | PROTECTED | PACKAGE);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   548
        private static final int TRUSTED   = -1;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   549
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   550
        private static int fixmods(int mods) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   551
            mods &= (ALL_MODES - PACKAGE);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   552
            return (mods != 0) ? mods : PACKAGE;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   553
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   554
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   555
        /** Tells which class is performing the lookup.  It is this class against
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   556
         *  which checks are performed for visibility and access permissions.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   557
         *  <p>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   558
         *  The class implies a maximum level of access permission,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   559
         *  but the permissions may be additionally limited by the bitmask
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   560
         *  {@link #lookupModes lookupModes}, which controls whether non-public members
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   561
         *  can be accessed.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   562
         *  @return the lookup class, on behalf of which this lookup object finds members
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   563
         */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   564
        public Class<?> lookupClass() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   565
            return lookupClass;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   566
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   567
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   568
        // This is just for calling out to MethodHandleImpl.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   569
        private Class<?> lookupClassOrNull() {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   570
            return (allowedModes == TRUSTED) ? null : lookupClass;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   571
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   572
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   573
        /** Tells which access-protection classes of members this lookup object can produce.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   574
         *  The result is a bit-mask of the bits
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   575
         *  {@linkplain #PUBLIC PUBLIC (0x01)},
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   576
         *  {@linkplain #PRIVATE PRIVATE (0x02)},
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   577
         *  {@linkplain #PROTECTED PROTECTED (0x04)},
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   578
         *  and {@linkplain #PACKAGE PACKAGE (0x08)}.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   579
         *  <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   580
         *  A freshly-created lookup object
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   581
         *  on the {@linkplain java.lang.invoke.MethodHandles#lookup() caller's class}
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   582
         *  has all possible bits set, since the caller class can access all its own members.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   583
         *  A lookup object on a new lookup class
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   584
         *  {@linkplain java.lang.invoke.MethodHandles.Lookup#in created from a previous lookup object}
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   585
         *  may have some mode bits set to zero.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   586
         *  The purpose of this is to restrict access via the new lookup object,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   587
         *  so that it can access only names which can be reached by the original
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   588
         *  lookup object, and also by the new lookup class.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   589
         *  @return the lookup modes, which limit the kinds of access performed by this lookup object
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   590
         */
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   591
        public int lookupModes() {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   592
            return allowedModes & ALL_MODES;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   593
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   594
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   595
        /** Embody the current class (the lookupClass) as a lookup class
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   596
         * for method handle creation.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   597
         * Must be called by from a method in this package,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   598
         * which in turn is called by a method not in this package.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   599
         */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
   600
        Lookup(Class<?> lookupClass) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
   601
            this(lookupClass, ALL_MODES);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   602
            // make sure we haven't accidentally picked up a privileged class:
20825
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   603
            checkUnprivilegedlookupClass(lookupClass, ALL_MODES);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   604
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   605
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   606
        private Lookup(Class<?> lookupClass, int allowedModes) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   607
            this.lookupClass = lookupClass;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   608
            this.allowedModes = allowedModes;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   609
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   610
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   611
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   612
         * Creates a lookup on the specified new lookup class.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   613
         * The resulting object will report the specified
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   614
         * class as its own {@link #lookupClass lookupClass}.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   615
         * <p>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   616
         * However, the resulting {@code Lookup} object is guaranteed
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   617
         * to have no more access capabilities than the original.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   618
         * In particular, access capabilities can be lost as follows:<ul>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   619
         * <li>If the new lookup class differs from the old one,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   620
         * protected members will not be accessible by virtue of inheritance.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   621
         * (Protected members may continue to be accessible because of package sharing.)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   622
         * <li>If the new lookup class is in a different package
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   623
         * than the old one, protected and default (package) members will not be accessible.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   624
         * <li>If the new lookup class is not within the same package member
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   625
         * as the old one, private members will not be accessible.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   626
         * <li>If the new lookup class is not accessible to the old lookup class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   627
         * then no members, not even public members, will be accessible.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   628
         * (In all other cases, public members will continue to be accessible.)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   629
         * </ul>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   630
         *
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   631
         * @param requestedLookupClass the desired lookup class for the new lookup object
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   632
         * @return a lookup object which reports the desired lookup class
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   633
         * @throws NullPointerException if the argument is null
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   634
         */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   635
        public Lookup in(Class<?> requestedLookupClass) {
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
   636
            Objects.requireNonNull(requestedLookupClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   637
            if (allowedModes == TRUSTED)  // IMPL_LOOKUP can make any lookup at all
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   638
                return new Lookup(requestedLookupClass, ALL_MODES);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   639
            if (requestedLookupClass == this.lookupClass)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   640
                return this;  // keep same capabilities
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   641
            int newModes = (allowedModes & (ALL_MODES & ~PROTECTED));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   642
            if ((newModes & PACKAGE) != 0
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   643
                && !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   644
                newModes &= ~(PACKAGE|PRIVATE);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   645
            }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   646
            // Allow nestmate lookups to be created without special privilege:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   647
            if ((newModes & PRIVATE) != 0
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   648
                && !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   649
                newModes &= ~PRIVATE;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   650
            }
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents: 11535
diff changeset
   651
            if ((newModes & PUBLIC) != 0
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents: 11535
diff changeset
   652
                && !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, allowedModes)) {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   653
                // The requested class it not accessible from the lookup class.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   654
                // No permissions.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   655
                newModes = 0;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   656
            }
20825
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   657
            checkUnprivilegedlookupClass(requestedLookupClass, newModes);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   658
            return new Lookup(requestedLookupClass, newModes);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   659
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   660
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   661
        // Make sure outer class is initialized first.
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
   662
        static { IMPL_NAMES.getClass(); }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   663
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   664
        /** Version of lookup which is trusted minimally.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   665
         *  It can only be used to create method handles to
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   666
         *  publicly accessible members.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   667
         */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   668
        static final Lookup PUBLIC_LOOKUP = new Lookup(Object.class, PUBLIC);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   669
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   670
        /** Package-private version of lookup which is trusted. */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   671
        static final Lookup IMPL_LOOKUP = new Lookup(Object.class, TRUSTED);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   672
20825
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   673
        private static void checkUnprivilegedlookupClass(Class<?> lookupClass, int allowedModes) {
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2763
diff changeset
   674
            String name = lookupClass.getName();
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   675
            if (name.startsWith("java.lang.invoke."))
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   676
                throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);
20825
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   677
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   678
            // For caller-sensitive MethodHandles.lookup()
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   679
            // disallow lookup more restricted packages
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   680
            if (allowedModes == ALL_MODES && lookupClass.getClassLoader() == null) {
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   681
                if (name.startsWith("java.") ||
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   682
                        (name.startsWith("sun.") && !name.startsWith("sun.invoke."))) {
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   683
                    throw newIllegalArgumentException("illegal lookupClass: " + lookupClass);
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   684
                }
3d5429b4b601 8017196: Ensure Proxies are handled appropriately
mchung
parents: 18569
diff changeset
   685
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   686
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   687
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   688
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   689
         * Displays the name of the class from which lookups are to be made.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   690
         * (The name is the one reported by {@link java.lang.Class#getName() Class.getName}.)
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   691
         * If there are restrictions on the access permitted to this lookup,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   692
         * this is indicated by adding a suffix to the class name, consisting
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   693
         * of a slash and a keyword.  The keyword represents the strongest
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   694
         * allowed access, and is chosen as follows:
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   695
         * <ul>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   696
         * <li>If no access is allowed, the suffix is "/noaccess".
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   697
         * <li>If only public access is allowed, the suffix is "/public".
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   698
         * <li>If only public and package access are allowed, the suffix is "/package".
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   699
         * <li>If only public, package, and private access are allowed, the suffix is "/private".
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   700
         * </ul>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   701
         * If none of the above cases apply, it is the case that full
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   702
         * access (public, package, private, and protected) is allowed.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   703
         * In this case, no suffix is added.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   704
         * This is true only of an object obtained originally from
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   705
         * {@link java.lang.invoke.MethodHandles#lookup MethodHandles.lookup}.
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   706
         * Objects created by {@link java.lang.invoke.MethodHandles.Lookup#in Lookup.in}
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   707
         * always have restricted access, and will display a suffix.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   708
         * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   709
         * (It may seem strange that protected access should be
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   710
         * stronger than private access.  Viewed independently from
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   711
         * package access, protected access is the first to be lost,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   712
         * because it requires a direct subclass relationship between
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   713
         * caller and callee.)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   714
         * @see #in
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   715
         */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   716
        @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   717
        public String toString() {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   718
            String cname = lookupClass.getName();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   719
            switch (allowedModes) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   720
            case 0:  // no privileges
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   721
                return cname + "/noaccess";
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   722
            case PUBLIC:
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   723
                return cname + "/public";
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   724
            case PUBLIC|PACKAGE:
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   725
                return cname + "/package";
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   726
            case ALL_MODES & ~PROTECTED:
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   727
                return cname + "/private";
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   728
            case ALL_MODES:
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   729
                return cname;
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   730
            case TRUSTED:
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   731
                return "/trusted";  // internal only; not exported
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   732
            default:  // Should not happen, but it's a bitfield...
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   733
                cname = cname + "/" + Integer.toHexString(allowedModes);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   734
                assert(false) : cname;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   735
                return cname;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   736
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   737
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   738
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   739
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   740
         * Produces a method handle for a static method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   741
         * The type of the method handle will be that of the method.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   742
         * (Since static methods do not take receivers, there is no
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   743
         * additional receiver argument inserted into the method handle type,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   744
         * as there would be with {@link #findVirtual findVirtual} or {@link #findSpecial findSpecial}.)
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   745
         * The method and all its argument types must be accessible to the lookup object.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   746
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   747
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   748
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   749
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
20532
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
   750
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
   751
         * If the returned method handle is invoked, the method's class will
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
   752
         * be initialized, if it has not already been initialized.
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
   753
         * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
   754
         * <blockquote><pre>{@code
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   755
import static java.lang.invoke.MethodHandles.*;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   756
import static java.lang.invoke.MethodType.*;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   757
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   758
MethodHandle MH_asList = publicLookup().findStatic(Arrays.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   759
  "asList", methodType(List.class, Object[].class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   760
assertEquals("[x, y]", MH_asList.invoke("x", "y").toString());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   761
         * }</pre></blockquote>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   762
         * @param refc the class from which the method is accessed
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   763
         * @param name the name of the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   764
         * @param type the type of the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   765
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   766
         * @throws NoSuchMethodException if the method does not exist
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   767
         * @throws IllegalAccessException if access checking fails,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   768
         *                                or if the method is not {@code static},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   769
         *                                or if the method's variable arity modifier bit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   770
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   771
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   772
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   773
         * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   774
         */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   775
        public
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   776
        MethodHandle findStatic(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   777
            MemberName method = resolveOrFail(REF_invokeStatic, refc, name, type);
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
   778
            return getDirectMethod(REF_invokeStatic, refc, method, findBoundCallerClass(method));
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
   779
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   780
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   781
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   782
         * Produces a method handle for a virtual method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   783
         * The type of the method handle will be that of the method,
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   784
         * with the receiver type (usually {@code refc}) prepended.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   785
         * The method and all its argument types must be accessible to the lookup object.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   786
         * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   787
         * When called, the handle will treat the first argument as a receiver
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   788
         * and dispatch on the receiver's type to determine which method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   789
         * implementation to enter.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   790
         * (The dispatching action is identical with that performed by an
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   791
         * {@code invokevirtual} or {@code invokeinterface} instruction.)
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   792
         * <p>
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   793
         * The first argument will be of type {@code refc} if the lookup
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   794
         * class has full privileges to access the member.  Otherwise
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   795
         * the member must be {@code protected} and the first argument
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   796
         * will be restricted in type to the lookup class.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   797
         * <p>
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   798
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   799
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   800
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   801
         * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   802
         * Because of the general <a href="MethodHandles.Lookup.html#equiv">equivalence</a> between {@code invokevirtual}
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   803
         * instructions and method handles produced by {@code findVirtual},
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   804
         * if the class is {@code MethodHandle} and the name string is
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
   805
         * {@code invokeExact} or {@code invoke}, the resulting
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   806
         * method handle is equivalent to one produced by
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   807
         * {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker} or
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
   808
         * {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker}
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   809
         * with the same {@code type} argument.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   810
         *
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   811
         * <b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
   812
         * <blockquote><pre>{@code
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   813
import static java.lang.invoke.MethodHandles.*;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   814
import static java.lang.invoke.MethodType.*;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   815
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   816
MethodHandle MH_concat = publicLookup().findVirtual(String.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   817
  "concat", methodType(String.class, String.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   818
MethodHandle MH_hashCode = publicLookup().findVirtual(Object.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   819
  "hashCode", methodType(int.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   820
MethodHandle MH_hashCode_String = publicLookup().findVirtual(String.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   821
  "hashCode", methodType(int.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   822
assertEquals("xy", (String) MH_concat.invokeExact("x", "y"));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   823
assertEquals("xy".hashCode(), (int) MH_hashCode.invokeExact((Object)"xy"));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   824
assertEquals("xy".hashCode(), (int) MH_hashCode_String.invokeExact("xy"));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   825
// interface method:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   826
MethodHandle MH_subSequence = publicLookup().findVirtual(CharSequence.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   827
  "subSequence", methodType(CharSequence.class, int.class, int.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   828
assertEquals("def", MH_subSequence.invoke("abcdefghi", 3, 6).toString());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   829
// constructor "internal method" must be accessed differently:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   830
MethodType MT_newString = methodType(void.class); //()V for new String()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   831
try { assertEquals("impossible", lookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   832
        .findVirtual(String.class, "<init>", MT_newString));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   833
 } catch (NoSuchMethodException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   834
MethodHandle MH_newString = publicLookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   835
  .findConstructor(String.class, MT_newString);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   836
assertEquals("", (String) MH_newString.invokeExact());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   837
         * }</pre></blockquote>
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   838
         *
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   839
         * @param refc the class or interface from which the method is accessed
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   840
         * @param name the name of the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   841
         * @param type the type of the method, with the receiver argument omitted
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   842
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   843
         * @throws NoSuchMethodException if the method does not exist
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   844
         * @throws IllegalAccessException if access checking fails,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   845
         *                                or if the method is {@code static}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   846
         *                                or if the method's variable arity modifier bit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   847
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   848
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   849
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   850
         * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   851
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   852
        public MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   853
            if (refc == MethodHandle.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   854
                MethodHandle mh = findVirtualForMH(name, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   855
                if (mh != null)  return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   856
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   857
            byte refKind = (refc.isInterface() ? REF_invokeInterface : REF_invokeVirtual);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   858
            MemberName method = resolveOrFail(refKind, refc, name, type);
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
   859
            return getDirectMethod(refKind, refc, method, findBoundCallerClass(method));
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
   860
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   861
        private MethodHandle findVirtualForMH(String name, MethodType type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   862
            // these names require special lookups because of the implicit MethodType argument
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   863
            if ("invoke".equals(name))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   864
                return invoker(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   865
            if ("invokeExact".equals(name))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   866
                return exactInvoker(type);
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   867
            if ("invokeBasic".equals(name))
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   868
                return basicInvoker(type);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   869
            assert(!MemberName.isMethodHandleInvokeName(name));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   870
            return null;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   871
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   872
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   873
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   874
         * Produces a method handle which creates an object and initializes it, using
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   875
         * the constructor of the specified type.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   876
         * The parameter types of the method handle will be those of the constructor,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   877
         * while the return type will be a reference to the constructor's class.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   878
         * The constructor and all its argument types must be accessible to the lookup object.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   879
         * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   880
         * The requested type must have a return type of {@code void}.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   881
         * (This is consistent with the JVM's treatment of constructor type descriptors.)
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   882
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   883
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   884
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   885
         * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
20532
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
   886
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
   887
         * If the returned method handle is invoked, the constructor's class will
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
   888
         * be initialized, if it has not already been initialized.
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   889
         * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
   890
         * <blockquote><pre>{@code
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   891
import static java.lang.invoke.MethodHandles.*;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   892
import static java.lang.invoke.MethodType.*;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   893
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   894
MethodHandle MH_newArrayList = publicLookup().findConstructor(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   895
  ArrayList.class, methodType(void.class, Collection.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   896
Collection orig = Arrays.asList("x", "y");
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   897
Collection copy = (ArrayList) MH_newArrayList.invokeExact(orig);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   898
assert(orig != copy);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   899
assertEquals(orig, copy);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   900
// a variable-arity constructor:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   901
MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   902
  ProcessBuilder.class, methodType(void.class, String[].class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   903
ProcessBuilder pb = (ProcessBuilder)
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   904
  MH_newProcessBuilder.invoke("x", "y", "z");
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   905
assertEquals("[x, y, z]", pb.command().toString());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   906
         * }</pre></blockquote>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   907
         * @param refc the class or interface from which the method is accessed
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   908
         * @param type the type of the method, with the receiver argument omitted, and a void return type
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   909
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   910
         * @throws NoSuchMethodException if the constructor does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   911
         * @throws IllegalAccessException if access checking fails
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   912
         *                                or if the method's variable arity modifier bit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   913
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   914
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   915
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   916
         * @throws NullPointerException if any argument is null
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   917
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   918
        public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   919
            String name = "<init>";
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   920
            MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   921
            return getDirectConstructor(refc, ctor);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   922
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   923
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   924
        /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   925
         * Produces an early-bound method handle for a virtual method.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   926
         * It will bypass checks for overriding methods on the receiver,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   927
         * <a href="MethodHandles.Lookup.html#equiv">as if called</a> from an {@code invokespecial}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   928
         * instruction from within the explicitly specified {@code specialCaller}.
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
   929
         * The type of the method handle will be that of the method,
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   930
         * with a suitably restricted receiver type prepended.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   931
         * (The receiver type will be {@code specialCaller} or a subtype.)
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
   932
         * The method and all its argument types must be accessible
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   933
         * to the lookup object.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   934
         * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   935
         * Before method resolution,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   936
         * if the explicitly specified caller class is not identical with the
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   937
         * lookup class, or if this lookup object does not have
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   938
         * <a href="MethodHandles.Lookup.html#privacc">private access</a>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   939
         * privileges, the access fails.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   940
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   941
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   942
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   943
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   944
         * <p style="font-size:smaller;">
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   945
         * <em>(Note:  JVM internal methods named {@code "<init>"} are not visible to this API,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   946
         * even though the {@code invokespecial} instruction can refer to them
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   947
         * in special circumstances.  Use {@link #findConstructor findConstructor}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   948
         * to access instance initialization methods in a safe manner.)</em>
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   949
         * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
   950
         * <blockquote><pre>{@code
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   951
import static java.lang.invoke.MethodHandles.*;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   952
import static java.lang.invoke.MethodType.*;
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   953
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   954
static class Listie extends ArrayList {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   955
  public String toString() { return "[wee Listie]"; }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   956
  static Lookup lookup() { return MethodHandles.lookup(); }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   957
}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   958
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   959
// no access to constructor via invokeSpecial:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   960
MethodHandle MH_newListie = Listie.lookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   961
  .findConstructor(Listie.class, methodType(void.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   962
Listie l = (Listie) MH_newListie.invokeExact();
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   963
try { assertEquals("impossible", Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   964
        Listie.class, "<init>", methodType(void.class), Listie.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   965
 } catch (NoSuchMethodException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   966
// access to super and self methods via invokeSpecial:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   967
MethodHandle MH_super = Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   968
  ArrayList.class, "toString" , methodType(String.class), Listie.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   969
MethodHandle MH_this = Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   970
  Listie.class, "toString" , methodType(String.class), Listie.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   971
MethodHandle MH_duper = Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   972
  Object.class, "toString" , methodType(String.class), Listie.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   973
assertEquals("[]", (String) MH_super.invokeExact(l));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   974
assertEquals(""+l, (String) MH_this.invokeExact(l));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   975
assertEquals("[]", (String) MH_duper.invokeExact(l)); // ArrayList method
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   976
try { assertEquals("inaccessible", Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   977
        String.class, "toString", methodType(String.class), Listie.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   978
 } catch (IllegalAccessException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   979
Listie subl = new Listie() { public String toString() { return "[subclass]"; } };
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   980
assertEquals(""+l, (String) MH_this.invokeExact(subl)); // Listie method
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   981
         * }</pre></blockquote>
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   982
         *
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   983
         * @param refc the class or interface from which the method is accessed
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   984
         * @param name the name of the method (which must not be "&lt;init&gt;")
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   985
         * @param type the type of the method, with the receiver argument omitted
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   986
         * @param specialCaller the proposed calling class to perform the {@code invokespecial}
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   987
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   988
         * @throws NoSuchMethodException if the method does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   989
         * @throws IllegalAccessException if access checking fails
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   990
         *                                or if the method's variable arity modifier bit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   991
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   992
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
   993
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   994
         * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   995
         */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   996
        public MethodHandle findSpecial(Class<?> refc, String name, MethodType type,
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   997
                                        Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   998
            checkSpecialCaller(specialCaller);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   999
            Lookup specialLookup = this.in(specialCaller);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1000
            MemberName method = specialLookup.resolveOrFail(REF_invokeSpecial, refc, name, type);
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1001
            return specialLookup.getDirectMethod(REF_invokeSpecial, refc, method, findBoundCallerClass(method));
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1002
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1003
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1004
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1005
         * Produces a method handle giving read access to a non-static field.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1006
         * The type of the method handle will have a return type of the field's
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1007
         * value type.
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1008
         * The method handle's single argument will be the instance containing
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1009
         * the field.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1010
         * Access checking is performed immediately on behalf of the lookup class.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1011
         * @param refc the class or interface from which the method is accessed
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1012
         * @param name the field's name
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1013
         * @param type the field's type
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1014
         * @return a method handle which can load values from the field
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1015
         * @throws NoSuchFieldException if the field does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1016
         * @throws IllegalAccessException if access checking fails, or if the field is {@code static}
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1017
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1018
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1019
         * @throws NullPointerException if any argument is null
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1020
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1021
        public MethodHandle findGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1022
            MemberName field = resolveOrFail(REF_getField, refc, name, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1023
            return getDirectField(REF_getField, refc, field);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1024
        }
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1025
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1026
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1027
         * Produces a method handle giving write access to a non-static field.
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1028
         * The type of the method handle will have a void return type.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1029
         * The method handle will take two arguments, the instance containing
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1030
         * the field, and the value to be stored.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1031
         * The second argument will be of the field's value type.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1032
         * Access checking is performed immediately on behalf of the lookup class.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1033
         * @param refc the class or interface from which the method is accessed
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1034
         * @param name the field's name
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1035
         * @param type the field's type
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1036
         * @return a method handle which can store values into the field
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1037
         * @throws NoSuchFieldException if the field does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1038
         * @throws IllegalAccessException if access checking fails, or if the field is {@code static}
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1039
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1040
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1041
         * @throws NullPointerException if any argument is null
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1042
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1043
        public MethodHandle findSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1044
            MemberName field = resolveOrFail(REF_putField, refc, name, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1045
            return getDirectField(REF_putField, refc, field);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1046
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1047
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1048
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1049
         * Produces a method handle giving read access to a static field.
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1050
         * The type of the method handle will have a return type of the field's
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1051
         * value type.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1052
         * The method handle will take no arguments.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1053
         * Access checking is performed immediately on behalf of the lookup class.
20532
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1054
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1055
         * If the returned method handle is invoked, the field's class will
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1056
         * be initialized, if it has not already been initialized.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1057
         * @param refc the class or interface from which the method is accessed
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1058
         * @param name the field's name
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1059
         * @param type the field's type
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1060
         * @return a method handle which can load values from the field
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1061
         * @throws NoSuchFieldException if the field does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1062
         * @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1063
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1064
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1065
         * @throws NullPointerException if any argument is null
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1066
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1067
        public MethodHandle findStaticGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1068
            MemberName field = resolveOrFail(REF_getStatic, refc, name, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1069
            return getDirectField(REF_getStatic, refc, field);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1070
        }
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1071
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1072
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1073
         * Produces a method handle giving write access to a static field.
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1074
         * The type of the method handle will have a void return type.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1075
         * The method handle will take a single
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1076
         * argument, of the field's value type, the value to be stored.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1077
         * Access checking is performed immediately on behalf of the lookup class.
20532
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1078
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1079
         * If the returned method handle is invoked, the field's class will
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1080
         * be initialized, if it has not already been initialized.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1081
         * @param refc the class or interface from which the method is accessed
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1082
         * @param name the field's name
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1083
         * @param type the field's type
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1084
         * @return a method handle which can store values into the field
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1085
         * @throws NoSuchFieldException if the field does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1086
         * @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1087
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1088
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1089
         * @throws NullPointerException if any argument is null
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  1090
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1091
        public MethodHandle findStaticSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1092
            MemberName field = resolveOrFail(REF_putStatic, refc, name, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1093
            return getDirectField(REF_putStatic, refc, field);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1094
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1095
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1096
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1097
         * Produces an early-bound method handle for a non-static method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1098
         * The receiver must have a supertype {@code defc} in which a method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1099
         * of the given name and type is accessible to the lookup class.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1100
         * The method and all its argument types must be accessible to the lookup object.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1101
         * The type of the method handle will be that of the method,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1102
         * without any insertion of an additional receiver parameter.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1103
         * The given receiver will be bound into the method handle,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1104
         * so that every call to the method handle will invoke the
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1105
         * requested method on the given receiver.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1106
         * <p>
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1107
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1108
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1109
         * the method's variable arity modifier bit ({@code 0x0080}) is set
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1110
         * <em>and</em> the trailing array argument is not the only argument.
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1111
         * (If the trailing array argument is the only argument,
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1112
         * the given receiver value will be bound to it.)
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1113
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1114
         * This is equivalent to the following code:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1115
         * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1116
import static java.lang.invoke.MethodHandles.*;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1117
import static java.lang.invoke.MethodType.*;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1118
...
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1119
MethodHandle mh0 = lookup().findVirtual(defc, name, type);
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1120
MethodHandle mh1 = mh0.bindTo(receiver);
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1121
MethodType mt1 = mh1.type();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1122
if (mh0.isVarargsCollector())
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1123
  mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1124
return mh1;
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1125
         * }</pre></blockquote>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1126
         * where {@code defc} is either {@code receiver.getClass()} or a super
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1127
         * type of that class, in which the requested method is accessible
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1128
         * to the lookup class.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1129
         * (Note that {@code bindTo} does not preserve variable arity.)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1130
         * @param receiver the object from which the method is accessed
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1131
         * @param name the name of the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1132
         * @param type the type of the method, with the receiver argument omitted
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1133
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1134
         * @throws NoSuchMethodException if the method does not exist
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1135
         * @throws IllegalAccessException if access checking fails
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1136
         *                                or if the method's variable arity modifier bit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1137
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1138
         * @exception SecurityException if a security manager is present and it
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1139
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1140
         * @throws NullPointerException if any argument is null
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1141
         * @see MethodHandle#bindTo
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1142
         * @see #findVirtual
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1143
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1144
        public MethodHandle bind(Object receiver, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1145
            Class<? extends Object> refc = receiver.getClass(); // may get NPE
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1146
            MemberName method = resolveOrFail(REF_invokeSpecial, refc, name, type);
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1147
            MethodHandle mh = getDirectMethodNoRestrict(REF_invokeSpecial, refc, method, findBoundCallerClass(method));
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  1148
            return mh.bindArgumentL(0, receiver).setVarargs(method);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1149
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1150
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1151
        /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1152
         * Makes a <a href="MethodHandleInfo.html#directmh">direct method handle</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1153
         * to <i>m</i>, if the lookup class has permission.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1154
         * If <i>m</i> is non-static, the receiver argument is treated as an initial argument.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1155
         * If <i>m</i> is virtual, overriding is respected on every call.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1156
         * Unlike the Core Reflection API, exceptions are <em>not</em> wrapped.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1157
         * The type of the method handle will be that of the method,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1158
         * with the receiver type prepended (but only if it is non-static).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1159
         * If the method's {@code accessible} flag is not set,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1160
         * access checking is performed immediately on behalf of the lookup class.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1161
         * If <i>m</i> is not public, do not share the resulting handle with untrusted parties.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1162
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1163
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1164
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1165
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
20532
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1166
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1167
         * If <i>m</i> is static, and
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1168
         * if the returned method handle is invoked, the method's class will
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1169
         * be initialized, if it has not already been initialized.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1170
         * @param m the reflected method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1171
         * @return a method handle which can invoke the reflected method
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1172
         * @throws IllegalAccessException if access checking fails
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1173
         *                                or if the method's variable arity modifier bit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1174
         *                                is set and {@code asVarargsCollector} fails
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1175
         * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1176
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1177
        public MethodHandle unreflect(Method m) throws IllegalAccessException {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1178
            if (m.getDeclaringClass() == MethodHandle.class) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1179
                MethodHandle mh = unreflectForMH(m);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1180
                if (mh != null)  return mh;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1181
            }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1182
            MemberName method = new MemberName(m);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1183
            byte refKind = method.getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1184
            if (refKind == REF_invokeSpecial)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1185
                refKind = REF_invokeVirtual;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1186
            assert(method.isMethod());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1187
            Lookup lookup = m.isAccessible() ? IMPL_LOOKUP : this;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1188
            return lookup.getDirectMethodNoSecurityManager(refKind, method.getDeclaringClass(), method, findBoundCallerClass(method));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1189
        }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1190
        private MethodHandle unreflectForMH(Method m) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1191
            // these names require special lookups because they throw UnsupportedOperationException
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1192
            if (MemberName.isMethodHandleInvokeName(m.getName()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1193
                return MethodHandleImpl.fakeMethodHandleInvoke(new MemberName(m));
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1194
            return null;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1195
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1196
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1197
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1198
         * Produces a method handle for a reflected method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1199
         * It will bypass checks for overriding methods on the receiver,
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1200
         * <a href="MethodHandles.Lookup.html#equiv">as if called</a> from an {@code invokespecial}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1201
         * instruction from within the explicitly specified {@code specialCaller}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1202
         * The type of the method handle will be that of the method,
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1203
         * with a suitably restricted receiver type prepended.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1204
         * (The receiver type will be {@code specialCaller} or a subtype.)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1205
         * If the method's {@code accessible} flag is not set,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1206
         * access checking is performed immediately on behalf of the lookup class,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1207
         * as if {@code invokespecial} instruction were being linked.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1208
         * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1209
         * Before method resolution,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1210
         * if the explicitly specified caller class is not identical with the
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1211
         * lookup class, or if this lookup object does not have
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1212
         * <a href="MethodHandles.Lookup.html#privacc">private access</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1213
         * privileges, the access fails.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1214
         * <p>
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1215
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1216
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1217
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1218
         * @param m the reflected method
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1219
         * @param specialCaller the class nominally calling the method
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1220
         * @return a method handle which can invoke the reflected method
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1221
         * @throws IllegalAccessException if access checking fails
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1222
         *                                or if the method's variable arity modifier bit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1223
         *                                is set and {@code asVarargsCollector} fails
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1224
         * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1225
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1226
        public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1227
            checkSpecialCaller(specialCaller);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1228
            Lookup specialLookup = this.in(specialCaller);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1229
            MemberName method = new MemberName(m, true);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1230
            assert(method.isMethod());
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1231
            // ignore m.isAccessible:  this is a new kind of access
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1232
            return specialLookup.getDirectMethodNoSecurityManager(REF_invokeSpecial, method.getDeclaringClass(), method, findBoundCallerClass(method));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1233
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1234
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1235
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1236
         * Produces a method handle for a reflected constructor.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1237
         * The type of the method handle will be that of the constructor,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1238
         * with the return type changed to the declaring class.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1239
         * The method handle will perform a {@code newInstance} operation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1240
         * creating a new instance of the constructor's class on the
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1241
         * arguments passed to the method handle.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1242
         * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1243
         * If the constructor's {@code accessible} flag is not set,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1244
         * access checking is performed immediately on behalf of the lookup class.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1245
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1246
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1247
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1248
         * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
20532
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1249
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1250
         * If the returned method handle is invoked, the constructor's class will
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1251
         * be initialized, if it has not already been initialized.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1252
         * @param c the reflected constructor
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1253
         * @return a method handle which can invoke the reflected constructor
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1254
         * @throws IllegalAccessException if access checking fails
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1255
         *                                or if the method's variable arity modifier bit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1256
         *                                is set and {@code asVarargsCollector} fails
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1257
         * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1258
         */
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1259
        public MethodHandle unreflectConstructor(Constructor<?> c) throws IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1260
            MemberName ctor = new MemberName(c);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1261
            assert(ctor.isConstructor());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1262
            Lookup lookup = c.isAccessible() ? IMPL_LOOKUP : this;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1263
            return lookup.getDirectConstructorNoSecurityManager(ctor.getDeclaringClass(), ctor);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1264
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1265
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1266
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1267
         * Produces a method handle giving read access to a reflected field.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1268
         * The type of the method handle will have a return type of the field's
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1269
         * value type.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1270
         * If the field is static, the method handle will take no arguments.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1271
         * Otherwise, its single argument will be the instance containing
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1272
         * the field.
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  1273
         * If the field's {@code accessible} flag is not set,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1274
         * access checking is performed immediately on behalf of the lookup class.
20532
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1275
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1276
         * If the field is static, and
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1277
         * if the returned method handle is invoked, the field's class will
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1278
         * be initialized, if it has not already been initialized.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1279
         * @param f the reflected field
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1280
         * @return a method handle which can load values from the reflected field
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1281
         * @throws IllegalAccessException if access checking fails
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1282
         * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1283
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1284
        public MethodHandle unreflectGetter(Field f) throws IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1285
            return unreflectField(f, false);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1286
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1287
        private MethodHandle unreflectField(Field f, boolean isSetter) throws IllegalAccessException {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1288
            MemberName field = new MemberName(f, isSetter);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1289
            assert(isSetter
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1290
                    ? MethodHandleNatives.refKindIsSetter(field.getReferenceKind())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1291
                    : MethodHandleNatives.refKindIsGetter(field.getReferenceKind()));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1292
            Lookup lookup = f.isAccessible() ? IMPL_LOOKUP : this;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1293
            return lookup.getDirectFieldNoSecurityManager(field.getReferenceKind(), f.getDeclaringClass(), field);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1294
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1295
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1296
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1297
         * Produces a method handle giving write access to a reflected field.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1298
         * The type of the method handle will have a void return type.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1299
         * If the field is static, the method handle will take a single
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1300
         * argument, of the field's value type, the value to be stored.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1301
         * Otherwise, the two arguments will be the instance containing
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1302
         * the field, and the value to be stored.
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  1303
         * If the field's {@code accessible} flag is not set,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1304
         * access checking is performed immediately on behalf of the lookup class.
20532
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1305
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1306
         * If the field is static, and
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1307
         * if the returned method handle is invoked, the field's class will
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1308
         * be initialized, if it has not already been initialized.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1309
         * @param f the reflected field
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1310
         * @return a method handle which can store values into the reflected field
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1311
         * @throws IllegalAccessException if access checking fails
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1312
         * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1313
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1314
        public MethodHandle unreflectSetter(Field f) throws IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1315
            return unreflectField(f, true);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1316
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1317
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1318
        /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1319
         * Cracks a <a href="MethodHandleInfo.html#directmh">direct method handle</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1320
         * created by this lookup object or a similar one.
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1321
         * Security and access checks are performed to ensure that this lookup object
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1322
         * is capable of reproducing the target method handle.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1323
         * This means that the cracking may fail if target is a direct method handle
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1324
         * but was created by an unrelated lookup object.
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1325
         * This can happen if the method handle is <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a>
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1326
         * and was created by a lookup object for a different class.
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1327
         * @param target a direct method handle to crack into symbolic reference components
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1328
         * @return a symbolic reference which can be used to reconstruct this method handle from this lookup object
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1329
         * @exception SecurityException if a security manager is present and it
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1330
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1331
         * @throws IllegalArgumentException if the target is not a direct method handle or if access checking fails
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1332
         * @exception NullPointerException if the target is {@code null}
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1333
         * @see MethodHandleInfo
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1334
         * @since 1.8
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1335
         */
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1336
        public MethodHandleInfo revealDirect(MethodHandle target) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1337
            MemberName member = target.internalMemberName();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1338
            if (member == null || (!member.isResolved() && !member.isMethodHandleInvoke()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1339
                throw newIllegalArgumentException("not a direct method handle");
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1340
            Class<?> defc = member.getDeclaringClass();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1341
            byte refKind = member.getReferenceKind();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1342
            assert(MethodHandleNatives.refKindIsValid(refKind));
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1343
            if (refKind == REF_invokeSpecial && !target.isInvokeSpecial())
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1344
                // Devirtualized method invocation is usually formally virtual.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1345
                // To avoid creating extra MemberName objects for this common case,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1346
                // we encode this extra degree of freedom using MH.isInvokeSpecial.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1347
                refKind = REF_invokeVirtual;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1348
            if (refKind == REF_invokeVirtual && defc.isInterface())
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1349
                // Symbolic reference is through interface but resolves to Object method (toString, etc.)
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1350
                refKind = REF_invokeInterface;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1351
            // Check SM permissions and member access before cracking.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1352
            try {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1353
                checkAccess(refKind, defc, member);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1354
                checkSecurityManager(defc, member);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1355
            } catch (IllegalAccessException ex) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1356
                throw new IllegalArgumentException(ex);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1357
            }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1358
            if (allowedModes != TRUSTED && member.isCallerSensitive()) {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1359
                Class<?> callerClass = target.internalCallerClass();
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1360
                if (!hasPrivateAccess() || callerClass != lookupClass())
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1361
                    throw new IllegalArgumentException("method handle is caller sensitive: "+callerClass);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1362
            }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1363
            // Produce the handle to the results.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1364
            return new InfoFromMemberName(this, member, refKind);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1365
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1366
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1367
        /// Helper methods, all package-private.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1368
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1369
        MemberName resolveOrFail(byte refKind, Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1370
            checkSymbolicClass(refc);  // do this before attempting to resolve
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  1371
            Objects.requireNonNull(name);
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  1372
            Objects.requireNonNull(type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1373
            return IMPL_NAMES.resolveOrFail(refKind, new MemberName(refc, name, type, refKind), lookupClassOrNull(),
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1374
                                            NoSuchFieldException.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1375
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1376
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1377
        MemberName resolveOrFail(byte refKind, Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1378
            checkSymbolicClass(refc);  // do this before attempting to resolve
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  1379
            Objects.requireNonNull(name);
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  1380
            Objects.requireNonNull(type);
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1381
            checkMethodName(refKind, name);  // NPE check on name
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1382
            return IMPL_NAMES.resolveOrFail(refKind, new MemberName(refc, name, type, refKind), lookupClassOrNull(),
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1383
                                            NoSuchMethodException.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1384
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1385
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1386
        MemberName resolveOrFail(byte refKind, MemberName member) throws ReflectiveOperationException {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1387
            checkSymbolicClass(member.getDeclaringClass());  // do this before attempting to resolve
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  1388
            Objects.requireNonNull(member.getName());
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  1389
            Objects.requireNonNull(member.getType());
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1390
            return IMPL_NAMES.resolveOrFail(refKind, member, lookupClassOrNull(),
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1391
                                            ReflectiveOperationException.class);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1392
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1393
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1394
        void checkSymbolicClass(Class<?> refc) throws IllegalAccessException {
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  1395
            Objects.requireNonNull(refc);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1396
            Class<?> caller = lookupClassOrNull();
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents: 11535
diff changeset
  1397
            if (caller != null && !VerifyAccess.isClassAccessible(refc, caller, allowedModes))
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  1398
                throw new MemberName(refc).makeAccessException("symbolic reference class is not public", this);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1399
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1400
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1401
        /** Check name for an illegal leading "&lt;" character. */
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1402
        void checkMethodName(byte refKind, String name) throws NoSuchMethodException {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1403
            if (name.startsWith("<") && refKind != REF_newInvokeSpecial)
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1404
                throw new NoSuchMethodException("illegal method name: "+name);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1405
        }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1406
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1407
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1408
        /**
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1409
         * Find my trustable caller class if m is a caller sensitive method.
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1410
         * If this lookup object has private access, then the caller class is the lookupClass.
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1411
         * Otherwise, if m is caller-sensitive, throw IllegalAccessException.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1412
         */
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1413
        Class<?> findBoundCallerClass(MemberName m) throws IllegalAccessException {
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1414
            Class<?> callerClass = null;
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1415
            if (MethodHandleNatives.isCallerSensitive(m)) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1416
                // Only lookups with private access are allowed to resolve caller-sensitive methods
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1417
                if (hasPrivateAccess()) {
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1418
                    callerClass = lookupClass;
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1419
                } else {
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1420
                    throw new IllegalAccessException("Attempt to lookup caller-sensitive method using restricted lookup object");
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1421
                }
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1422
            }
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1423
            return callerClass;
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1424
        }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  1425
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1426
        private boolean hasPrivateAccess() {
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1427
            return (allowedModes & PRIVATE) != 0;
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1428
        }
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1429
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  1430
        /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1431
         * Perform necessary <a href="MethodHandles.Lookup.html#secmgr">access checks</a>.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1432
         * Determines a trustable caller class to compare with refc, the symbolic reference class.
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1433
         * If this lookup object has private access, then the caller class is the lookupClass.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1434
         */
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1435
        void checkSecurityManager(Class<?> refc, MemberName m) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1436
            SecurityManager smgr = System.getSecurityManager();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1437
            if (smgr == null)  return;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1438
            if (allowedModes == TRUSTED)  return;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  1439
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1440
            // Step 1:
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1441
            boolean fullPowerLookup = hasPrivateAccess();
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1442
            if (!fullPowerLookup ||
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1443
                !VerifyAccess.classLoaderIsAncestor(lookupClass, refc)) {
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1444
                ReflectUtil.checkPackageAccess(refc);
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1445
            }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  1446
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  1447
            // Step 2:
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1448
            if (m.isPublic()) return;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1449
            if (!fullPowerLookup) {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1450
                smgr.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  1451
            }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  1452
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  1453
            // Step 3:
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1454
            Class<?> defc = m.getDeclaringClass();
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1455
            if (!fullPowerLookup && defc != refc) {
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1456
                ReflectUtil.checkPackageAccess(defc);
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1457
            }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1458
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1459
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1460
        void checkMethod(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1461
            boolean wantStatic = (refKind == REF_invokeStatic);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1462
            String message;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1463
            if (m.isConstructor())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1464
                message = "expected a method, not a constructor";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1465
            else if (!m.isMethod())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1466
                message = "expected a method";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1467
            else if (wantStatic != m.isStatic())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1468
                message = wantStatic ? "expected a static method" : "expected a non-static method";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1469
            else
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1470
                { checkAccess(refKind, refc, m); return; }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  1471
            throw m.makeAccessException(message, this);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1472
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1473
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1474
        void checkField(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1475
            boolean wantStatic = !MethodHandleNatives.refKindHasReceiver(refKind);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1476
            String message;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1477
            if (wantStatic != m.isStatic())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1478
                message = wantStatic ? "expected a static field" : "expected a non-static field";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1479
            else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1480
                { checkAccess(refKind, refc, m); return; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1481
            throw m.makeAccessException(message, this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1482
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1483
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1484
        /** Check public/protected/private bits on the symbolic reference class and its member. */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1485
        void checkAccess(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1486
            assert(m.referenceKindIsConsistentWith(refKind) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1487
                   MethodHandleNatives.refKindIsValid(refKind) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1488
                   (MethodHandleNatives.refKindIsField(refKind) == m.isField()));
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1489
            int allowedModes = this.allowedModes;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1490
            if (allowedModes == TRUSTED)  return;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1491
            int mods = m.getModifiers();
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1492
            if (Modifier.isProtected(mods) &&
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1493
                    refKind == REF_invokeVirtual &&
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1494
                    m.getDeclaringClass() == Object.class &&
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1495
                    m.getName().equals("clone") &&
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1496
                    refc.isArray()) {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1497
                // The JVM does this hack also.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1498
                // (See ClassVerifier::verify_invoke_instructions
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1499
                // and LinkResolver::check_method_accessability.)
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1500
                // Because the JVM does not allow separate methods on array types,
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1501
                // there is no separate method for int[].clone.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1502
                // All arrays simply inherit Object.clone.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1503
                // But for access checking logic, we make Object.clone
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1504
                // (normally protected) appear to be public.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1505
                // Later on, when the DirectMethodHandle is created,
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1506
                // its leading argument will be restricted to the
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1507
                // requested array type.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1508
                // N.B. The return type is not adjusted, because
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1509
                // that is *not* the bytecode behavior.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1510
                mods ^= Modifier.PROTECTED | Modifier.PUBLIC;
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  1511
            }
25537
087de200d457 8035788: Provide more consistency for lookups
twisti
parents: 24860
diff changeset
  1512
            if (Modifier.isProtected(mods) && refKind == REF_newInvokeSpecial) {
087de200d457 8035788: Provide more consistency for lookups
twisti
parents: 24860
diff changeset
  1513
                // cannot "new" a protected ctor in a different package
087de200d457 8035788: Provide more consistency for lookups
twisti
parents: 24860
diff changeset
  1514
                mods ^= Modifier.PROTECTED;
087de200d457 8035788: Provide more consistency for lookups
twisti
parents: 24860
diff changeset
  1515
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1516
            if (Modifier.isFinal(mods) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1517
                    MethodHandleNatives.refKindIsSetter(refKind))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1518
                throw m.makeAccessException("unexpected set of a final field", this);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1519
            if (Modifier.isPublic(mods) && Modifier.isPublic(refc.getModifiers()) && allowedModes != 0)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1520
                return;  // common case
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1521
            int requestedModes = fixmods(mods);  // adjust 0 => PACKAGE
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1522
            if ((requestedModes & allowedModes) != 0) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1523
                if (VerifyAccess.isMemberAccessible(refc, m.getDeclaringClass(),
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1524
                                                    mods, lookupClass(), allowedModes))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1525
                    return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1526
            } else {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1527
                // Protected members can also be checked as if they were package-private.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1528
                if ((requestedModes & PROTECTED) != 0 && (allowedModes & PACKAGE) != 0
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1529
                        && VerifyAccess.isSamePackage(m.getDeclaringClass(), lookupClass()))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1530
                    return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1531
            }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  1532
            throw m.makeAccessException(accessFailedMessage(refc, m), this);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1533
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1534
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1535
        String accessFailedMessage(Class<?> refc, MemberName m) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1536
            Class<?> defc = m.getDeclaringClass();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1537
            int mods = m.getModifiers();
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1538
            // check the class first:
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1539
            boolean classOK = (Modifier.isPublic(defc.getModifiers()) &&
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1540
                               (defc == refc ||
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1541
                                Modifier.isPublic(refc.getModifiers())));
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1542
            if (!classOK && (allowedModes & PACKAGE) != 0) {
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents: 11535
diff changeset
  1543
                classOK = (VerifyAccess.isClassAccessible(defc, lookupClass(), ALL_MODES) &&
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1544
                           (defc == refc ||
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents: 11535
diff changeset
  1545
                            VerifyAccess.isClassAccessible(refc, lookupClass(), ALL_MODES)));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1546
            }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1547
            if (!classOK)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1548
                return "class is not public";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1549
            if (Modifier.isPublic(mods))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1550
                return "access to public member failed";  // (how?)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1551
            if (Modifier.isPrivate(mods))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1552
                return "member is private";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1553
            if (Modifier.isProtected(mods))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1554
                return "member is protected";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1555
            return "member is private to package";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1556
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1557
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1558
        private static final boolean ALLOW_NESTMATE_ACCESS = false;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1559
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1560
        private void checkSpecialCaller(Class<?> specialCaller) throws IllegalAccessException {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1561
            int allowedModes = this.allowedModes;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1562
            if (allowedModes == TRUSTED)  return;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1563
            if (!hasPrivateAccess()
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1564
                || (specialCaller != lookupClass()
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1565
                    && !(ALLOW_NESTMATE_ACCESS &&
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1566
                         VerifyAccess.isSamePackageMember(specialCaller, lookupClass()))))
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  1567
                throw new MemberName(specialCaller).
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  1568
                    makeAccessException("no private access for invokespecial", this);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1569
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1570
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1571
        private boolean restrictProtectedReceiver(MemberName method) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1572
            // The accessing class only has the right to use a protected member
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1573
            // on itself or a subclass.  Enforce that restriction, from JVMS 5.4.4, etc.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1574
            if (!method.isProtected() || method.isStatic()
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1575
                || allowedModes == TRUSTED
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1576
                || method.getDeclaringClass() == lookupClass()
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9730
diff changeset
  1577
                || VerifyAccess.isSamePackage(method.getDeclaringClass(), lookupClass())
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1578
                || (ALLOW_NESTMATE_ACCESS &&
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1579
                    VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass())))
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1580
                return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1581
            return true;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1582
        }
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1583
        private MethodHandle restrictReceiver(MemberName method, DirectMethodHandle mh, Class<?> caller) throws IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1584
            assert(!method.isStatic());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1585
            // receiver type of mh is too wide; narrow to caller
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1586
            if (!method.getDeclaringClass().isAssignableFrom(caller)) {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  1587
                throw method.makeAccessException("caller class must be a subclass below the method", caller);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1588
            }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1589
            MethodType rawType = mh.type();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1590
            if (rawType.parameterType(0) == caller)  return mh;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1591
            MethodType narrowType = rawType.changeParameterType(0, caller);
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1592
            assert(!mh.isVarargsCollector());  // viewAsType will lose varargs-ness
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1593
            assert(mh.viewAsTypeChecks(narrowType, true));
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1594
            return mh.copyWith(narrowType, mh.form);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1595
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1596
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1597
        /** Check access and get the requested method. */
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1598
        private MethodHandle getDirectMethod(byte refKind, Class<?> refc, MemberName method, Class<?> callerClass) throws IllegalAccessException {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1599
            final boolean doRestrict    = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1600
            final boolean checkSecurity = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1601
            return getDirectMethodCommon(refKind, refc, method, checkSecurity, doRestrict, callerClass);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1602
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1603
        /** Check access and get the requested method, eliding receiver narrowing rules. */
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1604
        private MethodHandle getDirectMethodNoRestrict(byte refKind, Class<?> refc, MemberName method, Class<?> callerClass) throws IllegalAccessException {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1605
            final boolean doRestrict    = false;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1606
            final boolean checkSecurity = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1607
            return getDirectMethodCommon(refKind, refc, method, checkSecurity, doRestrict, callerClass);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1608
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1609
        /** Check access and get the requested method, eliding security manager checks. */
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1610
        private MethodHandle getDirectMethodNoSecurityManager(byte refKind, Class<?> refc, MemberName method, Class<?> callerClass) throws IllegalAccessException {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1611
            final boolean doRestrict    = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1612
            final boolean checkSecurity = false;  // not needed for reflection or for linking CONSTANT_MH constants
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1613
            return getDirectMethodCommon(refKind, refc, method, checkSecurity, doRestrict, callerClass);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1614
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1615
        /** Common code for all methods; do not call directly except from immediately above. */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1616
        private MethodHandle getDirectMethodCommon(byte refKind, Class<?> refc, MemberName method,
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1617
                                                   boolean checkSecurity,
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1618
                                                   boolean doRestrict, Class<?> callerClass) throws IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1619
            checkMethod(refKind, refc, method);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1620
            // Optionally check with the security manager; this isn't needed for unreflect* calls.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1621
            if (checkSecurity)
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1622
                checkSecurityManager(refc, method);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1623
            assert(!method.isMethodHandleInvoke());
16898
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1624
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1625
            if (refKind == REF_invokeSpecial &&
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1626
                refc != lookupClass() &&
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1627
                !refc.isInterface() &&
24860
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1628
                refc != lookupClass().getSuperclass() &&
16898
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1629
                refc.isAssignableFrom(lookupClass())) {
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1630
                assert(!method.getName().equals("<init>"));  // not this code path
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1631
                // Per JVMS 6.5, desc. of invokespecial instruction:
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1632
                // If the method is in a superclass of the LC,
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1633
                // and if our original search was above LC.super,
24860
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1634
                // repeat the search (symbolic lookup) from LC.super
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1635
                // and continue with the direct superclass of that class,
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1636
                // and so forth, until a match is found or no further superclasses exist.
16898
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1637
                // FIXME: MemberName.resolve should handle this instead.
24860
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1638
                Class<?> refcAsSuper = lookupClass();
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1639
                MemberName m2;
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1640
                do {
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1641
                    refcAsSuper = refcAsSuper.getSuperclass();
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1642
                    m2 = new MemberName(refcAsSuper,
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1643
                                        method.getName(),
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1644
                                        method.getMethodType(),
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1645
                                        REF_invokeSpecial);
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1646
                    m2 = IMPL_NAMES.resolveOrNull(refKind, m2, lookupClassOrNull());
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1647
                } while (m2 == null &&         // no method is found yet
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  1648
                         refc != refcAsSuper); // search up to refc
16898
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1649
                if (m2 == null)  throw new InternalError(method.toString());
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1650
                method = m2;
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1651
                refc = refcAsSuper;
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1652
                // redo basic checks
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1653
                checkMethod(refKind, refc, method);
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1654
            }
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  1655
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1656
            DirectMethodHandle dmh = DirectMethodHandle.make(refKind, refc, method);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1657
            MethodHandle mh = dmh;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1658
            // Optionally narrow the receiver argument to refc using restrictReceiver.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1659
            if (doRestrict &&
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1660
                   (refKind == REF_invokeSpecial ||
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1661
                       (MethodHandleNatives.refKindHasReceiver(refKind) &&
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1662
                           restrictProtectedReceiver(method)))) {
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1663
                mh = restrictReceiver(method, dmh, lookupClass());
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1664
            }
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1665
            mh = maybeBindCaller(method, mh, callerClass);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1666
            mh = mh.setVarargs(method);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1667
            return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1668
        }
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1669
        private MethodHandle maybeBindCaller(MemberName method, MethodHandle mh,
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1670
                                             Class<?> callerClass)
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1671
                                             throws IllegalAccessException {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1672
            if (allowedModes == TRUSTED || !MethodHandleNatives.isCallerSensitive(method))
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1673
                return mh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1674
            Class<?> hostClass = lookupClass;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1675
            if (!hasPrivateAccess())  // caller must have private access
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  1676
                hostClass = callerClass;  // callerClass came from a security manager style stack walk
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1677
            MethodHandle cbmh = MethodHandleImpl.bindCaller(mh, hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1678
            // Note: caller will apply varargs after this step happens.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1679
            return cbmh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1680
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1681
        /** Check access and get the requested field. */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1682
        private MethodHandle getDirectField(byte refKind, Class<?> refc, MemberName field) throws IllegalAccessException {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1683
            final boolean checkSecurity = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1684
            return getDirectFieldCommon(refKind, refc, field, checkSecurity);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1685
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1686
        /** Check access and get the requested field, eliding security manager checks. */
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1687
        private MethodHandle getDirectFieldNoSecurityManager(byte refKind, Class<?> refc, MemberName field) throws IllegalAccessException {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1688
            final boolean checkSecurity = false;  // not needed for reflection or for linking CONSTANT_MH constants
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1689
            return getDirectFieldCommon(refKind, refc, field, checkSecurity);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1690
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1691
        /** Common code for all fields; do not call directly except from immediately above. */
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1692
        private MethodHandle getDirectFieldCommon(byte refKind, Class<?> refc, MemberName field,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1693
                                                  boolean checkSecurity) throws IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1694
            checkField(refKind, refc, field);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1695
            // Optionally check with the security manager; this isn't needed for unreflect* calls.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1696
            if (checkSecurity)
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1697
                checkSecurityManager(refc, field);
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1698
            DirectMethodHandle dmh = DirectMethodHandle.make(refc, field);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1699
            boolean doRestrict = (MethodHandleNatives.refKindHasReceiver(refKind) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1700
                                    restrictProtectedReceiver(field));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1701
            if (doRestrict)
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1702
                return restrictReceiver(field, dmh, lookupClass());
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  1703
            return dmh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1704
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1705
        /** Check access and get the requested constructor. */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1706
        private MethodHandle getDirectConstructor(Class<?> refc, MemberName ctor) throws IllegalAccessException {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1707
            final boolean checkSecurity = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1708
            return getDirectConstructorCommon(refc, ctor, checkSecurity);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1709
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1710
        /** Check access and get the requested constructor, eliding security manager checks. */
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1711
        private MethodHandle getDirectConstructorNoSecurityManager(Class<?> refc, MemberName ctor) throws IllegalAccessException {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1712
            final boolean checkSecurity = false;  // not needed for reflection or for linking CONSTANT_MH constants
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1713
            return getDirectConstructorCommon(refc, ctor, checkSecurity);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1714
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1715
        /** Common code for all constructors; do not call directly except from immediately above. */
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1716
        private MethodHandle getDirectConstructorCommon(Class<?> refc, MemberName ctor,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1717
                                                  boolean checkSecurity) throws IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1718
            assert(ctor.isConstructor());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1719
            checkAccess(REF_newInvokeSpecial, refc, ctor);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1720
            // Optionally check with the security manager; this isn't needed for unreflect* calls.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1721
            if (checkSecurity)
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1722
                checkSecurityManager(refc, ctor);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1723
            assert(!MethodHandleNatives.isCallerSensitive(ctor));  // maybeBindCaller not relevant here
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1724
            return DirectMethodHandle.make(ctor).setVarargs(ctor);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1725
        }
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1726
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1727
        /** Hook called from the JVM (via MethodHandleNatives) to link MH constants:
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1728
         */
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1729
        /*non-public*/
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1730
        MethodHandle linkMethodHandleConstant(byte refKind, Class<?> defc, String name, Object type) throws ReflectiveOperationException {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1731
            if (!(type instanceof Class || type instanceof MethodType))
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1732
                throw new InternalError("unresolved MemberName");
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1733
            MemberName member = new MemberName(refKind, defc, name, type);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1734
            MethodHandle mh = LOOKASIDE_TABLE.get(member);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1735
            if (mh != null) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1736
                checkSymbolicClass(defc);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1737
                return mh;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1738
            }
21362
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  1739
            // Treat MethodHandle.invoke and invokeExact specially.
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  1740
            if (defc == MethodHandle.class && refKind == REF_invokeVirtual) {
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  1741
                mh = findVirtualForMH(member.getName(), member.getMethodType());
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  1742
                if (mh != null) {
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  1743
                    return mh;
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  1744
                }
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  1745
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1746
            MemberName resolved = resolveOrFail(refKind, member);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1747
            mh = getDirectMethodForConstant(refKind, defc, resolved);
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1748
            if (mh instanceof DirectMethodHandle
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1749
                    && canBeCached(refKind, defc, resolved)) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1750
                MemberName key = mh.internalMemberName();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1751
                if (key != null) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1752
                    key = key.asNormalOriginal();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1753
                }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1754
                if (member.equals(key)) {  // better safe than sorry
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1755
                    LOOKASIDE_TABLE.put(key, (DirectMethodHandle) mh);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1756
                }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1757
            }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1758
            return mh;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1759
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1760
        private
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1761
        boolean canBeCached(byte refKind, Class<?> defc, MemberName member) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1762
            if (refKind == REF_invokeSpecial) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1763
                return false;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1764
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1765
            if (!Modifier.isPublic(defc.getModifiers()) ||
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1766
                    !Modifier.isPublic(member.getDeclaringClass().getModifiers()) ||
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1767
                    !member.isPublic() ||
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1768
                    member.isCallerSensitive()) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1769
                return false;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1770
            }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1771
            ClassLoader loader = defc.getClassLoader();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1772
            if (!sun.misc.VM.isSystemDomainLoader(loader)) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1773
                ClassLoader sysl = ClassLoader.getSystemClassLoader();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1774
                boolean found = false;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1775
                while (sysl != null) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1776
                    if (loader == sysl) { found = true; break; }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1777
                    sysl = sysl.getParent();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1778
                }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1779
                if (!found) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1780
                    return false;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1781
                }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1782
            }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1783
            try {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1784
                MemberName resolved2 = publicLookup().resolveOrFail(refKind,
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1785
                    new MemberName(refKind, defc, member.getName(), member.getType()));
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1786
                checkSecurityManager(defc, resolved2);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1787
            } catch (ReflectiveOperationException | SecurityException ex) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1788
                return false;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1789
            }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1790
            return true;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1791
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1792
        private
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1793
        MethodHandle getDirectMethodForConstant(byte refKind, Class<?> defc, MemberName member)
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1794
                throws ReflectiveOperationException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1795
            if (MethodHandleNatives.refKindIsField(refKind)) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1796
                return getDirectFieldNoSecurityManager(refKind, defc, member);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1797
            } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1798
                return getDirectMethodNoSecurityManager(refKind, defc, member, lookupClass);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1799
            } else if (refKind == REF_newInvokeSpecial) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1800
                return getDirectConstructorNoSecurityManager(defc, member);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1801
            }
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1802
            // oops
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1803
            throw newIllegalArgumentException("bad MethodHandle constant #"+member);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1804
        }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1805
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  1806
        static ConcurrentHashMap<MemberName, DirectMethodHandle> LOOKASIDE_TABLE = new ConcurrentHashMap<>();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1807
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1808
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1809
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1810
     * Produces a method handle giving read access to elements of an array.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1811
     * The type of the method handle will have a return type of the array's
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1812
     * element type.  Its first argument will be the array type,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1813
     * and the second will be {@code int}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1814
     * @param arrayClass an array type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1815
     * @return a method handle which can load values from the given array type
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1816
     * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1817
     * @throws  IllegalArgumentException if arrayClass is not an array type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1818
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1819
    public static
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1820
    MethodHandle arrayElementGetter(Class<?> arrayClass) throws IllegalArgumentException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1821
        return MethodHandleImpl.makeArrayElementAccessor(arrayClass, false);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1822
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1823
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1824
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1825
     * Produces a method handle giving write access to elements of an array.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1826
     * The type of the method handle will have a void return type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1827
     * Its last argument will be the array's element type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1828
     * The first and second arguments will be the array type and int.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  1829
     * @param arrayClass the class of an array
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1830
     * @return a method handle which can store values into the array type
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1831
     * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1832
     * @throws IllegalArgumentException if arrayClass is not an array type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1833
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1834
    public static
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1835
    MethodHandle arrayElementSetter(Class<?> arrayClass) throws IllegalArgumentException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1836
        return MethodHandleImpl.makeArrayElementAccessor(arrayClass, true);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1837
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1838
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1839
    /// method handle invocation (reflective style)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1840
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1841
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1842
     * Produces a method handle which will invoke any method handle of the
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1843
     * given {@code type}, with a given number of trailing arguments replaced by
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1844
     * a single trailing {@code Object[]} array.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1845
     * The resulting invoker will be a method handle with the following
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1846
     * arguments:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1847
     * <ul>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1848
     * <li>a single {@code MethodHandle} target
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1849
     * <li>zero or more leading values (counted by {@code leadingArgCount})
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1850
     * <li>an {@code Object[]} array containing trailing arguments
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1851
     * </ul>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1852
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1853
     * The invoker will invoke its target like a call to {@link MethodHandle#invoke invoke} with
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1854
     * the indicated {@code type}.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1855
     * That is, if the target is exactly of the given {@code type}, it will behave
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1856
     * like {@code invokeExact}; otherwise it behave as if {@link MethodHandle#asType asType}
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1857
     * is used to convert the target to the required {@code type}.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1858
     * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1859
     * The type of the returned invoker will not be the given {@code type}, but rather
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1860
     * will have all parameters except the first {@code leadingArgCount}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1861
     * replaced by a single array of type {@code Object[]}, which will be
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1862
     * the final parameter.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1863
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1864
     * Before invoking its target, the invoker will spread the final array, apply
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1865
     * reference casts as necessary, and unbox and widen primitive arguments.
20530
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20529
diff changeset
  1866
     * If, when the invoker is called, the supplied array argument does
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20529
diff changeset
  1867
     * not have the correct number of elements, the invoker will throw
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20529
diff changeset
  1868
     * an {@link IllegalArgumentException} instead of invoking the target.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1869
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1870
     * This method is equivalent to the following code (though it may be more efficient):
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1871
     * <blockquote><pre>{@code
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1872
MethodHandle invoker = MethodHandles.invoker(type);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1873
int spreadArgCount = type.parameterCount() - leadingArgCount;
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1874
invoker = invoker.asSpreader(Object[].class, spreadArgCount);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1875
return invoker;
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1876
     * }</pre></blockquote>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1877
     * This method throws no reflective or security exceptions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1878
     * @param type the desired target type
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1879
     * @param leadingArgCount number of fixed arguments, to be passed unchanged to the target
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1880
     * @return a method handle suitable for invoking any method handle of the given type
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1881
     * @throws NullPointerException if {@code type} is null
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1882
     * @throws IllegalArgumentException if {@code leadingArgCount} is not in
20529
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  1883
     *                  the range from 0 to {@code type.parameterCount()} inclusive,
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  1884
     *                  or if the resulting method handle's type would have
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  1885
     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1886
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
  1887
    public static
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1888
    MethodHandle spreadInvoker(MethodType type, int leadingArgCount) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1889
        if (leadingArgCount < 0 || leadingArgCount > type.parameterCount())
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
  1890
            throw newIllegalArgumentException("bad argument count", leadingArgCount);
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
  1891
        type = type.asSpreaderType(Object[].class, type.parameterCount() - leadingArgCount);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1892
        return type.invokers().spreadInvoker(leadingArgCount);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1893
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1894
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1895
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1896
     * Produces a special <em>invoker method handle</em> which can be used to
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1897
     * invoke any method handle of the given type, as if by {@link MethodHandle#invokeExact invokeExact}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1898
     * The resulting invoker will have a type which is
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1899
     * exactly equal to the desired type, except that it will accept
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1900
     * an additional leading argument of type {@code MethodHandle}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1901
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1902
     * This method is equivalent to the following code (though it may be more efficient):
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1903
     * {@code publicLookup().findVirtual(MethodHandle.class, "invokeExact", type)}
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1904
     *
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1905
     * <p style="font-size:smaller;">
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1906
     * <em>Discussion:</em>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1907
     * Invoker method handles can be useful when working with variable method handles
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1908
     * of unknown types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1909
     * For example, to emulate an {@code invokeExact} call to a variable method
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1910
     * handle {@code M}, extract its type {@code T},
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1911
     * look up the invoker method {@code X} for {@code T},
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1912
     * and call the invoker method, as {@code X.invoke(T, A...)}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1913
     * (It would not work to call {@code X.invokeExact}, since the type {@code T}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1914
     * is unknown.)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1915
     * If spreading, collecting, or other argument transformations are required,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1916
     * they can be applied once to the invoker {@code X} and reused on many {@code M}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1917
     * method handle values, as long as they are compatible with the type of {@code X}.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1918
     * <p style="font-size:smaller;">
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1919
     * <em>(Note:  The invoker method is not available via the Core Reflection API.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1920
     * An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1921
     * on the declared {@code invokeExact} or {@code invoke} method will raise an
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1922
     * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1923
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1924
     * This method throws no reflective or security exceptions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1925
     * @param type the desired target type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1926
     * @return a method handle suitable for invoking any method handle of the given type
20529
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  1927
     * @throws IllegalArgumentException if the resulting method handle's type would have
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  1928
     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1929
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
  1930
    public static
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1931
    MethodHandle exactInvoker(MethodType type) {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  1932
        return type.invokers().exactInvoker();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1933
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1934
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1935
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1936
     * Produces a special <em>invoker method handle</em> which can be used to
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1937
     * invoke any method handle compatible with the given type, as if by {@link MethodHandle#invoke invoke}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1938
     * The resulting invoker will have a type which is
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1939
     * exactly equal to the desired type, except that it will accept
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1940
     * an additional leading argument of type {@code MethodHandle}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1941
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1942
     * Before invoking its target, if the target differs from the expected type,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1943
     * the invoker will apply reference casts as
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1944
     * necessary and box, unbox, or widen primitive values, as if by {@link MethodHandle#asType asType}.
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1945
     * Similarly, the return value will be converted as necessary.
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1946
     * If the target is a {@linkplain MethodHandle#asVarargsCollector variable arity method handle},
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1947
     * the required arity conversion will be made, again as if by {@link MethodHandle#asType asType}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1948
     * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1949
     * This method is equivalent to the following code (though it may be more efficient):
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1950
     * {@code publicLookup().findVirtual(MethodHandle.class, "invoke", type)}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1951
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1952
     * <em>Discussion:</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1953
     * A {@linkplain MethodType#genericMethodType general method type} is one which
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1954
     * mentions only {@code Object} arguments and return values.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1955
     * An invoker for such a type is capable of calling any method handle
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1956
     * of the same arity as the general type.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1957
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1958
     * <em>(Note:  The invoker method is not available via the Core Reflection API.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1959
     * An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1960
     * on the declared {@code invokeExact} or {@code invoke} method will raise an
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1961
     * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1962
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1963
     * This method throws no reflective or security exceptions.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1964
     * @param type the desired target type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1965
     * @return a method handle suitable for invoking any method handle convertible to the given type
20529
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  1966
     * @throws IllegalArgumentException if the resulting method handle's type would have
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  1967
     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1968
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
  1969
    public static
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1970
    MethodHandle invoker(MethodType type) {
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
  1971
        return type.invokers().genericInvoker();
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1972
    }
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  1973
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1974
    static /*non-public*/
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1975
    MethodHandle basicInvoker(MethodType type) {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
  1976
        return type.invokers().basicInvoker();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1977
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1978
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1979
     /// method handle modification (creation from other method handles)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1980
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1981
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1982
     * Produces a method handle which adapts the type of the
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1983
     * given method handle to a new type by pairwise argument and return type conversion.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1984
     * The original type and new type must have the same number of arguments.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1985
     * The resulting method handle is guaranteed to report a type
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1986
     * which is equal to the desired new type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1987
     * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1988
     * If the original type and new type are equal, returns target.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1989
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1990
     * The same conversions are allowed as for {@link MethodHandle#asType MethodHandle.asType},
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1991
     * and some additional conversions are also applied if those conversions fail.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1992
     * Given types <em>T0</em>, <em>T1</em>, one of the following conversions is applied
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1993
     * if possible, before or instead of any conversions done by {@code asType}:
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1994
     * <ul>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1995
     * <li>If <em>T0</em> and <em>T1</em> are references, and <em>T1</em> is an interface type,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1996
     *     then the value of type <em>T0</em> is passed as a <em>T1</em> without a cast.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1997
     *     (This treatment of interfaces follows the usage of the bytecode verifier.)
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1998
     * <li>If <em>T0</em> is boolean and <em>T1</em> is another primitive,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1999
     *     the boolean is converted to a byte value, 1 for true, 0 for false.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2000
     *     (This treatment follows the usage of the bytecode verifier.)
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2001
     * <li>If <em>T1</em> is boolean and <em>T0</em> is another primitive,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2002
     *     <em>T0</em> is converted to byte via Java casting conversion (JLS 5.5),
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2003
     *     and the low order bit of the result is tested, as if by {@code (x & 1) != 0}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2004
     * <li>If <em>T0</em> and <em>T1</em> are primitives other than boolean,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2005
     *     then a Java casting conversion (JLS 5.5) is applied.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2006
     *     (Specifically, <em>T0</em> will convert to <em>T1</em> by
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2007
     *     widening and/or narrowing.)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2008
     * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, an unboxing
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2009
     *     conversion will be applied at runtime, possibly followed
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2010
     *     by a Java casting conversion (JLS 5.5) on the primitive value,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2011
     *     possibly followed by a conversion from byte to boolean by testing
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2012
     *     the low-order bit.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2013
     * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2014
     *     and if the reference is null at runtime, a zero value is introduced.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2015
     * </ul>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2016
     * @param target the method handle to invoke after arguments are retyped
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2017
     * @param newType the expected type of the new method handle
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2018
     * @return a method handle which delegates to the target after performing
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2019
     *           any necessary argument conversions, and arranges for any
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2020
     *           necessary return value conversions
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2021
     * @throws NullPointerException if either argument is null
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2022
     * @throws WrongMethodTypeException if the conversion cannot be made
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2023
     * @see MethodHandle#asType
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2024
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2025
    public static
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2026
    MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
26625
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2027
        explicitCastArgumentsChecks(target, newType);
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2028
        // use the asTypeCache when possible:
26478
285691ed55e4 8057656: Improve MethodType.isCastableTo() & MethodType.isConvertibleTo() checks
vlivanov
parents: 26477
diff changeset
  2029
        MethodType oldType = target.type();
285691ed55e4 8057656: Improve MethodType.isCastableTo() & MethodType.isConvertibleTo() checks
vlivanov
parents: 26477
diff changeset
  2030
        if (oldType == newType)  return target;
285691ed55e4 8057656: Improve MethodType.isCastableTo() & MethodType.isConvertibleTo() checks
vlivanov
parents: 26477
diff changeset
  2031
        if (oldType.explicitCastEquivalentToAsType(newType)) {
27947
dba3bc0c087e 8066746: MHs.explicitCastArguments does incorrect type checks for VarargsCollector
vlivanov
parents: 27755
diff changeset
  2032
            return target.asFixedArity().asType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2033
        }
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26470
diff changeset
  2034
        return MethodHandleImpl.makePairwiseConvert(target, newType, false);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2035
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2036
26625
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2037
    private static void explicitCastArgumentsChecks(MethodHandle target, MethodType newType) {
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2038
        if (target.type().parameterCount() != newType.parameterCount()) {
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2039
            throw new WrongMethodTypeException("cannot explicitly cast " + target + " to " + newType);
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2040
        }
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2041
    }
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2042
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2043
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2044
     * Produces a method handle which adapts the calling sequence of the
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2045
     * given method handle to a new type, by reordering the arguments.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2046
     * The resulting method handle is guaranteed to report a type
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2047
     * which is equal to the desired new type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2048
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2049
     * The given array controls the reordering.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2050
     * Call {@code #I} the number of incoming parameters (the value
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2051
     * {@code newType.parameterCount()}, and call {@code #O} the number
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2052
     * of outgoing parameters (the value {@code target.type().parameterCount()}).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2053
     * Then the length of the reordering array must be {@code #O},
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2054
     * and each element must be a non-negative number less than {@code #I}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2055
     * For every {@code N} less than {@code #O}, the {@code N}-th
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2056
     * outgoing argument will be taken from the {@code I}-th incoming
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2057
     * argument, where {@code I} is {@code reorder[N]}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2058
     * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2059
     * No argument or return value conversions are applied.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2060
     * The type of each incoming argument, as determined by {@code newType},
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2061
     * must be identical to the type of the corresponding outgoing parameter
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2062
     * or parameters in the target method handle.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2063
     * The return type of {@code newType} must be identical to the return
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2064
     * type of the original target.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2065
     * <p>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2066
     * The reordering array need not specify an actual permutation.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2067
     * An incoming argument will be duplicated if its index appears
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2068
     * more than once in the array, and an incoming argument will be dropped
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2069
     * if its index does not appear in the array.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2070
     * As in the case of {@link #dropArguments(MethodHandle,int,List) dropArguments},
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2071
     * incoming arguments which are not mentioned in the reordering array
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2072
     * are may be any type, as determined only by {@code newType}.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16906
diff changeset
  2073
     * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2074
import static java.lang.invoke.MethodHandles.*;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2075
import static java.lang.invoke.MethodType.*;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2076
...
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2077
MethodType intfn1 = methodType(int.class, int.class);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2078
MethodType intfn2 = methodType(int.class, int.class, int.class);
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  2079
MethodHandle sub = ... (int x, int y) -> (x-y) ...;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2080
assert(sub.type().equals(intfn2));
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2081
MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2082
MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2083
assert((int)rsub.invokeExact(1, 100) == 99);
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  2084
MethodHandle add = ... (int x, int y) -> (x+y) ...;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2085
assert(add.type().equals(intfn2));
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2086
MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2087
assert(twice.type().equals(intfn1));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2088
assert((int)twice.invokeExact(21) == 42);
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16906
diff changeset
  2089
     * }</pre></blockquote>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2090
     * @param target the method handle to invoke after arguments are reordered
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2091
     * @param newType the expected type of the new method handle
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2092
     * @param reorder an index array which controls the reordering
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2093
     * @return a method handle which delegates to the target after it
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2094
     *           drops unused arguments and moves and/or duplicates the other arguments
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2095
     * @throws NullPointerException if any argument is null
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2096
     * @throws IllegalArgumentException if the index array length is not equal to
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2097
     *                  the arity of the target, or if any index array element
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2098
     *                  not a valid index for a parameter of {@code newType},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2099
     *                  or if two corresponding parameter types in
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2100
     *                  {@code target.type()} and {@code newType} are not identical,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2101
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2102
    public static
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2103
    MethodHandle permuteArguments(MethodHandle target, MethodType newType, int... reorder) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2104
        reorder = reorder.clone();  // get a private copy
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2105
        MethodType oldType = target.type();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2106
        permuteArgumentChecks(reorder, newType, oldType);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2107
        // first detect dropped arguments and handle them separately
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2108
        int[] originalReorder = reorder;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2109
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2110
        LambdaForm form = result.form;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2111
        int newArity = newType.parameterCount();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2112
        // Normalize the reordering into a real permutation,
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2113
        // by removing duplicates and adding dropped elements.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2114
        // This somewhat improves lambda form caching, as well
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2115
        // as simplifying the transform by breaking it up into steps.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2116
        for (int ddIdx; (ddIdx = findFirstDupOrDrop(reorder, newArity)) != 0; ) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2117
            if (ddIdx > 0) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2118
                // We found a duplicated entry at reorder[ddIdx].
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2119
                // Example:  (x,y,z)->asList(x,y,z)
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2120
                // permuted by [1*,0,1] => (a0,a1)=>asList(a1,a0,a1)
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2121
                // permuted by [0,1,0*] => (a0,a1)=>asList(a0,a1,a0)
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2122
                // The starred element corresponds to the argument
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2123
                // deleted by the dupArgumentForm transform.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2124
                int srcPos = ddIdx, dstPos = srcPos, dupVal = reorder[srcPos];
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2125
                boolean killFirst = false;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2126
                for (int val; (val = reorder[--dstPos]) != dupVal; ) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2127
                    // Set killFirst if the dup is larger than an intervening position.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2128
                    // This will remove at least one inversion from the permutation.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2129
                    if (dupVal > val) killFirst = true;
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2130
                }
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2131
                if (!killFirst) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2132
                    srcPos = dstPos;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2133
                    dstPos = ddIdx;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2134
                }
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2135
                form = form.editor().dupArgumentForm(1 + srcPos, 1 + dstPos);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2136
                assert (reorder[srcPos] == reorder[dstPos]);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2137
                oldType = oldType.dropParameterTypes(dstPos, dstPos + 1);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2138
                // contract the reordering by removing the element at dstPos
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2139
                int tailPos = dstPos + 1;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2140
                System.arraycopy(reorder, tailPos, reorder, dstPos, reorder.length - tailPos);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2141
                reorder = Arrays.copyOf(reorder, reorder.length - 1);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2142
            } else {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2143
                int dropVal = ~ddIdx, insPos = 0;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2144
                while (insPos < reorder.length && reorder[insPos] < dropVal) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2145
                    // Find first element of reorder larger than dropVal.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2146
                    // This is where we will insert the dropVal.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2147
                    insPos += 1;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2148
                }
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2149
                Class<?> ptype = newType.parameterType(dropVal);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2150
                form = form.editor().addArgumentForm(1 + insPos, BasicType.basicType(ptype));
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2151
                oldType = oldType.insertParameterTypes(insPos, ptype);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2152
                // expand the reordering by inserting an element at insPos
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2153
                int tailPos = insPos + 1;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2154
                reorder = Arrays.copyOf(reorder, reorder.length + 1);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2155
                System.arraycopy(reorder, insPos, reorder, tailPos, reorder.length - tailPos);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2156
                reorder[insPos] = dropVal;
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2157
            }
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2158
            assert (permuteArgumentChecks(reorder, newType, oldType));
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2159
        }
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2160
        assert (reorder.length == newArity);  // a perfect permutation
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2161
        // Note:  This may cache too many distinct LFs. Consider backing off to varargs code.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2162
        form = form.editor().permuteArgumentsForm(1, reorder);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2163
        if (newType == result.type() && form == result.internalForm())
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2164
            return result;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2165
        return result.copyWith(newType, form);
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2166
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2167
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2168
    /**
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2169
     * Return an indication of any duplicate or omission in reorder.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2170
     * If the reorder contains a duplicate entry, return the index of the second occurrence.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2171
     * Otherwise, return ~(n), for the first n in [0..newArity-1] that is not present in reorder.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2172
     * Otherwise, return zero.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2173
     * If an element not in [0..newArity-1] is encountered, return reorder.length.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2174
     */
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2175
    private static int findFirstDupOrDrop(int[] reorder, int newArity) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2176
        final int BIT_LIMIT = 63;  // max number of bits in bit mask
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2177
        if (newArity < BIT_LIMIT) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2178
            long mask = 0;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2179
            for (int i = 0; i < reorder.length; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2180
                int arg = reorder[i];
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2181
                if (arg >= newArity) {
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2182
                    return reorder.length;
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2183
                }
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2184
                long bit = 1L << arg;
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2185
                if ((mask & bit) != 0) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2186
                    return i;  // >0 indicates a dup
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2187
                }
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2188
                mask |= bit;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2189
            }
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2190
            if (mask == (1L << newArity) - 1) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2191
                assert(Long.numberOfTrailingZeros(Long.lowestOneBit(~mask)) == newArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2192
                return 0;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2193
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2194
            // find first zero
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2195
            long zeroBit = Long.lowestOneBit(~mask);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2196
            int zeroPos = Long.numberOfTrailingZeros(zeroBit);
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2197
            assert(zeroPos <= newArity);
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2198
            if (zeroPos == newArity) {
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2199
                return 0;
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2200
            }
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2201
            return ~zeroPos;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2202
        } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2203
            // same algorithm, different bit set
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2204
            BitSet mask = new BitSet(newArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2205
            for (int i = 0; i < reorder.length; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2206
                int arg = reorder[i];
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2207
                if (arg >= newArity) {
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2208
                    return reorder.length;
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2209
                }
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2210
                if (mask.get(arg)) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2211
                    return i;  // >0 indicates a dup
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2212
                }
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2213
                mask.set(arg);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2214
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2215
            int zeroPos = mask.nextClearBit(0);
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  2216
            assert(zeroPos <= newArity);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2217
            if (zeroPos == newArity) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2218
                return 0;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2219
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2220
            return ~zeroPos;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2221
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2222
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2223
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2224
    private static boolean permuteArgumentChecks(int[] reorder, MethodType newType, MethodType oldType) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2225
        if (newType.returnType() != oldType.returnType())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2226
            throw newIllegalArgumentException("return types do not match",
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2227
                    oldType, newType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2228
        if (reorder.length == oldType.parameterCount()) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2229
            int limit = newType.parameterCount();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2230
            boolean bad = false;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2231
            for (int j = 0; j < reorder.length; j++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2232
                int i = reorder[j];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2233
                if (i < 0 || i >= limit) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2234
                    bad = true; break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2235
                }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2236
                Class<?> src = newType.parameterType(i);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2237
                Class<?> dst = oldType.parameterType(j);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2238
                if (src != dst)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2239
                    throw newIllegalArgumentException("parameter types do not match after reorder",
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2240
                            oldType, newType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2241
            }
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2242
            if (!bad)  return true;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2243
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2244
        throw newIllegalArgumentException("bad reorder array: "+Arrays.toString(reorder));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2245
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2246
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2247
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2248
     * Produces a method handle of the requested return type which returns the given
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2249
     * constant value every time it is invoked.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2250
     * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2251
     * Before the method handle is returned, the passed-in value is converted to the requested type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2252
     * If the requested type is primitive, widening primitive conversions are attempted,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2253
     * else reference conversions are attempted.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2254
     * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2255
     * @param type the return type of the desired method handle
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2256
     * @param value the value to return
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2257
     * @return a method handle of the given return type and no arguments, which always returns the given value
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2258
     * @throws NullPointerException if the {@code type} argument is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2259
     * @throws ClassCastException if the value cannot be converted to the required return type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2260
     * @throws IllegalArgumentException if the given type is {@code void.class}
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2261
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2262
    public static
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2263
    MethodHandle constant(Class<?> type, Object value) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2264
        if (type.isPrimitive()) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2265
            if (type == void.class)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2266
                throw newIllegalArgumentException("void type");
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2267
            Wrapper w = Wrapper.forPrimitiveType(type);
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2268
            value = w.convert(value, type);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2269
            if (w.zero().equals(value))
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2270
                return zero(w, type);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2271
            return insertArguments(identity(type), 0, value);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2272
        } else {
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2273
            if (value == null)
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2274
                return zero(Wrapper.OBJECT, type);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2275
            return identity(type).bindTo(value);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2276
        }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2277
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2278
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2279
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2280
     * Produces a method handle which returns its sole argument when invoked.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2281
     * @param type the type of the sole parameter and return value of the desired method handle
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2282
     * @return a unary method handle which accepts and returns the given type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2283
     * @throws NullPointerException if the argument is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2284
     * @throws IllegalArgumentException if the given type is {@code void.class}
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2285
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2286
    public static
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2287
    MethodHandle identity(Class<?> type) {
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2288
        Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2289
        int pos = btw.ordinal();
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2290
        MethodHandle ident = IDENTITY_MHS[pos];
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2291
        if (ident == null) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2292
            ident = setCachedMethodHandle(IDENTITY_MHS, pos, makeIdentity(btw.primitiveType()));
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2293
        }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2294
        if (ident.type().returnType() == type)
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2295
            return ident;
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2296
        // something like identity(Foo.class); do not bother to intern these
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2297
        assert(btw == Wrapper.OBJECT);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2298
        return makeIdentity(type);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2299
    }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2300
    private static final MethodHandle[] IDENTITY_MHS = new MethodHandle[Wrapper.values().length];
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2301
    private static MethodHandle makeIdentity(Class<?> ptype) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2302
        MethodType mtype = MethodType.methodType(ptype, ptype);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2303
        LambdaForm lform = LambdaForm.identityForm(BasicType.basicType(ptype));
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2304
        return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.IDENTITY);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2305
    }
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2306
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2307
    private static MethodHandle zero(Wrapper btw, Class<?> rtype) {
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2308
        int pos = btw.ordinal();
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2309
        MethodHandle zero = ZERO_MHS[pos];
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2310
        if (zero == null) {
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2311
            zero = setCachedMethodHandle(ZERO_MHS, pos, makeZero(btw.primitiveType()));
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2312
        }
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2313
        if (zero.type().returnType() == rtype)
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2314
            return zero;
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2315
        assert(btw == Wrapper.OBJECT);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2316
        return makeZero(rtype);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2317
    }
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2318
    private static final MethodHandle[] ZERO_MHS = new MethodHandle[Wrapper.values().length];
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2319
    private static MethodHandle makeZero(Class<?> rtype) {
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2320
        MethodType mtype = MethodType.methodType(rtype);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2321
        LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2322
        return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2323
    }
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  2324
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
  2325
    private static synchronized MethodHandle setCachedMethodHandle(MethodHandle[] cache, int pos, MethodHandle value) {
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2326
        // Simulate a CAS, to avoid racy duplication of results.
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2327
        MethodHandle prev = cache[pos];
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2328
        if (prev != null) return prev;
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  2329
        return cache[pos] = value;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2330
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2331
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2332
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2333
     * Provides a target method handle with one or more <em>bound arguments</em>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2334
     * in advance of the method handle's invocation.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2335
     * The formal parameters to the target corresponding to the bound
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2336
     * arguments are called <em>bound parameters</em>.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2337
     * Returns a new method handle which saves away the bound arguments.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2338
     * When it is invoked, it receives arguments for any non-bound parameters,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2339
     * binds the saved arguments to their corresponding parameters,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2340
     * and calls the original target.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2341
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2342
     * The type of the new method handle will drop the types for the bound
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2343
     * parameters from the original target type, since the new method handle
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2344
     * will no longer require those arguments to be supplied by its callers.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2345
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2346
     * Each given argument object must match the corresponding bound parameter type.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2347
     * If a bound parameter type is a primitive, the argument object
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2348
     * must be a wrapper, and will be unboxed to produce the primitive value.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2349
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2350
     * The {@code pos} argument selects which parameters are to be bound.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2351
     * It may range between zero and <i>N-L</i> (inclusively),
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2352
     * where <i>N</i> is the arity of the target method handle
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2353
     * and <i>L</i> is the length of the values array.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2354
     * @param target the method handle to invoke after the argument is inserted
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2355
     * @param pos where to insert the argument (zero for the first)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2356
     * @param values the series of arguments to insert
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2357
     * @return a method handle which inserts an additional argument,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2358
     *         before calling the original method handle
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2359
     * @throws NullPointerException if the target or the {@code values} array is null
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2360
     * @see MethodHandle#bindTo
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2361
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2362
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2363
    MethodHandle insertArguments(MethodHandle target, int pos, Object... values) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2364
        int insCount = values.length;
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2365
        Class<?>[] ptypes = insertArgumentsChecks(target, insCount, pos);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2366
        if (insCount == 0)  return target;
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2367
        BoundMethodHandle result = target.rebind();
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2368
        for (int i = 0; i < insCount; i++) {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2369
            Object value = values[i];
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2370
            Class<?> ptype = ptypes[pos+i];
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2371
            if (ptype.isPrimitive()) {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2372
                result = insertArgumentPrimitive(result, pos, ptype, value);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2373
            } else {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2374
                value = ptype.cast(value);  // throw CCE if needed
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2375
                result = result.bindArgumentL(pos, value);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2376
            }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2377
        }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2378
        return result;
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2379
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2380
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2381
    private static BoundMethodHandle insertArgumentPrimitive(BoundMethodHandle result, int pos,
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2382
                                                             Class<?> ptype, Object value) {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2383
        Wrapper w = Wrapper.forPrimitiveType(ptype);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2384
        // perform unboxing and/or primitive conversion
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2385
        value = w.convert(value, ptype);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2386
        switch (w) {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2387
        case INT:     return result.bindArgumentI(pos, (int)value);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2388
        case LONG:    return result.bindArgumentJ(pos, (long)value);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2389
        case FLOAT:   return result.bindArgumentF(pos, (float)value);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2390
        case DOUBLE:  return result.bindArgumentD(pos, (double)value);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2391
        default:      return result.bindArgumentI(pos, ValueConversions.widenSubword(value));
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2392
        }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2393
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2394
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2395
    private static Class<?>[] insertArgumentsChecks(MethodHandle target, int insCount, int pos) throws RuntimeException {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2396
        MethodType oldType = target.type();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2397
        int outargs = oldType.parameterCount();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2398
        int inargs  = outargs - insCount;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2399
        if (inargs < 0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2400
            throw newIllegalArgumentException("too many values to insert");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2401
        if (pos < 0 || pos > inargs)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2402
            throw newIllegalArgumentException("no argument type to append");
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2403
        return oldType.ptypes();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2404
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2405
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2406
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2407
     * Produces a method handle which will discard some dummy arguments
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2408
     * before calling some other specified <i>target</i> method handle.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2409
     * The type of the new method handle will be the same as the target's type,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2410
     * except it will also include the dummy argument types,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2411
     * at some given position.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2412
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2413
     * The {@code pos} argument may range between zero and <i>N</i>,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2414
     * where <i>N</i> is the arity of the target.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2415
     * If {@code pos} is zero, the dummy arguments will precede
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2416
     * the target's real arguments; if {@code pos} is <i>N</i>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2417
     * they will come after.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2418
     * <p>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2419
     * <b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2420
     * <blockquote><pre>{@code
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2421
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2422
import static java.lang.invoke.MethodType.*;
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2423
...
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2424
MethodHandle cat = lookup().findVirtual(String.class,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2425
  "concat", methodType(String.class, String.class));
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2426
assertEquals("xy", (String) cat.invokeExact("x", "y"));
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2427
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2428
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2429
assertEquals(bigType, d0.type());
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2430
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2431
     * }</pre></blockquote>
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2432
     * <p>
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2433
     * This method is also equivalent to the following code:
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2434
     * <blockquote><pre>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2435
     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2436
     * </pre></blockquote>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2437
     * @param target the method handle to invoke after the arguments are dropped
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2438
     * @param valueTypes the type(s) of the argument(s) to drop
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2439
     * @param pos position of first argument to drop (zero for the leftmost)
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2440
     * @return a method handle which drops arguments of the given types,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2441
     *         before calling the original method handle
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2442
     * @throws NullPointerException if the target is null,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2443
     *                              or if the {@code valueTypes} list or any of its elements is null
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2444
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2445
     *                  or if {@code pos} is negative or greater than the arity of the target,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2446
     *                  or if the new method handle's type would have too many parameters
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2447
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2448
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2449
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2450
        MethodType oldType = target.type();  // get NPE
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2451
        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
26625
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  2452
        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2453
        if (dropped == 0)  return target;
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2454
        BoundMethodHandle result = target.rebind();
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2455
        LambdaForm lform = result.form;
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2456
        int insertFormArg = 1 + pos;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2457
        for (Class<?> ptype : valueTypes) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2458
            lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2459
        }
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2460
        result = result.copyWith(newType, lform);
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2461
        return result;
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2462
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2463
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2464
    private static int dropArgumentChecks(MethodType oldType, int pos, List<Class<?>> valueTypes) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2465
        int dropped = valueTypes.size();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2466
        MethodType.checkSlotCount(dropped);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2467
        int outargs = oldType.parameterCount();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2468
        int inargs  = outargs + dropped;
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2469
        if (pos < 0 || pos > outargs)
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2470
            throw newIllegalArgumentException("no argument type to remove"
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2471
                    + Arrays.asList(oldType, pos, valueTypes, inargs, outargs)
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2472
                    );
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2473
        return dropped;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2474
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2475
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2476
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2477
     * Produces a method handle which will discard some dummy arguments
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2478
     * before calling some other specified <i>target</i> method handle.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2479
     * The type of the new method handle will be the same as the target's type,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2480
     * except it will also include the dummy argument types,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2481
     * at some given position.
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2482
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2483
     * The {@code pos} argument may range between zero and <i>N</i>,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2484
     * where <i>N</i> is the arity of the target.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2485
     * If {@code pos} is zero, the dummy arguments will precede
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2486
     * the target's real arguments; if {@code pos} is <i>N</i>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2487
     * they will come after.
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2488
     * <p>
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2489
     * <b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2490
     * <blockquote><pre>{@code
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2491
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2492
import static java.lang.invoke.MethodType.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2493
...
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2494
MethodHandle cat = lookup().findVirtual(String.class,
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2495
  "concat", methodType(String.class, String.class));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2496
assertEquals("xy", (String) cat.invokeExact("x", "y"));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2497
MethodHandle d0 = dropArguments(cat, 0, String.class);
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2498
assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2499
MethodHandle d1 = dropArguments(cat, 1, String.class);
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2500
assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2501
MethodHandle d2 = dropArguments(cat, 2, String.class);
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2502
assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2503
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2504
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2505
     * }</pre></blockquote>
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2506
     * <p>
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2507
     * This method is also equivalent to the following code:
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2508
     * <blockquote><pre>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2509
     * {@link #dropArguments(MethodHandle,int,List) dropArguments}{@code (target, pos, Arrays.asList(valueTypes))}
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2510
     * </pre></blockquote>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2511
     * @param target the method handle to invoke after the arguments are dropped
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2512
     * @param valueTypes the type(s) of the argument(s) to drop
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2513
     * @param pos position of first argument to drop (zero for the leftmost)
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2514
     * @return a method handle which drops arguments of the given types,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2515
     *         before calling the original method handle
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2516
     * @throws NullPointerException if the target is null,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2517
     *                              or if the {@code valueTypes} array or any of its elements is null
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2518
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2519
     *                  or if {@code pos} is negative or greater than the arity of the target,
20529
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  2520
     *                  or if the new method handle's type would have
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  2521
     *                  <a href="MethodHandle.html#maxarity">too many parameters</a>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2522
     */
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2523
    public static
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2524
    MethodHandle dropArguments(MethodHandle target, int pos, Class<?>... valueTypes) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2525
        return dropArguments(target, pos, Arrays.asList(valueTypes));
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2526
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2527
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2528
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2529
     * Adapts a target method handle by pre-processing
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2530
     * one or more of its arguments, each with its own unary filter function,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2531
     * and then calling the target with each pre-processed argument
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2532
     * replaced by the result of its corresponding filter function.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2533
     * <p>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2534
     * The pre-processing is performed by one or more method handles,
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2535
     * specified in the elements of the {@code filters} array.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2536
     * The first element of the filter array corresponds to the {@code pos}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2537
     * argument of the target, and so on in sequence.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2538
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2539
     * Null arguments in the array are treated as identity functions,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2540
     * and the corresponding arguments left unchanged.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2541
     * (If there are no non-null elements in the array, the original target is returned.)
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2542
     * Each filter is applied to the corresponding argument of the adapter.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2543
     * <p>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2544
     * If a filter {@code F} applies to the {@code N}th argument of
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2545
     * the target, then {@code F} must be a method handle which
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2546
     * takes exactly one argument.  The type of {@code F}'s sole argument
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2547
     * replaces the corresponding argument type of the target
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2548
     * in the resulting adapted method handle.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2549
     * The return type of {@code F} must be identical to the corresponding
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2550
     * parameter type of the target.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2551
     * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2552
     * It is an error if there are elements of {@code filters}
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2553
     * (null or not)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2554
     * which do not correspond to argument positions in the target.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2555
     * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2556
     * <blockquote><pre>{@code
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2557
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2558
import static java.lang.invoke.MethodType.*;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2559
...
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2560
MethodHandle cat = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2561
  "concat", methodType(String.class, String.class));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2562
MethodHandle upcase = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2563
  "toUpperCase", methodType(String.class));
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2564
assertEquals("xy", (String) cat.invokeExact("x", "y"));
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2565
MethodHandle f0 = filterArguments(cat, 0, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2566
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2567
MethodHandle f1 = filterArguments(cat, 1, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2568
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2569
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2570
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2571
     * }</pre></blockquote>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2572
     * <p> Here is pseudocode for the resulting adapter:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2573
     * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2574
     * V target(P... p, A[i]... a[i], B... b);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2575
     * A[i] filter[i](V[i]);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2576
     * T adapter(P... p, V[i]... v[i], B... b) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2577
     *   return target(p..., f[i](v[i])..., b...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2578
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2579
     * }</pre></blockquote>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2580
     *
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2581
     * @param target the method handle to invoke after arguments are filtered
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2582
     * @param pos the position of the first argument to filter
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2583
     * @param filters method handles to call initially on filtered arguments
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2584
     * @return method handle which incorporates the specified argument filtering logic
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2585
     * @throws NullPointerException if the target is null
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2586
     *                              or if the {@code filters} array is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2587
     * @throws IllegalArgumentException if a non-null element of {@code filters}
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2588
     *          does not match a corresponding argument type of target as described above,
20529
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  2589
     *          or if the {@code pos+filters.length} is greater than {@code target.type().parameterCount()},
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  2590
     *          or if the resulting method handle's type would have
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
  2591
     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2592
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2593
    public static
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2594
    MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) {
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2595
        filterArgumentsCheckArity(target, pos, filters);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2596
        MethodHandle adapter = target;
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2597
        int curPos = pos-1;  // pre-incremented
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2598
        for (MethodHandle filter : filters) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2599
            curPos += 1;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2600
            if (filter == null)  continue;  // ignore null elements of filters
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2601
            adapter = filterArgument(adapter, curPos, filter);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2602
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2603
        return adapter;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2604
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2605
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2606
    /*non-public*/ static
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2607
    MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2608
        filterArgumentChecks(target, pos, filter);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2609
        MethodType targetType = target.type();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2610
        MethodType filterType = filter.type();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2611
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2612
        Class<?> newParamType = filterType.parameterType(0);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2613
        LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2614
        MethodType newType = targetType.changeParameterType(pos, newParamType);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2615
        result = result.copyWithExtendL(newType, lform, filter);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2616
        return result;
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2617
    }
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2618
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2619
    private static void filterArgumentsCheckArity(MethodHandle target, int pos, MethodHandle[] filters) {
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2620
        MethodType targetType = target.type();
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2621
        int maxPos = targetType.parameterCount();
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2622
        if (pos + filters.length > maxPos)
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2623
            throw newIllegalArgumentException("too many filters");
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2624
    }
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2625
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2626
    private static void filterArgumentChecks(MethodHandle target, int pos, MethodHandle filter) throws RuntimeException {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2627
        MethodType targetType = target.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2628
        MethodType filterType = filter.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2629
        if (filterType.parameterCount() != 1
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2630
            || filterType.returnType() != targetType.parameterType(pos))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2631
            throw newIllegalArgumentException("target and filter types do not match", targetType, filterType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2632
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2633
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2634
    /**
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2635
     * Adapts a target method handle by pre-processing
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2636
     * a sub-sequence of its arguments with a filter (another method handle).
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2637
     * The pre-processed arguments are replaced by the result (if any) of the
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2638
     * filter function.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2639
     * The target is then called on the modified (usually shortened) argument list.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2640
     * <p>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2641
     * If the filter returns a value, the target must accept that value as
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2642
     * its argument in position {@code pos}, preceded and/or followed by
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2643
     * any arguments not passed to the filter.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2644
     * If the filter returns void, the target must accept all arguments
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2645
     * not passed to the filter.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2646
     * No arguments are reordered, and a result returned from the filter
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2647
     * replaces (in order) the whole subsequence of arguments originally
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2648
     * passed to the adapter.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2649
     * <p>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2650
     * The argument types (if any) of the filter
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2651
     * replace zero or one argument types of the target, at position {@code pos},
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2652
     * in the resulting adapted method handle.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2653
     * The return type of the filter (if any) must be identical to the
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2654
     * argument type of the target at position {@code pos}, and that target argument
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2655
     * is supplied by the return value of the filter.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2656
     * <p>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2657
     * In all cases, {@code pos} must be greater than or equal to zero, and
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2658
     * {@code pos} must also be less than or equal to the target's arity.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2659
     * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2660
     * <blockquote><pre>{@code
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2661
import static java.lang.invoke.MethodHandles.*;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2662
import static java.lang.invoke.MethodType.*;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2663
...
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2664
MethodHandle deepToString = publicLookup()
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2665
  .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2666
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2667
MethodHandle ts1 = deepToString.asCollector(String[].class, 1);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2668
assertEquals("[strange]", (String) ts1.invokeExact("strange"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2669
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2670
MethodHandle ts2 = deepToString.asCollector(String[].class, 2);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2671
assertEquals("[up, down]", (String) ts2.invokeExact("up", "down"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2672
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2673
MethodHandle ts3 = deepToString.asCollector(String[].class, 3);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2674
MethodHandle ts3_ts2 = collectArguments(ts3, 1, ts2);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2675
assertEquals("[top, [up, down], strange]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2676
             (String) ts3_ts2.invokeExact("top", "up", "down", "strange"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2677
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2678
MethodHandle ts3_ts2_ts1 = collectArguments(ts3_ts2, 3, ts1);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2679
assertEquals("[top, [up, down], [strange]]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2680
             (String) ts3_ts2_ts1.invokeExact("top", "up", "down", "strange"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2681
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2682
MethodHandle ts3_ts2_ts3 = collectArguments(ts3_ts2, 1, ts3);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2683
assertEquals("[top, [[up, down, strange], charm], bottom]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2684
             (String) ts3_ts2_ts3.invokeExact("top", "up", "down", "strange", "charm", "bottom"));
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2685
     * }</pre></blockquote>
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2686
     * <p> Here is pseudocode for the resulting adapter:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2687
     * <blockquote><pre>{@code
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2688
     * T target(A...,V,C...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2689
     * V filter(B...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2690
     * T adapter(A... a,B... b,C... c) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2691
     *   V v = filter(b...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2692
     *   return target(a...,v,c...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2693
     * }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2694
     * // and if the filter has no arguments:
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2695
     * T target2(A...,V,C...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2696
     * V filter2();
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2697
     * T adapter2(A... a,C... c) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2698
     *   V v = filter2();
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2699
     *   return target2(a...,v,c...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2700
     * }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2701
     * // and if the filter has a void return:
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2702
     * T target3(A...,C...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2703
     * void filter3(B...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2704
     * void adapter3(A... a,B... b,C... c) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2705
     *   filter3(b...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2706
     *   return target3(a...,c...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2707
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2708
     * }</pre></blockquote>
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2709
     * <p>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2710
     * A collection adapter {@code collectArguments(mh, 0, coll)} is equivalent to
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2711
     * one which first "folds" the affected arguments, and then drops them, in separate
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2712
     * steps as follows:
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2713
     * <blockquote><pre>{@code
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2714
     * mh = MethodHandles.dropArguments(mh, 1, coll.type().parameterList()); //step 2
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2715
     * mh = MethodHandles.foldArguments(mh, coll); //step 1
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2716
     * }</pre></blockquote>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2717
     * If the target method handle consumes no arguments besides than the result
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2718
     * (if any) of the filter {@code coll}, then {@code collectArguments(mh, 0, coll)}
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2719
     * is equivalent to {@code filterReturnValue(coll, mh)}.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2720
     * If the filter method handle {@code coll} consumes one argument and produces
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2721
     * a non-void result, then {@code collectArguments(mh, N, coll)}
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2722
     * is equivalent to {@code filterArguments(mh, N, coll)}.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2723
     * Other equivalences are possible but would require argument permutation.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2724
     *
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2725
     * @param target the method handle to invoke after filtering the subsequence of arguments
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2726
     * @param pos the position of the first adapter argument to pass to the filter,
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2727
     *            and/or the target argument which receives the result of the filter
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2728
     * @param filter method handle to call on the subsequence of arguments
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2729
     * @return method handle which incorporates the specified argument subsequence filtering logic
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2730
     * @throws NullPointerException if either argument is null
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2731
     * @throws IllegalArgumentException if the return type of {@code filter}
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2732
     *          is non-void and is not the same as the {@code pos} argument of the target,
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2733
     *          or if {@code pos} is not between 0 and the target's arity, inclusive,
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2734
     *          or if the resulting method handle's type would have
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2735
     *          <a href="MethodHandle.html#maxarity">too many parameters</a>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2736
     * @see MethodHandles#foldArguments
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2737
     * @see MethodHandles#filterArguments
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2738
     * @see MethodHandles#filterReturnValue
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2739
     */
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2740
    public static
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2741
    MethodHandle collectArguments(MethodHandle target, int pos, MethodHandle filter) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2742
        MethodType newType = collectArgumentsChecks(target, pos, filter);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2743
        MethodType collectorType = filter.type();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2744
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2745
        LambdaForm lform;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2746
        if (collectorType.returnType().isArray() && filter.intrinsicName() == Intrinsic.NEW_ARRAY) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2747
            lform = result.editor().collectArgumentArrayForm(1 + pos, filter);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2748
            if (lform != null) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2749
                return result.copyWith(newType, lform);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2750
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2751
        }
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2752
        lform = result.editor().collectArgumentsForm(1 + pos, collectorType.basicType());
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2753
        return result.copyWithExtendL(newType, lform, filter);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2754
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2755
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2756
    private static MethodType collectArgumentsChecks(MethodHandle target, int pos, MethodHandle filter) throws RuntimeException {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2757
        MethodType targetType = target.type();
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  2758
        MethodType filterType = filter.type();
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2759
        Class<?> rtype = filterType.returnType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2760
        List<Class<?>> filterArgs = filterType.parameterList();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2761
        if (rtype == void.class) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2762
            return targetType.insertParameterTypes(pos, filterArgs);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2763
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2764
        if (rtype != targetType.parameterType(pos)) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2765
            throw newIllegalArgumentException("target and filter types do not match", targetType, filterType);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2766
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  2767
        return targetType.dropParameterTypes(pos, pos+1).insertParameterTypes(pos, filterArgs);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2768
    }
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  2769
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2770
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2771
     * Adapts a target method handle by post-processing
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2772
     * its return value (if any) with a filter (another method handle).
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2773
     * The result of the filter is returned from the adapter.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2774
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2775
     * If the target returns a value, the filter must accept that value as
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2776
     * its only argument.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2777
     * If the target returns void, the filter must accept no arguments.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2778
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2779
     * The return type of the filter
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2780
     * replaces the return type of the target
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2781
     * in the resulting adapted method handle.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2782
     * The argument type of the filter (if any) must be identical to the
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2783
     * return type of the target.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2784
     * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2785
     * <blockquote><pre>{@code
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2786
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2787
import static java.lang.invoke.MethodType.*;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2788
...
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2789
MethodHandle cat = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2790
  "concat", methodType(String.class, String.class));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2791
MethodHandle length = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2792
  "length", methodType(int.class));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2793
System.out.println((String) cat.invokeExact("x", "y")); // xy
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2794
MethodHandle f0 = filterReturnValue(cat, length);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2795
System.out.println((int) f0.invokeExact("x", "y")); // 2
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2796
     * }</pre></blockquote>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2797
     * <p> Here is pseudocode for the resulting adapter:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2798
     * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2799
     * V target(A...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2800
     * T filter(V);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2801
     * T adapter(A... a) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2802
     *   V v = target(a...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2803
     *   return filter(v);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2804
     * }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2805
     * // and if the target has a void return:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2806
     * void target2(A...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2807
     * T filter2();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2808
     * T adapter2(A... a) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2809
     *   target2(a...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2810
     *   return filter2();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2811
     * }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2812
     * // and if the filter has a void return:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2813
     * V target3(A...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2814
     * void filter3(V);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2815
     * void adapter3(A... a) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2816
     *   V v = target3(a...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2817
     *   filter3(v);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2818
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2819
     * }</pre></blockquote>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2820
     * @param target the method handle to invoke before filtering the return value
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2821
     * @param filter method handle to call on the return value
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2822
     * @return method handle which incorporates the specified return value filtering logic
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2823
     * @throws NullPointerException if either argument is null
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2824
     * @throws IllegalArgumentException if the argument list of {@code filter}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2825
     *          does not match the return type of target as described above
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  2826
     */
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2827
    public static
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  2828
    MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  2829
        MethodType targetType = target.type();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  2830
        MethodType filterType = filter.type();
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2831
        filterReturnValueChecks(targetType, filterType);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2832
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2833
        BasicType rtype = BasicType.basicType(filterType.returnType());
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2834
        LambdaForm lform = result.editor().filterReturnForm(rtype, false);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2835
        MethodType newType = targetType.changeReturnType(filterType.returnType());
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2836
        result = result.copyWithExtendL(newType, lform, filter);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2837
        return result;
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2838
    }
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2839
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2840
    private static void filterReturnValueChecks(MethodType targetType, MethodType filterType) throws RuntimeException {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2841
        Class<?> rtype = targetType.returnType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2842
        int filterValues = filterType.parameterCount();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2843
        if (filterValues == 0
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2844
                ? (rtype != void.class)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2845
                : (rtype != filterType.parameterType(0)))
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2846
            throw newIllegalArgumentException("target and filter types do not match", targetType, filterType);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  2847
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  2848
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2849
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2850
     * Adapts a target method handle by pre-processing
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2851
     * some of its arguments, and then calling the target with
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2852
     * the result of the pre-processing, inserted into the original
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2853
     * sequence of arguments.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2854
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2855
     * The pre-processing is performed by {@code combiner}, a second method handle.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2856
     * Of the arguments passed to the adapter, the first {@code N} arguments
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2857
     * are copied to the combiner, which is then called.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2858
     * (Here, {@code N} is defined as the parameter count of the combiner.)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2859
     * After this, control passes to the target, with any result
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2860
     * from the combiner inserted before the original {@code N} incoming
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2861
     * arguments.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2862
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2863
     * If the combiner returns a value, the first parameter type of the target
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2864
     * must be identical with the return type of the combiner, and the next
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2865
     * {@code N} parameter types of the target must exactly match the parameters
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2866
     * of the combiner.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2867
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2868
     * If the combiner has a void return, no result will be inserted,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2869
     * and the first {@code N} parameter types of the target
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2870
     * must exactly match the parameters of the combiner.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2871
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2872
     * The resulting adapter is the same type as the target, except that the
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2873
     * first parameter type is dropped,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2874
     * if it corresponds to the result of the combiner.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2875
     * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2876
     * (Note that {@link #dropArguments(MethodHandle,int,List) dropArguments} can be used to remove any arguments
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2877
     * that either the combiner or the target does not wish to receive.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2878
     * If some of the incoming arguments are destined only for the combiner,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2879
     * consider using {@link MethodHandle#asCollector asCollector} instead, since those
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2880
     * arguments will not need to be live on the stack on entry to the
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2881
     * target.)
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2882
     * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2883
     * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2884
import static java.lang.invoke.MethodHandles.*;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2885
import static java.lang.invoke.MethodType.*;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2886
...
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2887
MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2888
  "println", methodType(void.class, String.class))
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2889
    .bindTo(System.out);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2890
MethodHandle cat = lookup().findVirtual(String.class,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2891
  "concat", methodType(String.class, String.class));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2892
assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2893
MethodHandle catTrace = foldArguments(cat, trace);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2894
// also prints "boo":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2895
assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2896
     * }</pre></blockquote>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2897
     * <p> Here is pseudocode for the resulting adapter:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2898
     * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2899
     * // there are N arguments in A...
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2900
     * T target(V, A[N]..., B...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2901
     * V combiner(A...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2902
     * T adapter(A... a, B... b) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2903
     *   V v = combiner(a...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2904
     *   return target(v, a..., b...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2905
     * }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2906
     * // and if the combiner has a void return:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2907
     * T target2(A[N]..., B...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2908
     * void combiner2(A...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2909
     * T adapter2(A... a, B... b) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2910
     *   combiner2(a...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2911
     *   return target2(a..., b...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2912
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2913
     * }</pre></blockquote>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2914
     * @param target the method handle to invoke after arguments are combined
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2915
     * @param combiner method handle to call initially on the incoming arguments
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2916
     * @return method handle which incorporates the specified argument folding logic
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2917
     * @throws NullPointerException if either argument is null
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2918
     * @throws IllegalArgumentException if {@code combiner}'s return type
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2919
     *          is non-void and not the same as the first argument type of
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2920
     *          the target, or if the initial {@code N} argument types
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2921
     *          of the target
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2922
     *          (skipping one matching the {@code combiner}'s return type)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2923
     *          are not identical with the argument types of {@code combiner}
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2924
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2925
    public static
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2926
    MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) {
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2927
        int foldPos = 0;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2928
        MethodType targetType = target.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2929
        MethodType combinerType = combiner.type();
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2930
        Class<?> rtype = foldArgumentChecks(foldPos, targetType, combinerType);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2931
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2932
        boolean dropResult = (rtype == void.class);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2933
        // Note:  This may cache too many distinct LFs. Consider backing off to varargs code.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2934
        LambdaForm lform = result.editor().foldArgumentsForm(1 + foldPos, dropResult, combinerType.basicType());
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2935
        MethodType newType = targetType;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2936
        if (!dropResult)
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2937
            newType = newType.dropParameterTypes(foldPos, foldPos + 1);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2938
        result = result.copyWithExtendL(newType, lform, combiner);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  2939
        return result;
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2940
    }
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2941
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2942
    private static Class<?> foldArgumentChecks(int foldPos, MethodType targetType, MethodType combinerType) {
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2943
        int foldArgs   = combinerType.parameterCount();
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2944
        Class<?> rtype = combinerType.returnType();
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2945
        int foldVals = rtype == void.class ? 0 : 1;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2946
        int afterInsertPos = foldPos + foldVals;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2947
        boolean ok = (targetType.parameterCount() >= afterInsertPos + foldArgs);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2948
        if (ok && !(combinerType.parameterList()
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2949
                    .equals(targetType.parameterList().subList(afterInsertPos,
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  2950
                                                               afterInsertPos + foldArgs))))
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  2951
            ok = false;
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2952
        if (ok && foldVals != 0 && combinerType.returnType() != targetType.parameterType(0))
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  2953
            ok = false;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2954
        if (!ok)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2955
            throw misMatchedTypes("target and combiner types", targetType, combinerType);
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  2956
        return rtype;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2957
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2958
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2959
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  2960
     * Makes a method handle which adapts a target method handle,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2961
     * by guarding it with a test, a boolean-valued method handle.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2962
     * If the guard fails, a fallback handle is called instead.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2963
     * All three method handles must have the same corresponding
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2964
     * argument and return types, except that the return type
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2965
     * of the test must be boolean, and the test is allowed
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2966
     * to have fewer arguments than the other two method handles.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2967
     * <p> Here is pseudocode for the resulting adapter:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2968
     * <blockquote><pre>{@code
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2969
     * boolean test(A...);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2970
     * T target(A...,B...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2971
     * T fallback(A...,B...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2972
     * T adapter(A... a,B... b) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2973
     *   if (test(a...))
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2974
     *     return target(a..., b...);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2975
     *   else
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2976
     *     return fallback(a..., b...);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2977
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2978
     * }</pre></blockquote>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2979
     * Note that the test arguments ({@code a...} in the pseudocode) cannot
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2980
     * be modified by execution of the test, and so are passed unchanged
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2981
     * from the caller to the target or fallback as appropriate.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2982
     * @param test method handle used for test, must return boolean
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2983
     * @param target method handle to call if test passes
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2984
     * @param fallback method handle to call if test fails
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2985
     * @return method handle which incorporates the specified if/then/else logic
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2986
     * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2987
     * @throws IllegalArgumentException if {@code test} does not return boolean,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2988
     *          or if all three method types do not match (with the return
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2989
     *          type of {@code test} changed to match that of the target).
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2990
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2991
    public static
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2992
    MethodHandle guardWithTest(MethodHandle test,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2993
                               MethodHandle target,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2994
                               MethodHandle fallback) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2995
        MethodType gtype = test.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2996
        MethodType ttype = target.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2997
        MethodType ftype = fallback.type();
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  2998
        if (!ttype.equals(ftype))
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2999
            throw misMatchedTypes("target and fallback types", ttype, ftype);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3000
        if (gtype.returnType() != boolean.class)
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3001
            throw newIllegalArgumentException("guard type is not a predicate "+gtype);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3002
        List<Class<?>> targs = ttype.parameterList();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3003
        List<Class<?>> gargs = gtype.parameterList();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3004
        if (!targs.equals(gargs)) {
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3005
            int gpc = gargs.size(), tpc = targs.size();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3006
            if (gpc >= tpc || !targs.subList(0, gpc).equals(gargs))
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3007
                throw misMatchedTypes("target and test types", ttype, gtype);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3008
            test = dropArguments(test, gpc, targs.subList(gpc, tpc));
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3009
            gtype = test.type();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3010
        }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  3011
        return MethodHandleImpl.makeGuardWithTest(test, target, fallback);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3012
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3013
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3014
    static RuntimeException misMatchedTypes(String what, MethodType t1, MethodType t2) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3015
        return newIllegalArgumentException(what + " must match: " + t1 + " != " + t2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3016
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3017
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3018
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  3019
     * Makes a method handle which adapts a target method handle,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3020
     * by running it inside an exception handler.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3021
     * If the target returns normally, the adapter returns that value.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3022
     * If an exception matching the specified type is thrown, the fallback
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3023
     * handle is called instead on the exception, plus the original arguments.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3024
     * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3025
     * The target and handler must have the same corresponding
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3026
     * argument and return types, except that handler may omit trailing arguments
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3027
     * (similarly to the predicate in {@link #guardWithTest guardWithTest}).
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3028
     * Also, the handler must have an extra leading parameter of {@code exType} or a supertype.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3029
     * <p> Here is pseudocode for the resulting adapter:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3030
     * <blockquote><pre>{@code
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3031
     * T target(A..., B...);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3032
     * T handler(ExType, A...);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3033
     * T adapter(A... a, B... b) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3034
     *   try {
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3035
     *     return target(a..., b...);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3036
     *   } catch (ExType ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3037
     *     return handler(ex, a...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3038
     *   }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3039
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3040
     * }</pre></blockquote>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3041
     * Note that the saved arguments ({@code a...} in the pseudocode) cannot
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3042
     * be modified by execution of the target, and so are passed unchanged
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3043
     * from the caller to the handler, if the handler is invoked.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3044
     * <p>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3045
     * The target and handler must return the same type, even if the handler
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3046
     * always throws.  (This might happen, for instance, because the handler
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3047
     * is simulating a {@code finally} clause).
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3048
     * To create such a throwing handler, compose the handler creation logic
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3049
     * with {@link #throwException throwException},
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3050
     * in order to create a method handle of the correct return type.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3051
     * @param target method handle to call
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3052
     * @param exType the type of exception which the handler will catch
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3053
     * @param handler method handle to call if a matching exception is thrown
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3054
     * @return method handle which incorporates the specified try/catch logic
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3055
     * @throws NullPointerException if any argument is null
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3056
     * @throws IllegalArgumentException if {@code handler} does not accept
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3057
     *          the given exception type, or if the method handle types do
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3058
     *          not match in their return types and their
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3059
     *          corresponding parameters
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3060
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3061
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3062
    MethodHandle catchException(MethodHandle target,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3063
                                Class<? extends Throwable> exType,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3064
                                MethodHandle handler) {
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3065
        MethodType ttype = target.type();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3066
        MethodType htype = handler.type();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3067
        if (htype.parameterCount() < 1 ||
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3068
            !htype.parameterType(0).isAssignableFrom(exType))
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3069
            throw newIllegalArgumentException("handler does not accept exception type "+exType);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3070
        if (htype.returnType() != ttype.returnType())
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3071
            throw misMatchedTypes("target and handler return types", ttype, htype);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3072
        List<Class<?>> targs = ttype.parameterList();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3073
        List<Class<?>> hargs = htype.parameterList();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3074
        hargs = hargs.subList(1, hargs.size());  // omit leading parameter from handler
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3075
        if (!targs.equals(hargs)) {
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3076
            int hpc = hargs.size(), tpc = targs.size();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3077
            if (hpc >= tpc || !targs.subList(0, hpc).equals(hargs))
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3078
                throw misMatchedTypes("target and handler types", ttype, htype);
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
  3079
            handler = dropArguments(handler, 1+hpc, targs.subList(hpc, tpc));
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3080
            htype = handler.type();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  3081
        }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  3082
        return MethodHandleImpl.makeGuardWithCatch(target, exType, handler);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3083
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3084
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3085
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3086
     * Produces a method handle which will throw exceptions of the given {@code exType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3087
     * The method handle will accept a single argument of {@code exType},
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3088
     * and immediately throw it as an exception.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3089
     * The method type will nominally specify a return of {@code returnType}.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3090
     * The return type may be anything convenient:  It doesn't matter to the
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3091
     * method handle's behavior, since it will never return normally.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  3092
     * @param returnType the return type of the desired method handle
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  3093
     * @param exType the parameter type of the desired method handle
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3094
     * @return method handle which can throw the given exceptions
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3095
     * @throws NullPointerException if either argument is null
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3096
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3097
    public static
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3098
    MethodHandle throwException(Class<?> returnType, Class<? extends Throwable> exType) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3099
        if (!Throwable.class.isAssignableFrom(exType))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3100
            throw new ClassCastException(exType.getName());
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  3101
        return MethodHandleImpl.throwException(MethodType.methodType(returnType, exType));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3102
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3103
}