src/java.base/share/classes/java/lang/invoke/MethodHandles.java
author mchung
Tue, 13 Aug 2019 15:49:11 -0700
changeset 57735 7ba5e49258de
parent 57558 5e637f790bb8
child 58056 db92a157dd70
permissions -rw-r--r--
8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works 8209078: Unable to call default method from interface in another module from named module Reviewed-by: dfuchs, 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
/*
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
     2
 * Copyright (c) 2008, 2019, 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
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52226
diff changeset
    28
import jdk.internal.access.SharedSecrets;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    29
import jdk.internal.module.IllegalAccessLogger;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    30
import jdk.internal.org.objectweb.asm.ClassReader;
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    31
import jdk.internal.reflect.CallerSensitive;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    32
import jdk.internal.reflect.Reflection;
40175
8df87018d96a 8161379: Force inline methods calling Reflection.getCallerClass
redestad
parents: 39756
diff changeset
    33
import jdk.internal.vm.annotation.ForceInline;
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    34
import sun.invoke.util.ValueConversions;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    35
import sun.invoke.util.VerifyAccess;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    36
import sun.invoke.util.Wrapper;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    37
import sun.reflect.misc.ReflectUtil;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    38
import sun.security.util.SecurityConstants;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    39
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    40
import java.lang.invoke.LambdaForm.BasicType;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    41
import java.lang.reflect.Constructor;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    42
import java.lang.reflect.Field;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    43
import java.lang.reflect.Member;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    44
import java.lang.reflect.Method;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    45
import java.lang.reflect.Modifier;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    46
import java.lang.reflect.ReflectPermission;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    47
import java.nio.ByteOrder;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    48
import java.security.AccessController;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    49
import java.security.PrivilegedAction;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    50
import java.security.ProtectionDomain;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
    51
import java.util.ArrayList;
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    52
import java.util.Arrays;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
    53
import java.util.BitSet;
37539
fc220bc54b59 8150956: j.l.i.MethodHandles.whileLoop(...) and .iteratedLoop(...) throw unexpected exceptions in the case of 'init' return type is void
mhaupt
parents: 37363
diff changeset
    54
import java.util.Iterator;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
    55
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
    56
import java.util.Objects;
51798
f55a4bc91ef4 8210496: Improve filtering for classes with security sensitive fields
alanb
parents: 51632
diff changeset
    57
import java.util.Set;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
    58
import java.util.concurrent.ConcurrentHashMap;
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
    59
import java.util.stream.Collectors;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
    60
import java.util.stream.Stream;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    61
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    62
import static java.lang.invoke.MethodHandleImpl.Intrinsic;
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
    63
import static java.lang.invoke.MethodHandleNatives.Constants.*;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
    64
import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
37539
fc220bc54b59 8150956: j.l.i.MethodHandles.whileLoop(...) and .iteratedLoop(...) throw unexpected exceptions in the case of 'init' return type is void
mhaupt
parents: 37363
diff changeset
    65
import static java.lang.invoke.MethodType.methodType;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
    66
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    67
/**
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    68
 * 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
    69
 * method handles. They fall into several categories:
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    70
 * <ul>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
    71
 * <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
    72
 * <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
    73
 * <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
    74
 * </ul>
50927
d99e206cc32e 8177275: IllegalArgumentException when MH would have too many parameters is not specified for several methods
vtheeyarath
parents: 50771
diff changeset
    75
 * A lookup, combinator, or factory method will fail and throw an
d99e206cc32e 8177275: IllegalArgumentException when MH would have too many parameters is not specified for several methods
vtheeyarath
parents: 50771
diff changeset
    76
 * {@code IllegalArgumentException} if the created method handle's type
d99e206cc32e 8177275: IllegalArgumentException when MH would have too many parameters is not specified for several methods
vtheeyarath
parents: 50771
diff changeset
    77
 * would have <a href="MethodHandle.html#maxarity">too many parameters</a>.
24367
705490680527 8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents: 23898
diff changeset
    78
 *
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    79
 * @author John Rose, JSR 292 EG
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
    80
 * @since 1.7
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    81
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    82
public class MethodHandles {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    83
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    84
    private MethodHandles() { }  // do not instantiate
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    85
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
    86
    static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 34430
diff changeset
    87
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    88
    // See IMPL_LOOKUP below.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    89
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    90
    //// Method handle creation from ordinary methods.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    91
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    92
    /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    93
     * Returns a {@link Lookup lookup object} with
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    94
     * 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
    95
     * 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
    96
     * Factory methods on the lookup object can create
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    97
     * <a href="MethodHandleInfo.html#directmh">direct method handles</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
    98
     * 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
    99
     * including protected and private fields and methods.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   100
     * 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
   101
     * 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
   102
     * <p>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   103
     * 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
   104
     * values to different callers.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   105
     * @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
   106
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
   107
    @CallerSensitive
40175
8df87018d96a 8161379: Force inline methods calling Reflection.getCallerClass
redestad
parents: 39756
diff changeset
   108
    @ForceInline // to ensure Reflection.getCallerClass optimization
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   109
    public static Lookup lookup() {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
   110
        return new Lookup(Reflection.getCallerClass());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   111
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   112
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   113
    /**
45027
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   114
     * This reflected$lookup method is the alternate implementation of
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   115
     * the lookup method when being invoked by reflection.
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   116
     */
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   117
    @CallerSensitive
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   118
    private static Lookup reflected$lookup() {
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   119
        Class<?> caller = Reflection.getCallerClass();
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   120
        if (caller.getClassLoader() == null) {
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   121
            throw newIllegalArgumentException("illegal lookupClass: "+caller);
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   122
        }
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   123
        return new Lookup(caller);
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   124
    }
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   125
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
   126
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   127
     * Returns a {@link Lookup lookup object} which is trusted minimally.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   128
     * The lookup has the {@code UNCONDITIONAL} mode.
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   129
     * It can only be used to create method handles to public members of
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
   130
     * public classes in packages that are exported unconditionally.
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   131
     * <p>
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46148
diff changeset
   132
     * As a matter of pure convention, the {@linkplain Lookup#lookupClass() lookup class}
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   133
     * of this lookup object will be {@link java.lang.Object}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   134
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   135
     * @apiNote The use of Object is conventional, and because the lookup modes are
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   136
     * limited, there is no special access provided to the internals of Object, its package
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   137
     * or its module.  This public lookup object or other lookup object with
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   138
     * {@code UNCONDITIONAL} mode assumes readability. Consequently, the lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   139
     * is not used to determine the lookup context.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   140
     *
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   141
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   142
     * <em>Discussion:</em>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   143
     * 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
   144
     * {@link Lookup#in publicLookup().in(C.class)}.
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   145
     * 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
   146
     * <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
   147
     * Also, it cannot access
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
   148
     * <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
   149
     * @return a lookup object which is trusted minimally
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   150
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   151
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   152
     * @spec JPMS
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   153
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   154
    public static Lookup publicLookup() {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   155
        return Lookup.PUBLIC_LOOKUP;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   156
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   157
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   158
    /**
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   159
     * Returns a {@link Lookup lookup} object on a target class to emulate all supported
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   160
     * bytecode behaviors, including <a href="MethodHandles.Lookup.html#privacc"> private access</a>.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   161
     * The returned lookup object can provide access to classes in modules and packages,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   162
     * and members of those classes, outside the normal rules of Java access control,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   163
     * instead conforming to the more permissive rules for modular <em>deep reflection</em>.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   164
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   165
     * A caller, specified as a {@code Lookup} object, in module {@code M1} is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   166
     * allowed to do deep reflection on module {@code M2} and package of the target class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   167
     * if and only if all of the following conditions are {@code true}:
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   168
     * <ul>
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   169
     * <li>If there is a security manager, its {@code checkPermission} method is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   170
     * called to check {@code ReflectPermission("suppressAccessChecks")} and
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   171
     * that must return normally.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   172
     * <li>The caller lookup object must have the {@link Lookup#MODULE MODULE} lookup mode.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   173
     * (This is because otherwise there would be no way to ensure the original lookup
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   174
     * creator was a member of any particular module, and so any subsequent checks
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   175
     * for readability and qualified exports would become ineffective.)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   176
     * <li>The caller lookup object must have {@link Lookup#PRIVATE PRIVATE} access.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   177
     * (This is because an application intending to share intra-module access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   178
     * using {@link Lookup#MODULE MODULE} alone will inadvertently also share
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   179
     * deep reflection to its own module.)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   180
     * <li>The target class must be a proper class, not a primitive or array class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   181
     * (Thus, {@code M2} is well-defined.)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   182
     * <li>If the caller module {@code M1} differs from
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   183
     * the target module {@code M2} then both of the following must be true:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   184
     *   <ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   185
     *     <li>{@code M1} {@link Module#canRead reads} {@code M2}.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   186
     *     <li>{@code M2} {@link Module#isOpen(String,Module) opens} the package
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   187
     *         containing the target class to at least {@code M1}.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   188
     *   </ul>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   189
     * </ul>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   190
     * <p>
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   191
     * If any of the above checks is violated, this method fails with an
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   192
     * exception.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   193
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   194
     * Otherwise, if {@code M1} and {@code M2} are the same module, this method
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   195
     * returns a {@code Lookup} on {@code targetClass} with full capabilities and
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   196
     * {@code null} previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   197
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   198
     * Otherwise, {@code M1} and {@code M2} are two different modules.  This method
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   199
     * returns a {@code Lookup} on {@code targetClass} that records
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   200
     * the lookup class of the caller as the new previous lookup class and
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   201
     * drops {@code MODULE} access from the full capabilities mode.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   202
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   203
     * @param targetClass the target class
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   204
     * @param caller the caller lookup object
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   205
     * @return a lookup object for the target class, with private access
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   206
     * @throws IllegalArgumentException if {@code targetClass} is a primitive type or array class
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   207
     * @throws NullPointerException if {@code targetClass} or {@code caller} is {@code null}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   208
     * @throws SecurityException if denied by the security manager
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   209
     * @throws IllegalAccessException if any of the other access checks specified above fails
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   210
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
   211
     * @spec JPMS
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   212
     * @see Lookup#dropLookupMode
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   213
     * @see <a href="MethodHandles.Lookup.html#cross-module-lookup">Cross-module lookups</a>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   214
     */
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   215
    public static Lookup privateLookupIn(Class<?> targetClass, Lookup caller) throws IllegalAccessException {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   216
        SecurityManager sm = System.getSecurityManager();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   217
        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   218
        if (targetClass.isPrimitive())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   219
            throw new IllegalArgumentException(targetClass + " is a primitive class");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   220
        if (targetClass.isArray())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   221
            throw new IllegalArgumentException(targetClass + " is an array class");
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   222
        // Ensure that we can reason accurately about private and module access.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   223
        if ((caller.lookupModes() & Lookup.PRIVATE) == 0)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   224
            throw new IllegalAccessException("caller does not have PRIVATE lookup mode");
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   225
        if ((caller.lookupModes() & Lookup.MODULE) == 0)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   226
            throw new IllegalAccessException("caller does not have MODULE lookup mode");
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   227
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   228
        // previous lookup class is never set if it has MODULE access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   229
        assert caller.previousLookupClass() == null;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   230
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   231
        Class<?> callerClass = caller.lookupClass();
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   232
        Module callerModule = callerClass.getModule();  // M1
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   233
        Module targetModule = targetClass.getModule();  // M2
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   234
        Class<?> newPreviousClass = null;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   235
        int newModes = Lookup.FULL_POWER_MODES;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   236
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   237
        if (targetModule != callerModule) {
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   238
            if (!callerModule.canRead(targetModule))
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   239
                throw new IllegalAccessException(callerModule + " does not read " + targetModule);
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   240
            if (targetModule.isNamed()) {
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   241
                String pn = targetClass.getPackageName();
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   242
                assert !pn.isEmpty() : "unnamed package cannot be in named module";
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   243
                if (!targetModule.isOpen(pn, callerModule))
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   244
                    throw new IllegalAccessException(targetModule + " does not open " + pn + " to " + callerModule);
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   245
            }
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   246
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   247
            // M2 != M1, set previous lookup class to M1 and drop MODULE access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   248
            newPreviousClass = callerClass;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   249
            newModes &= ~Lookup.MODULE;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   250
        }
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   251
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   252
        if (!callerModule.isNamed() && targetModule.isNamed()) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   253
            IllegalAccessLogger logger = IllegalAccessLogger.illegalAccessLogger();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   254
            if (logger != null) {
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   255
                logger.logIfOpenedForIllegalAccess(caller, targetClass);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   256
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   257
        }
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   258
        return Lookup.newLookup(targetClass, newPreviousClass, newModes);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   259
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   260
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
   261
    /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   262
     * Performs an unchecked "crack" of a
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   263
     * <a href="MethodHandleInfo.html#directmh">direct method handle</a>.
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   264
     * The result is as if the user had obtained a lookup object capable enough
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   265
     * to crack the target method handle, called
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   266
     * {@link java.lang.invoke.MethodHandles.Lookup#revealDirect Lookup.revealDirect}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   267
     * on the target to obtain its symbolic reference, and then called
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   268
     * {@link java.lang.invoke.MethodHandleInfo#reflectAs MethodHandleInfo.reflectAs}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   269
     * to resolve the symbolic reference to a member.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   270
     * <p>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   271
     * If there is a security manager, its {@code checkPermission} method
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   272
     * is called with a {@code ReflectPermission("suppressAccessChecks")} permission.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   273
     * @param <T> the desired type of the result, either {@link Member} or a subtype
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   274
     * @param target a direct method handle to crack into symbolic reference components
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   275
     * @param expected a class object representing the desired result type {@code T}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   276
     * @return a reference to the method, constructor, or field object
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   277
     * @exception SecurityException if the caller is not privileged to call {@code setAccessible}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   278
     * @exception NullPointerException if either argument is {@code null}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   279
     * @exception IllegalArgumentException if the target is not a direct method handle
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   280
     * @exception ClassCastException if the member is not of the expected type
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   281
     * @since 1.8
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   282
     */
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   283
    public static <T extends Member> T
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   284
    reflectAs(Class<T> expected, MethodHandle target) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   285
        SecurityManager smgr = System.getSecurityManager();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   286
        if (smgr != null)  smgr.checkPermission(ACCESS_PERMISSION);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   287
        Lookup lookup = Lookup.IMPL_LOOKUP;  // use maximally privileged lookup
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   288
        return lookup.revealDirect(target).reflectAs(expected, lookup);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   289
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   290
    // Copied from AccessibleObject, as used by Method.setAccessible, etc.:
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
   291
    private static final java.security.Permission ACCESS_PERMISSION =
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   292
        new ReflectPermission("suppressAccessChecks");
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   293
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
   294
    /**
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   295
     * 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
   296
     * when the creation requires access checking.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   297
     * Method handles do not perform
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   298
     * 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
   299
     * Therefore, method handle access
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   300
     * 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
   301
     * The caller class against which those restrictions are enforced
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46148
diff changeset
   302
     * is known as the {@linkplain #lookupClass() lookup class}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   303
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   304
     * A lookup class which needs to create method handles will call
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46148
diff changeset
   305
     * {@link MethodHandles#lookup() MethodHandles.lookup} to create a factory for itself.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   306
     * 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
   307
     * determined, and securely stored in the {@code Lookup} object.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   308
     * 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
   309
     * 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
   310
     * 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
   311
     * even private ones.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   312
     *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   313
     * <h2><a id="lookups"></a>Lookup Factory Methods</h2>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   314
     * 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
   315
     * use cases for methods, constructors, and fields.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   316
     * 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
   317
     * equivalent of a particular <em>bytecode behavior</em>.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   318
     * (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
   319
     * Here is a summary of the correspondence between these factory methods and
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
   320
     * the behavior of the resulting method handles:
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45027
diff changeset
   321
     * <table class="striped">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45027
diff changeset
   322
     * <caption style="display:none">lookup method behaviors</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45027
diff changeset
   323
     * <thead>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   324
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   325
     *     <th scope="col"><a id="equiv"></a>lookup expression</th>
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   326
     *     <th scope="col">member</th>
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   327
     *     <th scope="col">bytecode behavior</th>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   328
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45027
diff changeset
   329
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45027
diff changeset
   330
     * <tbody>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   331
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   332
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   333
     *     <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
   334
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   335
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   336
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</th>
57735
7ba5e49258de 8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents: 57558
diff changeset
   337
     *     <td>{@code static}<br>{@code FT f;}</td><td>{@code (FT) C.f;}</td>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   338
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   339
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   340
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   341
     *     <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
   342
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   343
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   344
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findStaticSetter lookup.findStaticSetter(C.class,"f",FT.class)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   345
     *     <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
   346
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   347
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   348
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findVirtual lookup.findVirtual(C.class,"m",MT)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   349
     *     <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
   350
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   351
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   352
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findStatic lookup.findStatic(C.class,"m",MT)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   353
     *     <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
   354
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   355
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   356
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findSpecial lookup.findSpecial(C.class,"m",MT,this.class)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   357
     *     <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
   358
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   359
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   360
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findConstructor lookup.findConstructor(C.class,MT)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   361
     *     <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
   362
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   363
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   364
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflectGetter lookup.unreflectGetter(aField)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   365
     *     <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
   366
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   367
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   368
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflectSetter lookup.unreflectSetter(aField)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   369
     *     <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
   370
     * </tr>
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   371
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   372
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   373
     *     <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
   374
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   375
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   376
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor lookup.unreflectConstructor(aConstructor)}</th>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   377
     *     <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
   378
     * </tr>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   379
     * <tr>
57735
7ba5e49258de 8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents: 57558
diff changeset
   380
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#unreflectSpecial lookup.unreflectSpecial(aMethod,this.class)}</th>
7ba5e49258de 8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents: 57558
diff changeset
   381
     *     <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
   382
     * </tr>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
   383
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45705
diff changeset
   384
     *     <th scope="row">{@link java.lang.invoke.MethodHandles.Lookup#findClass lookup.findClass("C")}</th>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
   385
     *     <td>{@code class C { ... }}</td><td>{@code C.class;}</td>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
   386
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45027
diff changeset
   387
     * </tbody>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   388
     * </table>
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   389
     *
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   390
     * 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
   391
     * 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
   392
     * 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
   393
     * and the sequence of argument types {@code A*}.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
   394
     * 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
   395
     * 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
   396
     * 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
   397
     * 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
   398
     * 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
   399
     * (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
   400
     * 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
   401
     * 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
   402
     * 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
   403
     * 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
   404
     * and {@code this} otherwise.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   405
     * The names {@code aMethod}, {@code aField}, and {@code aConstructor} stand
57735
7ba5e49258de 8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents: 57558
diff changeset
   406
     * for reflective objects corresponding to the given members declared in type {@code C}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   407
     * <p>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
   408
     * The bytecode behavior for a {@code findClass} operation is a load of a constant class,
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
   409
     * as if by {@code ldc CONSTANT_Class}.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
   410
     * The behavior is represented, not as a method handle, but directly as a {@code Class} constant.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
   411
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   412
     * 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
   413
     * 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
   414
     * 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
   415
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   416
     * <em>Discussion:</em>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   417
     * 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
   418
     * class members and bytecode behaviors
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   419
     * can break down in a few ways:
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   420
     * <ul style="font-size:smaller;">
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   421
     * <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
   422
     * 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
   423
     * Java expression or bytecoded constant.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   424
     * <li>Likewise, if {@code T} or {@code MT}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   425
     * 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
   426
     * the lookup can still succeed.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   427
     * 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
   428
     * {@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
   429
     * <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
   430
     * 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
   431
     * 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
   432
     * 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
   433
     * <li>If the looked-up method has a
b49b07206f7d 8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents: 20528
diff changeset
   434
     * <a href="MethodHandle.html#maxarity">very large arity</a>,
50927
d99e206cc32e 8177275: IllegalArgumentException when MH would have too many parameters is not specified for several methods
vtheeyarath
parents: 50771
diff changeset
   435
     * the method handle creation may fail with an
d99e206cc32e 8177275: IllegalArgumentException when MH would have too many parameters is not specified for several methods
vtheeyarath
parents: 50771
diff changeset
   436
     * {@code IllegalArgumentException}, due to the method handle type having
d99e206cc32e 8177275: IllegalArgumentException when MH would have too many parameters is not specified for several methods
vtheeyarath
parents: 50771
diff changeset
   437
     * <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
   438
     * </ul>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   439
     *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   440
     * <h2><a id="access"></a>Access checking</h2>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   441
     * 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
   442
     * when a method handle is created.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   443
     * 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
   444
     * {@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
   445
     * performs access checking against every caller, on every call.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   446
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   447
     * 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
   448
     * compares its recorded lookup class against all requests to
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   449
     * create method handles.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   450
     * 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
   451
     * 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
   452
     * lookup class.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   453
     * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   454
     * 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
   455
     * such as a metaobject protocol.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   456
     * A shared {@code Lookup} object delegates the capability
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   457
     * 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
   458
     * Even if privileged code uses the {@code Lookup} object,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   459
     * 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
   460
     * original lookup class.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   461
     * <p>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   462
     * A lookup can fail, because
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   463
     * 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
   464
     * 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
   465
     * 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
   466
     * because the lookup object is not trusted enough to access the member.
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
   467
     * In the case of a field setter function on a {@code final} field,
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
   468
     * finality enforcement is treated as a kind of access control,
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
   469
     * and the lookup will fail, except in special cases of
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
   470
     * {@link Lookup#unreflectSetter Lookup.unreflectSetter}.
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   471
     * 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
   472
     * 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
   473
     * the following:
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   474
     * <ul>
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   475
     * <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
   476
     * <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
   477
     * <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
   478
     * </ul>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   479
     * <p>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   480
     * 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
   481
     * 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
   482
     * 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
   483
     * 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
   484
     * 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
   485
     * checked exception, such as {@code NoSuchMethodException}.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   486
     * 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
   487
     * 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
   488
     * 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
   489
     * 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
   490
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   491
     * <em>Discussion:</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   492
     * Access checks only apply to named and reflected methods,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   493
     * constructors, and fields.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   494
     * Other method handle creation methods, such as
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   495
     * {@link MethodHandle#asType MethodHandle.asType},
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   496
     * do not require any access checks, and are used
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   497
     * independently of any {@code Lookup} object.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   498
     * <p>
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   499
     * If the desired member is {@code protected}, the usual JVM rules apply,
51632
ed04bc1ff453 8210366: Typo in MethodHandles.Lookup: must be either be
dfuchs
parents: 50927
diff changeset
   500
     * including the requirement that the lookup class must either be in the
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   501
     * 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
   502
     * (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
   503
     * 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
   504
     * 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
   505
     * 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
   506
     * 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
   507
     * {@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
   508
     * (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
   509
     * to the lookup class itself.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
   510
     * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   511
     * 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
   512
     * 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
   513
     * 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
   514
     * 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
   515
     * (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
   516
     * <p>
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   517
     * 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
   518
     * 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
   519
     * 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
   520
     * 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
   521
     * 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
   522
     * <p>
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   523
     * If the relationship between nested types is expressed directly through the
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   524
     * {@code NestHost} and {@code NestMembers} attributes
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   525
     * (see the Java Virtual Machine Specification, sections 4.7.28 and 4.7.29),
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   526
     * then the associated {@code Lookup} object provides direct access to
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   527
     * the lookup class and all of its nestmates
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   528
     * (see {@link java.lang.Class#getNestHost Class.getNestHost}).
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   529
     * Otherwise, access between nested classes is obtained by the Java compiler creating
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   530
     * a wrapper method to access a private method of another class in the same nest.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   531
     * For example, a nested class {@code C.D}
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   532
     * 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
   533
     * {@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
   534
     * 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
   535
     * those related classes.  In such cases, a {@code Lookup} object on
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   536
     * {@code C.E} would be unable to access those private members.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   537
     * 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
   538
     * 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
   539
     * classes, without special elevation of privilege.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   540
     * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   541
     * 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
   542
     * according to its set of {@link #lookupModes lookupModes},
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   543
     * 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
   544
     * For example, the {@link MethodHandles#publicLookup publicLookup}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   545
     * method produces a lookup object which is only allowed to access
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
   546
     * public members in public classes of exported packages.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   547
     * The caller sensitive method {@link MethodHandles#lookup lookup}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   548
     * produces a lookup object with full capabilities relative to
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   549
     * its caller class, to emulate all supported bytecode behaviors.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   550
     * 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
   551
     * with fewer access modes than the original lookup object.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   552
     *
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   553
     * <p style="font-size:smaller;">
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44545
diff changeset
   554
     * <a id="privacc"></a>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   555
     * <em>Discussion of private access:</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   556
     * 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
   557
     * if its {@linkplain #lookupModes lookup modes}
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   558
     * include the possibility of accessing {@code private} members
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   559
     * (which includes the private members of nestmates).
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   560
     * As documented in the relevant methods elsewhere,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   561
     * only lookups with private access possess the following capabilities:
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   562
     * <ul style="font-size:smaller;">
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
   563
     * <li>access private fields, methods, and constructors of the lookup class and its nestmates
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   564
     * <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
   565
     *     such as {@code Class.forName}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   566
     * <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
   567
     * <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
   568
     *     for classes accessible to the lookup class
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   569
     * <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
   570
     *     within the same package member
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   571
     * </ul>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   572
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   573
     * 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
   574
     * 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
   575
     * 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
   576
     * can be reliably determined and emulated by method handles.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
   577
     *
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   578
     * <h2><a id="cross-module-lookup"></a>Cross-module lookups</h2>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   579
     * When a lookup class in one module {@code M1} accesses a class in another module
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   580
     * {@code M2}, extra access checking is performed beyond the access mode bits.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   581
     * A {@code Lookup} with {@link #PUBLIC} mode and a lookup class in {@code M1}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   582
     * can access public types in {@code M2} when {@code M2} is readable to {@code M1}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   583
     * and when the type is in a package of {@code M2} that is exported to
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   584
     * at least {@code M1}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   585
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   586
     * A {@code Lookup} on {@code C} can also <em>teleport</em> to a target class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   587
     * via {@link #in(Class) Lookup.in} and {@link MethodHandles#privateLookupIn(Class, Lookup)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   588
     * MethodHandles.privateLookupIn} methods.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   589
     * Teleporting across modules will always record the original lookup class as
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   590
     * the <em>{@linkplain #previousLookupClass() previous lookup class}</em>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   591
     * and drops {@link Lookup#MODULE MODULE} access.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   592
     * If the target class is in the same module as the lookup class {@code C},
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   593
     * then the target class becomes the new lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   594
     * and there is no change to the previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   595
     * If the target class is in a different module from {@code M1} ({@code C}'s module),
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   596
     * {@code C} becomes the new previous lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   597
     * and the target class becomes the new lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   598
     * In that case, if there was already a previous lookup class in {@code M0},
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   599
     * and it differs from {@code M1} and {@code M2}, then the resulting lookup
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   600
     * drops all privileges.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   601
     * For example,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   602
     * <blockquote><pre>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   603
     * {@code
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   604
     * Lookup lookup = MethodHandles.lookup();   // in class C
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   605
     * Lookup lookup2 = lookup.in(D.class);
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   606
     * MethodHandle mh = lookup2.findStatic(E.class, "m", MT);
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   607
     * }</pre></blockquote>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   608
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   609
     * The {@link #lookup()} factory method produces a {@code Lookup} object
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   610
     * with {@code null} previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   611
     * {@link Lookup#in lookup.in(D.class)} transforms the {@code lookup} on class {@code C}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   612
     * to class {@code D} without elevation of privileges.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   613
     * If {@code C} and {@code D} are in the same module,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   614
     * {@code lookup2} records {@code D} as the new lookup class and keeps the
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   615
     * same previous lookup class as the original {@code lookup}, or
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   616
     * {@code null} if not present.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   617
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   618
     * When a {@code Lookup} teleports from a class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   619
     * in one nest to another nest, {@code PRIVATE} access is dropped.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   620
     * When a {@code Lookup} teleports from a class in one package to
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   621
     * another package, {@code PACKAGE} access is dropped.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   622
     * When a {@code Lookup} teleports from a class in one module to another module,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   623
     * {@code MODULE} access is dropped.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   624
     * Teleporting across modules drops the ability to access non-exported classes
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   625
     * in both the module of the new lookup class and the module of the old lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   626
     * and the resulting {@code Lookup} remains only {@code PUBLIC} access.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   627
     * A {@code Lookup} can teleport back and forth to a class in the module of
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   628
     * the lookup class and the module of the previous class lookup.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   629
     * Teleporting across modules can only decrease access but cannot increase it.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   630
     * Teleporting to some third module drops all accesses.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   631
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   632
     * In the above example, if {@code C} and {@code D} are in different modules,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   633
     * {@code lookup2} records {@code D} as its lookup class and
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   634
     * {@code C} as its previous lookup class and {@code lookup2} has only
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   635
     * {@code PUBLIC} access. {@code lookup2} can teleport to other class in
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   636
     * {@code C}'s module and {@code D}'s module.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   637
     * If class {@code E} is in a third module, {@code lookup2.in(E.class)} creates
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   638
     * a {@code Lookup} on {@code E} with no access and {@code lookup2}'s lookup
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   639
     * class {@code D} is recorded as its previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   640
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   641
     * Teleporting across modules restricts access to the public types that
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   642
     * both the lookup class and the previous lookup class can equally access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   643
     * (see below).
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   644
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   645
     * {@link MethodHandles#privateLookupIn(Class, Lookup) MethodHandles.privateLookupIn(T.class, lookup)}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   646
     * can be used to teleport a {@code lookup} from class {@code C} to class {@code T}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   647
     * and create a new {@code Lookup} with <a href="#privcc">private access</a>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   648
     * if the lookup class is allowed to do <em>deep reflection</em> on {@code T}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   649
     * The {@code lookup} must have {@link #MODULE} and {@link #PRIVATE} access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   650
     * to call {@code privateLookupIn}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   651
     * A {@code lookup} on {@code C} in module {@code M1} is allowed to do deep reflection
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   652
     * on all classes in {@code M1}.  If {@code T} is in {@code M1}, {@code privateLookupIn}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   653
     * produces a new {@code Lookup} on {@code T} with full capabilities.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   654
     * A {@code lookup} on {@code C} is also allowed
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   655
     * to do deep reflection on {@code T} in another module {@code M2} if
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   656
     * {@code M1} reads {@code M2} and {@code M2} {@link Module#isOpen(String,Module) opens}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   657
     * the package containing {@code T} to at least {@code M1}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   658
     * {@code T} becomes the new lookup class and {@code C} becomes the new previous
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   659
     * lookup class and {@code MODULE} access is dropped from the resulting {@code Lookup}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   660
     * The resulting {@code Lookup} can be used to do member lookup or teleport
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   661
     * to another lookup class by calling {@link #in Lookup::in}.  But
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   662
     * it cannot be used to obtain another private {@code Lookup} by calling
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   663
     * {@link MethodHandles#privateLookupIn(Class, Lookup) privateLookupIn}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   664
     * because it has no {@code MODULE} access.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   665
     *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   666
     * <h2><a id="module-access-check"></a>Cross-module access checks</h2>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   667
     *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   668
     * A {@code Lookup} with {@link #PUBLIC} or with {@link #UNCONDITIONAL} mode
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   669
     * allows cross-module access. The access checking is performed with respect
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   670
     * to both the lookup class and the previous lookup class if present.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   671
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   672
     * A {@code Lookup} with {@link #UNCONDITIONAL} mode can access public type
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   673
     * in all modules when the type is in a package that is {@linkplain Module#isExported(String)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   674
     * exported unconditionally}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   675
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   676
     * If a {@code Lookup} on {@code LC} in {@code M1} has no previous lookup class,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   677
     * the lookup with {@link #PUBLIC} mode can access all public types in modules
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   678
     * that are readable to {@code M1} and the type is in a package that is exported
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   679
     * at least to {@code M1}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   680
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   681
     * If a {@code Lookup} on {@code LC} in {@code M1} has a previous lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   682
     * {@code PLC} on {@code M0}, the lookup with {@link #PUBLIC} mode can access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   683
     * the intersection of all public types that are accessible to {@code M1}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   684
     * with all public types that are accessible to {@code M0}. {@code M0}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   685
     * reads {@code M1} and hence the set of accessible types includes:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   686
     *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   687
     * <table class="striped">
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   688
     * <caption style="display:none">
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   689
     * Public types in the following packages are accessible to the
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   690
     * lookup class and the previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   691
     * </caption>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   692
     * <thead>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   693
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   694
     * <th scope="col">Equally accessible types to {@code M0} and {@code M1}</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   695
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   696
     * </thead>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   697
     * <tbody>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   698
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   699
     * <th scope="row" style="text-align:left">unconditional-exported packages from {@code M1}</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   700
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   701
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   702
     * <th scope="row" style="text-align:left">unconditional-exported packages from {@code M0} if {@code M1} reads {@code M0}</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   703
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   704
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   705
     * <th scope="row" style="text-align:left">unconditional-exported packages from a third module {@code M2}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   706
     * if both {@code M0} and {@code M1} read {@code M2}</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   707
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   708
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   709
     * <th scope="row" style="text-align:left">qualified-exported packages from {@code M1} to {@code M0}</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   710
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   711
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   712
     * <th scope="row" style="text-align:left">qualified-exported packages from {@code M0} to {@code M1}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   713
     * if {@code M1} reads {@code M0}</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   714
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   715
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   716
     * <th scope="row" style="text-align:left">qualified-exported packages from a third module {@code M2} to
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   717
     * both {@code M0} and {@code M1} if both {@code M0} and {@code M1} read {@code M2}</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   718
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   719
     * </tbody>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   720
     * </table>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   721
     *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   722
     * <h2><a id="access-modes"></a>Access modes</h2>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   723
     *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   724
     * The table below shows the access modes of a {@code Lookup} produced by
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   725
     * any of the following factory or transformation methods:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   726
     * <ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   727
     * <li>{@link #lookup() MethodHandles.lookup()}</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   728
     * <li>{@link #publicLookup() MethodHandles.publicLookup()}</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   729
     * <li>{@link #privateLookupIn(Class, Lookup) MethodHandles.privateLookupIn}</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   730
     * <li>{@link Lookup#in}</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   731
     * <li>{@link Lookup#dropLookupMode(int)}</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   732
     * </ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   733
     *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   734
     * <table class="striped">
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   735
     * <caption style="display:none">
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   736
     * Access mode summary
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   737
     * </caption>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   738
     * <thead>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   739
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   740
     * <th scope="col">Lookup object</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   741
     * <th style="text-align:center">protected</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   742
     * <th style="text-align:center">private</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   743
     * <th style="text-align:center">package</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   744
     * <th style="text-align:center">module</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   745
     * <th style="text-align:center">public</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   746
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   747
     * </thead>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   748
     * <tbody>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   749
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   750
     * <th scope="row" style="text-align:left">{@code CL = MethodHandles.lookup()} in {@code C}</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   751
     * <td style="text-align:center">PRO</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   752
     * <td style="text-align:center">PRI</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   753
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   754
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   755
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   756
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   757
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   758
     * <th scope="row" style="text-align:left">{@code CL.in(C1)} same package</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   759
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   760
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   761
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   762
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   763
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   764
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   765
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   766
     * <th scope="row" style="text-align:left">{@code CL.in(C1)} same module</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   767
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   768
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   769
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   770
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   771
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   772
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   773
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   774
     * <th scope="row" style="text-align:left">{@code CL.in(D)} different module</th>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   775
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   776
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   777
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   778
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   779
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   780
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   781
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   782
     * <td>{@code CL.in(D).in(C)} hop back to module</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   783
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   784
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   785
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   786
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   787
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   788
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   789
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   790
     * <td>{@code PRI1 = privateLookupIn(C1,CL)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   791
     * <td style="text-align:center">PRO</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   792
     * <td style="text-align:center">PRI</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   793
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   794
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   795
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   796
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   797
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   798
     * <td>{@code PRI1a = privateLookupIn(C,PRI1)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   799
     * <td style="text-align:center">PRO</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   800
     * <td style="text-align:center">PRI</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   801
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   802
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   803
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   804
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   805
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   806
     * <td>{@code PRI1.in(C1)} same package</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   807
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   808
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   809
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   810
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   811
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   812
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   813
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   814
     * <td>{@code PRI1.in(C1)} different package</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   815
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   816
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   817
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   818
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   819
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   820
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   821
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   822
     * <td>{@code PRI1.in(D)} different module</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   823
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   824
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   825
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   826
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   827
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   828
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   829
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   830
     * <td>{@code PRI1.dropLookupMode(PROTECTED)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   831
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   832
     * <td style="text-align:center">PRI</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   833
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   834
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   835
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   836
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   837
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   838
     * <td>{@code PRI1.dropLookupMode(PRIVATE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   839
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   840
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   841
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   842
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   843
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   844
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   845
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   846
     * <td>{@code PRI1.dropLookupMode(PACKAGE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   847
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   848
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   849
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   850
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   851
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   852
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   853
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   854
     * <td>{@code PRI1.dropLookupMode(MODULE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   855
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   856
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   857
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   858
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   859
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   860
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   861
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   862
     * <td>{@code PRI1.dropLookupMode(PUBLIC)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   863
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   864
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   865
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   866
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   867
     * <td style="text-align:center">none</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   868
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   869
     * <td>{@code PRI2 = privateLookupIn(D,CL)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   870
     * <td style="text-align:center">PRO</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   871
     * <td style="text-align:center">PRI</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   872
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   873
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   874
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   875
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   876
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   877
     * <td>{@code privateLookupIn(D,PRI1)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   878
     * <td style="text-align:center">PRO</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   879
     * <td style="text-align:center">PRI</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   880
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   881
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   882
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   883
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   884
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   885
     * <td>{@code privateLookupIn(C,PRI2)} fails</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   886
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   887
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   888
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   889
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   890
     * <td style="text-align:center">IAE</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   891
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   892
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   893
     * <td>{@code PRI2.in(D2)} same package</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   894
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   895
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   896
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   897
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   898
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   899
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   900
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   901
     * <td>{@code PRI2.in(D2)} different package</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   902
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   903
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   904
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   905
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   906
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   907
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   908
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   909
     * <td>{@code PRI2.in(C1)} hop back to module</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   910
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   911
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   912
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   913
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   914
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   915
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   916
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   917
     * <td>{@code PRI2.in(E)} hop to third module</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   918
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   919
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   920
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   921
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   922
     * <td style="text-align:center">none</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   923
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   924
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   925
     * <td>{@code PRI2.dropLookupMode(PROTECTED)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   926
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   927
     * <td style="text-align:center">PRI</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   928
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   929
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   930
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   931
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   932
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   933
     * <td>{@code PRI2.dropLookupMode(PRIVATE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   934
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   935
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   936
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   937
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   938
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   939
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   940
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   941
     * <td>{@code PRI2.dropLookupMode(PACKAGE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   942
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   943
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   944
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   945
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   946
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   947
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   948
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   949
     * <td>{@code PRI2.dropLookupMode(MODULE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   950
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   951
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   952
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   953
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   954
     * <td style="text-align:center">2R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   955
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   956
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   957
     * <td>{@code PRI2.dropLookupMode(PUBLIC)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   958
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   959
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   960
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   961
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   962
     * <td style="text-align:center">none</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   963
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   964
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   965
     * <td>{@code CL.dropLookupMode(PROTECTED)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   966
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   967
     * <td style="text-align:center">PRI</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   968
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   969
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   970
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   971
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   972
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   973
     * <td>{@code CL.dropLookupMode(PRIVATE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   974
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   975
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   976
     * <td style="text-align:center">PAC</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   977
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   978
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   979
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   980
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   981
     * <td>{@code CL.dropLookupMode(PACKAGE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   982
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   983
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   984
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   985
     * <td style="text-align:center">MOD</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   986
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   987
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   988
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   989
     * <td>{@code CL.dropLookupMode(MODULE)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   990
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   991
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   992
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   993
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   994
     * <td style="text-align:center">1R</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   995
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   996
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   997
     * <td>{@code CL.dropLookupMode(PUBLIC)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   998
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
   999
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1000
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1001
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1002
     * <td style="text-align:center">none</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1003
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1004
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1005
     * <td>{@code PUB = publicLookup()}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1006
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1007
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1008
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1009
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1010
     * <td style="text-align:center">U</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1011
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1012
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1013
     * <td>{@code PUB.in(D)} different module</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1014
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1015
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1016
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1017
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1018
     * <td style="text-align:center">U</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1019
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1020
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1021
     * <td>{@code PUB.in(D).in(E)} third module</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1022
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1023
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1024
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1025
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1026
     * <td style="text-align:center">U</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1027
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1028
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1029
     * <td>{@code PUB.dropLookupMode(UNCONDITIONAL)}</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1030
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1031
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1032
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1033
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1034
     * <td style="text-align:center">none</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1035
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1036
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1037
     * <td>{@code privateLookupIn(C1,PUB)} fails</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1038
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1039
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1040
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1041
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1042
     * <td style="text-align:center">IAE</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1043
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1044
     * <tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1045
     * <td>{@code ANY.in(X)}, for inaccessible <code>X</code></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1046
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1047
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1048
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1049
     * <td></td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1050
     * <td style="text-align:center">none</td>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1051
     * </tr>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1052
     * </tbody>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1053
     * </table>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1054
     *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1055
     * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1056
     * Notes:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1057
     * <ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1058
     * <li>Class {@code C} and class {@code C1} are in module {@code M1},
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1059
     *     but {@code D} and {@code D2} are in module {@code M2}, and {@code E}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1060
     *     is in module {@code M3}. {@code X} stands for class which is inaccessible
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1061
     *     to the lookup. {@code ANY} stands for any of the example lookups.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1062
     * <li>{@code PRO} indicates {@link #PROTECTED} bit set,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1063
     *     {@code PRI} indicates {@link #PRIVATE} bit set,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1064
     *     {@code PAC} indicates {@link #PACKAGE} bit set,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1065
     *     {@code MOD} indicates {@link #MODULE} bit set,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1066
     *     {@code 1R} and {@code 2R} indicate {@link #PUBLIC} bit set,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1067
     *     {@code U} indicates {@link #UNCONDITIONAL} bit set,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1068
     *     {@code IAE} indicates {@code IllegalAccessException} thrown.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1069
     * <li>Public access comes in three kinds:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1070
     * <ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1071
     * <li>unconditional ({@code U}): the lookup assumes readability.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1072
     *     The lookup has {@code null} previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1073
     * <li>one-module-reads ({@code 1R}): the module access checking is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1074
     *     performed with respect to the lookup class.  The lookup has {@code null}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1075
     *     previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1076
     * <li>two-module-reads ({@code 2R}): the module access checking is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1077
     *     performed with respect to the lookup class and the previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1078
     *     The lookup has a non-null previous lookup class which is in a
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1079
     *     different module from the current lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1080
     * </ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1081
     * <li>Any attempt to reach a third module loses all access.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1082
     * <li>If a target class {@code X} is not accessible to {@code Lookup::in}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1083
     * all access modes are dropped.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1084
     * </ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1085
     *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
  1086
     * <h2><a id="secmgr"></a>Security manager interactions</h2>
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1087
     * Although bytecode instructions can only refer to classes in
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1088
     * 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
  1089
     * 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
  1090
     * 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
  1091
     * Core Reflection API, and are impossible to bytecode instructions
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1092
     * such as {@code invokestatic} or {@code getfield}.
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1093
     * 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
  1094
     * to allow applications to check such cross-loader references.
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1095
     * 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
  1096
     * and the Core Reflection API
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1097
     * (as found on {@link java.lang.Class Class}).
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1098
     * <p>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1099
     * If a security manager is present, member and class lookups are subject to
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1100
     * additional checks.
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  1101
     * 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
  1102
     * 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
  1103
     * {@link java.lang.SecurityException SecurityException}.
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1104
     * Define {@code smgr} as the security manager,
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  1105
     * {@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
  1106
     * {@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
  1107
     * 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
  1108
     * member is actually defined.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1109
     * (If a class or other type is being accessed,
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1110
     * the {@code refc} and {@code defc} values are the class itself.)
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  1111
     * 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
  1112
     * if the current lookup object does not have
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1113
     * <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
  1114
     * The calls are made according to the following rules:
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1115
     * <ul>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1116
     * <li><b>Step 1:</b>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1117
     *     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
  1118
     *     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
  1119
     *     then {@link SecurityManager#checkPackageAccess
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1120
     *     smgr.checkPackageAccess(refcPkg)} is called,
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1121
     *     where {@code refcPkg} is the package of {@code refc}.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1122
     * <li><b>Step 2a:</b>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1123
     *     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
  1124
     *     {@code lookc} is not present, then
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  1125
     *     {@link SecurityManager#checkPermission smgr.checkPermission}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  1126
     *     with {@code RuntimePermission("accessDeclaredMembers")} is called.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1127
     * <li><b>Step 2b:</b>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1128
     *     If the retrieved class has a {@code null} class loader,
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1129
     *     and {@code lookc} is not present, then
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1130
     *     {@link SecurityManager#checkPermission smgr.checkPermission}
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1131
     *     with {@code RuntimePermission("getClassLoader")} is called.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1132
     * <li><b>Step 3:</b>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1133
     *     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
  1134
     *     and if {@code lookc} is not present,
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  1135
     *     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
  1136
     *     then {@link SecurityManager#checkPackageAccess
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1137
     *     smgr.checkPackageAccess(defcPkg)} is called,
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1138
     *     where {@code defcPkg} is the package of {@code defc}.
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1139
     * </ul>
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1140
     * Security checks are performed after other access checks have passed.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1141
     * Therefore, the above rules presuppose a member or class that is public,
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1142
     * or else that is being accessed from a lookup class that has
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1143
     * rights to access the member or class.
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1144
     *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
  1145
     * <h2><a id="callsens"></a>Caller sensitive methods</h2>
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1146
     * 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
  1147
     * 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
  1148
     * identity of its immediate caller.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1149
     * <p>
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1150
     * 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
  1151
     * 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
  1152
     * 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
  1153
     * 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
  1154
     * 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
  1155
     * 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
  1156
     * (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
  1157
     * 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
  1158
     * different lookup classes may give rise to
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1159
     * differently behaving method handles.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1160
     * <p>
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1161
     * In cases where the lookup object is
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1162
     * {@link MethodHandles#publicLookup() publicLookup()},
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1163
     * or some other lookup object without
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1164
     * <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
  1165
     * the lookup class is disregarded.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  1166
     * 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
  1167
     * 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
  1168
     * {@code IllegalAccessException}.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1169
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1170
     * <em>Discussion:</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1171
     * For example, the caller-sensitive method
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1172
     * {@link java.lang.Class#forName(String) Class.forName(x)}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1173
     * can return varying classes or throw varying exceptions,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1174
     * 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
  1175
     * 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
  1176
     * 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
  1177
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1178
     * If an application caches method handles for broad sharing,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1179
     * it should use {@code publicLookup()} to create them.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1180
     * 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
  1181
     * 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
  1182
     * 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
  1183
     * bootstrap method, can incorporate the specific identity
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1184
     * of the caller, making the method accessible.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1185
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1186
     * The function {@code MethodHandles.lookup} is caller sensitive
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1187
     * 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
  1188
     * 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
  1189
     * objects to check access requests.
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1190
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1191
     * @revised 9
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1192
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1193
    public static final
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1194
    class Lookup {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1195
        /** 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
  1196
        private final Class<?> lookupClass;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1197
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1198
        /** previous lookup class */
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1199
        private final Class<?> prevLookupClass;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1200
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1201
        /** 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
  1202
        private final int allowedModes;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1203
51798
f55a4bc91ef4 8210496: Improve filtering for classes with security sensitive fields
alanb
parents: 51632
diff changeset
  1204
        static {
f55a4bc91ef4 8210496: Improve filtering for classes with security sensitive fields
alanb
parents: 51632
diff changeset
  1205
            Reflection.registerFieldsToFilter(Lookup.class, Set.of("lookupClass", "allowedModes"));
f55a4bc91ef4 8210496: Improve filtering for classes with security sensitive fields
alanb
parents: 51632
diff changeset
  1206
        }
f55a4bc91ef4 8210496: Improve filtering for classes with security sensitive fields
alanb
parents: 51632
diff changeset
  1207
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1208
        /** A single-bit mask representing {@code public} access,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1209
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1210
         *  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
  1211
         *  {@code public} {@linkplain java.lang.reflect.Modifier#PUBLIC modifier bit}.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1212
         *  <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1213
         *  A {@code Lookup} with this lookup mode performs cross-module access check
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1214
         *  with respect to the {@linkplain #lookupClass() lookup class} and
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1215
         *  {@linkplain #previousLookupClass() previous lookup class} if present.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1216
         */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1217
        public static final int PUBLIC = Modifier.PUBLIC;
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1218
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1219
        /** A single-bit mask representing {@code private} access,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1220
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1221
         *  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
  1222
         *  {@code private} {@linkplain java.lang.reflect.Modifier#PRIVATE modifier bit}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1223
         */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1224
        public static final int PRIVATE = Modifier.PRIVATE;
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1225
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1226
        /** A single-bit mask representing {@code protected} access,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1227
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1228
         *  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
  1229
         *  {@code protected} {@linkplain java.lang.reflect.Modifier#PROTECTED modifier bit}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1230
         */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1231
        public static final int PROTECTED = Modifier.PROTECTED;
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1232
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1233
        /** A single-bit mask representing {@code package} access (default access),
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1234
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1235
         *  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
  1236
         *  any particular {@linkplain java.lang.reflect.Modifier modifier bit}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1237
         */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1238
        public static final int PACKAGE = Modifier.STATIC;
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1239
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1240
        /** A single-bit mask representing {@code module} access,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1241
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1242
         *  The value is {@code 0x10}, which does not correspond meaningfully to
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1243
         *  any particular {@linkplain java.lang.reflect.Modifier modifier bit}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1244
         *  In conjunction with the {@code PUBLIC} modifier bit, a {@code Lookup}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1245
         *  with this lookup mode can access all public types in the module of the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1246
         *  lookup class and public types in packages exported by other modules
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1247
         *  to the module of the lookup class.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1248
         *  <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1249
         *  If this lookup mode is set, the {@linkplain #previousLookupClass()
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1250
         *  previous lookup class} is always {@code null}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1251
         *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1252
         *  @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1253
         *  @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1254
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1255
        public static final int MODULE = PACKAGE << 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1256
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1257
        /** A single-bit mask representing {@code unconditional} access
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1258
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1259
         *  The value is {@code 0x20}, which does not correspond meaningfully to
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1260
         *  any particular {@linkplain java.lang.reflect.Modifier modifier bit}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1261
         *  A {@code Lookup} with this lookup mode assumes {@linkplain
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
  1262
         *  java.lang.Module#canRead(java.lang.Module) readability}.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1263
         *  This lookup mode can access all public members of public types
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1264
         *  of all modules when the type is in a package that is {@link
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
  1265
         *  java.lang.Module#isExported(String) exported unconditionally}.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1266
         *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1267
         *  <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1268
         *  If this lookup mode is set, the {@linkplain #previousLookupClass()
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1269
         *  previous lookup class} is always {@code null}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1270
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1271
         *  @since 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1272
         *  @spec JPMS
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1273
         *  @see #publicLookup()
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1274
         */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1275
        public static final int UNCONDITIONAL = PACKAGE << 2;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1276
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1277
        private static final int ALL_MODES = (PUBLIC | PRIVATE | PROTECTED | PACKAGE | MODULE | UNCONDITIONAL);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1278
        private static final int FULL_POWER_MODES = (ALL_MODES & ~UNCONDITIONAL);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1279
        private static final int TRUSTED   = -1;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1280
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1281
        /*
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1282
         * Adjust PUBLIC => PUBLIC|MODULE|UNCONDITIONAL
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1283
         * Adjust 0 => PACKAGE
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1284
         */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1285
        private static int fixmods(int mods) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1286
            mods &= (ALL_MODES - PACKAGE - MODULE - UNCONDITIONAL);
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1287
            if (Modifier.isPublic(mods))
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1288
                mods |= UNCONDITIONAL;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1289
            return (mods != 0) ? mods : PACKAGE;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1290
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1291
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1292
        /** 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
  1293
         *  which checks are performed for visibility and access permissions.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1294
         *  <p>
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1295
         *  If this lookup object has a {@linkplain #previousLookupClass() previous lookup class},
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1296
         *  access checks are performed against both the lookup class and the previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1297
         *  <p>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
  1298
         *  The class implies a maximum level of access permission,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
  1299
         *  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
  1300
         *  {@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
  1301
         *  can be accessed.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  1302
         *  @return the lookup class, on behalf of which this lookup object finds members
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1303
         *  @see <a href="#cross-module-lookup">Cross-module lookups</a>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1304
         */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1305
        public Class<?> lookupClass() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1306
            return lookupClass;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1307
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1308
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1309
        /** Reports a lookup class in another module that this lookup object
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1310
         * was previously teleported from, or {@code null}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1311
         * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1312
         * A {@code Lookup} object produced by the factory methods, such as the
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1313
         * {@link #lookup() lookup()} and {@link #publicLookup() publicLookup()} method,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1314
         * has {@code null} previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1315
         * A {@code Lookup} object has a non-null previous lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1316
         * when this lookup was teleported from an old lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1317
         * in one module to a new lookup class in another module.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1318
         *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1319
         * @return the lookup class in another module that this lookup object was
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1320
         *         previously teleported from, or {@code null}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1321
         * @since 14
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1322
         * @see #in(Class)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1323
         * @see MethodHandles#privateLookupIn(Class, Lookup)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1324
         * @see <a href="#cross-module-lookup">Cross-module lookups</a>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1325
         */
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1326
        public Class<?> previousLookupClass() {
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1327
            return prevLookupClass;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1328
        }
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1329
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1330
        // This is just for calling out to MethodHandleImpl.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1331
        private Class<?> lookupClassOrNull() {
57558
5e637f790bb8 8228671: Fastdebug VM throws InternalError when publicLookup.in(T) is used to resolve a member
mchung
parents: 57512
diff changeset
  1332
            if (allowedModes == TRUSTED) {
5e637f790bb8 8228671: Fastdebug VM throws InternalError when publicLookup.in(T) is used to resolve a member
mchung
parents: 57512
diff changeset
  1333
                return null;
5e637f790bb8 8228671: Fastdebug VM throws InternalError when publicLookup.in(T) is used to resolve a member
mchung
parents: 57512
diff changeset
  1334
            }
5e637f790bb8 8228671: Fastdebug VM throws InternalError when publicLookup.in(T) is used to resolve a member
mchung
parents: 57512
diff changeset
  1335
            if (allowedModes == UNCONDITIONAL) {
5e637f790bb8 8228671: Fastdebug VM throws InternalError when publicLookup.in(T) is used to resolve a member
mchung
parents: 57512
diff changeset
  1336
                // use Object as the caller to pass to VM doing resolution
5e637f790bb8 8228671: Fastdebug VM throws InternalError when publicLookup.in(T) is used to resolve a member
mchung
parents: 57512
diff changeset
  1337
                return Object.class;
5e637f790bb8 8228671: Fastdebug VM throws InternalError when publicLookup.in(T) is used to resolve a member
mchung
parents: 57512
diff changeset
  1338
            }
5e637f790bb8 8228671: Fastdebug VM throws InternalError when publicLookup.in(T) is used to resolve a member
mchung
parents: 57512
diff changeset
  1339
            return lookupClass;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1340
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1341
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1342
        /** 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
  1343
         *  The result is a bit-mask of the bits
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1344
         *  {@linkplain #PUBLIC PUBLIC (0x01)},
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1345
         *  {@linkplain #PRIVATE PRIVATE (0x02)},
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1346
         *  {@linkplain #PROTECTED PROTECTED (0x04)},
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1347
         *  {@linkplain #PACKAGE PACKAGE (0x08)},
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1348
         *  {@linkplain #MODULE MODULE (0x10)},
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1349
         *  and {@linkplain #UNCONDITIONAL UNCONDITIONAL (0x20)}.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1350
         *  <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1351
         *  A freshly-created lookup object
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1352
         *  on the {@linkplain java.lang.invoke.MethodHandles#lookup() caller's class} has
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
  1353
         *  all possible bits set, except {@code UNCONDITIONAL}.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1354
         *  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
  1355
         *  {@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
  1356
         *  may have some mode bits set to zero.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1357
         *  Mode bits can also be
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1358
         *  {@linkplain java.lang.invoke.MethodHandles.Lookup#dropLookupMode directly cleared}.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1359
         *  Once cleared, mode bits cannot be restored from the downgraded lookup object.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1360
         *  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
  1361
         *  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
  1362
         *  lookup object, and also by the new lookup class.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  1363
         *  @return the lookup modes, which limit the kinds of access performed by this lookup object
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1364
         *  @see #in
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1365
         *  @see #dropLookupMode
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1366
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1367
         *  @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1368
         *  @spec JPMS
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1369
         */
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
  1370
        public int lookupModes() {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1371
            return allowedModes & ALL_MODES;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1372
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1373
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1374
        /** 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
  1375
         * for method handle creation.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1376
         * 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
  1377
         * 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
  1378
         */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  1379
        Lookup(Class<?> lookupClass) {
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1380
            this(lookupClass, null, FULL_POWER_MODES);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1381
            // make sure we haven't accidentally picked up a privileged class:
45027
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
  1382
            checkUnprivilegedlookupClass(lookupClass);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1383
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1384
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1385
        private Lookup(Class<?> lookupClass, Class<?> prevLookupClass, int allowedModes) {
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1386
            assert prevLookupClass == null || ((allowedModes & MODULE) == 0
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1387
                    && prevLookupClass.getModule() != lookupClass.getModule());
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1388
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1389
            this.lookupClass = lookupClass;
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1390
            this.prevLookupClass = prevLookupClass;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1391
            this.allowedModes = allowedModes;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1392
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1393
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1394
        private static Lookup newLookup(Class<?> lookupClass, Class<?> prevLookupClass, int allowedModes) {
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1395
            // make sure we haven't accidentally picked up a privileged class:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1396
            checkUnprivilegedlookupClass(lookupClass);
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1397
            return new Lookup(lookupClass, prevLookupClass, allowedModes);
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1398
        }
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1399
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1400
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1401
         * Creates a lookup on the specified new lookup class.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1402
         * The resulting object will report the specified
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46148
diff changeset
  1403
         * class as its own {@link #lookupClass() lookupClass}.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1404
         *
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1405
         * <p>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1406
         * However, the resulting {@code Lookup} object is guaranteed
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1407
         * 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
  1408
         * In particular, access capabilities can be lost as follows:<ul>
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1409
         * <li>If the new lookup class is in a different module from the old one,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1410
         * i.e. {@link #MODULE MODULE} access is lost.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1411
         * <li>If the new lookup class is in a different package
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1412
         * than the old one, protected and default (package) members will not be accessible,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1413
         * i.e. {@link #PROTECTED PROTECTED} and {@link #PACKAGE PACKAGE} access are lost.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1414
         * <li>If the new lookup class is not within the same package member
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1415
         * as the old one, private members will not be accessible, and protected members
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1416
         * will not be accessible by virtue of inheritance,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1417
         * i.e. {@link #PRIVATE PRIVATE} access is lost.
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1418
         * (Protected members may continue to be accessible because of package sharing.)
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1419
         * <li>If the new lookup class is not
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1420
         * {@linkplain #accessClass(Class) accessible} to this lookup,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1421
         * then no members, not even public members, will be accessible
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1422
         * i.e. all access modes are lost.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1423
         * <li>If the new lookup class, the old lookup class and the previous lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1424
         * are all in different modules i.e. teleporting to a third module,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1425
         * all access modes are lost.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1426
         * </ul>
36623
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1427
         * <p>
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1428
         * The new previous lookup class is chosen as follows:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1429
         * <ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1430
         * <li>If the new lookup object has {@link #UNCONDITIONAL UNCONDITIONAL} bit,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1431
         * the new previous lookup class is {@code null}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1432
         * <li>If the new lookup class is in the same module as the old lookup class,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1433
         * the new previous lookup class is the old previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1434
         * <li>If the new lookup class is in a different module from the old lookup class,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1435
         * the new previous lookup class is the the old lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1436
         *</ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1437
         * <p>
36623
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1438
         * The resulting lookup's capabilities for loading classes
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1439
         * (used during {@link #findClass} invocations)
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1440
         * are determined by the lookup class' loader,
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1441
         * which may change due to this operation.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1442
         * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1443
         * @param requestedLookupClass the desired lookup class for the new lookup object
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1444
         * @return a lookup object which reports the desired lookup class, or the same object
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1445
         * if there is no change
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1446
         * @throws NullPointerException if the argument is null
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1447
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1448
         * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1449
         * @spec JPMS
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1450
         * @see #accessClass(Class)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1451
         * @see <a href="#cross-module-lookup">Cross-module lookups</a>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1452
         */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1453
        public Lookup in(Class<?> requestedLookupClass) {
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  1454
            Objects.requireNonNull(requestedLookupClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1455
            if (allowedModes == TRUSTED)  // IMPL_LOOKUP can make any lookup at all
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1456
                return new Lookup(requestedLookupClass, null, FULL_POWER_MODES);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1457
            if (requestedLookupClass == this.lookupClass)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1458
                return this;  // keep same capabilities
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1459
            int newModes = (allowedModes & FULL_POWER_MODES);
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1460
            Module fromModule = this.lookupClass.getModule();
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1461
            Module targetModule = requestedLookupClass.getModule();
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1462
            Class<?> plc = this.previousLookupClass();
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1463
            if ((this.allowedModes & UNCONDITIONAL) != 0) {
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1464
                assert plc == null;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1465
                newModes = UNCONDITIONAL;
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1466
            } else if (fromModule != targetModule) {
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1467
                if (plc != null && !VerifyAccess.isSameModule(plc, requestedLookupClass)) {
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1468
                    // allow hopping back and forth between fromModule and plc's module
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1469
                    // but not the third module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1470
                    newModes = 0;
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1471
                }
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1472
                // drop MODULE access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1473
                newModes &= ~(MODULE|PACKAGE|PRIVATE|PROTECTED);
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1474
                // teleport from this lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1475
                plc = this.lookupClass;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1476
            }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1477
            if ((newModes & PACKAGE) != 0
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1478
                && !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1479
                newModes &= ~(PACKAGE|PRIVATE|PROTECTED);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1480
            }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1481
            // Allow nestmate lookups to be created without special privilege:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1482
            if ((newModes & PRIVATE) != 0
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1483
                && !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1484
                newModes &= ~(PRIVATE|PROTECTED);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1485
            }
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1486
            if ((newModes & (PUBLIC|UNCONDITIONAL)) != 0
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1487
                && !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, this.prevLookupClass, allowedModes)) {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1488
                // 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
  1489
                // No permissions.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1490
                newModes = 0;
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  1491
            }
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1492
            return newLookup(requestedLookupClass, plc, newModes);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1493
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1494
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1495
        /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1496
         * Creates a lookup on the same lookup class which this lookup object
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1497
         * finds members, but with a lookup mode that has lost the given lookup mode.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1498
         * The lookup mode to drop is one of {@link #PUBLIC PUBLIC}, {@link #MODULE
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1499
         * MODULE}, {@link #PACKAGE PACKAGE}, {@link #PROTECTED PROTECTED} or {@link #PRIVATE PRIVATE}.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1500
         * {@link #PROTECTED PROTECTED} is always
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1501
         * dropped and so the resulting lookup mode will never have this access capability.
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1502
         * When dropping {@code PACKAGE} then the resulting lookup will not have {@code PACKAGE}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1503
         * or {@code PRIVATE} access. When dropping {@code MODULE} then the resulting lookup will
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1504
         * not have {@code MODULE}, {@code PACKAGE}, or {@code PRIVATE} access. If {@code PUBLIC}
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1505
         * is dropped then the resulting lookup has no access. If {@code UNCONDITIONAL}
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1506
         * is dropped then the resulting lookup has no access.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1507
         *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1508
         * @apiNote
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1509
         * A lookup with {@code PACKAGE} but not {@code PRIVATE} mode can safely
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1510
         * delegate non-public access within the package of the lookup class without
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1511
         * conferring private access.  A lookup with {@code MODULE} but not
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1512
         * {@code PACKAGE} mode can safely delegate {@code PUBLIC} access within
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1513
         * the module of the lookup class without conferring package access.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1514
         * A lookup with a {@linkplain #previousLookupClass() previous lookup class}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1515
         * (and {@code PUBLIC} but not {@code MODULE} mode) can safely delegate access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1516
         * to public classes accessible to both the module of the lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1517
         * and the module of the previous lookup class.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1518
         *
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1519
         * @param modeToDrop the lookup mode to drop
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1520
         * @return a lookup object which lacks the indicated mode, or the same object if there is no change
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1521
         * @throws IllegalArgumentException if {@code modeToDrop} is not one of {@code PUBLIC},
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1522
         * {@code MODULE}, {@code PACKAGE}, {@code PROTECTED}, {@code PRIVATE} or {@code UNCONDITIONAL}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1523
         * @see MethodHandles#privateLookupIn
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1524
         * @since 9
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1525
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1526
        public Lookup dropLookupMode(int modeToDrop) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1527
            int oldModes = lookupModes();
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1528
            int newModes = oldModes & ~(modeToDrop | PROTECTED);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1529
            switch (modeToDrop) {
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1530
                case PUBLIC: newModes &= ~(FULL_POWER_MODES); break;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1531
                case MODULE: newModes &= ~(PACKAGE | PRIVATE); break;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1532
                case PACKAGE: newModes &= ~(PRIVATE); break;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1533
                case PROTECTED:
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1534
                case PRIVATE:
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1535
                case UNCONDITIONAL: break;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1536
                default: throw new IllegalArgumentException(modeToDrop + " is not a valid mode to drop");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1537
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1538
            if (newModes == oldModes) return this;  // return self if no change
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1539
            return newLookup(lookupClass(), previousLookupClass(), newModes);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1540
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1541
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1542
        /**
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1543
         * Defines a class to the same class loader and in the same runtime package and
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1544
         * {@linkplain java.security.ProtectionDomain protection domain} as this lookup's
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1545
         * {@linkplain #lookupClass() lookup class}.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1546
         *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1547
         * <p> The {@linkplain #lookupModes() lookup modes} for this lookup must include
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1548
         * {@link #PACKAGE PACKAGE} access as default (package) members will be
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1549
         * accessible to the class. The {@code PACKAGE} lookup mode serves to authenticate
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1550
         * that the lookup object was created by a caller in the runtime package (or derived
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1551
         * from a lookup originally created by suitably privileged code to a target class in
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1552
         * the runtime package). </p>
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1553
         *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1554
         * <p> The {@code bytes} parameter is the class bytes of a valid class file (as defined
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1555
         * by the <em>The Java Virtual Machine Specification</em>) with a class name in the
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1556
         * same package as the lookup class. </p>
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1557
         *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1558
         * <p> This method does not run the class initializer. The class initializer may
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1559
         * run at a later time, as detailed in section 12.4 of the <em>The Java Language
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1560
         * Specification</em>. </p>
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1561
         *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1562
         * <p> If there is a security manager, its {@code checkPermission} method is first called
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1563
         * to check {@code RuntimePermission("defineClass")}. </p>
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1564
         *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1565
         * @param bytes the class bytes
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1566
         * @return the {@code Class} object for the class
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1567
         * @throws IllegalArgumentException the bytes are for a class in a different package
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1568
         * to the lookup class
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1569
         * @throws IllegalAccessException if this lookup does not have {@code PACKAGE} access
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1570
         * @throws LinkageError if the class is malformed ({@code ClassFormatError}), cannot be
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1571
         * verified ({@code VerifyError}), is already defined, or another linkage error occurs
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1572
         * @throws SecurityException if denied by the security manager
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1573
         * @throws NullPointerException if {@code bytes} is {@code null}
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1574
         * @since 9
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1575
         * @spec JPMS
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1576
         * @see Lookup#privateLookupIn
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1577
         * @see Lookup#dropLookupMode
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1578
         * @see ClassLoader#defineClass(String,byte[],int,int,ProtectionDomain)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1579
         */
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1580
        public Class<?> defineClass(byte[] bytes) throws IllegalAccessException {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1581
            SecurityManager sm = System.getSecurityManager();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1582
            if (sm != null)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1583
                sm.checkPermission(new RuntimePermission("defineClass"));
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1584
            if ((lookupModes() & PACKAGE) == 0)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1585
                throw new IllegalAccessException("Lookup does not have PACKAGE access");
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1586
            assert (lookupModes() & (MODULE|PUBLIC)) != 0;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1587
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1588
            // parse class bytes to get class name (in internal form)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1589
            bytes = bytes.clone();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1590
            String name;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1591
            try {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1592
                ClassReader reader = new ClassReader(bytes);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1593
                name = reader.getClassName();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1594
            } catch (RuntimeException e) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1595
                // ASM exceptions are poorly specified
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1596
                ClassFormatError cfe = new ClassFormatError();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1597
                cfe.initCause(e);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1598
                throw cfe;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1599
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1600
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1601
            // get package and class name in binary form
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1602
            String cn, pn;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1603
            int index = name.lastIndexOf('/');
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1604
            if (index == -1) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1605
                cn = name;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1606
                pn = "";
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1607
            } else {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1608
                cn = name.replace('/', '.');
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1609
                pn = cn.substring(0, index);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1610
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1611
            if (!pn.equals(lookupClass.getPackageName())) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1612
                throw new IllegalArgumentException("Class not in same package as lookup class");
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1613
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1614
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1615
            // invoke the class loader's defineClass method
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1616
            ClassLoader loader = lookupClass.getClassLoader();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1617
            ProtectionDomain pd = (loader != null) ? lookupClassProtectionDomain() : null;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1618
            String source = "__Lookup_defineClass__";
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1619
            Class<?> clazz = SharedSecrets.getJavaLangAccess().defineClass(loader, cn, bytes, pd, source);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1620
            return clazz;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1621
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1622
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1623
        private ProtectionDomain lookupClassProtectionDomain() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1624
            ProtectionDomain pd = cachedProtectionDomain;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1625
            if (pd == null) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1626
                cachedProtectionDomain = pd = protectionDomain(lookupClass);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1627
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1628
            return pd;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1629
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1630
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1631
        private ProtectionDomain protectionDomain(Class<?> clazz) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1632
            PrivilegedAction<ProtectionDomain> pa = clazz::getProtectionDomain;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1633
            return AccessController.doPrivileged(pa);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1634
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1635
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1636
        // cached protection domain
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1637
        private volatile ProtectionDomain cachedProtectionDomain;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1638
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1639
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1640
        // Make sure outer class is initialized first.
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  1641
        static { IMPL_NAMES.getClass(); }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1642
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1643
        /** Package-private version of lookup which is trusted. */
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1644
        static final Lookup IMPL_LOOKUP = new Lookup(Object.class, null, TRUSTED);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1645
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1646
        /** Version of lookup which is trusted minimally.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1647
         *  It can only be used to create method handles to publicly accessible
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1648
         *  members in packages that are exported unconditionally.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1649
         */
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1650
        static final Lookup PUBLIC_LOOKUP = new Lookup(Object.class, null, UNCONDITIONAL);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1651
45027
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 45005
diff changeset
  1652
        private static void checkUnprivilegedlookupClass(Class<?> lookupClass) {
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2763
diff changeset
  1653
            String name = lookupClass.getName();
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  1654
            if (name.startsWith("java.lang.invoke."))
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1655
                throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1656
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1657
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1658
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1659
         * Displays the name of the class from which lookups are to be made.
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1660
         * followed with "/" and the name of the {@linkplain #previousLookupClass()
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1661
         * previous lookup class} if present.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1662
         * (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
  1663
         * 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
  1664
         * 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
  1665
         * 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
  1666
         * allowed access, and is chosen as follows:
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1667
         * <ul>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1668
         * <li>If no access is allowed, the suffix is "/noaccess".
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1669
         * <li>If only unconditional access is allowed, the suffix is "/publicLookup".
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1670
         * <li>If only public access to types in exported packages is allowed, the suffix is "/public".
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1671
         * <li>If only public and module access are allowed, the suffix is "/module".
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1672
         * <li>If public and package access are allowed, the suffix is "/package".
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1673
         * <li>If public, package, and private access are allowed, the suffix is "/private".
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1674
         * </ul>
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1675
         * If none of the above cases apply, it is the case that full access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1676
         * (public, module, package, private, and protected) is allowed.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1677
         * In this case, no suffix is added.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1678
         * 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
  1679
         * {@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
  1680
         * 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
  1681
         * 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
  1682
         * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1683
         * (It may seem strange that protected access should be
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1684
         * stronger than private access.  Viewed independently from
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1685
         * 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
  1686
         * because it requires a direct subclass relationship between
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1687
         * caller and callee.)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1688
         * @see #in
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1689
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1690
         * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1691
         * @spec JPMS
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1692
         */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1693
        @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1694
        public String toString() {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1695
            String cname = lookupClass.getName();
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1696
            if (prevLookupClass != null)
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1697
                cname += "/" + prevLookupClass.getName();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1698
            switch (allowedModes) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1699
            case 0:  // no privileges
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1700
                return cname + "/noaccess";
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1701
            case UNCONDITIONAL:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1702
                return cname + "/publicLookup";
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1703
            case PUBLIC:
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  1704
                return cname + "/public";
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1705
            case PUBLIC|MODULE:
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1706
                return cname + "/module";
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1707
            case PUBLIC|PACKAGE:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  1708
            case PUBLIC|MODULE|PACKAGE:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1709
                return cname + "/package";
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1710
            case FULL_POWER_MODES & (~PROTECTED):
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1711
            case FULL_POWER_MODES & ~(PROTECTED|MODULE):
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1712
                    return cname + "/private";
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  1713
            case FULL_POWER_MODES:
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1714
            case FULL_POWER_MODES & (~MODULE):
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1715
                return cname;
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1716
            case TRUSTED:
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1717
                return "/trusted";  // internal only; not exported
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1718
            default:  // Should not happen, but it's a bitfield...
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1719
                cname = cname + "/" + Integer.toHexString(allowedModes);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1720
                assert(false) : cname;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1721
                return cname;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1722
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1723
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1724
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1725
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1726
         * Produces a method handle for a static method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1727
         * 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
  1728
         * (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
  1729
         * 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
  1730
         * 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
  1731
         * 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
  1732
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1733
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1734
         * {@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
  1735
         * 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
  1736
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1737
         * 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
  1738
         * 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
  1739
         * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  1740
         * <blockquote><pre>{@code
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1741
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
  1742
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
  1743
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1744
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
  1745
  "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
  1746
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
  1747
         * }</pre></blockquote>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1748
         * @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
  1749
         * @param name the name of the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1750
         * @param type the type of the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1751
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1752
         * @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
  1753
         * @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
  1754
         *                                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
  1755
         *                                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
  1756
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1757
         * @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
  1758
         *                              <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
  1759
         * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1760
         */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1761
        public
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1762
        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
  1763
            MemberName method = resolveOrFail(REF_invokeStatic, refc, name, type);
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1764
            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
  1765
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1766
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1767
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1768
         * Produces a method handle for a virtual method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1769
         * 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
  1770
         * with the receiver type (usually {@code refc}) prepended.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1771
         * 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
  1772
         * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1773
         * When called, the handle will treat the first argument as a receiver
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
  1774
         * and, for non-private methods, dispatch on the receiver's type to determine which method
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1775
         * implementation to enter.
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
  1776
         * For private methods the named method in {@code refc} will be invoked on the receiver.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1777
         * (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
  1778
         * {@code invokevirtual} or {@code invokeinterface} instruction.)
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1779
         * <p>
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1780
         * 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
  1781
         * 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
  1782
         * 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
  1783
         * 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
  1784
         * <p>
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1785
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1786
         * {@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
  1787
         * 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
  1788
         * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1789
         * 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
  1790
         * instructions and method handles produced by {@code findVirtual},
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1791
         * 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
  1792
         * {@code invokeExact} or {@code invoke}, the resulting
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1793
         * 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
  1794
         * {@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
  1795
         * {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker}
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1796
         * with the same {@code type} argument.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1797
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1798
         * If the class is {@code VarHandle} and the name string corresponds to
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1799
         * the name of a signature-polymorphic access mode method, the resulting
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1800
         * method handle is equivalent to one produced by
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1801
         * {@link java.lang.invoke.MethodHandles#varHandleInvoker} with
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1802
         * the access mode corresponding to the name string and with the same
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1803
         * {@code type} arguments.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1804
         * <p>
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1805
         * <b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  1806
         * <blockquote><pre>{@code
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1807
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
  1808
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
  1809
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1810
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
  1811
  "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
  1812
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
  1813
  "hashCode", methodType(int.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1814
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
  1815
  "hashCode", methodType(int.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1816
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
  1817
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
  1818
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
  1819
// interface method:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1820
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
  1821
  "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
  1822
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
  1823
// 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
  1824
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
  1825
try { assertEquals("impossible", lookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1826
        .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
  1827
 } catch (NoSuchMethodException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1828
MethodHandle MH_newString = publicLookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1829
  .findConstructor(String.class, MT_newString);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1830
assertEquals("", (String) MH_newString.invokeExact());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1831
         * }</pre></blockquote>
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1832
         *
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1833
         * @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
  1834
         * @param name the name of the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1835
         * @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
  1836
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1837
         * @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
  1838
         * @throws IllegalAccessException if access checking fails,
35772
044f660251cc 8138884: MethodHandles.Lookup.findVirtual() Javadoc fails to consider private interface methods
srastogi
parents: 35716
diff changeset
  1839
         *                                or if the method is {@code static},
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  1840
         *                                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
  1841
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1842
         * @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
  1843
         *                              <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
  1844
         * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1845
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1846
        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
  1847
            if (refc == MethodHandle.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1848
                MethodHandle mh = findVirtualForMH(name, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1849
                if (mh != null)  return mh;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1850
            } else if (refc == VarHandle.class) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1851
                MethodHandle mh = findVirtualForVH(name, type);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1852
                if (mh != null)  return mh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1853
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1854
            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
  1855
            MemberName method = resolveOrFail(refKind, refc, name, type);
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  1856
            return getDirectMethod(refKind, refc, method, findBoundCallerClass(method));
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  1857
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1858
        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
  1859
            // 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
  1860
            if ("invoke".equals(name))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1861
                return invoker(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1862
            if ("invokeExact".equals(name))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1863
                return exactInvoker(type);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  1864
            assert(!MemberName.isMethodHandleInvokeName(name));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1865
            return null;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1866
        }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1867
        private MethodHandle findVirtualForVH(String name, MethodType type) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1868
            try {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 37340
diff changeset
  1869
                return varHandleInvoker(VarHandle.AccessMode.valueFromMethodName(name), type);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1870
            } catch (IllegalArgumentException e) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1871
                return null;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1872
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  1873
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1874
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1875
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  1876
         * 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
  1877
         * the constructor of the specified type.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1878
         * 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
  1879
         * 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
  1880
         * 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
  1881
         * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  1882
         * 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
  1883
         * (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
  1884
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1885
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  1886
         * {@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
  1887
         * 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
  1888
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  1889
         * 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
  1890
         * 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
  1891
         * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  1892
         * <blockquote><pre>{@code
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1893
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
  1894
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
  1895
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1896
MethodHandle MH_newArrayList = publicLookup().findConstructor(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1897
  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
  1898
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
  1899
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
  1900
assert(orig != copy);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1901
assertEquals(orig, copy);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1902
// a variable-arity constructor:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1903
MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1904
  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
  1905
ProcessBuilder pb = (ProcessBuilder)
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  1906
  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
  1907
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
  1908
         * }</pre></blockquote>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1909
         * @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
  1910
         * @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
  1911
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1912
         * @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
  1913
         * @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
  1914
         *                                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
  1915
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  1916
         * @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
  1917
         *                              <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
  1918
         * @throws NullPointerException if any argument is null
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1919
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  1920
        public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  1921
            if (refc.isArray()) {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  1922
                throw new NoSuchMethodException("no constructor for array class: " + refc.getName());
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  1923
            }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1924
            String name = "<init>";
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  1925
            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
  1926
            return getDirectConstructor(refc, ctor);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1927
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1928
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1929
        /**
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1930
         * Looks up a class by name from the lookup context defined by this {@code Lookup} object. The static
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1931
         * initializer of the class is not run.
36623
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1932
         * <p>
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1933
         * The lookup context here is determined by the {@linkplain #lookupClass() lookup class}, its class
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1934
         * loader, and the {@linkplain #lookupModes() lookup modes}. In particular, the method first attempts to
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1935
         * load the requested class, and then determines whether the class is accessible to this lookup object.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1936
         *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1937
         * @param targetName the fully qualified name of the class to be looked up.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1938
         * @return the requested class.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1939
         * @exception SecurityException if a security manager is present and it
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1940
         *            <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1941
         * @throws LinkageError if the linkage fails
36623
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1942
         * @throws ClassNotFoundException if the class cannot be loaded by the lookup class' loader.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1943
         * @throws IllegalAccessException if the class is not accessible, using the allowed access
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1944
         * modes.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1945
         * @exception SecurityException if a security manager is present and it
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1946
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1947
         * @since 9
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1948
         */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1949
        public Class<?> findClass(String targetName) throws ClassNotFoundException, IllegalAccessException {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1950
            Class<?> targetClass = Class.forName(targetName, false, lookupClass.getClassLoader());
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1951
            return accessClass(targetClass);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1952
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1953
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  1954
        /**
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1955
         * Determines if a class can be accessed from the lookup context defined by
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1956
         * this {@code Lookup} object. The static initializer of the class is not run.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1957
         * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1958
         * If the {@code targetClass} is in the same module as the lookup class,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1959
         * the lookup class is {@code LC} in module {@code M1} and
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1960
         * the previous lookup class is in module {@code M0} or
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1961
         * {@code null} if not present,
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1962
         * {@code targetClass} is accessible if and only if one of the following is true:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1963
         * <ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1964
         * <li>If this lookup has {@link #PRIVATE} access, {@code targetClass} is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1965
         *     {@code LC} or other class in the same nest of {@code LC}.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1966
         * <li>If this lookup has {@link #PACKAGE} access, {@code targetClass} is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1967
         *     in the same runtime package of {@code LC}.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1968
         * <li>If this lookup has {@link #MODULE} access, {@code targetClass} is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1969
         *     a public type in {@code M1}.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1970
         * <li>If this lookup has {@link #PUBLIC} access, {@code targetClass} is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1971
         *     a public type in a package exported by {@code M1} to at least  {@code M0}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1972
         *     if the previous lookup class is present; otherwise, {@code targetClass}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1973
         *     is a public type in a package exported by {@code M1} unconditionally.</li>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1974
         * </ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1975
         *
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1976
         * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1977
         * Otherwise, if this lookup has {@link #UNCONDITIONAL} access, this lookup
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1978
         * can access public types in all modules when the type is in a package
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1979
         * that is exported unconditionally.
36623
79e0e71cdb20 8150782: findClass / accessClass throw unexpected exceptions
mhaupt
parents: 36220
diff changeset
  1980
         * <p>
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1981
         * Otherwise, the target class is in a different module from {@code lookupClass},
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1982
         * and if this lookup does not have {@code PUBLIC} access, {@code lookupClass}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1983
         * is inaccessible.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1984
         * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1985
         * Otherwise, if this lookup has no {@linkplain #previousLookupClass() previous lookup class},
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1986
         * {@code M1} is the module containing {@code lookupClass} and
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1987
         * {@code M2} is the module containing {@code targetClass},
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1988
         * then {@code targetClass} is accessible if and only if
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1989
         * <ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1990
         * <li>{@code M1} reads {@code M2}, and
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1991
         * <li>{@code targetClass} is public and in a package exported by
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1992
         *     {@code M2} at least to {@code M1}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1993
         * </ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1994
         * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1995
         * Otherwise, if this lookup has a {@linkplain #previousLookupClass() previous lookup class},
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1996
         * {@code M1} and {@code M2} are as before, and {@code M0} is the module
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1997
         * containing the previous lookup class, then {@code targetClass} is accessible
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1998
         * if and only if one of the following is true:
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  1999
         * <ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2000
         * <li>{@code targetClass} is in {@code M0} and {@code M1}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2001
         *     {@linkplain Module#reads reads} {@code M0} and the type is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2002
         *     in a package that is exported to at least {@code M1}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2003
         * <li>{@code targetClass} is in {@code M1} and {@code M0}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2004
         *     {@linkplain Module#reads reads} {@code M1} and the type is
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2005
         *     in a package that is exported to at least {@code M0}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2006
         * <li>{@code targetClass} is in a third module {@code M2} and both {@code M0}
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2007
         *     and {@code M1} reads {@code M2} and the type is in a package
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2008
         *     that is exported to at least both {@code M0} and {@code M2}.
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2009
         * </ul>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2010
         * <p>
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2011
         * Otherwise, {@code targetClass} is not accessible.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2012
         *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2013
         * @param targetClass the class to be access-checked
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2014
         * @return the class that has been access-checked
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2015
         * @throws IllegalAccessException if the class is not accessible from the lookup class
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2016
         * and previous lookup class, if present, using the allowed access modes.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2017
         * @exception SecurityException if a security manager is present and it
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2018
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2019
         * @since 9
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2020
         * @see <a href="#cross-module-lookup">Cross-module lookups</a>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2021
         */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2022
        public Class<?> accessClass(Class<?> targetClass) throws IllegalAccessException {
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2023
            if (!VerifyAccess.isClassAccessible(targetClass, lookupClass, prevLookupClass, allowedModes)) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2024
                throw new MemberName(targetClass).makeAccessException("access violation", this);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2025
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2026
            checkSecurityManager(targetClass, null);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2027
            return targetClass;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2028
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2029
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2030
        /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2031
         * 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
  2032
         * 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
  2033
         * <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
  2034
         * 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
  2035
         * 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
  2036
         * with a suitably restricted receiver type prepended.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2037
         * (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
  2038
         * 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
  2039
         * to the lookup object.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2040
         * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2041
         * Before method resolution,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2042
         * 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
  2043
         * 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
  2044
         * <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
  2045
         * privileges, the access fails.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2046
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2047
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2048
         * {@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
  2049
         * 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
  2050
         * <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
  2051
         * <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
  2052
         * 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
  2053
         * 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
  2054
         * 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
  2055
         * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  2056
         * <blockquote><pre>{@code
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2057
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
  2058
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
  2059
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2060
static class Listie extends ArrayList {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2061
  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
  2062
  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
  2063
}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2064
...
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2065
// 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
  2066
MethodHandle MH_newListie = Listie.lookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2067
  .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
  2068
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
  2069
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
  2070
        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
  2071
 } catch (NoSuchMethodException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2072
// 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
  2073
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
  2074
  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
  2075
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
  2076
  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
  2077
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
  2078
  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
  2079
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
  2080
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
  2081
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
  2082
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
  2083
        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
  2084
 } catch (IllegalAccessException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2085
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
  2086
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
  2087
         * }</pre></blockquote>
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2088
         *
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2089
         * @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
  2090
         * @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
  2091
         * @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
  2092
         * @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
  2093
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2094
         * @throws NoSuchMethodException if the method does not exist
35716
07ecc6d51751 8138578: MethodHandles.Lookup.findSpecial() Javadoc fails to consider static methods
mhaupt
parents: 34959
diff changeset
  2095
         * @throws IllegalAccessException if access checking fails,
07ecc6d51751 8138578: MethodHandles.Lookup.findSpecial() Javadoc fails to consider static methods
mhaupt
parents: 34959
diff changeset
  2096
         *                                or if the method is {@code static},
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2097
         *                                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
  2098
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  2099
         * @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
  2100
         *                              <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
  2101
         * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2102
         */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2103
        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
  2104
                                        Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2105
            checkSpecialCaller(specialCaller, refc);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2106
            Lookup specialLookup = this.in(specialCaller);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2107
            MemberName method = specialLookup.resolveOrFail(REF_invokeSpecial, refc, name, type);
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2108
            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
  2109
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2110
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2111
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2112
         * 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
  2113
         * 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
  2114
         * value type.
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2115
         * 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
  2116
         * the field.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2117
         * 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
  2118
         * @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
  2119
         * @param name the field's name
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2120
         * @param type the field's type
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2121
         * @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
  2122
         * @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
  2123
         * @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
  2124
         * @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
  2125
         *                              <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
  2126
         * @throws NullPointerException if any argument is null
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2127
         * @see #findVarHandle(Class, String, Class)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2128
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2129
        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
  2130
            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
  2131
            return getDirectField(REF_getField, refc, field);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  2132
        }
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2133
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2134
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2135
         * 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
  2136
         * 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
  2137
         * The method handle will take two arguments, the instance containing
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2138
         * the field, and the value to be stored.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2139
         * The second argument will be of the field's value type.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2140
         * 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
  2141
         * @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
  2142
         * @param name the field's name
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2143
         * @param type the field's type
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2144
         * @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
  2145
         * @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
  2146
         * @throws IllegalAccessException if access checking fails, or if the field is {@code static}
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2147
         *                                or {@code final}
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  2148
         * @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
  2149
         *                              <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
  2150
         * @throws NullPointerException if any argument is null
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2151
         * @see #findVarHandle(Class, String, Class)
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2152
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2153
        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
  2154
            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
  2155
            return getDirectField(REF_putField, refc, field);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  2156
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2157
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2158
        /**
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2159
         * Produces a VarHandle giving access to a non-static field {@code name}
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2160
         * of type {@code type} declared in a class of type {@code recv}.
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2161
         * The VarHandle's variable type is {@code type} and it has one
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2162
         * coordinate type, {@code recv}.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2163
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2164
         * Access checking is performed immediately on behalf of the lookup
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2165
         * class.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2166
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2167
         * Certain access modes of the returned VarHandle are unsupported under
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2168
         * the following conditions:
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2169
         * <ul>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2170
         * <li>if the field is declared {@code final}, then the write, atomic
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2171
         *     update, numeric atomic update, and bitwise atomic update access
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2172
         *     modes are unsupported.
39470
d6f8b4a85fb0 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38774
diff changeset
  2173
         * <li>if the field type is anything other than {@code byte},
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2174
         *     {@code short}, {@code char}, {@code int}, {@code long},
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2175
         *     {@code float}, or {@code double} then numeric atomic update
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2176
         *     access modes are unsupported.
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2177
         * <li>if the field type is anything other than {@code boolean},
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2178
         *     {@code byte}, {@code short}, {@code char}, {@code int} or
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2179
         *     {@code long} then bitwise atomic update access modes are
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2180
         *     unsupported.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2181
         * </ul>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2182
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2183
         * If the field is declared {@code volatile} then the returned VarHandle
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2184
         * will override access to the field (effectively ignore the
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2185
         * {@code volatile} declaration) in accordance to its specified
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2186
         * access modes.
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2187
         * <p>
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2188
         * If the field type is {@code float} or {@code double} then numeric
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2189
         * and atomic update access modes compare values using their bitwise
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2190
         * representation (see {@link Float#floatToRawIntBits} and
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2191
         * {@link Double#doubleToRawLongBits}, respectively).
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2192
         * @apiNote
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2193
         * Bitwise comparison of {@code float} values or {@code double} values,
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2194
         * as performed by the numeric and atomic update access modes, differ
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2195
         * from the primitive {@code ==} operator and the {@link Float#equals}
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2196
         * and {@link Double#equals} methods, specifically with respect to
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2197
         * comparing NaN values or comparing {@code -0.0} with {@code +0.0}.
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2198
         * Care should be taken when performing a compare and set or a compare
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2199
         * and exchange operation with such values since the operation may
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2200
         * unexpectedly fail.
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2201
         * There are many possible NaN values that are considered to be
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2202
         * {@code NaN} in Java, although no IEEE 754 floating-point operation
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2203
         * provided by Java can distinguish between them.  Operation failure can
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2204
         * occur if the expected or witness value is a NaN value and it is
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2205
         * transformed (perhaps in a platform specific manner) into another NaN
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2206
         * value, and thus has a different bitwise representation (see
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2207
         * {@link Float#intBitsToFloat} or {@link Double#longBitsToDouble} for more
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2208
         * details).
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2209
         * The values {@code -0.0} and {@code +0.0} have different bitwise
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2210
         * representations but are considered equal when using the primitive
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2211
         * {@code ==} operator.  Operation failure can occur if, for example, a
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2212
         * numeric algorithm computes an expected value to be say {@code -0.0}
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2213
         * and previously computed the witness value to be say {@code +0.0}.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2214
         * @param recv the receiver class, of type {@code R}, that declares the
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2215
         * non-static field
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2216
         * @param name the field's name
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2217
         * @param type the field's type, of type {@code T}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2218
         * @return a VarHandle giving access to non-static fields.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2219
         * @throws NoSuchFieldException if the field does not exist
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2220
         * @throws IllegalAccessException if access checking fails, or if the field is {@code static}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2221
         * @exception SecurityException if a security manager is present and it
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2222
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2223
         * @throws NullPointerException if any argument is null
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2224
         * @since 9
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2225
         */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2226
        public VarHandle findVarHandle(Class<?> recv, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2227
            MemberName getField = resolveOrFail(REF_getField, recv, name, type);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2228
            MemberName putField = resolveOrFail(REF_putField, recv, name, type);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2229
            return getFieldVarHandle(REF_getField, REF_putField, recv, getField, putField);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2230
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2231
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2232
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2233
         * 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
  2234
         * 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
  2235
         * value type.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2236
         * The method handle will take no arguments.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2237
         * 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
  2238
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  2239
         * 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
  2240
         * 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
  2241
         * @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
  2242
         * @param name the field's name
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2243
         * @param type the field's type
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2244
         * @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
  2245
         * @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
  2246
         * @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
  2247
         * @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
  2248
         *                              <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
  2249
         * @throws NullPointerException if any argument is null
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2250
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2251
        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
  2252
            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
  2253
            return getDirectField(REF_getStatic, refc, field);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  2254
        }
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2255
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2256
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2257
         * 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
  2258
         * 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
  2259
         * The method handle will take a single
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2260
         * 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
  2261
         * 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
  2262
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  2263
         * 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
  2264
         * 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
  2265
         * @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
  2266
         * @param name the field's name
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2267
         * @param type the field's type
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2268
         * @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
  2269
         * @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
  2270
         * @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2271
         *                                or is {@code final}
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  2272
         * @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
  2273
         *                              <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
  2274
         * @throws NullPointerException if any argument is null
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
  2275
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2276
        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
  2277
            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
  2278
            return getDirectField(REF_putStatic, refc, field);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  2279
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2280
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2281
        /**
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2282
         * Produces a VarHandle giving access to a static field {@code name} of
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2283
         * type {@code type} declared in a class of type {@code decl}.
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2284
         * The VarHandle's variable type is {@code type} and it has no
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2285
         * coordinate types.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2286
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2287
         * Access checking is performed immediately on behalf of the lookup
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2288
         * class.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2289
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2290
         * If the returned VarHandle is operated on, the declaring class will be
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2291
         * initialized, if it has not already been initialized.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2292
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2293
         * Certain access modes of the returned VarHandle are unsupported under
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2294
         * the following conditions:
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2295
         * <ul>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2296
         * <li>if the field is declared {@code final}, then the write, atomic
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2297
         *     update, numeric atomic update, and bitwise atomic update access
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2298
         *     modes are unsupported.
39470
d6f8b4a85fb0 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38774
diff changeset
  2299
         * <li>if the field type is anything other than {@code byte},
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2300
         *     {@code short}, {@code char}, {@code int}, {@code long},
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2301
         *     {@code float}, or {@code double}, then numeric atomic update
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2302
         *     access modes are unsupported.
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2303
         * <li>if the field type is anything other than {@code boolean},
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2304
         *     {@code byte}, {@code short}, {@code char}, {@code int} or
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2305
         *     {@code long} then bitwise atomic update access modes are
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2306
         *     unsupported.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2307
         * </ul>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2308
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2309
         * If the field is declared {@code volatile} then the returned VarHandle
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2310
         * will override access to the field (effectively ignore the
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2311
         * {@code volatile} declaration) in accordance to its specified
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2312
         * access modes.
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2313
         * <p>
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2314
         * If the field type is {@code float} or {@code double} then numeric
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2315
         * and atomic update access modes compare values using their bitwise
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2316
         * representation (see {@link Float#floatToRawIntBits} and
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2317
         * {@link Double#doubleToRawLongBits}, respectively).
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2318
         * @apiNote
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2319
         * Bitwise comparison of {@code float} values or {@code double} values,
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2320
         * as performed by the numeric and atomic update access modes, differ
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2321
         * from the primitive {@code ==} operator and the {@link Float#equals}
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2322
         * and {@link Double#equals} methods, specifically with respect to
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2323
         * comparing NaN values or comparing {@code -0.0} with {@code +0.0}.
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2324
         * Care should be taken when performing a compare and set or a compare
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2325
         * and exchange operation with such values since the operation may
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2326
         * unexpectedly fail.
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2327
         * There are many possible NaN values that are considered to be
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2328
         * {@code NaN} in Java, although no IEEE 754 floating-point operation
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2329
         * provided by Java can distinguish between them.  Operation failure can
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2330
         * occur if the expected or witness value is a NaN value and it is
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2331
         * transformed (perhaps in a platform specific manner) into another NaN
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2332
         * value, and thus has a different bitwise representation (see
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2333
         * {@link Float#intBitsToFloat} or {@link Double#longBitsToDouble} for more
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2334
         * details).
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2335
         * The values {@code -0.0} and {@code +0.0} have different bitwise
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2336
         * representations but are considered equal when using the primitive
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2337
         * {@code ==} operator.  Operation failure can occur if, for example, a
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2338
         * numeric algorithm computes an expected value to be say {@code -0.0}
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2339
         * and previously computed the witness value to be say {@code +0.0}.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2340
         * @param decl the class that declares the static field
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2341
         * @param name the field's name
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2342
         * @param type the field's type, of type {@code T}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2343
         * @return a VarHandle giving access to a static field
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2344
         * @throws NoSuchFieldException if the field does not exist
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2345
         * @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2346
         * @exception SecurityException if a security manager is present and it
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2347
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2348
         * @throws NullPointerException if any argument is null
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2349
         * @since 9
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2350
         */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2351
        public VarHandle findStaticVarHandle(Class<?> decl, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2352
            MemberName getField = resolveOrFail(REF_getStatic, decl, name, type);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2353
            MemberName putField = resolveOrFail(REF_putStatic, decl, name, type);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2354
            return getFieldVarHandle(REF_getStatic, REF_putStatic, decl, getField, putField);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2355
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2356
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2357
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2358
         * 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
  2359
         * 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
  2360
         * 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
  2361
         * 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
  2362
         * 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
  2363
         * without any insertion of an additional receiver parameter.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2364
         * 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
  2365
         * 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
  2366
         * requested method on the given receiver.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2367
         * <p>
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2368
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2369
         * {@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
  2370
         * 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
  2371
         * <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
  2372
         * (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
  2373
         * 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
  2374
         * <p>
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44860
diff changeset
  2375
         * This is almost equivalent to the following code, with some differences noted below:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2376
         * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2377
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
  2378
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
  2379
...
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2380
MethodHandle mh0 = lookup().findVirtual(defc, name, type);
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2381
MethodHandle mh1 = mh0.bindTo(receiver);
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  2382
mh1 = mh1.withVarargs(mh0.isVarargsCollector());
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2383
return mh1;
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2384
         * }</pre></blockquote>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2385
         * 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
  2386
         * 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
  2387
         * to the lookup class.
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44860
diff changeset
  2388
         * (Unlike {@code bind}, {@code bindTo} does not preserve variable arity.
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44860
diff changeset
  2389
         * Also, {@code bindTo} may throw a {@code ClassCastException} in instances where {@code bind} would
45140
2901d2f1a6aa 8180075: Javadoc of MethodHandles.Lookup::bind should note the difference from MethodHandle::bindTo
psandoz
parents: 45124
diff changeset
  2390
         * throw an {@code IllegalAccessException}, as in the case where the member is {@code protected} and
2901d2f1a6aa 8180075: Javadoc of MethodHandles.Lookup::bind should note the difference from MethodHandle::bindTo
psandoz
parents: 45124
diff changeset
  2391
         * the receiver is restricted by {@code findVirtual} to the lookup class.)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2392
         * @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
  2393
         * @param name the name of the method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2394
         * @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
  2395
         * @return the desired method handle
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2396
         * @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
  2397
         * @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
  2398
         *                                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
  2399
         *                                is set and {@code asVarargsCollector} fails
8349
e1ba54c43609 7014755: JSR 292 member lookup interaction with security manager
jrose
parents: 8347
diff changeset
  2400
         * @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
  2401
         *                              <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
  2402
         * @throws NullPointerException if any argument is null
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2403
         * @see MethodHandle#bindTo
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2404
         * @see #findVirtual
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2405
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2406
        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
  2407
            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
  2408
            MemberName method = resolveOrFail(REF_invokeSpecial, refc, name, type);
44859
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  2409
            MethodHandle mh = getDirectMethodNoRestrictInvokeSpecial(refc, method, findBoundCallerClass(method));
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  2410
            if (!mh.type().leadingReferenceParameter().isAssignableFrom(receiver.getClass())) {
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  2411
                throw new IllegalAccessException("The restricted defining class " +
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  2412
                                                 mh.type().leadingReferenceParameter().getName() +
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  2413
                                                 " is not assignable from receiver class " +
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  2414
                                                 receiver.getClass().getName());
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  2415
            }
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  2416
            return mh.bindArgumentL(0, receiver).setVarargs(method);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2417
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2418
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2419
        /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2420
         * 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
  2421
         * 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
  2422
         * 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
  2423
         * 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
  2424
         * 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
  2425
         * 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
  2426
         * 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
  2427
         * 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
  2428
         * 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
  2429
         * 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
  2430
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2431
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2432
         * {@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
  2433
         * 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
  2434
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  2435
         * 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
  2436
         * 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
  2437
         * 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
  2438
         * @param m the reflected method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2439
         * @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
  2440
         * @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
  2441
         *                                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
  2442
         *                                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
  2443
         * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2444
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2445
        public MethodHandle unreflect(Method m) throws IllegalAccessException {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2446
            if (m.getDeclaringClass() == MethodHandle.class) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2447
                MethodHandle mh = unreflectForMH(m);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2448
                if (mh != null)  return mh;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2449
            }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2450
            if (m.getDeclaringClass() == VarHandle.class) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2451
                MethodHandle mh = unreflectForVH(m);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2452
                if (mh != null)  return mh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2453
            }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2454
            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
  2455
            byte refKind = method.getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2456
            if (refKind == REF_invokeSpecial)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2457
                refKind = REF_invokeVirtual;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2458
            assert(method.isMethod());
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  2459
            @SuppressWarnings("deprecation")
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2460
            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
  2461
            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
  2462
        }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2463
        private MethodHandle unreflectForMH(Method m) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2464
            // these names require special lookups because they throw UnsupportedOperationException
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2465
            if (MemberName.isMethodHandleInvokeName(m.getName()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2466
                return MethodHandleImpl.fakeMethodHandleInvoke(new MemberName(m));
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2467
            return null;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2468
        }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2469
        private MethodHandle unreflectForVH(Method m) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2470
            // these names require special lookups because they throw UnsupportedOperationException
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2471
            if (MemberName.isVarHandleMethodInvokeName(m.getName()))
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2472
                return MethodHandleImpl.fakeVarHandleInvoke(new MemberName(m));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2473
            return null;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2474
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2475
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2476
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2477
         * Produces a method handle for a reflected method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2478
         * 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
  2479
         * <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
  2480
         * instruction from within the explicitly specified {@code specialCaller}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2481
         * 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
  2482
         * with a suitably restricted receiver type prepended.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2483
         * (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
  2484
         * 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
  2485
         * 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
  2486
         * 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
  2487
         * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2488
         * Before method resolution,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2489
         * 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
  2490
         * 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
  2491
         * <a href="MethodHandles.Lookup.html#privacc">private access</a>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2492
         * privileges, the access fails.
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2493
         * <p>
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2494
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2495
         * {@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
  2496
         * 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
  2497
         * @param m the reflected method
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2498
         * @param specialCaller the class nominally calling the method
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2499
         * @return a method handle which can invoke the reflected method
35716
07ecc6d51751 8138578: MethodHandles.Lookup.findSpecial() Javadoc fails to consider static methods
mhaupt
parents: 34959
diff changeset
  2500
         * @throws IllegalAccessException if access checking fails,
07ecc6d51751 8138578: MethodHandles.Lookup.findSpecial() Javadoc fails to consider static methods
mhaupt
parents: 34959
diff changeset
  2501
         *                                or if the method is {@code static},
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2502
         *                                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
  2503
         *                                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
  2504
         * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2505
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2506
        public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException {
57735
7ba5e49258de 8209005: Lookup.unreflectSpecial fails for default methods when Lookup.findSpecial works
mchung
parents: 57558
diff changeset
  2507
            checkSpecialCaller(specialCaller, m.getDeclaringClass());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2508
            Lookup specialLookup = this.in(specialCaller);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2509
            MemberName method = new MemberName(m, true);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2510
            assert(method.isMethod());
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2511
            // 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
  2512
            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
  2513
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2514
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2515
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2516
         * Produces a method handle for a reflected constructor.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2517
         * 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
  2518
         * 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
  2519
         * The method handle will perform a {@code newInstance} operation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2520
         * 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
  2521
         * arguments passed to the method handle.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2522
         * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2523
         * 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
  2524
         * 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
  2525
         * <p>
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2526
         * The returned method handle will have
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  2527
         * {@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
  2528
         * 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
  2529
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  2530
         * 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
  2531
         * be initialized, if it has not already been initialized.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2532
         * @param c the reflected constructor
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2533
         * @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
  2534
         * @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
  2535
         *                                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
  2536
         *                                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
  2537
         * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2538
         */
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2539
        public MethodHandle unreflectConstructor(Constructor<?> c) throws IllegalAccessException {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2540
            MemberName ctor = new MemberName(c);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2541
            assert(ctor.isConstructor());
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  2542
            @SuppressWarnings("deprecation")
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2543
            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
  2544
            return lookup.getDirectConstructorNoSecurityManager(ctor.getDeclaringClass(), ctor);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2545
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2546
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2547
        /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  2548
         * 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
  2549
         * 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
  2550
         * value type.
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2551
         * If the field is {@code static}, the method handle will take no arguments.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2552
         * Otherwise, its single argument will be the instance containing
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  2553
         * the field.
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2554
         * If the {@code Field} object's {@code accessible} flag is not set,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2555
         * 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
  2556
         * <p>
50fba462caa5 8024599: JSR 292 direct method handles need to respect initialization rules for static members
jrose
parents: 20531
diff changeset
  2557
         * 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
  2558
         * 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
  2559
         * 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
  2560
         * @param f the reflected field
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2561
         * @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
  2562
         * @throws IllegalAccessException if access checking fails
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2563
         * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2564
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2565
        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
  2566
            return unreflectField(f, false);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2567
        }
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2568
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2569
        /**
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2570
         * Produces a method handle giving write access to a reflected field.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2571
         * The type of the method handle will have a void return type.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2572
         * If the field is {@code static}, the method handle will take a single
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2573
         * argument, of the field's value type, the value to be stored.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2574
         * Otherwise, the two arguments will be the instance containing
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2575
         * the field, and the value to be stored.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2576
         * If the {@code Field} object's {@code accessible} flag is not set,
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2577
         * access checking is performed immediately on behalf of the lookup class.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2578
         * <p>
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2579
         * If the field is {@code final}, write access will not be
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2580
         * allowed and access checking will fail, except under certain
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2581
         * narrow circumstances documented for {@link Field#set Field.set}.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2582
         * A method handle is returned only if a corresponding call to
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2583
         * the {@code Field} object's {@code set} method could return
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2584
         * normally.  In particular, fields which are both {@code static}
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2585
         * and {@code final} may never be set.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2586
         * <p>
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2587
         * If the field is {@code static}, and
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2588
         * if the returned method handle is invoked, the field's class will
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2589
         * be initialized, if it has not already been initialized.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2590
         * @param f the reflected field
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2591
         * @return a method handle which can store values into the reflected field
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2592
         * @throws IllegalAccessException if access checking fails,
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2593
         *         or if the field is {@code final} and write access
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2594
         *         is not enabled on the {@code Field} object
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2595
         * @throws NullPointerException if the argument is null
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2596
         */
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2597
        public MethodHandle unreflectSetter(Field f) throws IllegalAccessException {
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2598
            return unreflectField(f, true);
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2599
        }
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2600
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2601
        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
  2602
            MemberName field = new MemberName(f, isSetter);
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2603
            if (isSetter && field.isStatic() && field.isFinal())
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 54206
diff changeset
  2604
                throw field.makeAccessException("static final field has no write access", this);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2605
            assert(isSetter
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2606
                    ? MethodHandleNatives.refKindIsSetter(field.getReferenceKind())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2607
                    : MethodHandleNatives.refKindIsGetter(field.getReferenceKind()));
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43219
diff changeset
  2608
            @SuppressWarnings("deprecation")
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2609
            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
  2610
            return lookup.getDirectFieldNoSecurityManager(field.getReferenceKind(), f.getDeclaringClass(), field);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2611
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2612
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2613
        /**
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2614
         * Produces a VarHandle giving access to a reflected field {@code f}
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2615
         * of type {@code T} declared in a class of type {@code R}.
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2616
         * The VarHandle's variable type is {@code T}.
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2617
         * If the field is non-static the VarHandle has one coordinate type,
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2618
         * {@code R}.  Otherwise, the field is static, and the VarHandle has no
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2619
         * coordinate types.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2620
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2621
         * Access checking is performed immediately on behalf of the lookup
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2622
         * class, regardless of the value of the field's {@code accessible}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2623
         * flag.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2624
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2625
         * If the field is static, and if the returned VarHandle is operated
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2626
         * on, the field's declaring class will be initialized, if it has not
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2627
         * already been initialized.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2628
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2629
         * Certain access modes of the returned VarHandle are unsupported under
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2630
         * the following conditions:
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2631
         * <ul>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2632
         * <li>if the field is declared {@code final}, then the write, atomic
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2633
         *     update, numeric atomic update, and bitwise atomic update access
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2634
         *     modes are unsupported.
39470
d6f8b4a85fb0 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38774
diff changeset
  2635
         * <li>if the field type is anything other than {@code byte},
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2636
         *     {@code short}, {@code char}, {@code int}, {@code long},
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2637
         *     {@code float}, or {@code double} then numeric atomic update
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2638
         *     access modes are unsupported.
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2639
         * <li>if the field type is anything other than {@code boolean},
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2640
         *     {@code byte}, {@code short}, {@code char}, {@code int} or
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2641
         *     {@code long} then bitwise atomic update access modes are
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  2642
         *     unsupported.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2643
         * </ul>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2644
         * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2645
         * If the field is declared {@code volatile} then the returned VarHandle
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2646
         * will override access to the field (effectively ignore the
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  2647
         * {@code volatile} declaration) in accordance to its specified
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2648
         * access modes.
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2649
         * <p>
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2650
         * If the field type is {@code float} or {@code double} then numeric
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2651
         * and atomic update access modes compare values using their bitwise
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2652
         * representation (see {@link Float#floatToRawIntBits} and
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2653
         * {@link Double#doubleToRawLongBits}, respectively).
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2654
         * @apiNote
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2655
         * Bitwise comparison of {@code float} values or {@code double} values,
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2656
         * as performed by the numeric and atomic update access modes, differ
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2657
         * from the primitive {@code ==} operator and the {@link Float#equals}
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2658
         * and {@link Double#equals} methods, specifically with respect to
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2659
         * comparing NaN values or comparing {@code -0.0} with {@code +0.0}.
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2660
         * Care should be taken when performing a compare and set or a compare
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2661
         * and exchange operation with such values since the operation may
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2662
         * unexpectedly fail.
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2663
         * There are many possible NaN values that are considered to be
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2664
         * {@code NaN} in Java, although no IEEE 754 floating-point operation
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2665
         * provided by Java can distinguish between them.  Operation failure can
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2666
         * occur if the expected or witness value is a NaN value and it is
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2667
         * transformed (perhaps in a platform specific manner) into another NaN
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2668
         * value, and thus has a different bitwise representation (see
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2669
         * {@link Float#intBitsToFloat} or {@link Double#longBitsToDouble} for more
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2670
         * details).
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2671
         * The values {@code -0.0} and {@code +0.0} have different bitwise
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2672
         * representations but are considered equal when using the primitive
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2673
         * {@code ==} operator.  Operation failure can occur if, for example, a
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2674
         * numeric algorithm computes an expected value to be say {@code -0.0}
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  2675
         * and previously computed the witness value to be say {@code +0.0}.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2676
         * @param f the reflected field, with a field of type {@code T}, and
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2677
         * a declaring class of type {@code R}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2678
         * @return a VarHandle giving access to non-static fields or a static
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2679
         * field
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2680
         * @throws IllegalAccessException if access checking fails
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2681
         * @throws NullPointerException if the argument is null
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2682
         * @since 9
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2683
         */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2684
        public VarHandle unreflectVarHandle(Field f) throws IllegalAccessException {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2685
            MemberName getField = new MemberName(f, false);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2686
            MemberName putField = new MemberName(f, true);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2687
            return getFieldVarHandleNoSecurityManager(getField.getReferenceKind(), putField.getReferenceKind(),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2688
                                                      f.getDeclaringClass(), getField, putField);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2689
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2690
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2691
        /**
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2692
         * 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
  2693
         * created by this lookup object or a similar one.
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2694
         * Security and access checks are performed to ensure that this lookup object
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2695
         * is capable of reproducing the target method handle.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2696
         * This means that the cracking may fail if target is a direct method handle
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2697
         * 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
  2698
         * 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
  2699
         * and was created by a lookup object for a different class.
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2700
         * @param target a direct method handle to crack into symbolic reference components
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2701
         * @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
  2702
         * @exception SecurityException if a security manager is present and it
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2703
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2704
         * @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
  2705
         * @exception NullPointerException if the target is {@code null}
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2706
         * @see MethodHandleInfo
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2707
         * @since 1.8
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2708
         */
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2709
        public MethodHandleInfo revealDirect(MethodHandle target) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2710
            MemberName member = target.internalMemberName();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2711
            if (member == null || (!member.isResolved() &&
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2712
                                   !member.isMethodHandleInvoke() &&
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  2713
                                   !member.isVarHandleMethodInvoke()))
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2714
                throw newIllegalArgumentException("not a direct method handle");
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2715
            Class<?> defc = member.getDeclaringClass();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2716
            byte refKind = member.getReferenceKind();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2717
            assert(MethodHandleNatives.refKindIsValid(refKind));
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2718
            if (refKind == REF_invokeSpecial && !target.isInvokeSpecial())
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2719
                // Devirtualized method invocation is usually formally virtual.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2720
                // To avoid creating extra MemberName objects for this common case,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2721
                // we encode this extra degree of freedom using MH.isInvokeSpecial.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2722
                refKind = REF_invokeVirtual;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2723
            if (refKind == REF_invokeVirtual && defc.isInterface())
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2724
                // Symbolic reference is through interface but resolves to Object method (toString, etc.)
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2725
                refKind = REF_invokeInterface;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2726
            // Check SM permissions and member access before cracking.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2727
            try {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2728
                checkAccess(refKind, defc, member);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2729
                checkSecurityManager(defc, member);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2730
            } catch (IllegalAccessException ex) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2731
                throw new IllegalArgumentException(ex);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2732
            }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2733
            if (allowedModes != TRUSTED && member.isCallerSensitive()) {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2734
                Class<?> callerClass = target.internalCallerClass();
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2735
                if (!hasPrivateAccess() || callerClass != lookupClass())
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2736
                    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
  2737
            }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2738
            // Produce the handle to the results.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2739
            return new InfoFromMemberName(this, member, refKind);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2740
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  2741
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2742
        /// Helper methods, all package-private.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2743
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2744
        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
  2745
            checkSymbolicClass(refc);  // do this before attempting to resolve
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  2746
            Objects.requireNonNull(name);
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  2747
            Objects.requireNonNull(type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2748
            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
  2749
                                            NoSuchFieldException.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2750
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2751
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2752
        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
  2753
            checkSymbolicClass(refc);  // do this before attempting to resolve
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  2754
            Objects.requireNonNull(name);
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  2755
            Objects.requireNonNull(type);
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2756
            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
  2757
            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
  2758
                                            NoSuchMethodException.class);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2759
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2760
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  2761
        MemberName resolveOrFail(byte refKind, MemberName member) throws ReflectiveOperationException {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  2762
            checkSymbolicClass(member.getDeclaringClass());  // do this before attempting to resolve
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  2763
            Objects.requireNonNull(member.getName());
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  2764
            Objects.requireNonNull(member.getType());
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  2765
            return IMPL_NAMES.resolveOrFail(refKind, member, lookupClassOrNull(),
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  2766
                                            ReflectiveOperationException.class);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  2767
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  2768
49432
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2769
        MemberName resolveOrNull(byte refKind, MemberName member) {
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2770
            // do this before attempting to resolve
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2771
            if (!isClassAccessible(member.getDeclaringClass())) {
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2772
                return null;
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2773
            }
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2774
            Objects.requireNonNull(member.getName());
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2775
            Objects.requireNonNull(member.getType());
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2776
            return IMPL_NAMES.resolveOrNull(refKind, member, lookupClassOrNull());
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2777
        }
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2778
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2779
        void checkSymbolicClass(Class<?> refc) throws IllegalAccessException {
49432
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2780
            if (!isClassAccessible(refc)) {
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2781
                throw new MemberName(refc).makeAccessException("symbolic reference class is not accessible", this);
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2782
            }
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2783
        }
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2784
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  2785
        boolean isClassAccessible(Class<?> refc) {
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27947
diff changeset
  2786
            Objects.requireNonNull(refc);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2787
            Class<?> caller = lookupClassOrNull();
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2788
            return caller == null || VerifyAccess.isClassAccessible(refc, caller, prevLookupClass, allowedModes);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2789
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2790
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  2791
        /** 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
  2792
        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
  2793
            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
  2794
                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
  2795
        }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2796
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
  2797
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2798
        /**
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2799
         * Find my trustable caller class if m is a caller sensitive method.
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2800
         * 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
  2801
         * Otherwise, if m is caller-sensitive, throw IllegalAccessException.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2802
         */
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2803
        Class<?> findBoundCallerClass(MemberName m) throws IllegalAccessException {
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2804
            Class<?> callerClass = null;
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2805
            if (MethodHandleNatives.isCallerSensitive(m)) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2806
                // 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
  2807
                if (hasPrivateAccess()) {
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2808
                    callerClass = lookupClass;
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2809
                } else {
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2810
                    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
  2811
                }
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2812
            }
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2813
            return callerClass;
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2814
        }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  2815
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
  2816
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
  2817
         * Returns {@code true} if this lookup has {@code PRIVATE} access.
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2818
         * @return {@code true} if this lookup has {@code PRIVATE} access.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
  2819
         * @since 9
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
  2820
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41829
diff changeset
  2821
        public boolean hasPrivateAccess() {
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2822
            return (allowedModes & PRIVATE) != 0;
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2823
        }
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2824
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  2825
        /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2826
         * Perform necessary <a href="MethodHandles.Lookup.html#secmgr">access checks</a>.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2827
         * Determines a trustable caller class to compare with refc, the symbolic reference class.
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  2828
         * 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
  2829
         */
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2830
        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
  2831
            SecurityManager smgr = System.getSecurityManager();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2832
            if (smgr == null)  return;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2833
            if (allowedModes == TRUSTED)  return;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  2834
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2835
            // Step 1:
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2836
            boolean fullPowerLookup = hasPrivateAccess();
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2837
            if (!fullPowerLookup ||
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2838
                !VerifyAccess.classLoaderIsAncestor(lookupClass, refc)) {
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2839
                ReflectUtil.checkPackageAccess(refc);
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2840
            }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  2841
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2842
            if (m == null) {  // findClass or accessClass
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2843
                // Step 2b:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2844
                if (!fullPowerLookup) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2845
                    smgr.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2846
                }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2847
                return;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2848
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2849
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2850
            // Step 2a:
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2851
            if (m.isPublic()) return;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2852
            if (!fullPowerLookup) {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2853
                smgr.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  2854
            }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16898
diff changeset
  2855
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18569
diff changeset
  2856
            // Step 3:
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2857
            Class<?> defc = m.getDeclaringClass();
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2858
            if (!fullPowerLookup && defc != refc) {
18264
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2859
                ReflectUtil.checkPackageAccess(defc);
286a7973575a 8009424: Restrict publicLookup with additional checks
twisti
parents: 16906
diff changeset
  2860
            }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2861
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  2862
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2863
        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
  2864
            boolean wantStatic = (refKind == REF_invokeStatic);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2865
            String message;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2866
            if (m.isConstructor())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2867
                message = "expected a method, not a constructor";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2868
            else if (!m.isMethod())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2869
                message = "expected a method";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2870
            else if (wantStatic != m.isStatic())
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2871
                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
  2872
            else
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2873
                { checkAccess(refKind, refc, m); return; }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  2874
            throw m.makeAccessException(message, this);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2875
        }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2876
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2877
        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
  2878
            boolean wantStatic = !MethodHandleNatives.refKindHasReceiver(refKind);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2879
            String message;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2880
            if (wantStatic != m.isStatic())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2881
                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
  2882
            else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2883
                { checkAccess(refKind, refc, m); return; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2884
            throw m.makeAccessException(message, this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2885
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2886
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2887
        /** 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
  2888
        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
  2889
            assert(m.referenceKindIsConsistentWith(refKind) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2890
                   MethodHandleNatives.refKindIsValid(refKind) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2891
                   (MethodHandleNatives.refKindIsField(refKind) == m.isField()));
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2892
            int allowedModes = this.allowedModes;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2893
            if (allowedModes == TRUSTED)  return;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2894
            int mods = m.getModifiers();
20528
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2895
            if (Modifier.isProtected(mods) &&
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2896
                    refKind == REF_invokeVirtual &&
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2897
                    m.getDeclaringClass() == Object.class &&
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2898
                    m.getName().equals("clone") &&
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2899
                    refc.isArray()) {
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2900
                // The JVM does this hack also.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2901
                // (See ClassVerifier::verify_invoke_instructions
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2902
                // and LinkResolver::check_method_accessability.)
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2903
                // 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
  2904
                // there is no separate method for int[].clone.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2905
                // All arrays simply inherit Object.clone.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2906
                // But for access checking logic, we make Object.clone
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2907
                // (normally protected) appear to be public.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2908
                // Later on, when the DirectMethodHandle is created,
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2909
                // its leading argument will be restricted to the
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2910
                // requested array type.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2911
                // N.B. The return type is not adjusted, because
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2912
                // that is *not* the bytecode behavior.
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2913
                mods ^= Modifier.PROTECTED | Modifier.PUBLIC;
0b1e2130d3f7 8001105: findVirtual of Object[].clone produces internal error
jrose
parents: 20527
diff changeset
  2914
            }
25537
087de200d457 8035788: Provide more consistency for lookups
twisti
parents: 24860
diff changeset
  2915
            if (Modifier.isProtected(mods) && refKind == REF_newInvokeSpecial) {
087de200d457 8035788: Provide more consistency for lookups
twisti
parents: 24860
diff changeset
  2916
                // cannot "new" a protected ctor in a different package
087de200d457 8035788: Provide more consistency for lookups
twisti
parents: 24860
diff changeset
  2917
                mods ^= Modifier.PROTECTED;
087de200d457 8035788: Provide more consistency for lookups
twisti
parents: 24860
diff changeset
  2918
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2919
            if (Modifier.isFinal(mods) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2920
                    MethodHandleNatives.refKindIsSetter(refKind))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2921
                throw m.makeAccessException("unexpected set of a final field", this);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2922
            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
  2923
            if ((requestedModes & allowedModes) != 0) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2924
                if (VerifyAccess.isMemberAccessible(refc, m.getDeclaringClass(),
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2925
                                                    mods, lookupClass(), previousLookupClass(), allowedModes))
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2926
                    return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2927
            } else {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2928
                // 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
  2929
                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
  2930
                        && VerifyAccess.isSamePackage(m.getDeclaringClass(), lookupClass()))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2931
                    return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2932
            }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  2933
            throw m.makeAccessException(accessFailedMessage(refc, m), this);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2934
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2935
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2936
        String accessFailedMessage(Class<?> refc, MemberName m) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2937
            Class<?> defc = m.getDeclaringClass();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2938
            int mods = m.getModifiers();
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2939
            // check the class first:
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2940
            boolean classOK = (Modifier.isPublic(defc.getModifiers()) &&
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2941
                               (defc == refc ||
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2942
                                Modifier.isPublic(refc.getModifiers())));
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2943
            if (!classOK && (allowedModes & PACKAGE) != 0) {
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2944
                // ignore previous lookup class to check if default package access
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2945
                classOK = (VerifyAccess.isClassAccessible(defc, lookupClass(), null, FULL_POWER_MODES) &&
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2946
                           (defc == refc ||
57512
f557f260b787 8173978: Lookup.in should allow teleporting from a lookup class in a named module without dropping all access
mchung
parents: 55022
diff changeset
  2947
                            VerifyAccess.isClassAccessible(refc, lookupClass(), null, FULL_POWER_MODES)));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2948
            }
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
  2949
            if (!classOK)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2950
                return "class is not public";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2951
            if (Modifier.isPublic(mods))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36220
diff changeset
  2952
                return "access to public member failed";  // (how?, module not readable?)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2953
            if (Modifier.isPrivate(mods))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2954
                return "member is private";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2955
            if (Modifier.isProtected(mods))
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2956
                return "member is protected";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2957
            return "member is private to package";
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2958
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2959
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2960
        private void checkSpecialCaller(Class<?> specialCaller, Class<?> refc) throws IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2961
            int allowedModes = this.allowedModes;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2962
            if (allowedModes == TRUSTED)  return;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2963
            if (!hasPrivateAccess()
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2964
                || (specialCaller != lookupClass()
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  2965
                       // ensure non-abstract methods in superinterfaces can be special-invoked
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
  2966
                    && !(refc != null && refc.isInterface() && refc.isAssignableFrom(specialCaller))))
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  2967
                throw new MemberName(specialCaller).
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  2968
                    makeAccessException("no private access for invokespecial", this);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2969
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2970
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2971
        private boolean restrictProtectedReceiver(MemberName method) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2972
            // 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
  2973
            // 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
  2974
            if (!method.isProtected() || method.isStatic()
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2975
                || allowedModes == TRUSTED
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  2976
                || method.getDeclaringClass() == lookupClass()
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
  2977
                || VerifyAccess.isSamePackage(method.getDeclaringClass(), lookupClass()))
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2978
                return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2979
            return true;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2980
        }
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  2981
        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
  2982
            assert(!method.isStatic());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  2983
            // 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
  2984
            if (!method.getDeclaringClass().isAssignableFrom(caller)) {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  2985
                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
  2986
            }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2987
            MethodType rawType = mh.type();
44859
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  2988
            if (caller.isAssignableFrom(rawType.parameterType(0))) return mh; // no need to restrict; already narrow
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2989
            MethodType narrowType = rawType.changeParameterType(0, caller);
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  2990
            assert(!mh.isVarargsCollector());  // viewAsType will lose varargs-ness
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  2991
            assert(mh.viewAsTypeChecks(narrowType, true));
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  2992
            return mh.copyWith(narrowType, mh.form);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2993
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  2994
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2995
        /** Check access and get the requested method. */
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  2996
        private MethodHandle getDirectMethod(byte refKind, Class<?> refc, MemberName method, Class<?> boundCallerClass) throws IllegalAccessException {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2997
            final boolean doRestrict    = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  2998
            final boolean checkSecurity = true;
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  2999
            return getDirectMethodCommon(refKind, refc, method, checkSecurity, doRestrict, boundCallerClass);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3000
        }
44859
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  3001
        /** Check access and get the requested method, for invokespecial with no restriction on the application of narrowing rules. */
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3002
        private MethodHandle getDirectMethodNoRestrictInvokeSpecial(Class<?> refc, MemberName method, Class<?> boundCallerClass) throws IllegalAccessException {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3003
            final boolean doRestrict    = false;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3004
            final boolean checkSecurity = true;
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3005
            return getDirectMethodCommon(REF_invokeSpecial, refc, method, checkSecurity, doRestrict, boundCallerClass);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3006
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3007
        /** Check access and get the requested method, eliding security manager checks. */
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3008
        private MethodHandle getDirectMethodNoSecurityManager(byte refKind, Class<?> refc, MemberName method, Class<?> boundCallerClass) throws IllegalAccessException {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3009
            final boolean doRestrict    = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3010
            final boolean checkSecurity = false;  // not needed for reflection or for linking CONSTANT_MH constants
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3011
            return getDirectMethodCommon(refKind, refc, method, checkSecurity, doRestrict, boundCallerClass);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3012
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3013
        /** 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
  3014
        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
  3015
                                                   boolean checkSecurity,
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3016
                                                   boolean doRestrict, Class<?> boundCallerClass) throws IllegalAccessException {
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
  3017
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3018
            checkMethod(refKind, refc, method);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3019
            // 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
  3020
            if (checkSecurity)
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3021
                checkSecurityManager(refc, method);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  3022
            assert(!method.isMethodHandleInvoke());
16898
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3023
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3024
            if (refKind == REF_invokeSpecial &&
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3025
                refc != lookupClass() &&
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18766
diff changeset
  3026
                !refc.isInterface() &&
24860
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3027
                refc != lookupClass().getSuperclass() &&
16898
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3028
                refc.isAssignableFrom(lookupClass())) {
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3029
                assert(!method.getName().equals("<init>"));  // not this code path
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50328
diff changeset
  3030
16898
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3031
                // Per JVMS 6.5, desc. of invokespecial instruction:
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3032
                // If the method is in a superclass of the LC,
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3033
                // 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
  3034
                // repeat the search (symbolic lookup) from LC.super
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3035
                // and continue with the direct superclass of that class,
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3036
                // 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
  3037
                // FIXME: MemberName.resolve should handle this instead.
24860
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3038
                Class<?> refcAsSuper = lookupClass();
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3039
                MemberName m2;
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3040
                do {
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3041
                    refcAsSuper = refcAsSuper.getSuperclass();
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3042
                    m2 = new MemberName(refcAsSuper,
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3043
                                        method.getName(),
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3044
                                        method.getMethodType(),
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3045
                                        REF_invokeSpecial);
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3046
                    m2 = IMPL_NAMES.resolveOrNull(refKind, m2, lookupClassOrNull());
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3047
                } while (m2 == null &&         // no method is found yet
41b07c1952f4 8032400: JSR292: invokeSpecial: InternalError attempting to lookup a method
vlivanov
parents: 24572
diff changeset
  3048
                         refc != refcAsSuper); // search up to refc
16898
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3049
                if (m2 == null)  throw new InternalError(method.toString());
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3050
                method = m2;
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3051
                refc = refcAsSuper;
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3052
                // redo basic checks
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3053
                checkMethod(refKind, refc, method);
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3054
            }
b321dfd1163f 8008140: Better method handle resolution
vlivanov
parents: 16123
diff changeset
  3055
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3056
            DirectMethodHandle dmh = DirectMethodHandle.make(refKind, refc, method, lookupClass());
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  3057
            MethodHandle mh = dmh;
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3058
            // Optionally narrow the receiver argument to lookupClass using restrictReceiver.
44859
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  3059
            if ((doRestrict && refKind == REF_invokeSpecial) ||
5775b131af51 8177146: MethodHandles.Lookup::bind allows illegal protected access
psandoz
parents: 44844
diff changeset
  3060
                    (MethodHandleNatives.refKindHasReceiver(refKind) && restrictProtectedReceiver(method))) {
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  3061
                mh = restrictReceiver(method, dmh, lookupClass());
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  3062
            }
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3063
            mh = maybeBindCaller(method, mh, boundCallerClass);
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  3064
            mh = mh.setVarargs(method);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3065
            return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3066
        }
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  3067
        private MethodHandle maybeBindCaller(MemberName method, MethodHandle mh,
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3068
                                             Class<?> boundCallerClass)
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14222
diff changeset
  3069
                                             throws IllegalAccessException {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  3070
            if (allowedModes == TRUSTED || !MethodHandleNatives.isCallerSensitive(method))
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  3071
                return mh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  3072
            Class<?> hostClass = lookupClass;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3073
            if (!hasPrivateAccess())  // caller must have private access
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49790
diff changeset
  3074
                hostClass = boundCallerClass;  // boundCallerClass came from a security manager style stack walk
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  3075
            MethodHandle cbmh = MethodHandleImpl.bindCaller(mh, hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  3076
            // Note: caller will apply varargs after this step happens.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  3077
            return cbmh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  3078
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3079
        /** 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
  3080
        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
  3081
            final boolean checkSecurity = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3082
            return getDirectFieldCommon(refKind, refc, field, checkSecurity);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3083
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3084
        /** 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
  3085
        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
  3086
            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
  3087
            return getDirectFieldCommon(refKind, refc, field, checkSecurity);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3088
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3089
        /** 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
  3090
        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
  3091
                                                  boolean checkSecurity) throws IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3092
            checkField(refKind, refc, field);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3093
            // 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
  3094
            if (checkSecurity)
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3095
                checkSecurityManager(refc, field);
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  3096
            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
  3097
            boolean doRestrict = (MethodHandleNatives.refKindHasReceiver(refKind) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3098
                                    restrictProtectedReceiver(field));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3099
            if (doRestrict)
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  3100
                return restrictReceiver(field, dmh, lookupClass());
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
  3101
            return dmh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3102
        }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3103
        private VarHandle getFieldVarHandle(byte getRefKind, byte putRefKind,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3104
                                            Class<?> refc, MemberName getField, MemberName putField)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3105
                throws IllegalAccessException {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3106
            final boolean checkSecurity = true;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3107
            return getFieldVarHandleCommon(getRefKind, putRefKind, refc, getField, putField, checkSecurity);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3108
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3109
        private VarHandle getFieldVarHandleNoSecurityManager(byte getRefKind, byte putRefKind,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3110
                                                             Class<?> refc, MemberName getField, MemberName putField)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3111
                throws IllegalAccessException {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3112
            final boolean checkSecurity = false;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3113
            return getFieldVarHandleCommon(getRefKind, putRefKind, refc, getField, putField, checkSecurity);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3114
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3115
        private VarHandle getFieldVarHandleCommon(byte getRefKind, byte putRefKind,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3116
                                                  Class<?> refc, MemberName getField, MemberName putField,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3117
                                                  boolean checkSecurity) throws IllegalAccessException {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3118
            assert getField.isStatic() == putField.isStatic();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3119
            assert getField.isGetter() && putField.isSetter();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3120
            assert MethodHandleNatives.refKindIsStatic(getRefKind) == MethodHandleNatives.refKindIsStatic(putRefKind);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3121
            assert MethodHandleNatives.refKindIsGetter(getRefKind) && MethodHandleNatives.refKindIsSetter(putRefKind);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3122
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3123
            checkField(getRefKind, refc, getField);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3124
            if (checkSecurity)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3125
                checkSecurityManager(refc, getField);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3126
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3127
            if (!putField.isFinal()) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3128
                // A VarHandle does not support updates to final fields, any
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3129
                // such VarHandle to a final field will be read-only and
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3130
                // therefore the following write-based accessibility checks are
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3131
                // only required for non-final fields
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3132
                checkField(putRefKind, refc, putField);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3133
                if (checkSecurity)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3134
                    checkSecurityManager(refc, putField);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3135
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3136
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3137
            boolean doRestrict = (MethodHandleNatives.refKindHasReceiver(getRefKind) &&
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3138
                                  restrictProtectedReceiver(getField));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3139
            if (doRestrict) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3140
                assert !getField.isStatic();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3141
                // receiver type of VarHandle is too wide; narrow to caller
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3142
                if (!getField.getDeclaringClass().isAssignableFrom(lookupClass())) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3143
                    throw getField.makeAccessException("caller class must be a subclass below the method", lookupClass());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3144
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3145
                refc = lookupClass();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3146
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3147
            return VarHandles.makeFieldHandle(getField, refc, getField.getFieldType(), this.allowedModes == TRUSTED);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3148
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3149
        /** 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
  3150
        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
  3151
            final boolean checkSecurity = true;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3152
            return getDirectConstructorCommon(refc, ctor, checkSecurity);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3153
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3154
        /** 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
  3155
        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
  3156
            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
  3157
            return getDirectConstructorCommon(refc, ctor, checkSecurity);
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3158
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3159
        /** 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
  3160
        private MethodHandle getDirectConstructorCommon(Class<?> refc, MemberName ctor,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3161
                                                  boolean checkSecurity) throws IllegalAccessException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3162
            assert(ctor.isConstructor());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3163
            checkAccess(REF_newInvokeSpecial, refc, ctor);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3164
            // 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
  3165
            if (checkSecurity)
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3166
                checkSecurityManager(refc, ctor);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  3167
            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
  3168
            return DirectMethodHandle.make(ctor).setVarargs(ctor);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  3169
        }
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  3170
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  3171
        /** 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
  3172
         */
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  3173
        /*non-public*/
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3174
        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
  3175
            if (!(type instanceof Class || type instanceof MethodType))
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3176
                throw new InternalError("unresolved MemberName");
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3177
            MemberName member = new MemberName(refKind, defc, name, type);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3178
            MethodHandle mh = LOOKASIDE_TABLE.get(member);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3179
            if (mh != null) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3180
                checkSymbolicClass(defc);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3181
                return mh;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3182
            }
21362
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  3183
            if (defc == MethodHandle.class && refKind == REF_invokeVirtual) {
50771
9ca95539747d 8195650: Method references to VarHandle accessors
psandoz
parents: 50735
diff changeset
  3184
                // Treat MethodHandle.invoke and invokeExact specially.
21362
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  3185
                mh = findVirtualForMH(member.getName(), member.getMethodType());
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  3186
                if (mh != null) {
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  3187
                    return mh;
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  3188
                }
50771
9ca95539747d 8195650: Method references to VarHandle accessors
psandoz
parents: 50735
diff changeset
  3189
            } else if (defc == VarHandle.class && refKind == REF_invokeVirtual) {
9ca95539747d 8195650: Method references to VarHandle accessors
psandoz
parents: 50735
diff changeset
  3190
                // Treat signature-polymorphic methods on VarHandle specially.
9ca95539747d 8195650: Method references to VarHandle accessors
psandoz
parents: 50735
diff changeset
  3191
                mh = findVirtualForVH(member.getName(), member.getMethodType());
9ca95539747d 8195650: Method references to VarHandle accessors
psandoz
parents: 50735
diff changeset
  3192
                if (mh != null) {
9ca95539747d 8195650: Method references to VarHandle accessors
psandoz
parents: 50735
diff changeset
  3193
                    return mh;
9ca95539747d 8195650: Method references to VarHandle accessors
psandoz
parents: 50735
diff changeset
  3194
                }
21362
8b2d68e8632a 8026502: java/lang/invoke/MethodHandleConstants.java fails on all platforms
twisti
parents: 20877
diff changeset
  3195
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3196
            MemberName resolved = resolveOrFail(refKind, member);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3197
            mh = getDirectMethodForConstant(refKind, defc, resolved);
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3198
            if (mh instanceof DirectMethodHandle
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3199
                    && canBeCached(refKind, defc, resolved)) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3200
                MemberName key = mh.internalMemberName();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3201
                if (key != null) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3202
                    key = key.asNormalOriginal();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3203
                }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3204
                if (member.equals(key)) {  // better safe than sorry
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3205
                    LOOKASIDE_TABLE.put(key, (DirectMethodHandle) mh);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3206
                }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3207
            }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3208
            return mh;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3209
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3210
        private
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3211
        boolean canBeCached(byte refKind, Class<?> defc, MemberName member) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3212
            if (refKind == REF_invokeSpecial) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3213
                return false;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3214
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3215
            if (!Modifier.isPublic(defc.getModifiers()) ||
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3216
                    !Modifier.isPublic(member.getDeclaringClass().getModifiers()) ||
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3217
                    !member.isPublic() ||
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3218
                    member.isCallerSensitive()) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3219
                return false;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3220
            }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3221
            ClassLoader loader = defc.getClassLoader();
46047
97d615d81827 8161121: VM::isSystemDomainLoader should consider platform class loader
mchung
parents: 45881
diff changeset
  3222
            if (loader != null) {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3223
                ClassLoader sysl = ClassLoader.getSystemClassLoader();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3224
                boolean found = false;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3225
                while (sysl != null) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3226
                    if (loader == sysl) { found = true; break; }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3227
                    sysl = sysl.getParent();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3228
                }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3229
                if (!found) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3230
                    return false;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3231
                }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3232
            }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3233
            try {
49432
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  3234
                MemberName resolved2 = publicLookup().resolveOrNull(refKind,
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3235
                    new MemberName(refKind, defc, member.getName(), member.getType()));
49432
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  3236
                if (resolved2 == null) {
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  3237
                    return false;
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  3238
                }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3239
                checkSecurityManager(defc, resolved2);
49432
f76e1ac74f28 8200289: Reduce number of exceptions created when calling Lookup::canBeCached
redestad
parents: 48826
diff changeset
  3240
            } catch (SecurityException ex) {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3241
                return false;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3242
            }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3243
            return true;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3244
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3245
        private
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3246
        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
  3247
                throws ReflectiveOperationException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3248
            if (MethodHandleNatives.refKindIsField(refKind)) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3249
                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
  3250
            } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3251
                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
  3252
            } else if (refKind == REF_newInvokeSpecial) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20533
diff changeset
  3253
                return getDirectConstructorNoSecurityManager(defc, member);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  3254
            }
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  3255
            // oops
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3256
            throw newIllegalArgumentException("bad MethodHandle constant #"+member);
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
  3257
        }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3258
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
  3259
        static ConcurrentHashMap<MemberName, DirectMethodHandle> LOOKASIDE_TABLE = new ConcurrentHashMap<>();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3260
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3261
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3262
    /**
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3263
     * Produces a method handle constructing arrays of a desired type,
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3264
     * as if by the {@code anewarray} bytecode.
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3265
     * The return type of the method handle will be the array type.
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3266
     * The type of its sole argument will be {@code int}, which specifies the size of the array.
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3267
     *
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3268
     * <p> If the returned method handle is invoked with a negative
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3269
     * array size, a {@code NegativeArraySizeException} will be thrown.
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3270
     *
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3271
     * @param arrayClass an array type
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3272
     * @return a method handle which can create arrays of the given type
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3273
     * @throws NullPointerException if the argument is {@code null}
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3274
     * @throws IllegalArgumentException if {@code arrayClass} is not an array type
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3275
     * @see java.lang.reflect.Array#newInstance(Class, int)
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3276
     * @jvms 6.5 {@code anewarray} Instruction
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3277
     * @since 9
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3278
     */
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3279
    public static
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3280
    MethodHandle arrayConstructor(Class<?> arrayClass) throws IllegalArgumentException {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3281
        if (!arrayClass.isArray()) {
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3282
            throw newIllegalArgumentException("not an array class: " + arrayClass.getName());
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3283
        }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3284
        MethodHandle ani = MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_Array_newInstance).
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3285
                bindTo(arrayClass.getComponentType());
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3286
        return ani.asType(ani.type().changeReturnType(arrayClass));
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3287
    }
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3288
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37669
diff changeset
  3289
    /**
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3290
     * Produces a method handle returning the length of an array,
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3291
     * as if by the {@code arraylength} bytecode.
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3292
     * The type of the method handle will have {@code int} as return type,
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3293
     * and its sole argument will be the array type.
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3294
     *
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3295
     * <p> If the returned method handle is invoked with a {@code null}
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3296
     * array reference, a {@code NullPointerException} will be thrown.
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3297
     *
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3298
     * @param arrayClass an array type
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3299
     * @return a method handle which can retrieve the length of an array of the given array type
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3300
     * @throws NullPointerException if the argument is {@code null}
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3301
     * @throws IllegalArgumentException if arrayClass is not an array type
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3302
     * @jvms 6.5 {@code arraylength} Instruction
38472
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38376
diff changeset
  3303
     * @since 9
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3304
     */
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3305
    public static
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3306
    MethodHandle arrayLength(Class<?> arrayClass) throws IllegalArgumentException {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3307
        return MethodHandleImpl.makeArrayElementAccessor(arrayClass, MethodHandleImpl.ArrayAccess.LENGTH);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3308
    }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3309
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3310
    /**
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3311
     * Produces a method handle giving read access to elements of an array,
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3312
     * as if by the {@code aaload} bytecode.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3313
     * 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
  3314
     * 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
  3315
     * and the second will be {@code int}.
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3316
     *
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3317
     * <p> When the returned method handle is invoked,
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3318
     * the array reference and array index are checked.
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3319
     * A {@code NullPointerException} will be thrown if the array reference
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3320
     * is {@code null} and an {@code ArrayIndexOutOfBoundsException} will be
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3321
     * thrown if the index is negative or if it is greater than or equal to
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3322
     * the length of the array.
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3323
     *
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3324
     * @param arrayClass an array type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3325
     * @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
  3326
     * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3327
     * @throws  IllegalArgumentException if arrayClass is not an array type
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3328
     * @jvms 6.5 {@code aaload} Instruction
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3329
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3330
    public static
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3331
    MethodHandle arrayElementGetter(Class<?> arrayClass) throws IllegalArgumentException {
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3332
        return MethodHandleImpl.makeArrayElementAccessor(arrayClass, MethodHandleImpl.ArrayAccess.GET);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3333
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3334
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3335
    /**
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3336
     * Produces a method handle giving write access to elements of an array,
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3337
     * as if by the {@code astore} bytecode.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3338
     * 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
  3339
     * 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
  3340
     * The first and second arguments will be the array type and int.
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3341
     *
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3342
     * <p> When the returned method handle is invoked,
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3343
     * the array reference and array index are checked.
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3344
     * A {@code NullPointerException} will be thrown if the array reference
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3345
     * is {@code null} and an {@code ArrayIndexOutOfBoundsException} will be
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3346
     * thrown if the index is negative or if it is greater than or equal to
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3347
     * the length of the array.
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3348
     *
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  3349
     * @param arrayClass the class of an array
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3350
     * @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
  3351
     * @throws NullPointerException if the argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3352
     * @throws IllegalArgumentException if arrayClass is not an array type
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3353
     * @jvms 6.5 {@code aastore} Instruction
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3354
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3355
    public static
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3356
    MethodHandle arrayElementSetter(Class<?> arrayClass) throws IllegalArgumentException {
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  3357
        return MethodHandleImpl.makeArrayElementAccessor(arrayClass, MethodHandleImpl.ArrayAccess.SET);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3358
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3359
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3360
    /**
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3361
     * Produces a VarHandle giving access to elements of an array of type
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3362
     * {@code arrayClass}.  The VarHandle's variable type is the component type
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3363
     * of {@code arrayClass} and the list of coordinate types is
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3364
     * {@code (arrayClass, int)}, where the {@code int} coordinate type
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3365
     * corresponds to an argument that is an index into an array.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3366
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3367
     * Certain access modes of the returned VarHandle are unsupported under
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3368
     * the following conditions:
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3369
     * <ul>
39470
d6f8b4a85fb0 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38774
diff changeset
  3370
     * <li>if the component type is anything other than {@code byte},
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3371
     *     {@code short}, {@code char}, {@code int}, {@code long},
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3372
     *     {@code float}, or {@code double} then numeric atomic update access
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3373
     *     modes are unsupported.
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3374
     * <li>if the field type is anything other than {@code boolean},
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3375
     *     {@code byte}, {@code short}, {@code char}, {@code int} or
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3376
     *     {@code long} then bitwise atomic update access modes are
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3377
     *     unsupported.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3378
     * </ul>
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3379
     * <p>
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3380
     * If the component type is {@code float} or {@code double} then numeric
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3381
     * and atomic update access modes compare values using their bitwise
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3382
     * representation (see {@link Float#floatToRawIntBits} and
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3383
     * {@link Double#doubleToRawLongBits}, respectively).
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3384
     *
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3385
     * <p> When the returned {@code VarHandle} is invoked,
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3386
     * the array reference and array index are checked.
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3387
     * A {@code NullPointerException} will be thrown if the array reference
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3388
     * is {@code null} and an {@code ArrayIndexOutOfBoundsException} will be
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3389
     * thrown if the index is negative or if it is greater than or equal to
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3390
     * the length of the array.
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47216
diff changeset
  3391
     *
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3392
     * @apiNote
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3393
     * Bitwise comparison of {@code float} values or {@code double} values,
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3394
     * as performed by the numeric and atomic update access modes, differ
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3395
     * from the primitive {@code ==} operator and the {@link Float#equals}
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3396
     * and {@link Double#equals} methods, specifically with respect to
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3397
     * comparing NaN values or comparing {@code -0.0} with {@code +0.0}.
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3398
     * Care should be taken when performing a compare and set or a compare
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3399
     * and exchange operation with such values since the operation may
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3400
     * unexpectedly fail.
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3401
     * There are many possible NaN values that are considered to be
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3402
     * {@code NaN} in Java, although no IEEE 754 floating-point operation
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3403
     * provided by Java can distinguish between them.  Operation failure can
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3404
     * occur if the expected or witness value is a NaN value and it is
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3405
     * transformed (perhaps in a platform specific manner) into another NaN
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3406
     * value, and thus has a different bitwise representation (see
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3407
     * {@link Float#intBitsToFloat} or {@link Double#longBitsToDouble} for more
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3408
     * details).
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3409
     * The values {@code -0.0} and {@code +0.0} have different bitwise
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3410
     * representations but are considered equal when using the primitive
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3411
     * {@code ==} operator.  Operation failure can occur if, for example, a
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3412
     * numeric algorithm computes an expected value to be say {@code -0.0}
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3413
     * and previously computed the witness value to be say {@code +0.0}.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3414
     * @param arrayClass the class of an array, of type {@code T[]}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3415
     * @return a VarHandle giving access to elements of an array
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3416
     * @throws NullPointerException if the arrayClass is null
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3417
     * @throws IllegalArgumentException if arrayClass is not an array type
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3418
     * @since 9
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3419
     */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3420
    public static
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3421
    VarHandle arrayElementVarHandle(Class<?> arrayClass) throws IllegalArgumentException {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3422
        return VarHandles.makeArrayElementHandle(arrayClass);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3423
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3424
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3425
    /**
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3426
     * Produces a VarHandle giving access to elements of a {@code byte[]} array
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3427
     * viewed as if it were a different primitive array type, such as
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3428
     * {@code int[]} or {@code long[]}.
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3429
     * The VarHandle's variable type is the component type of
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3430
     * {@code viewArrayClass} and the list of coordinate types is
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3431
     * {@code (byte[], int)}, where the {@code int} coordinate type
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3432
     * corresponds to an argument that is an index into a {@code byte[]} array.
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3433
     * The returned VarHandle accesses bytes at an index in a {@code byte[]}
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3434
     * array, composing bytes to or from a value of the component type of
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3435
     * {@code viewArrayClass} according to the given endianness.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3436
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3437
     * The supported component types (variables types) are {@code short},
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3438
     * {@code char}, {@code int}, {@code long}, {@code float} and
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3439
     * {@code double}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3440
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3441
     * Access of bytes at a given index will result in an
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3442
     * {@code IndexOutOfBoundsException} if the index is less than {@code 0}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3443
     * or greater than the {@code byte[]} array length minus the size (in bytes)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3444
     * of {@code T}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3445
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3446
     * Access of bytes at an index may be aligned or misaligned for {@code T},
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3447
     * with respect to the underlying memory address, {@code A} say, associated
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3448
     * with the array and index.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3449
     * If access is misaligned then access for anything other than the
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3450
     * {@code get} and {@code set} access modes will result in an
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3451
     * {@code IllegalStateException}.  In such cases atomic access is only
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3452
     * guaranteed with respect to the largest power of two that divides the GCD
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3453
     * of {@code A} and the size (in bytes) of {@code T}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3454
     * If access is aligned then following access modes are supported and are
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3455
     * guaranteed to support atomic access:
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3456
     * <ul>
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3457
     * <li>read write access modes for all {@code T}, with the exception of
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3458
     *     access modes {@code get} and {@code set} for {@code long} and
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3459
     *     {@code double} on 32-bit platforms.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3460
     * <li>atomic update access modes for {@code int}, {@code long},
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3461
     *     {@code float} or {@code double}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3462
     *     (Future major platform releases of the JDK may support additional
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3463
     *     types for certain currently unsupported access modes.)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3464
     * <li>numeric atomic update access modes for {@code int} and {@code long}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3465
     *     (Future major platform releases of the JDK may support additional
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3466
     *     numeric types for certain currently unsupported access modes.)
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3467
     * <li>bitwise atomic update access modes for {@code int} and {@code long}.
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3468
     *     (Future major platform releases of the JDK may support additional
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3469
     *     numeric types for certain currently unsupported access modes.)
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3470
     * </ul>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3471
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3472
     * Misaligned access, and therefore atomicity guarantees, may be determined
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3473
     * for {@code byte[]} arrays without operating on a specific array.  Given
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3474
     * an {@code index}, {@code T} and it's corresponding boxed type,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3475
     * {@code T_BOX}, misalignment may be determined as follows:
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3476
     * <pre>{@code
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3477
     * int sizeOfT = T_BOX.BYTES;  // size in bytes of T
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3478
     * int misalignedAtZeroIndex = ByteBuffer.wrap(new byte[0]).
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3479
     *     alignmentOffset(0, sizeOfT);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3480
     * int misalignedAtIndex = (misalignedAtZeroIndex + index) % sizeOfT;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3481
     * boolean isMisaligned = misalignedAtIndex != 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3482
     * }</pre>
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3483
     * <p>
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3484
     * If the variable type is {@code float} or {@code double} then atomic
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3485
     * update access modes compare values using their bitwise representation
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3486
     * (see {@link Float#floatToRawIntBits} and
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3487
     * {@link Double#doubleToRawLongBits}, respectively).
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3488
     * @param viewArrayClass the view array class, with a component type of
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3489
     * type {@code T}
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3490
     * @param byteOrder the endianness of the view array elements, as
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3491
     * stored in the underlying {@code byte} array
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3492
     * @return a VarHandle giving access to elements of a {@code byte[]} array
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3493
     * viewed as if elements corresponding to the components type of the view
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3494
     * array class
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3495
     * @throws NullPointerException if viewArrayClass or byteOrder is null
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3496
     * @throws IllegalArgumentException if viewArrayClass is not an array type
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3497
     * @throws UnsupportedOperationException if the component type of
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3498
     * viewArrayClass is not supported as a variable type
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3499
     * @since 9
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3500
     */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3501
    public static
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3502
    VarHandle byteArrayViewVarHandle(Class<?> viewArrayClass,
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3503
                                     ByteOrder byteOrder) throws IllegalArgumentException {
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3504
        Objects.requireNonNull(byteOrder);
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3505
        return VarHandles.byteArrayViewHandle(viewArrayClass,
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3506
                                              byteOrder == ByteOrder.BIG_ENDIAN);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3507
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3508
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3509
    /**
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3510
     * Produces a VarHandle giving access to elements of a {@code ByteBuffer}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3511
     * viewed as if it were an array of elements of a different primitive
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3512
     * component type to that of {@code byte}, such as {@code int[]} or
44860
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3513
     * {@code long[]}.
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3514
     * The VarHandle's variable type is the component type of
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3515
     * {@code viewArrayClass} and the list of coordinate types is
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3516
     * {@code (ByteBuffer, int)}, where the {@code int} coordinate type
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3517
     * corresponds to an argument that is an index into a {@code byte[]} array.
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3518
     * The returned VarHandle accesses bytes at an index in a
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3519
     * {@code ByteBuffer}, composing bytes to or from a value of the component
a33107d3a388 8167229: Improve VarHandle documentation
psandoz
parents: 44859
diff changeset
  3520
     * type of {@code viewArrayClass} according to the given endianness.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3521
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3522
     * The supported component types (variables types) are {@code short},
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3523
     * {@code char}, {@code int}, {@code long}, {@code float} and
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3524
     * {@code double}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3525
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3526
     * Access will result in a {@code ReadOnlyBufferException} for anything
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3527
     * other than the read access modes if the {@code ByteBuffer} is read-only.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3528
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3529
     * Access of bytes at a given index will result in an
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3530
     * {@code IndexOutOfBoundsException} if the index is less than {@code 0}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3531
     * or greater than the {@code ByteBuffer} limit minus the size (in bytes) of
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3532
     * {@code T}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3533
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3534
     * Access of bytes at an index may be aligned or misaligned for {@code T},
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3535
     * with respect to the underlying memory address, {@code A} say, associated
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3536
     * with the {@code ByteBuffer} and index.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3537
     * If access is misaligned then access for anything other than the
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3538
     * {@code get} and {@code set} access modes will result in an
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3539
     * {@code IllegalStateException}.  In such cases atomic access is only
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3540
     * guaranteed with respect to the largest power of two that divides the GCD
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3541
     * of {@code A} and the size (in bytes) of {@code T}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3542
     * If access is aligned then following access modes are supported and are
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3543
     * guaranteed to support atomic access:
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3544
     * <ul>
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3545
     * <li>read write access modes for all {@code T}, with the exception of
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3546
     *     access modes {@code get} and {@code set} for {@code long} and
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3547
     *     {@code double} on 32-bit platforms.
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3548
     * <li>atomic update access modes for {@code int}, {@code long},
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3549
     *     {@code float} or {@code double}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3550
     *     (Future major platform releases of the JDK may support additional
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3551
     *     types for certain currently unsupported access modes.)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3552
     * <li>numeric atomic update access modes for {@code int} and {@code long}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3553
     *     (Future major platform releases of the JDK may support additional
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3554
     *     numeric types for certain currently unsupported access modes.)
40732
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3555
     * <li>bitwise atomic update access modes for {@code int} and {@code long}.
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3556
     *     (Future major platform releases of the JDK may support additional
2fd9cf42bb3c 8161444: VarHandles should provide access bitwise atomics
psandoz
parents: 40418
diff changeset
  3557
     *     numeric types for certain currently unsupported access modes.)
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3558
     * </ul>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3559
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3560
     * Misaligned access, and therefore atomicity guarantees, may be determined
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3561
     * for a {@code ByteBuffer}, {@code bb} (direct or otherwise), an
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3562
     * {@code index}, {@code T} and it's corresponding boxed type,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3563
     * {@code T_BOX}, as follows:
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3564
     * <pre>{@code
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3565
     * int sizeOfT = T_BOX.BYTES;  // size in bytes of T
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3566
     * ByteBuffer bb = ...
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3567
     * int misalignedAtIndex = bb.alignmentOffset(index, sizeOfT);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3568
     * boolean isMisaligned = misalignedAtIndex != 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3569
     * }</pre>
39471
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3570
     * <p>
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3571
     * If the variable type is {@code float} or {@code double} then atomic
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3572
     * update access modes compare values using their bitwise representation
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3573
     * (see {@link Float#floatToRawIntBits} and
6622892a347a 8158039: VarHandle float/double field/array access should support CAS/set/add atomics
psandoz
parents: 39470
diff changeset
  3574
     * {@link Double#doubleToRawLongBits}, respectively).
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3575
     * @param viewArrayClass the view array class, with a component type of
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3576
     * type {@code T}
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3577
     * @param byteOrder the endianness of the view array elements, as
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3578
     * stored in the underlying {@code ByteBuffer} (Note this overrides the
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3579
     * endianness of a {@code ByteBuffer})
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3580
     * @return a VarHandle giving access to elements of a {@code ByteBuffer}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3581
     * viewed as if elements corresponding to the components type of the view
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3582
     * array class
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3583
     * @throws NullPointerException if viewArrayClass or byteOrder is null
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3584
     * @throws IllegalArgumentException if viewArrayClass is not an array type
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3585
     * @throws UnsupportedOperationException if the component type of
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3586
     * viewArrayClass is not supported as a variable type
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3587
     * @since 9
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3588
     */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3589
    public static
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3590
    VarHandle byteBufferViewVarHandle(Class<?> viewArrayClass,
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3591
                                      ByteOrder byteOrder) throws IllegalArgumentException {
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3592
        Objects.requireNonNull(byteOrder);
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3593
        return VarHandles.makeByteBufferViewHandle(viewArrayClass,
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37604
diff changeset
  3594
                                                   byteOrder == ByteOrder.BIG_ENDIAN);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3595
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3596
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3597
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3598
    /// method handle invocation (reflective style)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3599
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3600
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3601
     * 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
  3602
     * 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
  3603
     * a single trailing {@code Object[]} array.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3604
     * 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
  3605
     * arguments:
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3606
     * <ul>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3607
     * <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
  3608
     * <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
  3609
     * <li>an {@code Object[]} array containing trailing arguments
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3610
     * </ul>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3611
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3612
     * 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
  3613
     * the indicated {@code type}.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3614
     * 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
  3615
     * 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
  3616
     * 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
  3617
     * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3618
     * 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
  3619
     * 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
  3620
     * 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
  3621
     * the final parameter.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3622
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3623
     * 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
  3624
     * 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
  3625
     * 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
  3626
     * 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
  3627
     * an {@link IllegalArgumentException} instead of invoking the target.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3628
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3629
     * 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
  3630
     * <blockquote><pre>{@code
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  3631
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
  3632
int spreadArgCount = type.parameterCount() - leadingArgCount;
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  3633
invoker = invoker.asSpreader(Object[].class, spreadArgCount);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
  3634
return invoker;
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3635
     * }</pre></blockquote>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3636
     * This method throws no reflective or security exceptions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3637
     * @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
  3638
     * @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
  3639
     * @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
  3640
     * @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
  3641
     * @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
  3642
     *                  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
  3643
     *                  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
  3644
     *          <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
  3645
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
  3646
    public static
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3647
    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
  3648
        if (leadingArgCount < 0 || leadingArgCount > type.parameterCount())
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
  3649
            throw newIllegalArgumentException("bad argument count", leadingArgCount);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  3650
        type = type.asSpreaderType(Object[].class, leadingArgCount, type.parameterCount() - leadingArgCount);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3651
        return type.invokers().spreadInvoker(leadingArgCount);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3652
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3653
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3654
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3655
     * 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
  3656
     * 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
  3657
     * 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
  3658
     * 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
  3659
     * an additional leading argument of type {@code MethodHandle}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3660
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  3661
     * 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
  3662
     * {@code publicLookup().findVirtual(MethodHandle.class, "invokeExact", type)}
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3663
     *
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3664
     * <p style="font-size:smaller;">
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3665
     * <em>Discussion:</em>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3666
     * 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
  3667
     * of unknown types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3668
     * 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
  3669
     * handle {@code M}, extract its type {@code T},
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3670
     * 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
  3671
     * 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
  3672
     * (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
  3673
     * is unknown.)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3674
     * If spreading, collecting, or other argument transformations are required,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3675
     * 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
  3676
     * 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
  3677
     * <p style="font-size:smaller;">
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3678
     * <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
  3679
     * 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
  3680
     * 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
  3681
     * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3682
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3683
     * This method throws no reflective or security exceptions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3684
     * @param type the desired target type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3685
     * @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
  3686
     * @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
  3687
     *          <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
  3688
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
  3689
    public static
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3690
    MethodHandle exactInvoker(MethodType type) {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  3691
        return type.invokers().exactInvoker();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3692
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3693
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3694
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3695
     * 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
  3696
     * 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
  3697
     * The resulting invoker will have a type which is
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3698
     * 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
  3699
     * an additional leading argument of type {@code MethodHandle}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3700
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3701
     * 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
  3702
     * the invoker will apply reference casts as
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  3703
     * 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
  3704
     * 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
  3705
     * 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
  3706
     * 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
  3707
     * <p>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3708
     * 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
  3709
     * {@code publicLookup().findVirtual(MethodHandle.class, "invoke", type)}
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3710
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3711
     * <em>Discussion:</em>
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3712
     * 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
  3713
     * 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
  3714
     * 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
  3715
     * of the same arity as the general type.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3716
     * <p style="font-size:smaller;">
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  3717
     * <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
  3718
     * 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
  3719
     * 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
  3720
     * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3721
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3722
     * This method throws no reflective or security exceptions.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3723
     * @param type the desired target type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3724
     * @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
  3725
     * @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
  3726
     *          <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
  3727
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
  3728
    public static
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  3729
    MethodHandle invoker(MethodType type) {
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
  3730
        return type.invokers().genericInvoker();
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  3731
    }
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
  3732
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3733
    /**
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3734
     * Produces a special <em>invoker method handle</em> which can be used to
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3735
     * invoke a signature-polymorphic access mode method on any VarHandle whose
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3736
     * associated access mode type is compatible with the given type.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3737
     * The resulting invoker will have a type which is exactly equal to the
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3738
     * desired given type, except that it will accept an additional leading
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3739
     * argument of type {@code VarHandle}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3740
     *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3741
     * @param accessMode the VarHandle access mode
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3742
     * @param type the desired target type
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3743
     * @return a method handle suitable for invoking an access mode method of
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3744
     *         any VarHandle whose access mode type is of the given type.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3745
     * @since 9
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3746
     */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3747
    static public
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3748
    MethodHandle varHandleExactInvoker(VarHandle.AccessMode accessMode, MethodType type) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3749
        return type.invokers().varHandleMethodExactInvoker(accessMode);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3750
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3751
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3752
    /**
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3753
     * Produces a special <em>invoker method handle</em> which can be used to
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3754
     * invoke a signature-polymorphic access mode method on any VarHandle whose
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3755
     * associated access mode type is compatible with the given type.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3756
     * The resulting invoker will have a type which is exactly equal to the
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3757
     * desired given type, except that it will accept an additional leading
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3758
     * argument of type {@code VarHandle}.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3759
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3760
     * Before invoking its target, if the access mode type differs from the
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3761
     * desired given type, the invoker will apply reference casts as necessary
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3762
     * and box, unbox, or widen primitive values, as if by
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3763
     * {@link MethodHandle#asType asType}.  Similarly, the return value will be
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3764
     * converted as necessary.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3765
     * <p>
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3766
     * This method is equivalent to the following code (though it may be more
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3767
     * efficient): {@code publicLookup().findVirtual(VarHandle.class, accessMode.name(), type)}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3768
     *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3769
     * @param accessMode the VarHandle access mode
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3770
     * @param type the desired target type
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3771
     * @return a method handle suitable for invoking an access mode method of
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3772
     *         any VarHandle whose access mode type is convertible to the given
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3773
     *         type.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3774
     * @since 9
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3775
     */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3776
    static public
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3777
    MethodHandle varHandleInvoker(VarHandle.AccessMode accessMode, MethodType type) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3778
        return type.invokers().varHandleMethodInvoker(accessMode);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3779
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36623
diff changeset
  3780
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3781
    static /*non-public*/
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3782
    MethodHandle basicInvoker(MethodType type) {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
  3783
        return type.invokers().basicInvoker();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3784
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3785
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  3786
     /// method handle modification (creation from other method handles)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3787
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3788
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3789
     * 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
  3790
     * 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
  3791
     * 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
  3792
     * 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
  3793
     * which is equal to the desired new type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3794
     * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3795
     * 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
  3796
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3797
     * 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
  3798
     * 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
  3799
     * 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
  3800
     * 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
  3801
     * <ul>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3802
     * <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
  3803
     *     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
  3804
     *     (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
  3805
     * <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
  3806
     *     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
  3807
     *     (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
  3808
     * <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
  3809
     *     <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
  3810
     *     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
  3811
     * <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
  3812
     *     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
  3813
     *     (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
  3814
     *     widening and/or narrowing.)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3815
     * <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
  3816
     *     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
  3817
     *     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
  3818
     *     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
  3819
     *     the low-order bit.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3820
     * <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
  3821
     *     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
  3822
     * </ul>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3823
     * @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
  3824
     * @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
  3825
     * @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
  3826
     *           any necessary argument conversions, and arranges for any
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3827
     *           necessary return value conversions
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3828
     * @throws NullPointerException if either argument is null
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3829
     * @throws WrongMethodTypeException if the conversion cannot be made
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3830
     * @see MethodHandle#asType
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3831
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3832
    public static
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3833
    MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
26625
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  3834
        explicitCastArgumentsChecks(target, newType);
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  3835
        // use the asTypeCache when possible:
26478
285691ed55e4 8057656: Improve MethodType.isCastableTo() & MethodType.isConvertibleTo() checks
vlivanov
parents: 26477
diff changeset
  3836
        MethodType oldType = target.type();
285691ed55e4 8057656: Improve MethodType.isCastableTo() & MethodType.isConvertibleTo() checks
vlivanov
parents: 26477
diff changeset
  3837
        if (oldType == newType)  return target;
285691ed55e4 8057656: Improve MethodType.isCastableTo() & MethodType.isConvertibleTo() checks
vlivanov
parents: 26477
diff changeset
  3838
        if (oldType.explicitCastEquivalentToAsType(newType)) {
27947
dba3bc0c087e 8066746: MHs.explicitCastArguments does incorrect type checks for VarargsCollector
vlivanov
parents: 27755
diff changeset
  3839
            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
  3840
        }
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26470
diff changeset
  3841
        return MethodHandleImpl.makePairwiseConvert(target, newType, false);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3842
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3843
26625
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  3844
    private static void explicitCastArgumentsChecks(MethodHandle target, MethodType newType) {
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  3845
        if (target.type().parameterCount() != newType.parameterCount()) {
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  3846
            throw new WrongMethodTypeException("cannot explicitly cast " + target + " to " + newType);
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  3847
        }
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  3848
    }
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  3849
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3850
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  3851
     * 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
  3852
     * 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
  3853
     * 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
  3854
     * which is equal to the desired new type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3855
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3856
     * The given array controls the reordering.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3857
     * 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
  3858
     * {@code newType.parameterCount()}, and call {@code #O} the number
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3859
     * of outgoing parameters (the value {@code target.type().parameterCount()}).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3860
     * 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
  3861
     * 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
  3862
     * 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
  3863
     * 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
  3864
     * argument, where {@code I} is {@code reorder[N]}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3865
     * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3866
     * No argument or return value conversions are applied.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3867
     * 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
  3868
     * 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
  3869
     * or parameters in the target method handle.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3870
     * 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
  3871
     * type of the original target.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3872
     * <p>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3873
     * The reordering array need not specify an actual permutation.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3874
     * 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
  3875
     * 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
  3876
     * 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
  3877
     * 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
  3878
     * incoming arguments which are not mentioned in the reordering array
36112
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  3879
     * may be of any type, as determined only by {@code newType}.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16906
diff changeset
  3880
     * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3881
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
  3882
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
  3883
...
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  3884
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
  3885
MethodType intfn2 = methodType(int.class, int.class, int.class);
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  3886
MethodHandle sub = ... (int x, int y) -> (x-y) ...;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3887
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
  3888
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
  3889
MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3890
assert((int)rsub.invokeExact(1, 100) == 99);
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  3891
MethodHandle add = ... (int x, int y) -> (x+y) ...;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3892
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
  3893
MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3894
assert(twice.type().equals(intfn1));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3895
assert((int)twice.invokeExact(21) == 42);
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16906
diff changeset
  3896
     * }</pre></blockquote>
36112
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  3897
     * <p>
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  3898
     * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  3899
     * variable-arity method handle}, even if the original target method handle was.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3900
     * @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
  3901
     * @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
  3902
     * @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
  3903
     * @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
  3904
     *           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
  3905
     * @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
  3906
     * @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
  3907
     *                  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
  3908
     *                  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
  3909
     *                  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
  3910
     *                  {@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
  3911
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  3912
    public static
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  3913
    MethodHandle permuteArguments(MethodHandle target, MethodType newType, int... reorder) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3914
        reorder = reorder.clone();  // get a private copy
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3915
        MethodType oldType = target.type();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3916
        permuteArgumentChecks(reorder, newType, oldType);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3917
        // first detect dropped arguments and handle them separately
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3918
        int[] originalReorder = reorder;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3919
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3920
        LambdaForm form = result.form;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3921
        int newArity = newType.parameterCount();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3922
        // Normalize the reordering into a real permutation,
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3923
        // by removing duplicates and adding dropped elements.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3924
        // This somewhat improves lambda form caching, as well
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3925
        // as simplifying the transform by breaking it up into steps.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3926
        for (int ddIdx; (ddIdx = findFirstDupOrDrop(reorder, newArity)) != 0; ) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3927
            if (ddIdx > 0) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3928
                // We found a duplicated entry at reorder[ddIdx].
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3929
                // Example:  (x,y,z)->asList(x,y,z)
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3930
                // permuted by [1*,0,1] => (a0,a1)=>asList(a1,a0,a1)
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3931
                // permuted by [0,1,0*] => (a0,a1)=>asList(a0,a1,a0)
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3932
                // The starred element corresponds to the argument
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3933
                // deleted by the dupArgumentForm transform.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3934
                int srcPos = ddIdx, dstPos = srcPos, dupVal = reorder[srcPos];
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3935
                boolean killFirst = false;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3936
                for (int val; (val = reorder[--dstPos]) != dupVal; ) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3937
                    // Set killFirst if the dup is larger than an intervening position.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3938
                    // This will remove at least one inversion from the permutation.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3939
                    if (dupVal > val) killFirst = true;
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3940
                }
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3941
                if (!killFirst) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3942
                    srcPos = dstPos;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3943
                    dstPos = ddIdx;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3944
                }
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3945
                form = form.editor().dupArgumentForm(1 + srcPos, 1 + dstPos);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3946
                assert (reorder[srcPos] == reorder[dstPos]);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3947
                oldType = oldType.dropParameterTypes(dstPos, dstPos + 1);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3948
                // contract the reordering by removing the element at dstPos
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3949
                int tailPos = dstPos + 1;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3950
                System.arraycopy(reorder, tailPos, reorder, dstPos, reorder.length - tailPos);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3951
                reorder = Arrays.copyOf(reorder, reorder.length - 1);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3952
            } else {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3953
                int dropVal = ~ddIdx, insPos = 0;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3954
                while (insPos < reorder.length && reorder[insPos] < dropVal) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3955
                    // Find first element of reorder larger than dropVal.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3956
                    // This is where we will insert the dropVal.
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3957
                    insPos += 1;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3958
                }
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3959
                Class<?> ptype = newType.parameterType(dropVal);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3960
                form = form.editor().addArgumentForm(1 + insPos, BasicType.basicType(ptype));
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3961
                oldType = oldType.insertParameterTypes(insPos, ptype);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3962
                // expand the reordering by inserting an element at insPos
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3963
                int tailPos = insPos + 1;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3964
                reorder = Arrays.copyOf(reorder, reorder.length + 1);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3965
                System.arraycopy(reorder, insPos, reorder, tailPos, reorder.length - tailPos);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3966
                reorder[insPos] = dropVal;
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3967
            }
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3968
            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
  3969
        }
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3970
        assert (reorder.length == newArity);  // a perfect permutation
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3971
        // 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
  3972
        form = form.editor().permuteArgumentsForm(1, reorder);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3973
        if (newType == result.type() && form == result.internalForm())
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3974
            return result;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  3975
        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
  3976
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  3977
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3978
    /**
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3979
     * 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
  3980
     * 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
  3981
     * 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
  3982
     * Otherwise, return zero.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3983
     * 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
  3984
     */
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3985
    private static int findFirstDupOrDrop(int[] reorder, int newArity) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3986
        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
  3987
        if (newArity < BIT_LIMIT) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3988
            long mask = 0;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3989
            for (int i = 0; i < reorder.length; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3990
                int arg = reorder[i];
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  3991
                if (arg >= newArity) {
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  3992
                    return reorder.length;
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  3993
                }
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  3994
                long bit = 1L << arg;
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  3995
                if ((mask & bit) != 0) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3996
                    return i;  // >0 indicates a dup
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  3997
                }
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3998
                mask |= bit;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  3999
            }
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4000
            if (mask == (1L << newArity) - 1) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4001
                assert(Long.numberOfTrailingZeros(Long.lowestOneBit(~mask)) == newArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4002
                return 0;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4003
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4004
            // find first zero
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4005
            long zeroBit = Long.lowestOneBit(~mask);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4006
            int zeroPos = Long.numberOfTrailingZeros(zeroBit);
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4007
            assert(zeroPos <= newArity);
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4008
            if (zeroPos == newArity) {
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4009
                return 0;
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4010
            }
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4011
            return ~zeroPos;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4012
        } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4013
            // same algorithm, different bit set
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4014
            BitSet mask = new BitSet(newArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4015
            for (int i = 0; i < reorder.length; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4016
                int arg = reorder[i];
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4017
                if (arg >= newArity) {
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4018
                    return reorder.length;
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4019
                }
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4020
                if (mask.get(arg)) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4021
                    return i;  // >0 indicates a dup
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4022
                }
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4023
                mask.set(arg);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4024
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4025
            int zeroPos = mask.nextClearBit(0);
26626
14bb608ce64e 8058293: Bit set computation in MHs.findFirstDupOrDrop/findFirstDrop is broken
vlivanov
parents: 26625
diff changeset
  4026
            assert(zeroPos <= newArity);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4027
            if (zeroPos == newArity) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4028
                return 0;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4029
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4030
            return ~zeroPos;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4031
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4032
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4033
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4034
    private static boolean permuteArgumentChecks(int[] reorder, MethodType newType, MethodType oldType) {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4035
        if (newType.returnType() != oldType.returnType())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4036
            throw newIllegalArgumentException("return types do not match",
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4037
                    oldType, newType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4038
        if (reorder.length == oldType.parameterCount()) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4039
            int limit = newType.parameterCount();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4040
            boolean bad = false;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4041
            for (int j = 0; j < reorder.length; j++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4042
                int i = reorder[j];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4043
                if (i < 0 || i >= limit) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4044
                    bad = true; break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4045
                }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4046
                Class<?> src = newType.parameterType(i);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4047
                Class<?> dst = oldType.parameterType(j);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4048
                if (src != dst)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4049
                    throw newIllegalArgumentException("parameter types do not match after reorder",
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4050
                            oldType, newType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4051
            }
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4052
            if (!bad)  return true;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4053
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4054
        throw newIllegalArgumentException("bad reorder array: "+Arrays.toString(reorder));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4055
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4056
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4057
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4058
     * 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
  4059
     * constant value every time it is invoked.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4060
     * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4061
     * 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
  4062
     * 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
  4063
     * 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
  4064
     * <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
  4065
     * @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
  4066
     * @param value the value to return
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4067
     * @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
  4068
     * @throws NullPointerException if the {@code type} argument is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4069
     * @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
  4070
     * @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
  4071
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4072
    public static
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4073
    MethodHandle constant(Class<?> type, Object value) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4074
        if (type.isPrimitive()) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4075
            if (type == void.class)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4076
                throw newIllegalArgumentException("void type");
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4077
            Wrapper w = Wrapper.forPrimitiveType(type);
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4078
            value = w.convert(value, type);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4079
            if (w.zero().equals(value))
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4080
                return zero(w, type);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4081
            return insertArguments(identity(type), 0, value);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4082
        } else {
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4083
            if (value == null)
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4084
                return zero(Wrapper.OBJECT, type);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4085
            return identity(type).bindTo(value);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4086
        }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4087
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4088
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4089
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4090
     * 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
  4091
     * @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
  4092
     * @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
  4093
     * @throws NullPointerException if the argument is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4094
     * @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
  4095
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4096
    public static
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4097
    MethodHandle identity(Class<?> type) {
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4098
        Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4099
        int pos = btw.ordinal();
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4100
        MethodHandle ident = IDENTITY_MHS[pos];
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4101
        if (ident == null) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4102
            ident = setCachedMethodHandle(IDENTITY_MHS, pos, makeIdentity(btw.primitiveType()));
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4103
        }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4104
        if (ident.type().returnType() == type)
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4105
            return ident;
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4106
        // something like identity(Foo.class); do not bother to intern these
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4107
        assert (btw == Wrapper.OBJECT);
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4108
        return makeIdentity(type);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4109
    }
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4110
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4111
    /**
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4112
     * Produces a constant method handle of the requested return type which
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4113
     * returns the default value for that type every time it is invoked.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4114
     * The resulting constant method handle will have no side effects.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4115
     * <p>The returned method handle is equivalent to {@code empty(methodType(type))}.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4116
     * It is also equivalent to {@code explicitCastArguments(constant(Object.class, null), methodType(type))},
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4117
     * since {@code explicitCastArguments} converts {@code null} to default values.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4118
     * @param type the expected return type of the desired method handle
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4119
     * @return a constant method handle that takes no arguments
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4120
     *         and returns the default value of the given type (or void, if the type is void)
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4121
     * @throws NullPointerException if the argument is null
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4122
     * @see MethodHandles#constant
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4123
     * @see MethodHandles#empty
38774
b2ca0c2ec115 8156868: MethodHandles.zero(Class) doc issues
mhaupt
parents: 38472
diff changeset
  4124
     * @see MethodHandles#explicitCastArguments
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4125
     * @since 9
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4126
     */
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4127
    public static MethodHandle zero(Class<?> type) {
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4128
        Objects.requireNonNull(type);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4129
        return type.isPrimitive() ?  zero(Wrapper.forPrimitiveType(type), type) : zero(Wrapper.OBJECT, type);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4130
    }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4131
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4132
    private static MethodHandle identityOrVoid(Class<?> type) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4133
        return type == void.class ? zero(type) : identity(type);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4134
    }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4135
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4136
    /**
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4137
     * Produces a method handle of the requested type which ignores any arguments, does nothing,
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4138
     * and returns a suitable default depending on the return type.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4139
     * That is, it returns a zero primitive value, a {@code null}, or {@code void}.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4140
     * <p>The returned method handle is equivalent to
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4141
     * {@code dropArguments(zero(type.returnType()), 0, type.parameterList())}.
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  4142
     *
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4143
     * @apiNote Given a predicate and target, a useful "if-then" construct can be produced as
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4144
     * {@code guardWithTest(pred, target, empty(target.type())}.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4145
     * @param type the type of the desired method handle
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4146
     * @return a constant method handle of the given type, which returns a default value of the given return type
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4147
     * @throws NullPointerException if the argument is null
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4148
     * @see MethodHandles#zero
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4149
     * @see MethodHandles#constant
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4150
     * @since 9
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4151
     */
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4152
    public static  MethodHandle empty(MethodType type) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4153
        Objects.requireNonNull(type);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4154
        return dropArguments(zero(type.returnType()), 0, type.parameterList());
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4155
    }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4156
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 40175
diff changeset
  4157
    private static final MethodHandle[] IDENTITY_MHS = new MethodHandle[Wrapper.COUNT];
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4158
    private static MethodHandle makeIdentity(Class<?> ptype) {
37539
fc220bc54b59 8150956: j.l.i.MethodHandles.whileLoop(...) and .iteratedLoop(...) throw unexpected exceptions in the case of 'init' return type is void
mhaupt
parents: 37363
diff changeset
  4159
        MethodType mtype = methodType(ptype, ptype);
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4160
        LambdaForm lform = LambdaForm.identityForm(BasicType.basicType(ptype));
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4161
        return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.IDENTITY);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4162
    }
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4163
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4164
    private static MethodHandle zero(Wrapper btw, Class<?> rtype) {
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4165
        int pos = btw.ordinal();
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4166
        MethodHandle zero = ZERO_MHS[pos];
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4167
        if (zero == null) {
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4168
            zero = setCachedMethodHandle(ZERO_MHS, pos, makeZero(btw.primitiveType()));
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4169
        }
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4170
        if (zero.type().returnType() == rtype)
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4171
            return zero;
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4172
        assert(btw == Wrapper.OBJECT);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4173
        return makeZero(rtype);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4174
    }
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 40175
diff changeset
  4175
    private static final MethodHandle[] ZERO_MHS = new MethodHandle[Wrapper.COUNT];
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4176
    private static MethodHandle makeZero(Class<?> rtype) {
37539
fc220bc54b59 8150956: j.l.i.MethodHandles.whileLoop(...) and .iteratedLoop(...) throw unexpected exceptions in the case of 'init' return type is void
mhaupt
parents: 37363
diff changeset
  4177
        MethodType mtype = methodType(rtype);
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4178
        LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4179
        return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4180
    }
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  4181
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29094
diff changeset
  4182
    private static synchronized MethodHandle setCachedMethodHandle(MethodHandle[] cache, int pos, MethodHandle value) {
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4183
        // Simulate a CAS, to avoid racy duplication of results.
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4184
        MethodHandle prev = cache[pos];
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4185
        if (prev != null) return prev;
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  4186
        return cache[pos] = value;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4187
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4188
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4189
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4190
     * 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
  4191
     * 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
  4192
     * 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
  4193
     * 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
  4194
     * 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
  4195
     * 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
  4196
     * 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
  4197
     * and calls the original target.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4198
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4199
     * 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
  4200
     * 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
  4201
     * 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
  4202
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4203
     * 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
  4204
     * 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
  4205
     * 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
  4206
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4207
     * 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
  4208
     * 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
  4209
     * 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
  4210
     * and <i>L</i> is the length of the values array.
36112
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4211
     * <p>
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4212
     * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4213
     * variable-arity method handle}, even if the original target method handle was.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4214
     * @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
  4215
     * @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
  4216
     * @param values the series of arguments to insert
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4217
     * @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
  4218
     *         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
  4219
     * @throws NullPointerException if the target or the {@code values} array is null
50328
6e9805157cf6 8177276: MethodHandles.insertArguments doesn't specify IllegalArgumentException on index mismatch
vtheeyarath
parents: 49935
diff changeset
  4220
     * @throws IllegalArgumentException if (@code pos) is less than {@code 0} or greater than
6e9805157cf6 8177276: MethodHandles.insertArguments doesn't specify IllegalArgumentException on index mismatch
vtheeyarath
parents: 49935
diff changeset
  4221
     *         {@code N - L} where {@code N} is the arity of the target method handle and {@code L}
6e9805157cf6 8177276: MethodHandles.insertArguments doesn't specify IllegalArgumentException on index mismatch
vtheeyarath
parents: 49935
diff changeset
  4222
     *         is the length of the values array.
6e9805157cf6 8177276: MethodHandles.insertArguments doesn't specify IllegalArgumentException on index mismatch
vtheeyarath
parents: 49935
diff changeset
  4223
     * @throws ClassCastException if an argument does not match the corresponding bound parameter
6e9805157cf6 8177276: MethodHandles.insertArguments doesn't specify IllegalArgumentException on index mismatch
vtheeyarath
parents: 49935
diff changeset
  4224
     *         type.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4225
     * @see MethodHandle#bindTo
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4226
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4227
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4228
    MethodHandle insertArguments(MethodHandle target, int pos, Object... values) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4229
        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
  4230
        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
  4231
        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
  4232
        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
  4233
        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
  4234
            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
  4235
            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
  4236
            if (ptype.isPrimitive()) {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4237
                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
  4238
            } else {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4239
                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
  4240
                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
  4241
            }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4242
        }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4243
        return result;
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4244
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4245
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4246
    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
  4247
                                                             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
  4248
        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
  4249
        // 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
  4250
        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
  4251
        switch (w) {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4252
        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
  4253
        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
  4254
        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
  4255
        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
  4256
        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
  4257
        }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4258
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4259
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4260
    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
  4261
        MethodType oldType = target.type();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4262
        int outargs = oldType.parameterCount();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4263
        int inargs  = outargs - insCount;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4264
        if (inargs < 0)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4265
            throw newIllegalArgumentException("too many values to insert");
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4266
        if (pos < 0 || pos > inargs)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4267
            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
  4268
        return oldType.ptypes();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4269
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4270
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4271
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4272
     * 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
  4273
     * 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
  4274
     * 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
  4275
     * 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
  4276
     * at some given position.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4277
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4278
     * 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
  4279
     * 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
  4280
     * 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
  4281
     * 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
  4282
     * they will come after.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4283
     * <p>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4284
     * <b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  4285
     * <blockquote><pre>{@code
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4286
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4287
import static java.lang.invoke.MethodType.*;
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4288
...
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4289
MethodHandle cat = lookup().findVirtual(String.class,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4290
  "concat", methodType(String.class, String.class));
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4291
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
  4292
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
  4293
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
  4294
assertEquals(bigType, d0.type());
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4295
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
  4296
     * }</pre></blockquote>
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4297
     * <p>
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4298
     * This method is also equivalent to the following code:
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  4299
     * <blockquote><pre>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4300
     * {@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
  4301
     * </pre></blockquote>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4302
     * @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
  4303
     * @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
  4304
     * @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
  4305
     * @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
  4306
     *         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
  4307
     * @throws NullPointerException if the target is null,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4308
     *                              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
  4309
     * @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
  4310
     *                  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
  4311
     *                  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
  4312
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4313
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4314
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  4315
        return dropArguments0(target, pos, copyTypes(valueTypes.toArray()));
39755
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  4316
    }
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  4317
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  4318
    private static List<Class<?>> copyTypes(Object[] array) {
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  4319
        return Arrays.asList(Arrays.copyOf(array, array.length, Class[].class));
39755
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  4320
    }
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  4321
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  4322
    private static
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  4323
    MethodHandle dropArguments0(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
  4324
        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
  4325
        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
26625
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
  4326
        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
  4327
        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
  4328
        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
  4329
        LambdaForm lform = result.form;
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4330
        int insertFormArg = 1 + pos;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4331
        for (Class<?> ptype : valueTypes) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4332
            lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4333
        }
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4334
        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
  4335
        return result;
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4336
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4337
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4338
    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
  4339
        int dropped = valueTypes.size();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  4340
        MethodType.checkSlotCount(dropped);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4341
        int outargs = oldType.parameterCount();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  4342
        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
  4343
        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
  4344
            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
  4345
                    + 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
  4346
                    );
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
  4347
        return dropped;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4348
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  4349
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4350
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4351
     * 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
  4352
     * 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
  4353
     * 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
  4354
     * 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
  4355
     * at some given position.
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4356
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4357
     * 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
  4358
     * 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
  4359
     * 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
  4360
     * 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
  4361
     * they will come after.
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4362
     * @apiNote
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  4363
     * <blockquote><pre>{@code
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4364
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4365
import static java.lang.invoke.MethodType.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4366
...
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4367
MethodHandle cat = lookup().findVirtual(String.class,
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4368
  "concat", methodType(String.class, String.class));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4369
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
  4370
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
  4371
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
  4372
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
  4373
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
  4374
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
  4375
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
  4376
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
  4377
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
  4378
     * }</pre></blockquote>
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4379
     * <p>
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4380
     * This method is also equivalent to the following code:
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  4381
     * <blockquote><pre>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4382
     * {@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
  4383
     * </pre></blockquote>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4384
     * @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
  4385
     * @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
  4386
     * @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
  4387
     * @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
  4388
     *         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
  4389
     * @throws NullPointerException if the target is null,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4390
     *                              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
  4391
     * @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
  4392
     *                  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
  4393
     *                  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
  4394
     *                  <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
  4395
     */
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4396
    public static
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4397
    MethodHandle dropArguments(MethodHandle target, int pos, Class<?>... valueTypes) {
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  4398
        return dropArguments0(target, pos, copyTypes(valueTypes));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4399
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4400
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4401
    // private version which allows caller some freedom with error handling
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4402
    private static MethodHandle dropArgumentsToMatch(MethodHandle target, int skip, List<Class<?>> newTypes, int pos,
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4403
                                      boolean nullOnFailure) {
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  4404
        newTypes = copyTypes(newTypes.toArray());
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4405
        List<Class<?>> oldTypes = target.type().parameterList();
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4406
        int match = oldTypes.size();
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4407
        if (skip != 0) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4408
            if (skip < 0 || skip > match) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4409
                throw newIllegalArgumentException("illegal skip", skip, target);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4410
            }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4411
            oldTypes = oldTypes.subList(skip, match);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4412
            match -= skip;
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4413
        }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4414
        List<Class<?>> addTypes = newTypes;
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4415
        int add = addTypes.size();
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4416
        if (pos != 0) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4417
            if (pos < 0 || pos > add) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4418
                throw newIllegalArgumentException("illegal pos", pos, newTypes);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4419
            }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4420
            addTypes = addTypes.subList(pos, add);
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4421
            add -= pos;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4422
            assert(addTypes.size() == add);
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4423
        }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4424
        // Do not add types which already match the existing arguments.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4425
        if (match > add || !oldTypes.equals(addTypes.subList(0, match))) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4426
            if (nullOnFailure) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4427
                return null;
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4428
            }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4429
            throw newIllegalArgumentException("argument lists do not match", oldTypes, newTypes);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4430
        }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4431
        addTypes = addTypes.subList(match, add);
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4432
        add -= match;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4433
        assert(addTypes.size() == add);
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4434
        // newTypes:     (   P*[pos], M*[match], A*[add] )
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4435
        // target: ( S*[skip],        M*[match]  )
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4436
        MethodHandle adapter = target;
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4437
        if (add > 0) {
39755
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  4438
            adapter = dropArguments0(adapter, skip+ match, addTypes);
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4439
        }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4440
        // adapter: (S*[skip],        M*[match], A*[add] )
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4441
        if (pos > 0) {
39755
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  4442
            adapter = dropArguments0(adapter, skip, newTypes.subList(0, pos));
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4443
        }
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4444
        // adapter: (S*[skip], P*[pos], M*[match], A*[add] )
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4445
        return adapter;
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4446
    }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4447
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4448
    /**
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4449
     * Adapts a target method handle to match the given parameter type list. If necessary, adds dummy arguments. Some
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4450
     * leading parameters can be skipped before matching begins. The remaining types in the {@code target}'s parameter
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4451
     * type list must be a sub-list of the {@code newTypes} type list at the starting position {@code pos}. The
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4452
     * resulting handle will have the target handle's parameter type list, with any non-matching parameter types (before
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4453
     * or after the matching sub-list) inserted in corresponding positions of the target's original parameters, as if by
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4454
     * {@link #dropArguments(MethodHandle, int, Class[])}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4455
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4456
     * The resulting handle will have the same return type as the target handle.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4457
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4458
     * In more formal terms, assume these two type lists:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4459
     * <li>The target handle has the parameter type list {@code S..., M...}, with as many types in {@code S} as
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4460
     * indicated by {@code skip}. The {@code M} types are those that are supposed to match part of the given type list,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4461
     * {@code newTypes}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4462
     * <li>The {@code newTypes} list contains types {@code P..., M..., A...}, with as many types in {@code P} as
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4463
     * indicated by {@code pos}. The {@code M} types are precisely those that the {@code M} types in the target handle's
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4464
     * parameter type list are supposed to match. The types in {@code A} are additional types found after the matching
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4465
     * sub-list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4466
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4467
     * Given these assumptions, the result of an invocation of {@code dropArgumentsToMatch} will have the parameter type
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4468
     * list {@code S..., P..., M..., A...}, with the {@code P} and {@code A} types inserted as if by
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4469
     * {@link #dropArguments(MethodHandle, int, Class[])}.
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  4470
     *
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4471
     * @apiNote
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4472
     * Two method handles whose argument lists are "effectively identical" (i.e., identical in a common prefix) may be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4473
     * mutually converted to a common type by two calls to {@code dropArgumentsToMatch}, as follows:
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4474
     * <blockquote><pre>{@code
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4475
import static java.lang.invoke.MethodHandles.*;
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4476
import static java.lang.invoke.MethodType.*;
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4477
...
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4478
...
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4479
MethodHandle h0 = constant(boolean.class, true);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4480
MethodHandle h1 = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4481
MethodType bigType = h1.type().insertParameterTypes(1, String.class, int.class);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4482
MethodHandle h2 = dropArguments(h1, 0, bigType.parameterList());
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4483
if (h1.type().parameterCount() < h2.type().parameterCount())
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4484
    h1 = dropArgumentsToMatch(h1, 0, h2.type().parameterList(), 0);  // lengthen h1
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4485
else
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4486
    h2 = dropArgumentsToMatch(h2, 0, h1.type().parameterList(), 0);    // lengthen h2
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4487
MethodHandle h3 = guardWithTest(h0, h1, h2);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4488
assertEquals("xy", h3.invoke("x", "y", 1, "a", "b", "c"));
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4489
     * }</pre></blockquote>
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4490
     * @param target the method handle to adapt
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4491
     * @param skip number of targets parameters to disregard (they will be unchanged)
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4492
     * @param newTypes the list of types to match {@code target}'s parameter type list to
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4493
     * @param pos place in {@code newTypes} where the non-skipped target parameters must occur
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4494
     * @return a possibly adapted method handle
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4495
     * @throws NullPointerException if either argument is null
38785
dacdc5add5eb 8158171: MethodHandles.dropArgumentsToMatch(...) non-documented IAE
srastogi
parents: 38774
diff changeset
  4496
     * @throws IllegalArgumentException if any element of {@code newTypes} is {@code void.class},
39488
8dee5a37bdc7 8158169: MethodHandles.dropArgumentsToMatch(...)
srastogi
parents: 39476
diff changeset
  4497
     *         or if {@code skip} is negative or greater than the arity of the target,
8dee5a37bdc7 8158169: MethodHandles.dropArgumentsToMatch(...)
srastogi
parents: 39476
diff changeset
  4498
     *         or if {@code pos} is negative or greater than the newTypes list size,
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4499
     *         or if {@code newTypes} does not contain the {@code target}'s non-skipped parameter types at position
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  4500
     *         {@code pos}.
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4501
     * @since 9
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4502
     */
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4503
    public static
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4504
    MethodHandle dropArgumentsToMatch(MethodHandle target, int skip, List<Class<?>> newTypes, int pos) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4505
        Objects.requireNonNull(target);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4506
        Objects.requireNonNull(newTypes);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4507
        return dropArgumentsToMatch(target, skip, newTypes, pos, false);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4508
    }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  4509
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4510
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4511
     * Adapts a target method handle by pre-processing
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4512
     * 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
  4513
     * 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
  4514
     * replaced by the result of its corresponding filter function.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4515
     * <p>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4516
     * 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
  4517
     * 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
  4518
     * 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
  4519
     * argument of the target, and so on in sequence.
48544
050352ed64d5 8194554: filterArguments runs multiple filters in the wrong order
mchung
parents: 47856
diff changeset
  4520
     * The filter functions are invoked in left to right order.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4521
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4522
     * 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
  4523
     * and the corresponding arguments left unchanged.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4524
     * (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
  4525
     * 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
  4526
     * <p>
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4527
     * 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
  4528
     * 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
  4529
     * 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
  4530
     * replaces the corresponding argument type of the target
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4531
     * in the resulting adapted method handle.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4532
     * 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
  4533
     * parameter type of the target.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4534
     * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4535
     * 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
  4536
     * (null or not)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4537
     * 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
  4538
     * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  4539
     * <blockquote><pre>{@code
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4540
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4541
import static java.lang.invoke.MethodType.*;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4542
...
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4543
MethodHandle cat = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4544
  "concat", methodType(String.class, String.class));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4545
MethodHandle upcase = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4546
  "toUpperCase", methodType(String.class));
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4547
assertEquals("xy", (String) cat.invokeExact("x", "y"));
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4548
MethodHandle f0 = filterArguments(cat, 0, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4549
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4550
MethodHandle f1 = filterArguments(cat, 1, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4551
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4552
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4553
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4554
     * }</pre></blockquote>
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4555
     * <p>Here is pseudocode for the resulting adapter. In the code, {@code T}
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4556
     * denotes the return type of both the {@code target} and resulting adapter.
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4557
     * {@code P}/{@code p} and {@code B}/{@code b} represent the types and values
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4558
     * of the parameters and arguments that precede and follow the filter position
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4559
     * {@code pos}, respectively. {@code A[i]}/{@code a[i]} stand for the types and
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4560
     * values of the filtered parameters and arguments; they also represent the
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4561
     * return types of the {@code filter[i]} handles. The latter accept arguments
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4562
     * {@code v[i]} of type {@code V[i]}, which also appear in the signature of
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4563
     * the resulting adapter.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4564
     * <blockquote><pre>{@code
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4565
     * T target(P... p, A[i]... a[i], B... b);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4566
     * 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
  4567
     * T adapter(P... p, V[i]... v[i], B... b) {
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4568
     *   return target(p..., filter[i](v[i])..., b...);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4569
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4570
     * }</pre></blockquote>
36112
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4571
     * <p>
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4572
     * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4573
     * variable-arity method handle}, even if the original target method handle was.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4574
     *
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4575
     * @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
  4576
     * @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
  4577
     * @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
  4578
     * @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
  4579
     * @throws NullPointerException if the target is null
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4580
     *                              or if the {@code filters} array is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  4581
     * @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
  4582
     *          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
  4583
     *          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
  4584
     *          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
  4585
     *          <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
  4586
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4587
    public static
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4588
    MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) {
52486
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4589
        // In method types arguments start at index 0, while the LF
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4590
        // editor have the MH receiver at position 0 - adjust appropriately.
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4591
        final int MH_RECEIVER_OFFSET = 1;
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4592
        filterArgumentsCheckArity(target, pos, filters);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4593
        MethodHandle adapter = target;
52486
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4594
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4595
        // keep track of currently matched filters, as to optimize repeated filters
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4596
        int index = 0;
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4597
        int[] positions = new int[filters.length];
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4598
        MethodHandle filter = null;
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4599
48544
050352ed64d5 8194554: filterArguments runs multiple filters in the wrong order
mchung
parents: 47856
diff changeset
  4600
        // process filters in reverse order so that the invocation of
050352ed64d5 8194554: filterArguments runs multiple filters in the wrong order
mchung
parents: 47856
diff changeset
  4601
        // the resulting adapter will invoke the filters in left-to-right order
050352ed64d5 8194554: filterArguments runs multiple filters in the wrong order
mchung
parents: 47856
diff changeset
  4602
        for (int i = filters.length - 1; i >= 0; --i) {
52486
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4603
            MethodHandle newFilter = filters[i];
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4604
            if (newFilter == null) continue;  // ignore null elements of filters
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4605
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4606
            // flush changes on update
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4607
            if (filter != newFilter) {
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4608
                if (filter != null) {
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4609
                    if (index > 1) {
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4610
                        adapter = filterRepeatedArgument(adapter, filter, Arrays.copyOf(positions, index));
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4611
                    } else {
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4612
                        adapter = filterArgument(adapter, positions[0] - 1, filter);
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4613
                    }
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4614
                }
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4615
                filter = newFilter;
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4616
                index = 0;
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4617
            }
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4618
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4619
            filterArgumentChecks(target, pos + i, newFilter);
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4620
            positions[index++] = pos + i + MH_RECEIVER_OFFSET;
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4621
        }
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4622
        if (index > 1) {
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4623
            adapter = filterRepeatedArgument(adapter, filter, Arrays.copyOf(positions, index));
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4624
        } else if (index == 1) {
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4625
            adapter = filterArgument(adapter, positions[0] - 1, filter);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4626
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4627
        return adapter;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4628
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4629
52486
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4630
    private static MethodHandle filterRepeatedArgument(MethodHandle adapter, MethodHandle filter, int[] positions) {
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4631
        MethodType targetType = adapter.type();
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4632
        MethodType filterType = filter.type();
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4633
        BoundMethodHandle result = adapter.rebind();
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4634
        Class<?> newParamType = filterType.parameterType(0);
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4635
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4636
        Class<?>[] ptypes = targetType.ptypes().clone();
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4637
        for (int pos : positions) {
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4638
            ptypes[pos - 1] = newParamType;
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4639
        }
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4640
        MethodType newType = MethodType.makeImpl(targetType.rtype(), ptypes, true);
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4641
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4642
        LambdaForm lform = result.editor().filterRepeatedArgumentForm(BasicType.basicType(newParamType), positions);
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4643
        return result.copyWithExtendL(newType, lform, filter);
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4644
    }
6f5948597697 8213478: Reduce rebinds when applying repeated filters and conversions
redestad
parents: 52427
diff changeset
  4645
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4646
    /*non-public*/ static
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4647
    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
  4648
        filterArgumentChecks(target, pos, filter);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4649
        MethodType targetType = target.type();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4650
        MethodType filterType = filter.type();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4651
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4652
        Class<?> newParamType = filterType.parameterType(0);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4653
        LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4654
        MethodType newType = targetType.changeParameterType(pos, newParamType);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4655
        result = result.copyWithExtendL(newType, lform, filter);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4656
        return result;
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4657
    }
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4658
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4659
    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
  4660
        MethodType targetType = target.type();
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4661
        int maxPos = targetType.parameterCount();
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4662
        if (pos + filters.length > maxPos)
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4663
            throw newIllegalArgumentException("too many filters");
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4664
    }
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4665
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4666
    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
  4667
        MethodType targetType = target.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4668
        MethodType filterType = filter.type();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4669
        if (filterType.parameterCount() != 1
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4670
            || filterType.returnType() != targetType.parameterType(pos))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4671
            throw newIllegalArgumentException("target and filter types do not match", targetType, filterType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4672
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4673
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4674
    /**
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4675
     * Adapts a target method handle by pre-processing
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4676
     * 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
  4677
     * 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
  4678
     * filter function.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4679
     * 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
  4680
     * <p>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4681
     * 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
  4682
     * 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
  4683
     * any arguments not passed to the filter.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4684
     * 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
  4685
     * not passed to the filter.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4686
     * 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
  4687
     * 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
  4688
     * passed to the adapter.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4689
     * <p>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4690
     * The argument types (if any) of the filter
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4691
     * 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
  4692
     * in the resulting adapted method handle.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4693
     * 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
  4694
     * 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
  4695
     * 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
  4696
     * <p>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4697
     * 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
  4698
     * {@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
  4699
     * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  4700
     * <blockquote><pre>{@code
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4701
import static java.lang.invoke.MethodHandles.*;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4702
import static java.lang.invoke.MethodType.*;
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4703
...
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4704
MethodHandle deepToString = publicLookup()
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4705
  .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
  4706
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4707
MethodHandle ts1 = deepToString.asCollector(String[].class, 1);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4708
assertEquals("[strange]", (String) ts1.invokeExact("strange"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4709
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4710
MethodHandle ts2 = deepToString.asCollector(String[].class, 2);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4711
assertEquals("[up, down]", (String) ts2.invokeExact("up", "down"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4712
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4713
MethodHandle ts3 = deepToString.asCollector(String[].class, 3);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4714
MethodHandle ts3_ts2 = collectArguments(ts3, 1, ts2);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4715
assertEquals("[top, [up, down], strange]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4716
             (String) ts3_ts2.invokeExact("top", "up", "down", "strange"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4717
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4718
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
  4719
assertEquals("[top, [up, down], [strange]]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4720
             (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
  4721
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4722
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
  4723
assertEquals("[top, [[up, down, strange], charm], bottom]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4724
             (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
  4725
     * }</pre></blockquote>
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4726
     * <p>Here is pseudocode for the resulting adapter. In the code, {@code T}
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4727
     * represents the return type of the {@code target} and resulting adapter.
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4728
     * {@code V}/{@code v} stand for the return type and value of the
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4729
     * {@code filter}, which are also found in the signature and arguments of
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4730
     * the {@code target}, respectively, unless {@code V} is {@code void}.
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4731
     * {@code A}/{@code a} and {@code C}/{@code c} represent the parameter types
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4732
     * and values preceding and following the collection position, {@code pos},
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4733
     * in the {@code target}'s signature. They also turn up in the resulting
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4734
     * adapter's signature and arguments, where they surround
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4735
     * {@code B}/{@code b}, which represent the parameter types and arguments
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4736
     * to the {@code filter} (if any).
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4737
     * <blockquote><pre>{@code
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4738
     * T target(A...,V,C...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4739
     * V filter(B...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4740
     * T adapter(A... a,B... b,C... c) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4741
     *   V v = filter(b...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4742
     *   return target(a...,v,c...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4743
     * }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4744
     * // and if the filter has no arguments:
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4745
     * T target2(A...,V,C...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4746
     * V filter2();
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4747
     * T adapter2(A... a,C... c) {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4748
     *   V v = filter2();
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4749
     *   return target2(a...,v,c...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4750
     * }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4751
     * // and if the filter has a void return:
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4752
     * T target3(A...,C...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4753
     * void filter3(B...);
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4754
     * T adapter3(A... a,B... b,C... c) {
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4755
     *   filter3(b...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4756
     *   return target3(a...,c...);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4757
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4758
     * }</pre></blockquote>
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4759
     * <p>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4760
     * 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
  4761
     * 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
  4762
     * steps as follows:
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4763
     * <blockquote><pre>{@code
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4764
     * 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
  4765
     * mh = MethodHandles.foldArguments(mh, coll); //step 1
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4766
     * }</pre></blockquote>
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4767
     * 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
  4768
     * (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
  4769
     * is equivalent to {@code filterReturnValue(coll, mh)}.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4770
     * 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
  4771
     * 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
  4772
     * is equivalent to {@code filterArguments(mh, N, coll)}.
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4773
     * Other equivalences are possible but would require argument permutation.
36112
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4774
     * <p>
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4775
     * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4776
     * variable-arity method handle}, even if the original target method handle was.
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4777
     *
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4778
     * @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
  4779
     * @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
  4780
     *            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
  4781
     * @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
  4782
     * @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
  4783
     * @throws NullPointerException if either argument is null
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4784
     * @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
  4785
     *          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
  4786
     *          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
  4787
     *          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
  4788
     *          <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
  4789
     * @see MethodHandles#foldArguments
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4790
     * @see MethodHandles#filterArguments
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4791
     * @see MethodHandles#filterReturnValue
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4792
     */
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4793
    public static
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4794
    MethodHandle collectArguments(MethodHandle target, int pos, MethodHandle filter) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4795
        MethodType newType = collectArgumentsChecks(target, pos, filter);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4796
        MethodType collectorType = filter.type();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4797
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4798
        LambdaForm lform;
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4799
        if (collectorType.returnType().isArray() && filter.intrinsicName() == Intrinsic.NEW_ARRAY) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4800
            lform = result.editor().collectArgumentArrayForm(1 + pos, filter);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4801
            if (lform != null) {
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4802
                return result.copyWith(newType, lform);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4803
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4804
        }
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4805
        lform = result.editor().collectArgumentsForm(1 + pos, collectorType.basicType());
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4806
        return result.copyWithExtendL(newType, lform, filter);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4807
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4808
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4809
    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
  4810
        MethodType targetType = target.type();
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20532
diff changeset
  4811
        MethodType filterType = filter.type();
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4812
        Class<?> rtype = filterType.returnType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4813
        List<Class<?>> filterArgs = filterType.parameterList();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4814
        if (rtype == void.class) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4815
            return targetType.insertParameterTypes(pos, filterArgs);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4816
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4817
        if (rtype != targetType.parameterType(pos)) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  4818
            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
  4819
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26478
diff changeset
  4820
        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
  4821
    }
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
  4822
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  4823
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4824
     * 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
  4825
     * 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
  4826
     * 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
  4827
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4828
     * 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
  4829
     * its only argument.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4830
     * 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
  4831
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4832
     * The return type of the filter
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4833
     * replaces the return type of the target
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4834
     * 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
  4835
     * 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
  4836
     * return type of the target.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4837
     * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  4838
     * <blockquote><pre>{@code
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4839
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  4840
import static java.lang.invoke.MethodType.*;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4841
...
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4842
MethodHandle cat = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4843
  "concat", methodType(String.class, String.class));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4844
MethodHandle length = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4845
  "length", methodType(int.class));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4846
System.out.println((String) cat.invokeExact("x", "y")); // xy
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4847
MethodHandle f0 = filterReturnValue(cat, length);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4848
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
  4849
     * }</pre></blockquote>
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4850
     * <p>Here is pseudocode for the resulting adapter. In the code,
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4851
     * {@code T}/{@code t} represent the result type and value of the
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4852
     * {@code target}; {@code V}, the result type of the {@code filter}; and
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4853
     * {@code A}/{@code a}, the types and values of the parameters and arguments
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4854
     * of the {@code target} as well as the resulting adapter.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4855
     * <blockquote><pre>{@code
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4856
     * T target(A...);
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4857
     * V filter(T);
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4858
     * V adapter(A... a) {
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4859
     *   T t = target(a...);
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4860
     *   return filter(t);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4861
     * }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4862
     * // 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
  4863
     * void target2(A...);
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4864
     * V filter2();
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4865
     * V adapter2(A... a) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4866
     *   target2(a...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4867
     *   return filter2();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4868
     * }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4869
     * // and if the filter has a void return:
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4870
     * T target3(A...);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4871
     * void filter3(V);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4872
     * void adapter3(A... a) {
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4873
     *   T t = target3(a...);
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4874
     *   filter3(t);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4875
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4876
     * }</pre></blockquote>
36112
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4877
     * <p>
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4878
     * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4879
     * variable-arity method handle}, even if the original target method handle was.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4880
     * @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
  4881
     * @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
  4882
     * @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
  4883
     * @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
  4884
     * @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
  4885
     *          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
  4886
     */
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4887
    public static
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  4888
    MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) {
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  4889
        MethodType targetType = target.type();
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  4890
        MethodType filterType = filter.type();
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4891
        filterReturnValueChecks(targetType, filterType);
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4892
        BoundMethodHandle result = target.rebind();
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4893
        BasicType rtype = BasicType.basicType(filterType.returnType());
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4894
        LambdaForm lform = result.editor().filterReturnForm(rtype, false);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4895
        MethodType newType = targetType.changeReturnType(filterType.returnType());
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4896
        result = result.copyWithExtendL(newType, lform, filter);
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 26626
diff changeset
  4897
        return result;
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4898
    }
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4899
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4900
    private static void filterReturnValueChecks(MethodType targetType, MethodType filterType) throws RuntimeException {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4901
        Class<?> rtype = targetType.returnType();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4902
        int filterValues = filterType.parameterCount();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4903
        if (filterValues == 0
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  4904
                ? (rtype != void.class)
39756
c50c31af8cd9 8158571: Additional method handle validation
mhaupt
parents: 39755
diff changeset
  4905
                : (rtype != filterType.parameterType(0) || filterValues != 1))
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4906
            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
  4907
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  4908
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4909
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4910
     * Adapts a target method handle by pre-processing
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4911
     * 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
  4912
     * 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
  4913
     * sequence of arguments.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4914
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4915
     * 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
  4916
     * 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
  4917
     * 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
  4918
     * (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
  4919
     * 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
  4920
     * 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
  4921
     * arguments.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4922
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4923
     * 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
  4924
     * 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
  4925
     * {@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
  4926
     * of the combiner.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4927
     * <p>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4928
     * 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
  4929
     * 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
  4930
     * 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
  4931
     * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4932
     * 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
  4933
     * first parameter type is dropped,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4934
     * 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
  4935
     * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
  4936
     * (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
  4937
     * 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
  4938
     * 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
  4939
     * 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
  4940
     * 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
  4941
     * target.)
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4942
     * <p><b>Example:</b>
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20853
diff changeset
  4943
     * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4944
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
  4945
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
  4946
...
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4947
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
  4948
  "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
  4949
    .bindTo(System.out);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4950
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
  4951
  "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
  4952
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
  4953
MethodHandle catTrace = foldArguments(cat, trace);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4954
// also prints "boo":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4955
assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4956
     * }</pre></blockquote>
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4957
     * <p>Here is pseudocode for the resulting adapter. In the code, {@code T}
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4958
     * represents the result type of the {@code target} and resulting adapter.
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4959
     * {@code V}/{@code v} represent the type and value of the parameter and argument
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4960
     * of {@code target} that precedes the folding position; {@code V} also is
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4961
     * the result type of the {@code combiner}. {@code A}/{@code a} denote the
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4962
     * types and values of the {@code N} parameters and arguments at the folding
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4963
     * position. {@code B}/{@code b} represent the types and values of the
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4964
     * {@code target} parameters and arguments that follow the folded parameters
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  4965
     * and arguments.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4966
     * <blockquote><pre>{@code
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4967
     * // there are N arguments in A...
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4968
     * T target(V, A[N]..., B...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4969
     * V combiner(A...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4970
     * T adapter(A... a, B... b) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4971
     *   V v = combiner(a...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4972
     *   return target(v, a..., b...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4973
     * }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4974
     * // 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
  4975
     * T target2(A[N]..., B...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4976
     * void combiner2(A...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4977
     * 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
  4978
     *   combiner2(a...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4979
     *   return target2(a..., b...);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4980
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  4981
     * }</pre></blockquote>
36112
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4982
     * <p>
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4983
     * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  4984
     * variable-arity method handle}, even if the original target method handle was.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4985
     * @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
  4986
     * @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
  4987
     * @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
  4988
     * @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
  4989
     * @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
  4990
     *          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
  4991
     *          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
  4992
     *          of the target
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
  4993
     *          (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
  4994
     *          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
  4995
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4996
    public static
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  4997
    MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  4998
        return foldArguments(target, 0, combiner);
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  4999
    }
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  5000
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5001
    /**
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5002
     * Adapts a target method handle by pre-processing some of its arguments, starting at a given position, and then
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5003
     * calling the target with the result of the pre-processing, inserted into the original sequence of arguments just
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5004
     * before the folded arguments.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5005
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5006
     * This method is closely related to {@link #foldArguments(MethodHandle, MethodHandle)}, but allows to control the
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5007
     * position in the parameter list at which folding takes place. The argument controlling this, {@code pos}, is a
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5008
     * zero-based index. The aforementioned method {@link #foldArguments(MethodHandle, MethodHandle)} assumes position
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5009
     * 0.
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  5010
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5011
     * @apiNote Example:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5012
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5013
    import static java.lang.invoke.MethodHandles.*;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5014
    import static java.lang.invoke.MethodType.*;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5015
    ...
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5016
    MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5017
    "println", methodType(void.class, String.class))
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5018
    .bindTo(System.out);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5019
    MethodHandle cat = lookup().findVirtual(String.class,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5020
    "concat", methodType(String.class, String.class));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5021
    assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5022
    MethodHandle catTrace = foldArguments(cat, 1, trace);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5023
    // also prints "jum":
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5024
    assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5025
     * }</pre></blockquote>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5026
     * <p>Here is pseudocode for the resulting adapter. In the code, {@code T}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5027
     * represents the result type of the {@code target} and resulting adapter.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5028
     * {@code V}/{@code v} represent the type and value of the parameter and argument
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5029
     * of {@code target} that precedes the folding position; {@code V} also is
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5030
     * the result type of the {@code combiner}. {@code A}/{@code a} denote the
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5031
     * types and values of the {@code N} parameters and arguments at the folding
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5032
     * position. {@code Z}/{@code z} and {@code B}/{@code b} represent the types
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5033
     * and values of the {@code target} parameters and arguments that precede and
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5034
     * follow the folded parameters and arguments starting at {@code pos},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5035
     * respectively.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5036
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5037
     * // there are N arguments in A...
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5038
     * T target(Z..., V, A[N]..., B...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5039
     * V combiner(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5040
     * T adapter(Z... z, A... a, B... b) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5041
     *   V v = combiner(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5042
     *   return target(z..., v, a..., b...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5043
     * }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5044
     * // and if the combiner has a void return:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5045
     * T target2(Z..., A[N]..., B...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5046
     * void combiner2(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5047
     * T adapter2(Z... z, A... a, B... b) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5048
     *   combiner2(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5049
     *   return target2(z..., a..., b...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5050
     * }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5051
     * }</pre></blockquote>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5052
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5053
     * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5054
     * variable-arity method handle}, even if the original target method handle was.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5055
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5056
     * @param target the method handle to invoke after arguments are combined
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5057
     * @param pos the position at which to start folding and at which to insert the folding result; if this is {@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5058
     *            0}, the effect is the same as for {@link #foldArguments(MethodHandle, MethodHandle)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5059
     * @param combiner method handle to call initially on the incoming arguments
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5060
     * @return method handle which incorporates the specified argument folding logic
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5061
     * @throws NullPointerException if either argument is null
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5062
     * @throws IllegalArgumentException if either of the following two conditions holds:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5063
     *          (1) {@code combiner}'s return type is non-{@code void} and not the same as the argument type at position
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5064
     *              {@code pos} of the target signature;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5065
     *          (2) the {@code N} argument types at position {@code pos} of the target signature (skipping one matching
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5066
     *              the {@code combiner}'s return type) are not identical with the argument types of {@code combiner}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5067
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5068
     * @see #foldArguments(MethodHandle, MethodHandle)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5069
     * @since 9
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5070
     */
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5071
    public static MethodHandle foldArguments(MethodHandle target, int pos, MethodHandle combiner) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5072
        MethodType targetType = target.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5073
        MethodType combinerType = combiner.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5074
        Class<?> rtype = foldArgumentChecks(pos, targetType, combinerType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5075
        BoundMethodHandle result = target.rebind();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5076
        boolean dropResult = rtype == void.class;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5077
        LambdaForm lform = result.editor().foldArgumentsForm(1 + pos, dropResult, combinerType.basicType());
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5078
        MethodType newType = targetType;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5079
        if (!dropResult) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5080
            newType = newType.dropParameterTypes(pos, pos + 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5081
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5082
        result = result.copyWithExtendL(newType, lform, combiner);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5083
        return result;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5084
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5085
26477
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  5086
    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
  5087
        int foldArgs   = combinerType.parameterCount();
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  5088
        Class<?> rtype = combinerType.returnType();
e095a2b8a1f8 8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents: 26476
diff changeset
  5089
        int foldVals = rtype == void.class ? 0 : 1;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  5090
        int afterInsertPos = foldPos + foldVals;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
  5091
        boolean ok = (targetType.parameterCount() >= afterInsertPos + foldArgs);
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  5092
        if (ok) {
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  5093
            for (int i = 0; i < foldArgs; i++) {
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  5094
                if (combinerType.parameterType(i) != targetType.parameterType(i + afterInsertPos)) {
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  5095
                    ok = false;
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  5096
                    break;
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  5097
                }
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  5098
            }
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40256
diff changeset
  5099
        }
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5100
        if (ok && foldVals != 0 && combinerType.returnType() != targetType.parameterType(foldPos))
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7053
diff changeset
  5101
            ok = false;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5102
        if (!ok)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5103
            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
  5104
        return rtype;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5105
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5106
52226
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5107
    /**
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5108
     * Adapts a target method handle by pre-processing some of its arguments, then calling the target with the result
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5109
     * of the pre-processing replacing the argument at the given position.
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5110
     *
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5111
     * @param target the method handle to invoke after arguments are combined
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5112
     * @param position the position at which to start folding and at which to insert the folding result; if this is {@code
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5113
     *            0}, the effect is the same as for {@link #foldArguments(MethodHandle, MethodHandle)}.
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5114
     * @param combiner method handle to call initially on the incoming arguments
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5115
     * @param argPositions indexes of the target to pick arguments sent to the combiner from
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5116
     * @return method handle which incorporates the specified argument folding logic
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5117
     * @throws NullPointerException if either argument is null
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5118
     * @throws IllegalArgumentException if either of the following two conditions holds:
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5119
     *          (1) {@code combiner}'s return type is not the same as the argument type at position
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5120
     *              {@code pos} of the target signature;
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5121
     *          (2) the {@code N} argument types at positions {@code argPositions[1...N]} of the target signature are
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5122
     *              not identical with the argument types of {@code combiner}.
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5123
     */
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5124
    /*non-public*/ static MethodHandle filterArgumentsWithCombiner(MethodHandle target, int position, MethodHandle combiner, int ... argPositions) {
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5125
        return argumentsWithCombiner(true, target, position, combiner, argPositions);
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5126
    }
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5127
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5128
    /**
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5129
     * Adapts a target method handle by pre-processing some of its arguments, calling the target with the result of
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5130
     * the pre-processing inserted into the original sequence of arguments at the given position.
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5131
     *
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5132
     * @param target the method handle to invoke after arguments are combined
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5133
     * @param position the position at which to start folding and at which to insert the folding result; if this is {@code
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5134
     *            0}, the effect is the same as for {@link #foldArguments(MethodHandle, MethodHandle)}.
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5135
     * @param combiner method handle to call initially on the incoming arguments
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5136
     * @param argPositions indexes of the target to pick arguments sent to the combiner from
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5137
     * @return method handle which incorporates the specified argument folding logic
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5138
     * @throws NullPointerException if either argument is null
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5139
     * @throws IllegalArgumentException if either of the following two conditions holds:
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5140
     *          (1) {@code combiner}'s return type is non-{@code void} and not the same as the argument type at position
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5141
     *              {@code pos} of the target signature;
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5142
     *          (2) the {@code N} argument types at positions {@code argPositions[1...N]} of the target signature
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5143
     *              (skipping {@code position} where the {@code combiner}'s return will be folded in) are not identical
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5144
     *              with the argument types of {@code combiner}.
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5145
     */
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5146
    /*non-public*/ static MethodHandle foldArgumentsWithCombiner(MethodHandle target, int position, MethodHandle combiner, int ... argPositions) {
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5147
        return argumentsWithCombiner(false, target, position, combiner, argPositions);
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5148
    }
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5149
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5150
    private static MethodHandle argumentsWithCombiner(boolean filter, MethodHandle target, int position, MethodHandle combiner, int ... argPositions) {
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5151
        MethodType targetType = target.type();
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5152
        MethodType combinerType = combiner.type();
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5153
        Class<?> rtype = argumentsWithCombinerChecks(position, filter, targetType, combinerType, argPositions);
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5154
        BoundMethodHandle result = target.rebind();
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5155
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5156
        MethodType newType = targetType;
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5157
        LambdaForm lform;
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5158
        if (filter) {
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5159
            lform = result.editor().filterArgumentsForm(1 + position, combinerType.basicType(), argPositions);
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5160
        } else {
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5161
            boolean dropResult = rtype == void.class;
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5162
            lform = result.editor().foldArgumentsForm(1 + position, dropResult, combinerType.basicType(), argPositions);
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5163
            if (!dropResult) {
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5164
                newType = newType.dropParameterTypes(position, position + 1);
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5165
            }
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5166
        }
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5167
        result = result.copyWithExtendL(newType, lform, combiner);
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5168
        return result;
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5169
    }
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5170
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5171
    private static Class<?> argumentsWithCombinerChecks(int position, boolean filter, MethodType targetType, MethodType combinerType, int ... argPos) {
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5172
        int combinerArgs = combinerType.parameterCount();
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5173
        if (argPos.length != combinerArgs) {
40810
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5174
            throw newIllegalArgumentException("combiner and argument map must be equal size", combinerType, argPos.length);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5175
        }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5176
        Class<?> rtype = combinerType.returnType();
52226
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5177
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5178
        for (int i = 0; i < combinerArgs; i++) {
40810
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5179
            int arg = argPos[i];
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5180
            if (arg < 0 || arg > targetType.parameterCount()) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5181
                throw newIllegalArgumentException("arg outside of target parameterRange", targetType, arg);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5182
            }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5183
            if (combinerType.parameterType(i) != targetType.parameterType(arg)) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5184
                throw newIllegalArgumentException("target argument type at position " + arg
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5185
                        + " must match combiner argument type at index " + i + ": " + targetType
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5186
                        + " -> " + combinerType + ", map: " + Arrays.toString(argPos));
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5187
            }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5188
        }
52226
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5189
        if (filter && combinerType.returnType() != targetType.parameterType(position)) {
b4b932c6001f 8212726: Replace some use of drop- and foldArguments with filtering argument combinator in StringConcatFactory
redestad
parents: 52086
diff changeset
  5190
            throw misMatchedTypes("target and combiner types", targetType, combinerType);
40810
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5191
        }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5192
        return rtype;
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5193
    }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40732
diff changeset
  5194
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5195
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  5196
     * 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
  5197
     * 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
  5198
     * 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
  5199
     * All three method handles must have the same corresponding
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5200
     * 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
  5201
     * 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
  5202
     * to have fewer arguments than the other two method handles.
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5203
     * <p>
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5204
     * Here is pseudocode for the resulting adapter. In the code, {@code T}
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5205
     * represents the uniform result type of the three involved handles;
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5206
     * {@code A}/{@code a}, the types and values of the {@code target}
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5207
     * parameters and arguments that are consumed by the {@code test}; and
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5208
     * {@code B}/{@code b}, those types and values of the {@code target}
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5209
     * parameters and arguments that are not consumed by the {@code test}.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  5210
     * <blockquote><pre>{@code
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5211
     * boolean test(A...);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5212
     * T target(A...,B...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5213
     * T fallback(A...,B...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5214
     * T adapter(A... a,B... b) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5215
     *   if (test(a...))
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5216
     *     return target(a..., b...);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5217
     *   else
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5218
     *     return fallback(a..., b...);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5219
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  5220
     * }</pre></blockquote>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5221
     * 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
  5222
     * 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
  5223
     * 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
  5224
     * @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
  5225
     * @param target method handle to call if test passes
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5226
     * @param fallback method handle to call if test fails
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5227
     * @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
  5228
     * @throws NullPointerException if any argument is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5229
     * @throws IllegalArgumentException if {@code test} does not return boolean,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5230
     *          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
  5231
     *          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
  5232
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5233
    public static
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5234
    MethodHandle guardWithTest(MethodHandle test,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5235
                               MethodHandle target,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5236
                               MethodHandle fallback) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5237
        MethodType gtype = test.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5238
        MethodType ttype = target.type();
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5239
        MethodType ftype = fallback.type();
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5240
        if (!ttype.equals(ftype))
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5241
            throw misMatchedTypes("target and fallback types", ttype, ftype);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5242
        if (gtype.returnType() != boolean.class)
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5243
            throw newIllegalArgumentException("guard type is not a predicate "+gtype);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5244
        List<Class<?>> targs = ttype.parameterList();
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  5245
        test = dropArgumentsToMatch(test, 0, targs, 0, true);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  5246
        if (test == null) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  5247
            throw misMatchedTypes("target and test types", ttype, gtype);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5248
        }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  5249
        return MethodHandleImpl.makeGuardWithTest(test, target, fallback);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5250
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5251
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5252
    static <T> RuntimeException misMatchedTypes(String what, T t1, T t2) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5253
        return newIllegalArgumentException(what + " must match: " + t1 + " != " + t2);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5254
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5255
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5256
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
  5257
     * 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
  5258
     * by running it inside an exception handler.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5259
     * 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
  5260
     * 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
  5261
     * 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
  5262
     * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5263
     * The target and handler must have the same corresponding
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5264
     * 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
  5265
     * (similarly to the predicate in {@link #guardWithTest guardWithTest}).
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5266
     * Also, the handler must have an extra leading parameter of {@code exType} or a supertype.
36114
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5267
     * <p>
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5268
     * Here is pseudocode for the resulting adapter. In the code, {@code T}
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5269
     * represents the return type of the {@code target} and {@code handler},
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5270
     * and correspondingly that of the resulting adapter; {@code A}/{@code a},
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5271
     * the types and values of arguments to the resulting handle consumed by
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5272
     * {@code handler}; and {@code B}/{@code b}, those of arguments to the
a5ed9456c9be 8143410: augment pseudo-code descriptions in MethodHandles API
mhaupt
parents: 36112
diff changeset
  5273
     * resulting handle discarded by {@code handler}.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  5274
     * <blockquote><pre>{@code
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5275
     * T target(A..., B...);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5276
     * T handler(ExType, A...);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5277
     * T adapter(A... a, B... b) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5278
     *   try {
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5279
     *     return target(a..., b...);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5280
     *   } catch (ExType ex) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5281
     *     return handler(ex, a...);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5282
     *   }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5283
     * }
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20534
diff changeset
  5284
     * }</pre></blockquote>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5285
     * 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
  5286
     * 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
  5287
     * 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
  5288
     * <p>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5289
     * 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
  5290
     * always throws.  (This might happen, for instance, because the handler
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5291
     * is simulating a {@code finally} clause).
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5292
     * 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
  5293
     * with {@link #throwException throwException},
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5294
     * 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
  5295
     * @param target method handle to call
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5296
     * @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
  5297
     * @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
  5298
     * @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
  5299
     * @throws NullPointerException if any argument is null
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5300
     * @throws IllegalArgumentException if {@code handler} does not accept
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5301
     *          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
  5302
     *          not match in their return types and their
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5303
     *          corresponding parameters
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5304
     * @see MethodHandles#tryFinally(MethodHandle, MethodHandle)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5305
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5306
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5307
    MethodHandle catchException(MethodHandle target,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5308
                                Class<? extends Throwable> exType,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5309
                                MethodHandle handler) {
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5310
        MethodType ttype = target.type();
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5311
        MethodType htype = handler.type();
34959
4e7903220c89 8147078: MethodHandles.catchException does not enforce Throwable subtype
mhaupt
parents: 34882
diff changeset
  5312
        if (!Throwable.class.isAssignableFrom(exType))
4e7903220c89 8147078: MethodHandles.catchException does not enforce Throwable subtype
mhaupt
parents: 34882
diff changeset
  5313
            throw new ClassCastException(exType.getName());
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5314
        if (htype.parameterCount() < 1 ||
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5315
            !htype.parameterType(0).isAssignableFrom(exType))
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5316
            throw newIllegalArgumentException("handler does not accept exception type "+exType);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5317
        if (htype.returnType() != ttype.returnType())
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5318
            throw misMatchedTypes("target and handler return types", ttype, htype);
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  5319
        handler = dropArgumentsToMatch(handler, 1, ttype.parameterList(), 0, true);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  5320
        if (handler == null) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  5321
            throw misMatchedTypes("target and handler types", ttype, htype);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
  5322
        }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8349
diff changeset
  5323
        return MethodHandleImpl.makeGuardWithCatch(target, exType, handler);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5324
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  5325
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5326
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  5327
     * 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
  5328
     * 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
  5329
     * and immediately throw it as an exception.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5330
     * 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
  5331
     * 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
  5332
     * 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
  5333
     * @param returnType the return type of the desired method handle
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18274
diff changeset
  5334
     * @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
  5335
     * @return method handle which can throw the given exceptions
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
  5336
     * @throws NullPointerException if either argument is null
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5337
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5338
    public static
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5339
    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
  5340
        if (!Throwable.class.isAssignableFrom(exType))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13044
diff changeset
  5341
            throw new ClassCastException(exType.getName());
37539
fc220bc54b59 8150956: j.l.i.MethodHandles.whileLoop(...) and .iteratedLoop(...) throw unexpected exceptions in the case of 'init' return type is void
mhaupt
parents: 37363
diff changeset
  5342
        return MethodHandleImpl.throwException(methodType(returnType, exType));
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  5343
    }
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5344
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5345
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5346
     * Constructs a method handle representing a loop with several loop variables that are updated and checked upon each
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5347
     * iteration. Upon termination of the loop due to one of the predicates, a corresponding finalizer is run and
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5348
     * delivers the loop's result, which is the return value of the resulting handle.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5349
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5350
     * Intuitively, every loop is formed by one or more "clauses", each specifying a local <em>iteration variable</em> and/or a loop
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5351
     * exit. Each iteration of the loop executes each clause in order. A clause can optionally update its iteration
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5352
     * variable; it can also optionally perform a test and conditional loop exit. In order to express this logic in
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5353
     * terms of method handles, each clause will specify up to four independent actions:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5354
     * <li><em>init:</em> Before the loop executes, the initialization of an iteration variable {@code v} of type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5355
     * <li><em>step:</em> When a clause executes, an update step for the iteration variable {@code v}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5356
     * <li><em>pred:</em> When a clause executes, a predicate execution to test for loop exit.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5357
     * <li><em>fini:</em> If a clause causes a loop exit, a finalizer execution to compute the loop's return value.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5358
     * </ul>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5359
     * The full sequence of all iteration variable types, in clause order, will be notated as {@code (V...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5360
     * The values themselves will be {@code (v...)}.  When we speak of "parameter lists", we will usually
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5361
     * be referring to types, but in some contexts (describing execution) the lists will be of actual values.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5362
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5363
     * Some of these clause parts may be omitted according to certain rules, and useful default behavior is provided in
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5364
     * this case. See below for a detailed description.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5365
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5366
     * <em>Parameters optional everywhere:</em>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5367
     * Each clause function is allowed but not required to accept a parameter for each iteration variable {@code v}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5368
     * As an exception, the init functions cannot take any {@code v} parameters,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5369
     * because those values are not yet computed when the init functions are executed.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5370
     * Any clause function may neglect to take any trailing subsequence of parameters it is entitled to take.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5371
     * In fact, any clause function may take no arguments at all.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5372
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5373
     * <em>Loop parameters:</em>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5374
     * A clause function may take all the iteration variable values it is entitled to, in which case
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5375
     * it may also take more trailing parameters. Such extra values are called <em>loop parameters</em>,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5376
     * with their types and values notated as {@code (A...)} and {@code (a...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5377
     * These become the parameters of the resulting loop handle, to be supplied whenever the loop is executed.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5378
     * (Since init functions do not accept iteration variables {@code v}, any parameter to an
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5379
     * init function is automatically a loop parameter {@code a}.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5380
     * As with iteration variables, clause functions are allowed but not required to accept loop parameters.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5381
     * These loop parameters act as loop-invariant values visible across the whole loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5382
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5383
     * <em>Parameters visible everywhere:</em>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5384
     * Each non-init clause function is permitted to observe the entire loop state, because it can be passed the full
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5385
     * list {@code (v... a...)} of current iteration variable values and incoming loop parameters.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5386
     * The init functions can observe initial pre-loop state, in the form {@code (a...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5387
     * Most clause functions will not need all of this information, but they will be formally connected to it
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5388
     * as if by {@link #dropArguments}.
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44545
diff changeset
  5389
     * <a id="astar"></a>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5390
     * More specifically, we shall use the notation {@code (V*)} to express an arbitrary prefix of a full
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5391
     * sequence {@code (V...)} (and likewise for {@code (v*)}, {@code (A*)}, {@code (a*)}).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5392
     * In that notation, the general form of an init function parameter list
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5393
     * is {@code (A*)}, and the general form of a non-init function parameter list is {@code (V*)} or {@code (V... A*)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5394
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5395
     * <em>Checking clause structure:</em>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5396
     * Given a set of clauses, there is a number of checks and adjustments performed to connect all the parts of the
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5397
     * loop. They are spelled out in detail in the steps below. In these steps, every occurrence of the word "must"
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5398
     * corresponds to a place where {@link IllegalArgumentException} will be thrown if the required constraint is not
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5399
     * met by the inputs to the loop combinator.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5400
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5401
     * <em>Effectively identical sequences:</em>
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44545
diff changeset
  5402
     * <a id="effid"></a>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5403
     * A parameter list {@code A} is defined to be <em>effectively identical</em> to another parameter list {@code B}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5404
     * if {@code A} and {@code B} are identical, or if {@code A} is shorter and is identical with a proper prefix of {@code B}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5405
     * When speaking of an unordered set of parameter lists, we say they the set is "effectively identical"
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5406
     * as a whole if the set contains a longest list, and all members of the set are effectively identical to
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5407
     * that longest list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5408
     * For example, any set of type sequences of the form {@code (V*)} is effectively identical,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5409
     * and the same is true if more sequences of the form {@code (V... A*)} are added.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5410
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5411
     * <em>Step 0: Determine clause structure.</em><ol type="a">
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5412
     * <li>The clause array (of type {@code MethodHandle[][]}) must be non-{@code null} and contain at least one element.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5413
     * <li>The clause array may not contain {@code null}s or sub-arrays longer than four elements.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5414
     * <li>Clauses shorter than four elements are treated as if they were padded by {@code null} elements to length
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5415
     * four. Padding takes place by appending elements to the array.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5416
     * <li>Clauses with all {@code null}s are disregarded.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5417
     * <li>Each clause is treated as a four-tuple of functions, called "init", "step", "pred", and "fini".
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5418
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5419
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5420
     * <em>Step 1A: Determine iteration variable types {@code (V...)}.</em><ol type="a">
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5421
     * <li>The iteration variable type for each clause is determined using the clause's init and step return types.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5422
     * <li>If both functions are omitted, there is no iteration variable for the corresponding clause ({@code void} is
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5423
     * used as the type to indicate that). If one of them is omitted, the other's return type defines the clause's
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5424
     * iteration variable type. If both are given, the common return type (they must be identical) defines the clause's
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5425
     * iteration variable type.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5426
     * <li>Form the list of return types (in clause order), omitting all occurrences of {@code void}.
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5427
     * <li>This list of types is called the "iteration variable types" ({@code (V...)}).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5428
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5429
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5430
     * <em>Step 1B: Determine loop parameters {@code (A...)}.</em><ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5431
     * <li>Examine and collect init function parameter lists (which are of the form {@code (A*)}).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5432
     * <li>Examine and collect the suffixes of the step, pred, and fini parameter lists, after removing the iteration variable types.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5433
     * (They must have the form {@code (V... A*)}; collect the {@code (A*)} parts only.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5434
     * <li>Do not collect suffixes from step, pred, and fini parameter lists that do not begin with all the iteration variable types.
51986
c1db377f6300 8200381: Typos in javadoc - missing verb "be" and alike
igerasim
parents: 51798
diff changeset
  5435
     * (These types will be checked in step 2, along with all the clause function types.)
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5436
     * <li>Omitted clause functions are ignored.  (Equivalently, they are deemed to have empty parameter lists.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5437
     * <li>All of the collected parameter lists must be effectively identical.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5438
     * <li>The longest parameter list (which is necessarily unique) is called the "external parameter list" ({@code (A...)}).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5439
     * <li>If there is no such parameter list, the external parameter list is taken to be the empty sequence.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5440
     * <li>The combined list consisting of iteration variable types followed by the external parameter types is called
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5441
     * the "internal parameter list".
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5442
     * </ul>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5443
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5444
     * <em>Step 1C: Determine loop return type.</em><ol type="a">
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5445
     * <li>Examine fini function return types, disregarding omitted fini functions.
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5446
     * <li>If there are no fini functions, the loop return type is {@code void}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5447
     * <li>Otherwise, the common return type {@code R} of the fini functions (their return types must be identical) defines the loop return
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5448
     * type.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5449
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5450
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5451
     * <em>Step 1D: Check other types.</em><ol type="a">
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5452
     * <li>There must be at least one non-omitted pred function.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5453
     * <li>Every non-omitted pred function must have a {@code boolean} return type.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5454
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5455
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5456
     * <em>Step 2: Determine parameter lists.</em><ol type="a">
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5457
     * <li>The parameter list for the resulting loop handle will be the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5458
     * <li>The parameter list for init functions will be adjusted to the external parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5459
     * (Note that their parameter lists are already effectively identical to this list.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5460
     * <li>The parameter list for every non-omitted, non-init (step, pred, and fini) function must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5461
     * effectively identical to the internal parameter list {@code (V... A...)}.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5462
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5463
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5464
     * <em>Step 3: Fill in omitted functions.</em><ol type="a">
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5465
     * <li>If an init function is omitted, use a {@linkplain #empty default value} for the clause's iteration variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5466
     * type.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5467
     * <li>If a step function is omitted, use an {@linkplain #identity identity function} of the clause's iteration
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5468
     * variable type; insert dropped argument parameters before the identity function parameter for the non-{@code void}
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5469
     * iteration variables of preceding clauses. (This will turn the loop variable into a local loop invariant.)
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5470
     * <li>If a pred function is omitted, use a constant {@code true} function. (This will keep the loop going, as far
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5471
     * as this clause is concerned.  Note that in such cases the corresponding fini function is unreachable.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5472
     * <li>If a fini function is omitted, use a {@linkplain #empty default value} for the
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5473
     * loop return type.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5474
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5475
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5476
     * <em>Step 4: Fill in missing parameter types.</em><ol type="a">
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5477
     * <li>At this point, every init function parameter list is effectively identical to the external parameter list {@code (A...)},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5478
     * but some lists may be shorter. For every init function with a short parameter list, pad out the end of the list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5479
     * <li>At this point, every non-init function parameter list is effectively identical to the internal parameter
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5480
     * list {@code (V... A...)}, but some lists may be shorter. For every non-init function with a short parameter list,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5481
     * pad out the end of the list.
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46148
diff changeset
  5482
     * <li>Argument lists are padded out by {@linkplain #dropArgumentsToMatch(MethodHandle, int, List, int) dropping unused trailing arguments}.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5483
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5484
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5485
     * <em>Final observations.</em><ol type="a">
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5486
     * <li>After these steps, all clauses have been adjusted by supplying omitted functions and arguments.
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5487
     * <li>All init functions have a common parameter type list {@code (A...)}, which the final loop handle will also have.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5488
     * <li>All fini functions have a common return type {@code R}, which the final loop handle will also have.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5489
     * <li>All non-init functions have a common parameter type list {@code (V... A...)}, of
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5490
     * (non-{@code void}) iteration variables {@code V} followed by loop parameters.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5491
     * <li>Each pair of init and step functions agrees in their return type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5492
     * <li>Each non-init function will be able to observe the current values {@code (v...)} of all iteration variables.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5493
     * <li>Every function will be able to observe the incoming values {@code (a...)} of all loop parameters.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5494
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5495
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5496
     * <em>Example.</em> As a consequence of step 1A above, the {@code loop} combinator has the following property:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5497
     * <ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5498
     * <li>Given {@code N} clauses {@code Cn = {null, Sn, Pn}} with {@code n = 1..N}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5499
     * <li>Suppose predicate handles {@code Pn} are either {@code null} or have no parameters.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5500
     * (Only one {@code Pn} has to be non-{@code null}.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5501
     * <li>Suppose step handles {@code Sn} have signatures {@code (B1..BX)Rn}, for some constant {@code X>=N}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5502
     * <li>Suppose {@code Q} is the count of non-void types {@code Rn}, and {@code (V1...VQ)} is the sequence of those types.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5503
     * <li>It must be that {@code Vn == Bn} for {@code n = 1..min(X,Q)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5504
     * <li>The parameter types {@code Vn} will be interpreted as loop-local state elements {@code (V...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5505
     * <li>Any remaining types {@code BQ+1..BX} (if {@code Q<X}) will determine
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5506
     * the resulting loop handle's parameter types {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5507
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5508
     * In this example, the loop handle parameters {@code (A...)} were derived from the step functions,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5509
     * which is natural if most of the loop computation happens in the steps.  For some loops,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5510
     * the burden of computation might be heaviest in the pred functions, and so the pred functions
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5511
     * might need to accept the loop parameter values.  For loops with complex exit logic, the fini
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5512
     * functions might need to accept loop parameters, and likewise for loops with complex entry logic,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5513
     * where the init functions will need the extra parameters.  For such reasons, the rules for
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5514
     * determining these parameters are as symmetric as possible, across all clause parts.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5515
     * In general, the loop parameters function as common invariant values across the whole
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5516
     * loop, while the iteration variables function as common variant values, or (if there is
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5517
     * no step function) as internal loop invariant temporaries.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5518
     * <p>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5519
     * <em>Loop execution.</em><ol type="a">
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5520
     * <li>When the loop is called, the loop input values are saved in locals, to be passed to
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5521
     * every clause function. These locals are loop invariant.
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5522
     * <li>Each init function is executed in clause order (passing the external arguments {@code (a...)})
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5523
     * and the non-{@code void} values are saved (as the iteration variables {@code (v...)}) into locals.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5524
     * These locals will be loop varying (unless their steps behave as identity functions, as noted above).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5525
     * <li>All function executions (except init functions) will be passed the internal parameter list, consisting of
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5526
     * the non-{@code void} iteration values {@code (v...)} (in clause order) and then the loop inputs {@code (a...)}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5527
     * (in argument order).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5528
     * <li>The step and pred functions are then executed, in clause order (step before pred), until a pred function
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5529
     * returns {@code false}.
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5530
     * <li>The non-{@code void} result from a step function call is used to update the corresponding value in the
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5531
     * sequence {@code (v...)} of loop variables.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5532
     * The updated value is immediately visible to all subsequent function calls.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5533
     * <li>If a pred function returns {@code false}, the corresponding fini function is called, and the resulting value
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5534
     * (of type {@code R}) is returned from the loop as a whole.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5535
     * <li>If all the pred functions always return true, no fini function is ever invoked, and the loop cannot exit
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5536
     * except by throwing an exception.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5537
     * </ol>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5538
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5539
     * <em>Usage tips.</em>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5540
     * <ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5541
     * <li>Although each step function will receive the current values of <em>all</em> the loop variables,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5542
     * sometimes a step function only needs to observe the current value of its own variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5543
     * In that case, the step function may need to explicitly {@linkplain #dropArguments drop all preceding loop variables}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5544
     * This will require mentioning their types, in an expression like {@code dropArguments(step, 0, V0.class, ...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5545
     * <li>Loop variables are not required to vary; they can be loop invariant.  A clause can create
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5546
     * a loop invariant by a suitable init function with no step, pred, or fini function.  This may be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5547
     * useful to "wire" an incoming loop argument into the step or pred function of an adjacent loop variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5548
     * <li>If some of the clause functions are virtual methods on an instance, the instance
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5549
     * itself can be conveniently placed in an initial invariant loop "variable", using an initial clause
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5550
     * like {@code new MethodHandle[]{identity(ObjType.class)}}.  In that case, the instance reference
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5551
     * will be the first iteration variable value, and it will be easy to use virtual
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5552
     * methods as clause parts, since all of them will take a leading instance reference matching that value.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5553
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5554
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5555
     * Here is pseudocode for the resulting loop handle. As above, {@code V} and {@code v} represent the types
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5556
     * and values of loop variables; {@code A} and {@code a} represent arguments passed to the whole loop;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5557
     * and {@code R} is the common result type of all finalizers as well as of the resulting loop.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5558
     * <blockquote><pre>{@code
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5559
     * V... init...(A...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5560
     * boolean pred...(V..., A...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5561
     * V... step...(V..., A...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5562
     * R fini...(V..., A...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5563
     * R loop(A... a) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5564
     *   V... v... = init...(a...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5565
     *   for (;;) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5566
     *     for ((v, p, s, f) in (v..., pred..., step..., fini...)) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5567
     *       v = s(v..., a...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5568
     *       if (!p(v..., a...)) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5569
     *         return f(v..., a...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5570
     *       }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5571
     *     }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5572
     *   }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5573
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5574
     * }</pre></blockquote>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5575
     * Note that the parameter type lists {@code (V...)} and {@code (A...)} have been expanded
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5576
     * to their full length, even though individual clause functions may neglect to take them all.
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46148
diff changeset
  5577
     * As noted above, missing parameters are filled in as if by {@link #dropArgumentsToMatch(MethodHandle, int, List, int)}.
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  5578
     *
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5579
     * @apiNote Example:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5580
     * <blockquote><pre>{@code
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5581
     * // iterative implementation of the factorial function as a loop handle
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5582
     * static int one(int k) { return 1; }
36220
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5583
     * static int inc(int i, int acc, int k) { return i + 1; }
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5584
     * static int mult(int i, int acc, int k) { return i * acc; }
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5585
     * static boolean pred(int i, int acc, int k) { return i < k; }
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5586
     * static int fin(int i, int acc, int k) { return acc; }
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5587
     * // assume MH_one, MH_inc, MH_mult, MH_pred, and MH_fin are handles to the above methods
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5588
     * // null initializer for counter, should initialize to 0
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5589
     * MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5590
     * MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5591
     * MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5592
     * assertEquals(120, loop.invoke(5));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5593
     * }</pre></blockquote>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5594
     * The same example, dropping arguments and using combinators:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5595
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5596
     * // simplified implementation of the factorial function as a loop handle
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5597
     * static int inc(int i) { return i + 1; } // drop acc, k
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5598
     * static int mult(int i, int acc) { return i * acc; } //drop k
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5599
     * static boolean cmp(int i, int k) { return i < k; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5600
     * // assume MH_inc, MH_mult, and MH_cmp are handles to the above methods
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5601
     * // null initializer for counter, should initialize to 0
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5602
     * MethodHandle MH_one = MethodHandles.constant(int.class, 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5603
     * MethodHandle MH_pred = MethodHandles.dropArguments(MH_cmp, 1, int.class); // drop acc
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5604
     * MethodHandle MH_fin = MethodHandles.dropArguments(MethodHandles.identity(int.class), 0, int.class); // drop i
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5605
     * MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5606
     * MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5607
     * MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5608
     * assertEquals(720, loop.invoke(6));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5609
     * }</pre></blockquote>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5610
     * A similar example, using a helper object to hold a loop parameter:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5611
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5612
     * // instance-based implementation of the factorial function as a loop handle
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5613
     * static class FacLoop {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5614
     *   final int k;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5615
     *   FacLoop(int k) { this.k = k; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5616
     *   int inc(int i) { return i + 1; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5617
     *   int mult(int i, int acc) { return i * acc; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5618
     *   boolean pred(int i) { return i < k; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5619
     *   int fin(int i, int acc) { return acc; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5620
     * }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5621
     * // assume MH_FacLoop is a handle to the constructor
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5622
     * // assume MH_inc, MH_mult, MH_pred, and MH_fin are handles to the above methods
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5623
     * // null initializer for counter, should initialize to 0
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5624
     * MethodHandle MH_one = MethodHandles.constant(int.class, 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5625
     * MethodHandle[] instanceClause = new MethodHandle[]{MH_FacLoop};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5626
     * MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5627
     * MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5628
     * MethodHandle loop = MethodHandles.loop(instanceClause, counterClause, accumulatorClause);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5629
     * assertEquals(5040, loop.invoke(7));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5630
     * }</pre></blockquote>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5631
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5632
     * @param clauses an array of arrays (4-tuples) of {@link MethodHandle}s adhering to the rules described above.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5633
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5634
     * @return a method handle embodying the looping behavior as defined by the arguments.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5635
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5636
     * @throws IllegalArgumentException in case any of the constraints described above is violated.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5637
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5638
     * @see MethodHandles#whileLoop(MethodHandle, MethodHandle, MethodHandle)
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5639
     * @see MethodHandles#doWhileLoop(MethodHandle, MethodHandle, MethodHandle)
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5640
     * @see MethodHandles#countedLoop(MethodHandle, MethodHandle, MethodHandle)
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5641
     * @see MethodHandles#iteratedLoop(MethodHandle, MethodHandle, MethodHandle)
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5642
     * @since 9
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5643
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5644
    public static MethodHandle loop(MethodHandle[]... clauses) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5645
        // Step 0: determine clause structure.
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5646
        loopChecks0(clauses);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5647
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5648
        List<MethodHandle> init = new ArrayList<>();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5649
        List<MethodHandle> step = new ArrayList<>();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5650
        List<MethodHandle> pred = new ArrayList<>();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5651
        List<MethodHandle> fini = new ArrayList<>();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5652
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5653
        Stream.of(clauses).filter(c -> Stream.of(c).anyMatch(Objects::nonNull)).forEach(clause -> {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5654
            init.add(clause[0]); // all clauses have at least length 1
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5655
            step.add(clause.length <= 1 ? null : clause[1]);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5656
            pred.add(clause.length <= 2 ? null : clause[2]);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5657
            fini.add(clause.length <= 3 ? null : clause[3]);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5658
        });
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5659
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5660
        assert Stream.of(init, step, pred, fini).map(List::size).distinct().count() == 1;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5661
        final int nclauses = init.size();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5662
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5663
        // Step 1A: determine iteration variables (V...).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5664
        final List<Class<?>> iterationVariableTypes = new ArrayList<>();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5665
        for (int i = 0; i < nclauses; ++i) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5666
            MethodHandle in = init.get(i);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5667
            MethodHandle st = step.get(i);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5668
            if (in == null && st == null) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5669
                iterationVariableTypes.add(void.class);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5670
            } else if (in != null && st != null) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5671
                loopChecks1a(i, in, st);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5672
                iterationVariableTypes.add(in.type().returnType());
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5673
            } else {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5674
                iterationVariableTypes.add(in == null ? st.type().returnType() : in.type().returnType());
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5675
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5676
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5677
        final List<Class<?>> commonPrefix = iterationVariableTypes.stream().filter(t -> t != void.class).
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5678
                collect(Collectors.toList());
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5679
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5680
        // Step 1B: determine loop parameters (A...).
36220
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5681
        final List<Class<?>> commonSuffix = buildCommonSuffix(init, step, pred, fini, commonPrefix.size());
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5682
        loopChecks1b(init, commonSuffix);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5683
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5684
        // Step 1C: determine loop return type.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5685
        // Step 1D: check other types.
55022
9785b9fb328e 8223553: Fix code constructs that do not compile with the Eclipse Java Compiler
clanger
parents: 54295
diff changeset
  5686
        // local variable required here; see JDK-8223553
9785b9fb328e 8223553: Fix code constructs that do not compile with the Eclipse Java Compiler
clanger
parents: 54295
diff changeset
  5687
        Stream<Class<?>> cstream = fini.stream().filter(Objects::nonNull).map(MethodHandle::type)
9785b9fb328e 8223553: Fix code constructs that do not compile with the Eclipse Java Compiler
clanger
parents: 54295
diff changeset
  5688
                .map(MethodType::returnType);
9785b9fb328e 8223553: Fix code constructs that do not compile with the Eclipse Java Compiler
clanger
parents: 54295
diff changeset
  5689
        final Class<?> loopReturnType = cstream.findFirst().orElse(void.class);
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5690
        loopChecks1cd(pred, fini, loopReturnType);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5691
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5692
        // Step 2: determine parameter lists.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5693
        final List<Class<?>> commonParameterSequence = new ArrayList<>(commonPrefix);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5694
        commonParameterSequence.addAll(commonSuffix);
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5695
        loopChecks2(step, pred, fini, commonParameterSequence);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5696
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5697
        // Step 3: fill in omitted functions.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5698
        for (int i = 0; i < nclauses; ++i) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5699
            Class<?> t = iterationVariableTypes.get(i);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5700
            if (init.get(i) == null) {
37539
fc220bc54b59 8150956: j.l.i.MethodHandles.whileLoop(...) and .iteratedLoop(...) throw unexpected exceptions in the case of 'init' return type is void
mhaupt
parents: 37363
diff changeset
  5701
                init.set(i, empty(methodType(t, commonSuffix)));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5702
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5703
            if (step.get(i) == null) {
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  5704
                step.set(i, dropArgumentsToMatch(identityOrVoid(t), 0, commonParameterSequence, i));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5705
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5706
            if (pred.get(i) == null) {
39755
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  5707
                pred.set(i, dropArguments0(constant(boolean.class, true), 0, commonParameterSequence));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5708
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5709
            if (fini.get(i) == null) {
37539
fc220bc54b59 8150956: j.l.i.MethodHandles.whileLoop(...) and .iteratedLoop(...) throw unexpected exceptions in the case of 'init' return type is void
mhaupt
parents: 37363
diff changeset
  5710
                fini.set(i, empty(methodType(t, commonParameterSequence)));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5711
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5712
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5713
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5714
        // Step 4: fill in missing parameter types.
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5715
        // Also convert all handles to fixed-arity handles.
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5716
        List<MethodHandle> finit = fixArities(fillParameterTypes(init, commonSuffix));
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5717
        List<MethodHandle> fstep = fixArities(fillParameterTypes(step, commonParameterSequence));
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5718
        List<MethodHandle> fpred = fixArities(fillParameterTypes(pred, commonParameterSequence));
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5719
        List<MethodHandle> ffini = fixArities(fillParameterTypes(fini, commonParameterSequence));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5720
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5721
        assert finit.stream().map(MethodHandle::type).map(MethodType::parameterList).
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5722
                allMatch(pl -> pl.equals(commonSuffix));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5723
        assert Stream.of(fstep, fpred, ffini).flatMap(List::stream).map(MethodHandle::type).map(MethodType::parameterList).
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5724
                allMatch(pl -> pl.equals(commonParameterSequence));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5725
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38861
diff changeset
  5726
        return MethodHandleImpl.makeLoop(loopReturnType, commonSuffix, finit, fstep, fpred, ffini);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5727
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5728
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5729
    private static void loopChecks0(MethodHandle[][] clauses) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5730
        if (clauses == null || clauses.length == 0) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5731
            throw newIllegalArgumentException("null or no clauses passed");
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5732
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5733
        if (Stream.of(clauses).anyMatch(Objects::isNull)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5734
            throw newIllegalArgumentException("null clauses are not allowed");
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5735
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5736
        if (Stream.of(clauses).anyMatch(c -> c.length > 4)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5737
            throw newIllegalArgumentException("All loop clauses must be represented as MethodHandle arrays with at most 4 elements.");
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5738
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5739
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5740
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5741
    private static void loopChecks1a(int i, MethodHandle in, MethodHandle st) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5742
        if (in.type().returnType() != st.type().returnType()) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5743
            throw misMatchedTypes("clause " + i + ": init and step return types", in.type().returnType(),
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5744
                    st.type().returnType());
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5745
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5746
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5747
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5748
    private static List<Class<?>> longestParameterList(Stream<MethodHandle> mhs, int skipSize) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5749
        final List<Class<?>> empty = List.of();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5750
        final List<Class<?>> longest = mhs.filter(Objects::nonNull).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5751
                // take only those that can contribute to a common suffix because they are longer than the prefix
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5752
                        map(MethodHandle::type).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5753
                        filter(t -> t.parameterCount() > skipSize).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5754
                        map(MethodType::parameterList).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5755
                        reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5756
        return longest.size() == 0 ? empty : longest.subList(skipSize, longest.size());
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5757
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5758
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5759
    private static List<Class<?>> longestParameterList(List<List<Class<?>>> lists) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5760
        final List<Class<?>> empty = List.of();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5761
        return lists.stream().reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5762
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5763
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5764
    private static List<Class<?>> buildCommonSuffix(List<MethodHandle> init, List<MethodHandle> step, List<MethodHandle> pred, List<MethodHandle> fini, int cpSize) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5765
        final List<Class<?>> longest1 = longestParameterList(Stream.of(step, pred, fini).flatMap(List::stream), cpSize);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5766
        final List<Class<?>> longest2 = longestParameterList(init.stream(), 0);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5767
        return longestParameterList(Arrays.asList(longest1, longest2));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5768
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5769
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5770
    private static void loopChecks1b(List<MethodHandle> init, List<Class<?>> commonSuffix) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5771
        if (init.stream().filter(Objects::nonNull).map(MethodHandle::type).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5772
                anyMatch(t -> !t.effectivelyIdenticalParameters(0, commonSuffix))) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5773
            throw newIllegalArgumentException("found non-effectively identical init parameter type lists: " + init +
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5774
                    " (common suffix: " + commonSuffix + ")");
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5775
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5776
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5777
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5778
    private static void loopChecks1cd(List<MethodHandle> pred, List<MethodHandle> fini, Class<?> loopReturnType) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5779
        if (fini.stream().filter(Objects::nonNull).map(MethodHandle::type).map(MethodType::returnType).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5780
                anyMatch(t -> t != loopReturnType)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5781
            throw newIllegalArgumentException("found non-identical finalizer return types: " + fini + " (return type: " +
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5782
                    loopReturnType + ")");
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5783
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5784
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5785
        if (!pred.stream().filter(Objects::nonNull).findFirst().isPresent()) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5786
            throw newIllegalArgumentException("no predicate found", pred);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5787
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5788
        if (pred.stream().filter(Objects::nonNull).map(MethodHandle::type).map(MethodType::returnType).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5789
                anyMatch(t -> t != boolean.class)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5790
            throw newIllegalArgumentException("predicates must have boolean return type", pred);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5791
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5792
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5793
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5794
    private static void loopChecks2(List<MethodHandle> step, List<MethodHandle> pred, List<MethodHandle> fini, List<Class<?>> commonParameterSequence) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5795
        if (Stream.of(step, pred, fini).flatMap(List::stream).filter(Objects::nonNull).map(MethodHandle::type).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5796
                anyMatch(t -> !t.effectivelyIdenticalParameters(0, commonParameterSequence))) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5797
            throw newIllegalArgumentException("found non-effectively identical parameter type lists:\nstep: " + step +
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5798
                    "\npred: " + pred + "\nfini: " + fini + " (common parameter sequence: " + commonParameterSequence + ")");
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5799
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5800
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5801
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5802
    private static List<MethodHandle> fillParameterTypes(List<MethodHandle> hs, final List<Class<?>> targetParams) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5803
        return hs.stream().map(h -> {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5804
            int pc = h.type().parameterCount();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5805
            int tpsize = targetParams.size();
39755
3924e70f9f08 8155985: Persistent Parameter Processing
mhaupt
parents: 39754
diff changeset
  5806
            return pc < tpsize ? dropArguments0(h, pc, targetParams.subList(pc, tpsize)) : h;
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5807
        }).collect(Collectors.toList());
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5808
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5809
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5810
    private static List<MethodHandle> fixArities(List<MethodHandle> hs) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5811
        return hs.stream().map(MethodHandle::asFixedArity).collect(Collectors.toList());
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5812
    }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  5813
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5814
    /**
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5815
     * Constructs a {@code while} loop from an initializer, a body, and a predicate.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5816
     * This is a convenience wrapper for the {@linkplain #loop(MethodHandle[][]) generic loop combinator}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5817
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5818
     * The {@code pred} handle describes the loop condition; and {@code body}, its body. The loop resulting from this
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5819
     * method will, in each iteration, first evaluate the predicate and then execute its body (if the predicate
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5820
     * evaluates to {@code true}).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5821
     * The loop will terminate once the predicate evaluates to {@code false} (the body will not be executed in this case).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5822
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5823
     * The {@code init} handle describes the initial value of an additional optional loop-local variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5824
     * In each iteration, this loop-local variable, if present, will be passed to the {@code body}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5825
     * and updated with the value returned from its invocation. The result of loop execution will be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5826
     * the final value of the additional loop-local variable (if present).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5827
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5828
     * The following rules hold for these argument handles:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5829
     * <li>The {@code body} handle must not be {@code null}; its type must be of the form
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5830
     * {@code (V A...)V}, where {@code V} is non-{@code void}, or else {@code (A...)void}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5831
     * (In the {@code void} case, we assign the type {@code void} to the name {@code V},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5832
     * and we will write {@code (V A...)V} with the understanding that a {@code void} type {@code V}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5833
     * is quietly dropped from the parameter list, leaving {@code (A...)V}.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5834
     * <li>The parameter list {@code (V A...)} of the body is called the <em>internal parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5835
     * It will constrain the parameter lists of the other loop parts.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5836
     * <li>If the iteration variable type {@code V} is dropped from the internal parameter list, the resulting shorter
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5837
     * list {@code (A...)} is called the <em>external parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5838
     * <li>The body return type {@code V}, if non-{@code void}, determines the type of an
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5839
     * additional state variable of the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5840
     * The body must both accept and return a value of this type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5841
     * <li>If {@code init} is non-{@code null}, it must have return type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5842
     * Its parameter list (of some <a href="MethodHandles.html#astar">form {@code (A*)}</a>) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5843
     * <a href="MethodHandles.html#effid">effectively identical</a>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5844
     * to the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5845
     * <li>If {@code init} is {@code null}, the loop variable will be initialized to its
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5846
     * {@linkplain #empty default value}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5847
     * <li>The {@code pred} handle must not be {@code null}.  It must have {@code boolean} as its return type.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5848
     * Its parameter list (either empty or of the form {@code (V A*)}) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5849
     * effectively identical to the internal parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5850
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5851
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5852
     * The resulting loop handle's result type and parameter signature are determined as follows:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5853
     * <li>The loop handle's result type is the result type {@code V} of the body.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5854
     * <li>The loop handle's parameter types are the types {@code (A...)},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5855
     * from the external parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5856
     * </ul>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5857
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5858
     * Here is pseudocode for the resulting loop handle. In the code, {@code V}/{@code v} represent the type / value of
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5859
     * the sole loop variable as well as the result type of the loop; and {@code A}/{@code a}, that of the argument
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5860
     * passed to the loop.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5861
     * <blockquote><pre>{@code
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5862
     * V init(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5863
     * boolean pred(V, A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5864
     * V body(V, A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5865
     * V whileLoop(A... a...) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5866
     *   V v = init(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5867
     *   while (pred(v, a...)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5868
     *     v = body(v, a...);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5869
     *   }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5870
     *   return v;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5871
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5872
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  5873
     *
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5874
     * @apiNote Example:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5875
     * <blockquote><pre>{@code
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5876
     * // implement the zip function for lists as a loop handle
36220
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5877
     * static List<String> initZip(Iterator<String> a, Iterator<String> b) { return new ArrayList<>(); }
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5878
     * static boolean zipPred(List<String> zip, Iterator<String> a, Iterator<String> b) { return a.hasNext() && b.hasNext(); }
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5879
     * static List<String> zipStep(List<String> zip, Iterator<String> a, Iterator<String> b) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5880
     *   zip.add(a.next());
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5881
     *   zip.add(b.next());
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5882
     *   return zip;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5883
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5884
     * // assume MH_initZip, MH_zipPred, and MH_zipStep are handles to the above methods
36218
f02215b8d857 8150953: j.l.i.MethodHandles: example section in whileLoop(...) provides example for doWhileLoop
mhaupt
parents: 36136
diff changeset
  5885
     * MethodHandle loop = MethodHandles.whileLoop(MH_initZip, MH_zipPred, MH_zipStep);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5886
     * List<String> a = Arrays.asList("a", "b", "c", "d");
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5887
     * List<String> b = Arrays.asList("e", "f", "g", "h");
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5888
     * List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h");
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5889
     * assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator()));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5890
     * }</pre></blockquote>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5891
     *
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  5892
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5893
     * @apiNote The implementation of this method can be expressed as follows:
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5894
     * <blockquote><pre>{@code
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5895
     * MethodHandle whileLoop(MethodHandle init, MethodHandle pred, MethodHandle body) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5896
     *     MethodHandle fini = (body.type().returnType() == void.class
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5897
     *                         ? null : identity(body.type().returnType()));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5898
     *     MethodHandle[]
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5899
     *         checkExit = { null, null, pred, fini },
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5900
     *         varBody   = { init, body };
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5901
     *     return loop(checkExit, varBody);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5902
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5903
     * }</pre></blockquote>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5904
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5905
     * @param init optional initializer, providing the initial value of the loop variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5906
     *             May be {@code null}, implying a default initial value.  See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5907
     * @param pred condition for the loop, which may not be {@code null}. Its result type must be {@code boolean}. See
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5908
     *             above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5909
     * @param body body of the loop, which may not be {@code null}. It controls the loop parameters and result type.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5910
     *             See above for other constraints.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5911
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5912
     * @return a method handle implementing the {@code while} loop as described by the arguments.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5913
     * @throws IllegalArgumentException if the rules for the arguments are violated.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5914
     * @throws NullPointerException if {@code pred} or {@code body} are {@code null}.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5915
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5916
     * @see #loop(MethodHandle[][])
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5917
     * @see #doWhileLoop(MethodHandle, MethodHandle, MethodHandle)
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5918
     * @since 9
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5919
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5920
    public static MethodHandle whileLoop(MethodHandle init, MethodHandle pred, MethodHandle body) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5921
        whileLoopChecks(init, pred, body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5922
        MethodHandle fini = identityOrVoid(body.type().returnType());
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5923
        MethodHandle[] checkExit = { null, null, pred, fini };
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5924
        MethodHandle[] varBody = { init, body };
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5925
        return loop(checkExit, varBody);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5926
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5927
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5928
    /**
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5929
     * Constructs a {@code do-while} loop from an initializer, a body, and a predicate.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5930
     * This is a convenience wrapper for the {@linkplain #loop(MethodHandle[][]) generic loop combinator}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5931
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5932
     * The {@code pred} handle describes the loop condition; and {@code body}, its body. The loop resulting from this
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5933
     * method will, in each iteration, first execute its body and then evaluate the predicate.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5934
     * The loop will terminate once the predicate evaluates to {@code false} after an execution of the body.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5935
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5936
     * The {@code init} handle describes the initial value of an additional optional loop-local variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5937
     * In each iteration, this loop-local variable, if present, will be passed to the {@code body}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5938
     * and updated with the value returned from its invocation. The result of loop execution will be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5939
     * the final value of the additional loop-local variable (if present).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5940
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5941
     * The following rules hold for these argument handles:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5942
     * <li>The {@code body} handle must not be {@code null}; its type must be of the form
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5943
     * {@code (V A...)V}, where {@code V} is non-{@code void}, or else {@code (A...)void}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5944
     * (In the {@code void} case, we assign the type {@code void} to the name {@code V},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5945
     * and we will write {@code (V A...)V} with the understanding that a {@code void} type {@code V}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5946
     * is quietly dropped from the parameter list, leaving {@code (A...)V}.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5947
     * <li>The parameter list {@code (V A...)} of the body is called the <em>internal parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5948
     * It will constrain the parameter lists of the other loop parts.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5949
     * <li>If the iteration variable type {@code V} is dropped from the internal parameter list, the resulting shorter
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5950
     * list {@code (A...)} is called the <em>external parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5951
     * <li>The body return type {@code V}, if non-{@code void}, determines the type of an
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5952
     * additional state variable of the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5953
     * The body must both accept and return a value of this type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5954
     * <li>If {@code init} is non-{@code null}, it must have return type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5955
     * Its parameter list (of some <a href="MethodHandles.html#astar">form {@code (A*)}</a>) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5956
     * <a href="MethodHandles.html#effid">effectively identical</a>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5957
     * to the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5958
     * <li>If {@code init} is {@code null}, the loop variable will be initialized to its
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5959
     * {@linkplain #empty default value}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5960
     * <li>The {@code pred} handle must not be {@code null}.  It must have {@code boolean} as its return type.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5961
     * Its parameter list (either empty or of the form {@code (V A*)}) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5962
     * effectively identical to the internal parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5963
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5964
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5965
     * The resulting loop handle's result type and parameter signature are determined as follows:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5966
     * <li>The loop handle's result type is the result type {@code V} of the body.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5967
     * <li>The loop handle's parameter types are the types {@code (A...)},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5968
     * from the external parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5969
     * </ul>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5970
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5971
     * Here is pseudocode for the resulting loop handle. In the code, {@code V}/{@code v} represent the type / value of
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5972
     * the sole loop variable as well as the result type of the loop; and {@code A}/{@code a}, that of the argument
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5973
     * passed to the loop.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5974
     * <blockquote><pre>{@code
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5975
     * V init(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5976
     * boolean pred(V, A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5977
     * V body(V, A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5978
     * V doWhileLoop(A... a...) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5979
     *   V v = init(a...);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5980
     *   do {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5981
     *     v = body(v, a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5982
     *   } while (pred(v, a...));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5983
     *   return v;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5984
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5985
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  5986
     *
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5987
     * @apiNote Example:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5988
     * <blockquote><pre>{@code
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5989
     * // int i = 0; while (i < limit) { ++i; } return i; => limit
36220
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5990
     * static int zero(int limit) { return 0; }
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5991
     * static int step(int i, int limit) { return i + 1; }
32754521e67e 8150635: j.l.i.MethodHandles.loop(...) throws IndexOutOfBoundsException
mhaupt
parents: 36218
diff changeset
  5992
     * static boolean pred(int i, int limit) { return i < limit; }
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5993
     * // assume MH_zero, MH_step, and MH_pred are handles to the above methods
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5994
     * MethodHandle loop = MethodHandles.doWhileLoop(MH_zero, MH_step, MH_pred);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5995
     * assertEquals(23, loop.invoke(23));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5996
     * }</pre></blockquote>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  5997
     *
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  5998
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  5999
     * @apiNote The implementation of this method can be expressed as follows:
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6000
     * <blockquote><pre>{@code
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6001
     * MethodHandle doWhileLoop(MethodHandle init, MethodHandle body, MethodHandle pred) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6002
     *     MethodHandle fini = (body.type().returnType() == void.class
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6003
     *                         ? null : identity(body.type().returnType()));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6004
     *     MethodHandle[] clause = { init, body, pred, fini };
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6005
     *     return loop(clause);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6006
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6007
     * }</pre></blockquote>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6008
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6009
     * @param init optional initializer, providing the initial value of the loop variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6010
     *             May be {@code null}, implying a default initial value.  See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6011
     * @param body body of the loop, which may not be {@code null}. It controls the loop parameters and result type.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6012
     *             See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6013
     * @param pred condition for the loop, which may not be {@code null}. Its result type must be {@code boolean}. See
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6014
     *             above for other constraints.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6015
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6016
     * @return a method handle implementing the {@code while} loop as described by the arguments.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6017
     * @throws IllegalArgumentException if the rules for the arguments are violated.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6018
     * @throws NullPointerException if {@code pred} or {@code body} are {@code null}.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6019
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6020
     * @see #loop(MethodHandle[][])
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6021
     * @see #whileLoop(MethodHandle, MethodHandle, MethodHandle)
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6022
     * @since 9
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6023
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6024
    public static MethodHandle doWhileLoop(MethodHandle init, MethodHandle body, MethodHandle pred) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6025
        whileLoopChecks(init, pred, body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6026
        MethodHandle fini = identityOrVoid(body.type().returnType());
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6027
        MethodHandle[] clause = {init, body, pred, fini };
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6028
        return loop(clause);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6029
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6030
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6031
    private static void whileLoopChecks(MethodHandle init, MethodHandle pred, MethodHandle body) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6032
        Objects.requireNonNull(pred);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6033
        Objects.requireNonNull(body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6034
        MethodType bodyType = body.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6035
        Class<?> returnType = bodyType.returnType();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6036
        List<Class<?>> innerList = bodyType.parameterList();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6037
        List<Class<?>> outerList = innerList;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6038
        if (returnType == void.class) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6039
            // OK
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6040
        } else if (innerList.size() == 0 || innerList.get(0) != returnType) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6041
            // leading V argument missing => error
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6042
            MethodType expected = bodyType.insertParameterTypes(0, returnType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6043
            throw misMatchedTypes("body function", bodyType, expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6044
        } else {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6045
            outerList = innerList.subList(1, innerList.size());
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6046
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6047
        MethodType predType = pred.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6048
        if (predType.returnType() != boolean.class ||
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6049
                !predType.effectivelyIdenticalParameters(0, innerList)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6050
            throw misMatchedTypes("loop predicate", predType, methodType(boolean.class, innerList));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6051
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6052
        if (init != null) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6053
            MethodType initType = init.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6054
            if (initType.returnType() != returnType ||
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6055
                    !initType.effectivelyIdenticalParameters(0, outerList)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6056
                throw misMatchedTypes("loop initializer", initType, methodType(returnType, outerList));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6057
            }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6058
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6059
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6060
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6061
    /**
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6062
     * Constructs a loop that runs a given number of iterations.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6063
     * This is a convenience wrapper for the {@linkplain #loop(MethodHandle[][]) generic loop combinator}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6064
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6065
     * The number of iterations is determined by the {@code iterations} handle evaluation result.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6066
     * The loop counter {@code i} is an extra loop iteration variable of type {@code int}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6067
     * It will be initialized to 0 and incremented by 1 in each iteration.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6068
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6069
     * If the {@code body} handle returns a non-{@code void} type {@code V}, a leading loop iteration variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6070
     * of that type is also present.  This variable is initialized using the optional {@code init} handle,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6071
     * or to the {@linkplain #empty default value} of type {@code V} if that handle is {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6072
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6073
     * In each iteration, the iteration variables are passed to an invocation of the {@code body} handle.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6074
     * A non-{@code void} value returned from the body (of type {@code V}) updates the leading
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6075
     * iteration variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6076
     * The result of the loop handle execution will be the final {@code V} value of that variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6077
     * (or {@code void} if there is no {@code V} variable).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6078
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6079
     * The following rules hold for the argument handles:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6080
     * <li>The {@code iterations} handle must not be {@code null}, and must return
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6081
     * the type {@code int}, referred to here as {@code I} in parameter type lists.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6082
     * <li>The {@code body} handle must not be {@code null}; its type must be of the form
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6083
     * {@code (V I A...)V}, where {@code V} is non-{@code void}, or else {@code (I A...)void}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6084
     * (In the {@code void} case, we assign the type {@code void} to the name {@code V},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6085
     * and we will write {@code (V I A...)V} with the understanding that a {@code void} type {@code V}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6086
     * is quietly dropped from the parameter list, leaving {@code (I A...)V}.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6087
     * <li>The parameter list {@code (V I A...)} of the body contributes to a list
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6088
     * of types called the <em>internal parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6089
     * It will constrain the parameter lists of the other loop parts.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6090
     * <li>As a special case, if the body contributes only {@code V} and {@code I} types,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6091
     * with no additional {@code A} types, then the internal parameter list is extended by
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6092
     * the argument types {@code A...} of the {@code iterations} handle.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6093
     * <li>If the iteration variable types {@code (V I)} are dropped from the internal parameter list, the resulting shorter
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6094
     * list {@code (A...)} is called the <em>external parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6095
     * <li>The body return type {@code V}, if non-{@code void}, determines the type of an
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6096
     * additional state variable of the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6097
     * The body must both accept a leading parameter and return a value of this type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6098
     * <li>If {@code init} is non-{@code null}, it must have return type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6099
     * Its parameter list (of some <a href="MethodHandles.html#astar">form {@code (A*)}</a>) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6100
     * <a href="MethodHandles.html#effid">effectively identical</a>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6101
     * to the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6102
     * <li>If {@code init} is {@code null}, the loop variable will be initialized to its
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6103
     * {@linkplain #empty default value}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6104
     * <li>The parameter list of {@code iterations} (of some form {@code (A*)}) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6105
     * effectively identical to the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6106
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6107
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6108
     * The resulting loop handle's result type and parameter signature are determined as follows:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6109
     * <li>The loop handle's result type is the result type {@code V} of the body.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6110
     * <li>The loop handle's parameter types are the types {@code (A...)},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6111
     * from the external parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6112
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6113
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6114
     * Here is pseudocode for the resulting loop handle. In the code, {@code V}/{@code v} represent the type / value of
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6115
     * the second loop variable as well as the result type of the loop; and {@code A...}/{@code a...} represent
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6116
     * arguments passed to the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6117
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6118
     * int iterations(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6119
     * V init(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6120
     * V body(V, int, A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6121
     * V countedLoop(A... a...) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6122
     *   int end = iterations(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6123
     *   V v = init(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6124
     *   for (int i = 0; i < end; ++i) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6125
     *     v = body(v, i, a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6126
     *   }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6127
     *   return v;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6128
     * }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6129
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  6130
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6131
     * @apiNote Example with a fully conformant body method:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6132
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6133
     * // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6134
     * // => a variation on a well known theme
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6135
     * static String step(String v, int counter, String init) { return "na " + v; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6136
     * // assume MH_step is a handle to the method above
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6137
     * MethodHandle fit13 = MethodHandles.constant(int.class, 13);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6138
     * MethodHandle start = MethodHandles.identity(String.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6139
     * MethodHandle loop = MethodHandles.countedLoop(fit13, start, MH_step);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6140
     * assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6141
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  6142
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6143
     * @apiNote Example with the simplest possible body method type,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6144
     * and passing the number of iterations to the loop invocation:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6145
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6146
     * // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6147
     * // => a variation on a well known theme
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6148
     * static String step(String v, int counter ) { return "na " + v; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6149
     * // assume MH_step is a handle to the method above
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6150
     * MethodHandle count = MethodHandles.dropArguments(MethodHandles.identity(int.class), 1, String.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6151
     * MethodHandle start = MethodHandles.dropArguments(MethodHandles.identity(String.class), 0, int.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6152
     * MethodHandle loop = MethodHandles.countedLoop(count, start, MH_step);  // (v, i) -> "na " + v
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6153
     * assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke(13, "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6154
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  6155
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6156
     * @apiNote Example that treats the number of iterations, string to append to, and string to append
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6157
     * as loop parameters:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6158
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6159
     * // String s = "Lambdaman!", t = "na"; for (int i = 0; i < 13; ++i) { s = t + " " + s; } return s;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6160
     * // => a variation on a well known theme
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6161
     * static String step(String v, int counter, int iterations_, String pre, String start_) { return pre + " " + v; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6162
     * // assume MH_step is a handle to the method above
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6163
     * MethodHandle count = MethodHandles.identity(int.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6164
     * MethodHandle start = MethodHandles.dropArguments(MethodHandles.identity(String.class), 0, int.class, String.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6165
     * MethodHandle loop = MethodHandles.countedLoop(count, start, MH_step);  // (v, i, _, pre, _) -> pre + " " + v
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6166
     * assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke(13, "na", "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6167
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  6168
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6169
     * @apiNote Example that illustrates the usage of {@link #dropArgumentsToMatch(MethodHandle, int, List, int)}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6170
     * to enforce a loop type:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6171
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6172
     * // String s = "Lambdaman!", t = "na"; for (int i = 0; i < 13; ++i) { s = t + " " + s; } return s;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6173
     * // => a variation on a well known theme
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6174
     * static String step(String v, int counter, String pre) { return pre + " " + v; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6175
     * // assume MH_step is a handle to the method above
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6176
     * MethodType loopType = methodType(String.class, String.class, int.class, String.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6177
     * MethodHandle count = MethodHandles.dropArgumentsToMatch(MethodHandles.identity(int.class),    0, loopType.parameterList(), 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6178
     * MethodHandle start = MethodHandles.dropArgumentsToMatch(MethodHandles.identity(String.class), 0, loopType.parameterList(), 2);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6179
     * MethodHandle body  = MethodHandles.dropArgumentsToMatch(MH_step,                              2, loopType.parameterList(), 0);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6180
     * MethodHandle loop = MethodHandles.countedLoop(count, start, body);  // (v, i, pre, _, _) -> pre + " " + v
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6181
     * assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("na", 13, "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6182
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  6183
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6184
     * @apiNote The implementation of this method can be expressed as follows:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6185
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6186
     * MethodHandle countedLoop(MethodHandle iterations, MethodHandle init, MethodHandle body) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6187
     *     return countedLoop(empty(iterations.type()), iterations, init, body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6188
     * }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6189
     * }</pre></blockquote>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6190
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6191
     * @param iterations a non-{@code null} handle to return the number of iterations this loop should run. The handle's
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6192
     *                   result type must be {@code int}. See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6193
     * @param init optional initializer, providing the initial value of the loop variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6194
     *             May be {@code null}, implying a default initial value.  See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6195
     * @param body body of the loop, which may not be {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6196
     *             It controls the loop parameters and result type in the standard case (see above for details).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6197
     *             It must accept its own return type (if non-void) plus an {@code int} parameter (for the counter),
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6198
     *             and may accept any number of additional types.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6199
     *             See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6200
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6201
     * @return a method handle representing the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6202
     * @throws NullPointerException if either of the {@code iterations} or {@code body} handles is {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6203
     * @throws IllegalArgumentException if any argument violates the rules formulated above.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6204
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6205
     * @see #countedLoop(MethodHandle, MethodHandle, MethodHandle, MethodHandle)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6206
     * @since 9
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6207
     */
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6208
    public static MethodHandle countedLoop(MethodHandle iterations, MethodHandle init, MethodHandle body) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6209
        return countedLoop(empty(iterations.type()), iterations, init, body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6210
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6211
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6212
    /**
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6213
     * Constructs a loop that counts over a range of numbers.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6214
     * This is a convenience wrapper for the {@linkplain #loop(MethodHandle[][]) generic loop combinator}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6215
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6216
     * The loop counter {@code i} is a loop iteration variable of type {@code int}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6217
     * The {@code start} and {@code end} handles determine the start (inclusive) and end (exclusive)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6218
     * values of the loop counter.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6219
     * The loop counter will be initialized to the {@code int} value returned from the evaluation of the
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6220
     * {@code start} handle and run to the value returned from {@code end} (exclusively) with a step width of 1.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6221
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6222
     * If the {@code body} handle returns a non-{@code void} type {@code V}, a leading loop iteration variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6223
     * of that type is also present.  This variable is initialized using the optional {@code init} handle,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6224
     * or to the {@linkplain #empty default value} of type {@code V} if that handle is {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6225
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6226
     * In each iteration, the iteration variables are passed to an invocation of the {@code body} handle.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6227
     * A non-{@code void} value returned from the body (of type {@code V}) updates the leading
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6228
     * iteration variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6229
     * The result of the loop handle execution will be the final {@code V} value of that variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6230
     * (or {@code void} if there is no {@code V} variable).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6231
     * <p>
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6232
     * The following rules hold for the argument handles:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6233
     * <li>The {@code start} and {@code end} handles must not be {@code null}, and must both return
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6234
     * the common type {@code int}, referred to here as {@code I} in parameter type lists.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6235
     * <li>The {@code body} handle must not be {@code null}; its type must be of the form
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6236
     * {@code (V I A...)V}, where {@code V} is non-{@code void}, or else {@code (I A...)void}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6237
     * (In the {@code void} case, we assign the type {@code void} to the name {@code V},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6238
     * and we will write {@code (V I A...)V} with the understanding that a {@code void} type {@code V}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6239
     * is quietly dropped from the parameter list, leaving {@code (I A...)V}.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6240
     * <li>The parameter list {@code (V I A...)} of the body contributes to a list
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6241
     * of types called the <em>internal parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6242
     * It will constrain the parameter lists of the other loop parts.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6243
     * <li>As a special case, if the body contributes only {@code V} and {@code I} types,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6244
     * with no additional {@code A} types, then the internal parameter list is extended by
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6245
     * the argument types {@code A...} of the {@code end} handle.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6246
     * <li>If the iteration variable types {@code (V I)} are dropped from the internal parameter list, the resulting shorter
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6247
     * list {@code (A...)} is called the <em>external parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6248
     * <li>The body return type {@code V}, if non-{@code void}, determines the type of an
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6249
     * additional state variable of the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6250
     * The body must both accept a leading parameter and return a value of this type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6251
     * <li>If {@code init} is non-{@code null}, it must have return type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6252
     * Its parameter list (of some <a href="MethodHandles.html#astar">form {@code (A*)}</a>) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6253
     * <a href="MethodHandles.html#effid">effectively identical</a>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6254
     * to the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6255
     * <li>If {@code init} is {@code null}, the loop variable will be initialized to its
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6256
     * {@linkplain #empty default value}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6257
     * <li>The parameter list of {@code start} (of some form {@code (A*)}) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6258
     * effectively identical to the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6259
     * <li>Likewise, the parameter list of {@code end} must be effectively identical
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6260
     * to the external parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6261
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6262
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6263
     * The resulting loop handle's result type and parameter signature are determined as follows:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6264
     * <li>The loop handle's result type is the result type {@code V} of the body.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6265
     * <li>The loop handle's parameter types are the types {@code (A...)},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6266
     * from the external parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6267
     * </ul>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6268
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6269
     * Here is pseudocode for the resulting loop handle. In the code, {@code V}/{@code v} represent the type / value of
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6270
     * the second loop variable as well as the result type of the loop; and {@code A...}/{@code a...} represent
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6271
     * arguments passed to the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6272
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6273
     * int start(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6274
     * int end(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6275
     * V init(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6276
     * V body(V, int, A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6277
     * V countedLoop(A... a...) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6278
     *   int e = end(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6279
     *   int s = start(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6280
     *   V v = init(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6281
     *   for (int i = s; i < e; ++i) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6282
     *     v = body(v, i, a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6283
     *   }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6284
     *   return v;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6285
     * }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6286
     * }</pre></blockquote>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6287
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6288
     * @apiNote The implementation of this method can be expressed as follows:
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6289
     * <blockquote><pre>{@code
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6290
     * MethodHandle countedLoop(MethodHandle start, MethodHandle end, MethodHandle init, MethodHandle body) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6291
     *     MethodHandle returnVar = dropArguments(identity(init.type().returnType()), 0, int.class, int.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6292
     *     // assume MH_increment and MH_predicate are handles to implementation-internal methods with
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6293
     *     // the following semantics:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6294
     *     // MH_increment: (int limit, int counter) -> counter + 1
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6295
     *     // MH_predicate: (int limit, int counter) -> counter < limit
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6296
     *     Class<?> counterType = start.type().returnType();  // int
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6297
     *     Class<?> returnType = body.type().returnType();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6298
     *     MethodHandle incr = MH_increment, pred = MH_predicate, retv = null;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6299
     *     if (returnType != void.class) {  // ignore the V variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6300
     *         incr = dropArguments(incr, 1, returnType);  // (limit, v, i) => (limit, i)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6301
     *         pred = dropArguments(pred, 1, returnType);  // ditto
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6302
     *         retv = dropArguments(identity(returnType), 0, counterType); // ignore limit
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6303
     *     }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6304
     *     body = dropArguments(body, 0, counterType);  // ignore the limit variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6305
     *     MethodHandle[]
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6306
     *         loopLimit  = { end, null, pred, retv }, // limit = end(); i < limit || return v
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6307
     *         bodyClause = { init, body },            // v = init(); v = body(v, i)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6308
     *         indexVar   = { start, incr };           // i = start(); i = i + 1
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6309
     *     return loop(loopLimit, bodyClause, indexVar);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6310
     * }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6311
     * }</pre></blockquote>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6312
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6313
     * @param start a non-{@code null} handle to return the start value of the loop counter, which must be {@code int}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6314
     *              See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6315
     * @param end a non-{@code null} handle to return the end value of the loop counter (the loop will run to
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6316
     *            {@code end-1}). The result type must be {@code int}. See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6317
     * @param init optional initializer, providing the initial value of the loop variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6318
     *             May be {@code null}, implying a default initial value.  See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6319
     * @param body body of the loop, which may not be {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6320
     *             It controls the loop parameters and result type in the standard case (see above for details).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6321
     *             It must accept its own return type (if non-void) plus an {@code int} parameter (for the counter),
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6322
     *             and may accept any number of additional types.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6323
     *             See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6324
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6325
     * @return a method handle representing the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6326
     * @throws NullPointerException if any of the {@code start}, {@code end}, or {@code body} handles is {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6327
     * @throws IllegalArgumentException if any argument violates the rules formulated above.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6328
     *
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6329
     * @see #countedLoop(MethodHandle, MethodHandle, MethodHandle)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6330
     * @since 9
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6331
     */
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6332
    public static MethodHandle countedLoop(MethodHandle start, MethodHandle end, MethodHandle init, MethodHandle body) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6333
        countedLoopChecks(start, end, init, body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6334
        Class<?> counterType = start.type().returnType();  // int, but who's counting?
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6335
        Class<?> limitType   = end.type().returnType();    // yes, int again
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6336
        Class<?> returnType  = body.type().returnType();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6337
        MethodHandle incr = MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_countedLoopStep);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6338
        MethodHandle pred = MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_countedLoopPred);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6339
        MethodHandle retv = null;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6340
        if (returnType != void.class) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6341
            incr = dropArguments(incr, 1, returnType);  // (limit, v, i) => (limit, i)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6342
            pred = dropArguments(pred, 1, returnType);  // ditto
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6343
            retv = dropArguments(identity(returnType), 0, counterType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6344
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6345
        body = dropArguments(body, 0, counterType);  // ignore the limit variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6346
        MethodHandle[]
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6347
            loopLimit  = { end, null, pred, retv }, // limit = end(); i < limit || return v
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6348
            bodyClause = { init, body },            // v = init(); v = body(v, i)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6349
            indexVar   = { start, incr };           // i = start(); i = i + 1
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6350
        return loop(loopLimit, bodyClause, indexVar);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6351
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6352
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6353
    private static void countedLoopChecks(MethodHandle start, MethodHandle end, MethodHandle init, MethodHandle body) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6354
        Objects.requireNonNull(start);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6355
        Objects.requireNonNull(end);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6356
        Objects.requireNonNull(body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6357
        Class<?> counterType = start.type().returnType();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6358
        if (counterType != int.class) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6359
            MethodType expected = start.type().changeReturnType(int.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6360
            throw misMatchedTypes("start function", start.type(), expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6361
        } else if (end.type().returnType() != counterType) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6362
            MethodType expected = end.type().changeReturnType(counterType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6363
            throw misMatchedTypes("end function", end.type(), expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6364
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6365
        MethodType bodyType = body.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6366
        Class<?> returnType = bodyType.returnType();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6367
        List<Class<?>> innerList = bodyType.parameterList();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6368
        // strip leading V value if present
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6369
        int vsize = (returnType == void.class ? 0 : 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6370
        if (vsize != 0 && (innerList.size() == 0 || innerList.get(0) != returnType)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6371
            // argument list has no "V" => error
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6372
            MethodType expected = bodyType.insertParameterTypes(0, returnType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6373
            throw misMatchedTypes("body function", bodyType, expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6374
        } else if (innerList.size() <= vsize || innerList.get(vsize) != counterType) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6375
            // missing I type => error
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6376
            MethodType expected = bodyType.insertParameterTypes(vsize, counterType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6377
            throw misMatchedTypes("body function", bodyType, expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6378
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6379
        List<Class<?>> outerList = innerList.subList(vsize + 1, innerList.size());
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6380
        if (outerList.isEmpty()) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6381
            // special case; take lists from end handle
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6382
            outerList = end.type().parameterList();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6383
            innerList = bodyType.insertParameterTypes(vsize + 1, outerList).parameterList();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6384
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6385
        MethodType expected = methodType(counterType, outerList);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6386
        if (!start.type().effectivelyIdenticalParameters(0, outerList)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6387
            throw misMatchedTypes("start parameter types", start.type(), expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6388
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6389
        if (end.type() != start.type() &&
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6390
            !end.type().effectivelyIdenticalParameters(0, outerList)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6391
            throw misMatchedTypes("end parameter types", end.type(), expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6392
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6393
        if (init != null) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6394
            MethodType initType = init.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6395
            if (initType.returnType() != returnType ||
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6396
                !initType.effectivelyIdenticalParameters(0, outerList)) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6397
                throw misMatchedTypes("loop initializer", initType, methodType(returnType, outerList));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6398
            }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6399
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6400
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6401
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6402
    /**
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6403
     * Constructs a loop that ranges over the values produced by an {@code Iterator<T>}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6404
     * This is a convenience wrapper for the {@linkplain #loop(MethodHandle[][]) generic loop combinator}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6405
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6406
     * The iterator itself will be determined by the evaluation of the {@code iterator} handle.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6407
     * Each value it produces will be stored in a loop iteration variable of type {@code T}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6408
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6409
     * If the {@code body} handle returns a non-{@code void} type {@code V}, a leading loop iteration variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6410
     * of that type is also present.  This variable is initialized using the optional {@code init} handle,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6411
     * or to the {@linkplain #empty default value} of type {@code V} if that handle is {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6412
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6413
     * In each iteration, the iteration variables are passed to an invocation of the {@code body} handle.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6414
     * A non-{@code void} value returned from the body (of type {@code V}) updates the leading
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6415
     * iteration variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6416
     * The result of the loop handle execution will be the final {@code V} value of that variable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6417
     * (or {@code void} if there is no {@code V} variable).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6418
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6419
     * The following rules hold for the argument handles:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6420
     * <li>The {@code body} handle must not be {@code null}; its type must be of the form
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6421
     * {@code (V T A...)V}, where {@code V} is non-{@code void}, or else {@code (T A...)void}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6422
     * (In the {@code void} case, we assign the type {@code void} to the name {@code V},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6423
     * and we will write {@code (V T A...)V} with the understanding that a {@code void} type {@code V}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6424
     * is quietly dropped from the parameter list, leaving {@code (T A...)V}.)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6425
     * <li>The parameter list {@code (V T A...)} of the body contributes to a list
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6426
     * of types called the <em>internal parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6427
     * It will constrain the parameter lists of the other loop parts.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6428
     * <li>As a special case, if the body contributes only {@code V} and {@code T} types,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6429
     * with no additional {@code A} types, then the internal parameter list is extended by
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6430
     * the argument types {@code A...} of the {@code iterator} handle; if it is {@code null} the
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6431
     * single type {@code Iterable} is added and constitutes the {@code A...} list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6432
     * <li>If the iteration variable types {@code (V T)} are dropped from the internal parameter list, the resulting shorter
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6433
     * list {@code (A...)} is called the <em>external parameter list</em>.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6434
     * <li>The body return type {@code V}, if non-{@code void}, determines the type of an
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6435
     * additional state variable of the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6436
     * The body must both accept a leading parameter and return a value of this type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6437
     * <li>If {@code init} is non-{@code null}, it must have return type {@code V}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6438
     * Its parameter list (of some <a href="MethodHandles.html#astar">form {@code (A*)}</a>) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6439
     * <a href="MethodHandles.html#effid">effectively identical</a>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6440
     * to the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6441
     * <li>If {@code init} is {@code null}, the loop variable will be initialized to its
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6442
     * {@linkplain #empty default value}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6443
     * <li>If the {@code iterator} handle is non-{@code null}, it must have the return
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6444
     * type {@code java.util.Iterator} or a subtype thereof.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6445
     * The iterator it produces when the loop is executed will be assumed
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6446
     * to yield values which can be converted to type {@code T}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6447
     * <li>The parameter list of an {@code iterator} that is non-{@code null} (of some form {@code (A*)}) must be
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6448
     * effectively identical to the external parameter list {@code (A...)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6449
     * <li>If {@code iterator} is {@code null} it defaults to a method handle which behaves
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6450
     * like {@link java.lang.Iterable#iterator()}.  In that case, the internal parameter list
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6451
     * {@code (V T A...)} must have at least one {@code A} type, and the default iterator
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6452
     * handle parameter is adjusted to accept the leading {@code A} type, as if by
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6453
     * the {@link MethodHandle#asType asType} conversion method.
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6454
     * The leading {@code A} type must be {@code Iterable} or a subtype thereof.
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6455
     * This conversion step, done at loop construction time, must not throw a {@code WrongMethodTypeException}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6456
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6457
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6458
     * The type {@code T} may be either a primitive or reference.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6459
     * Since type {@code Iterator<T>} is erased in the method handle representation to the raw type {@code Iterator},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6460
     * the {@code iteratedLoop} combinator adjusts the leading argument type for {@code body} to {@code Object}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6461
     * as if by the {@link MethodHandle#asType asType} conversion method.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6462
     * Therefore, if an iterator of the wrong type appears as the loop is executed, runtime exceptions may occur
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6463
     * as the result of dynamic conversions performed by {@link MethodHandle#asType(MethodType)}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6464
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6465
     * The resulting loop handle's result type and parameter signature are determined as follows:<ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6466
     * <li>The loop handle's result type is the result type {@code V} of the body.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6467
     * <li>The loop handle's parameter types are the types {@code (A...)},
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6468
     * from the external parameter list.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6469
     * </ul>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6470
     * <p>
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6471
     * Here is pseudocode for the resulting loop handle. In the code, {@code V}/{@code v} represent the type / value of
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6472
     * the loop variable as well as the result type of the loop; {@code T}/{@code t}, that of the elements of the
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6473
     * structure the loop iterates over, and {@code A...}/{@code a...} represent arguments passed to the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6474
     * <blockquote><pre>{@code
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6475
     * Iterator<T> iterator(A...);  // defaults to Iterable::iterator
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6476
     * V init(A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6477
     * V body(V,T,A...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6478
     * V iteratedLoop(A... a...) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6479
     *   Iterator<T> it = iterator(a...);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6480
     *   V v = init(a...);
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6481
     *   while (it.hasNext()) {
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6482
     *     T t = it.next();
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6483
     *     v = body(v, t, a...);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6484
     *   }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6485
     *   return v;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6486
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6487
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  6488
     *
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6489
     * @apiNote Example:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6490
     * <blockquote><pre>{@code
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6491
     * // get an iterator from a list
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6492
     * static List<String> reverseStep(List<String> r, String e) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6493
     *   r.add(0, e);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6494
     *   return r;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6495
     * }
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6496
     * static List<String> newArrayList() { return new ArrayList<>(); }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6497
     * // assume MH_reverseStep and MH_newArrayList are handles to the above methods
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6498
     * MethodHandle loop = MethodHandles.iteratedLoop(null, MH_newArrayList, MH_reverseStep);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6499
     * List<String> list = Arrays.asList("a", "b", "c", "d", "e");
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6500
     * List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a");
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6501
     * assertEquals(reversedList, (List<String>) loop.invoke(list));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6502
     * }</pre></blockquote>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 46047
diff changeset
  6503
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6504
     * @apiNote The implementation of this method can be expressed approximately as follows:
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6505
     * <blockquote><pre>{@code
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6506
     * MethodHandle iteratedLoop(MethodHandle iterator, MethodHandle init, MethodHandle body) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6507
     *     // assume MH_next, MH_hasNext, MH_startIter are handles to methods of Iterator/Iterable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6508
     *     Class<?> returnType = body.type().returnType();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6509
     *     Class<?> ttype = body.type().parameterType(returnType == void.class ? 0 : 1);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6510
     *     MethodHandle nextVal = MH_next.asType(MH_next.type().changeReturnType(ttype));
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6511
     *     MethodHandle retv = null, step = body, startIter = iterator;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6512
     *     if (returnType != void.class) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6513
     *         // the simple thing first:  in (I V A...), drop the I to get V
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6514
     *         retv = dropArguments(identity(returnType), 0, Iterator.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6515
     *         // body type signature (V T A...), internal loop types (I V A...)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6516
     *         step = swapArguments(body, 0, 1);  // swap V <-> T
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6517
     *     }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6518
     *     if (startIter == null)  startIter = MH_getIter;
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6519
     *     MethodHandle[]
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6520
     *         iterVar    = { startIter, null, MH_hasNext, retv }, // it = iterator; while (it.hasNext())
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6521
     *         bodyClause = { init, filterArguments(step, 0, nextVal) };  // v = body(v, t, a)
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6522
     *     return loop(iterVar, bodyClause);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6523
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6524
     * }</pre></blockquote>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6525
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6526
     * @param iterator an optional handle to return the iterator to start the loop.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6527
     *                 If non-{@code null}, the handle must return {@link java.util.Iterator} or a subtype.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6528
     *                 See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6529
     * @param init optional initializer, providing the initial value of the loop variable.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6530
     *             May be {@code null}, implying a default initial value.  See above for other constraints.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6531
     * @param body body of the loop, which may not be {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6532
     *             It controls the loop parameters and result type in the standard case (see above for details).
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6533
     *             It must accept its own return type (if non-void) plus a {@code T} parameter (for the iterated values),
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6534
     *             and may accept any number of additional types.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6535
     *             See above for other constraints.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6536
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6537
     * @return a method handle embodying the iteration loop functionality.
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6538
     * @throws NullPointerException if the {@code body} handle is {@code null}.
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6539
     * @throws IllegalArgumentException if any argument violates the above requirements.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6540
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6541
     * @since 9
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6542
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6543
    public static MethodHandle iteratedLoop(MethodHandle iterator, MethodHandle init, MethodHandle body) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6544
        Class<?> iterableType = iteratedLoopChecks(iterator, init, body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6545
        Class<?> returnType = body.type().returnType();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6546
        MethodHandle hasNext = MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_iteratePred);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6547
        MethodHandle nextRaw = MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_iterateNext);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6548
        MethodHandle startIter;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6549
        MethodHandle nextVal;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6550
        {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6551
            MethodType iteratorType;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6552
            if (iterator == null) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6553
                // derive argument type from body, if available, else use Iterable
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6554
                startIter = MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_initIterator);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6555
                iteratorType = startIter.type().changeParameterType(0, iterableType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6556
            } else {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6557
                // force return type to the internal iterator class
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6558
                iteratorType = iterator.type().changeReturnType(Iterator.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6559
                startIter = iterator;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6560
            }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6561
            Class<?> ttype = body.type().parameterType(returnType == void.class ? 0 : 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6562
            MethodType nextValType = nextRaw.type().changeReturnType(ttype);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6563
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6564
            // perform the asType transforms under an exception transformer, as per spec.:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6565
            try {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6566
                startIter = startIter.asType(iteratorType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6567
                nextVal = nextRaw.asType(nextValType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6568
            } catch (WrongMethodTypeException ex) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6569
                throw new IllegalArgumentException(ex);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6570
            }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6571
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6572
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6573
        MethodHandle retv = null, step = body;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6574
        if (returnType != void.class) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6575
            // the simple thing first:  in (I V A...), drop the I to get V
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6576
            retv = dropArguments(identity(returnType), 0, Iterator.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6577
            // body type signature (V T A...), internal loop types (I V A...)
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6578
            step = swapArguments(body, 0, 1);  // swap V <-> T
37602
3a9532bbd89e 8152667: MHs.iteratedLoop(...) throws unexpected WMTE, disallows Iterator subclasses, generates inconsistent loop result type
mhaupt
parents: 37539
diff changeset
  6579
        }
3a9532bbd89e 8152667: MHs.iteratedLoop(...) throws unexpected WMTE, disallows Iterator subclasses, generates inconsistent loop result type
mhaupt
parents: 37539
diff changeset
  6580
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6581
        MethodHandle[]
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6582
            iterVar    = { startIter, null, hasNext, retv },
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6583
            bodyClause = { init, filterArgument(step, 0, nextVal) };
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6584
        return loop(iterVar, bodyClause);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6585
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6586
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6587
    private static Class<?> iteratedLoopChecks(MethodHandle iterator, MethodHandle init, MethodHandle body) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6588
        Objects.requireNonNull(body);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6589
        MethodType bodyType = body.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6590
        Class<?> returnType = bodyType.returnType();
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6591
        List<Class<?>> internalParamList = bodyType.parameterList();
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6592
        // strip leading V value if present
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6593
        int vsize = (returnType == void.class ? 0 : 1);
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6594
        if (vsize != 0 && (internalParamList.size() == 0 || internalParamList.get(0) != returnType)) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6595
            // argument list has no "V" => error
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6596
            MethodType expected = bodyType.insertParameterTypes(0, returnType);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6597
            throw misMatchedTypes("body function", bodyType, expected);
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6598
        } else if (internalParamList.size() <= vsize) {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6599
            // missing T type => error
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6600
            MethodType expected = bodyType.insertParameterTypes(vsize, Object.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6601
            throw misMatchedTypes("body function", bodyType, expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6602
        }
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6603
        List<Class<?>> externalParamList = internalParamList.subList(vsize + 1, internalParamList.size());
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6604
        Class<?> iterableType = null;
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6605
        if (iterator != null) {
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6606
            // special case; if the body handle only declares V and T then
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6607
            // the external parameter list is obtained from iterator handle
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6608
            if (externalParamList.isEmpty()) {
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6609
                externalParamList = iterator.type().parameterList();
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6610
            }
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6611
            MethodType itype = iterator.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6612
            if (!Iterator.class.isAssignableFrom(itype.returnType())) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6613
                throw newIllegalArgumentException("iteratedLoop first argument must have Iterator return type");
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6614
            }
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6615
            if (!itype.effectivelyIdenticalParameters(0, externalParamList)) {
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6616
                MethodType expected = methodType(itype.returnType(), externalParamList);
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6617
                throw misMatchedTypes("iterator parameters", itype, expected);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6618
            }
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6619
        } else {
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6620
            if (externalParamList.isEmpty()) {
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6621
                // special case; if the iterator handle is null and the body handle
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6622
                // only declares V and T then the external parameter list consists
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6623
                // of Iterable
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6624
                externalParamList = Arrays.asList(Iterable.class);
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6625
                iterableType = Iterable.class;
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6626
            } else {
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6627
                // special case; if the iterator handle is null and the external
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6628
                // parameter list is not empty then the first parameter must be
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6629
                // assignable to Iterable
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6630
                iterableType = externalParamList.get(0);
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6631
                if (!Iterable.class.isAssignableFrom(iterableType)) {
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6632
                    throw newIllegalArgumentException(
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6633
                            "inferred first loop argument must inherit from Iterable: " + iterableType);
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6634
                }
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6635
            }
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6636
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6637
        if (init != null) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6638
            MethodType initType = init.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6639
            if (initType.returnType() != returnType ||
41829
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6640
                    !initType.effectivelyIdenticalParameters(0, externalParamList)) {
224facf0da6e 8167974: MethodHandles.iteratedLoop(...) fails with CCE in the case of iterating over array
psandoz
parents: 41207
diff changeset
  6641
                throw misMatchedTypes("loop initializer", initType, methodType(returnType, externalParamList));
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6642
            }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6643
        }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6644
        return iterableType;  // help the caller a bit
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6645
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6646
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6647
    /*non-public*/ static MethodHandle swapArguments(MethodHandle mh, int i, int j) {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6648
        // there should be a better way to uncross my wires
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6649
        int arity = mh.type().parameterCount();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6650
        int[] order = new int[arity];
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6651
        for (int k = 0; k < arity; k++)  order[k] = k;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6652
        order[i] = j; order[j] = i;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6653
        Class<?>[] types = mh.type().parameterArray();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6654
        Class<?> ti = types[i]; types[i] = types[j]; types[j] = ti;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6655
        MethodType swapType = methodType(mh.type().returnType(), types);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6656
        return permuteArguments(mh, swapType, order);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6657
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6658
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6659
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6660
     * Makes a method handle that adapts a {@code target} method handle by wrapping it in a {@code try-finally} block.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6661
     * Another method handle, {@code cleanup}, represents the functionality of the {@code finally} block. Any exception
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6662
     * thrown during the execution of the {@code target} handle will be passed to the {@code cleanup} handle. The
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6663
     * exception will be rethrown, unless {@code cleanup} handle throws an exception first.  The
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6664
     * value returned from the {@code cleanup} handle's execution will be the result of the execution of the
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6665
     * {@code try-finally} handle.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6666
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6667
     * The {@code cleanup} handle will be passed one or two additional leading arguments.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6668
     * The first is the exception thrown during the
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6669
     * execution of the {@code target} handle, or {@code null} if no exception was thrown.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6670
     * The second is the result of the execution of the {@code target} handle, or, if it throws an exception,
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6671
     * a {@code null}, zero, or {@code false} value of the required type is supplied as a placeholder.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6672
     * The second argument is not present if the {@code target} handle has a {@code void} return type.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6673
     * (Note that, except for argument type conversions, combinators represent {@code void} values in parameter lists
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6674
     * by omitting the corresponding paradoxical arguments, not by inserting {@code null} or zero values.)
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6675
     * <p>
36112
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  6676
     * The {@code target} and {@code cleanup} handles must have the same corresponding argument and return types, except
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  6677
     * that the {@code cleanup} handle may omit trailing arguments. Also, the {@code cleanup} handle must have one or
e71e97060bf5 8150360: augment/correct MethodHandle API documentation
mhaupt
parents: 35772
diff changeset
  6678
     * two extra leading parameters:<ul>
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6679
     * <li>a {@code Throwable}, which will carry the exception thrown by the {@code target} handle (if any); and
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6680
     * <li>a parameter of the same type as the return type of both {@code target} and {@code cleanup}, which will carry
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6681
     * the result from the execution of the {@code target} handle.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6682
     * This parameter is not present if the {@code target} returns {@code void}.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6683
     * </ul>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6684
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6685
     * The pseudocode for the resulting adapter looks as follows. In the code, {@code V} represents the result type of
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6686
     * the {@code try/finally} construct; {@code A}/{@code a}, the types and values of arguments to the resulting
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6687
     * handle consumed by the cleanup; and {@code B}/{@code b}, those of arguments to the resulting handle discarded by
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6688
     * the cleanup.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6689
     * <blockquote><pre>{@code
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6690
     * V target(A..., B...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6691
     * V cleanup(Throwable, V, A...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6692
     * V adapter(A... a, B... b) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6693
     *   V result = (zero value for V);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6694
     *   Throwable throwable = null;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6695
     *   try {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6696
     *     result = target(a..., b...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6697
     *   } catch (Throwable t) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6698
     *     throwable = t;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6699
     *     throw t;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6700
     *   } finally {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6701
     *     result = cleanup(throwable, result, a...);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6702
     *   }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6703
     *   return result;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6704
     * }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6705
     * }</pre></blockquote>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6706
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6707
     * Note that the saved arguments ({@code a...} in the pseudocode) cannot
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6708
     * be modified by execution of the target, and so are passed unchanged
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6709
     * from the caller to the cleanup, if it is invoked.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6710
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6711
     * The target and cleanup must return the same type, even if the cleanup
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6712
     * always throws.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6713
     * To create such a throwing cleanup, compose the cleanup logic
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6714
     * with {@link #throwException throwException},
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6715
     * in order to create a method handle of the correct return type.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6716
     * <p>
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6717
     * Note that {@code tryFinally} never converts exceptions into normal returns.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6718
     * In rare cases where exceptions must be converted in that way, first wrap
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6719
     * the target with {@link #catchException(MethodHandle, Class, MethodHandle)}
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6720
     * to capture an outgoing exception, and then wrap with {@code tryFinally}.
49790
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6721
     * <p>
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6722
     * It is recommended that the first parameter type of {@code cleanup} be
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6723
     * declared {@code Throwable} rather than a narrower subtype.  This ensures
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6724
     * {@code cleanup} will always be invoked with whatever exception that
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6725
     * {@code target} throws.  Declaring a narrower type may result in a
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6726
     * {@code ClassCastException} being thrown by the {@code try-finally}
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6727
     * handle if the type of the exception thrown by {@code target} is not
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6728
     * assignable to the first parameter type of {@code cleanup}.  Note that
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6729
     * various exception types of {@code VirtualMachineError},
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6730
     * {@code LinkageError}, and {@code RuntimeException} can in principle be
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6731
     * thrown by almost any kind of Java code, and a finally clause that
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6732
     * catches (say) only {@code IOException} would mask any of the others
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6733
     * behind a {@code ClassCastException}.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6734
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6735
     * @param target the handle whose execution is to be wrapped in a {@code try} block.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6736
     * @param cleanup the handle that is invoked in the finally block.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6737
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6738
     * @return a method handle embodying the {@code try-finally} block composed of the two arguments.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6739
     * @throws NullPointerException if any argument is null
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6740
     * @throws IllegalArgumentException if {@code cleanup} does not accept
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6741
     *          the required leading arguments, or if the method handle types do
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6742
     *          not match in their return types and their
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6743
     *          corresponding trailing parameters
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6744
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6745
     * @see MethodHandles#catchException(MethodHandle, Class, MethodHandle)
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6746
     * @since 9
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6747
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6748
    public static MethodHandle tryFinally(MethodHandle target, MethodHandle cleanup) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6749
        List<Class<?>> targetParamTypes = target.type().parameterList();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6750
        Class<?> rtype = target.type().returnType();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6751
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6752
        tryFinallyChecks(target, cleanup);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6753
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6754
        // Match parameter lists: if the cleanup has a shorter parameter list than the target, add ignored arguments.
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  6755
        // The cleanup parameter list (minus the leading Throwable and result parameters) must be a sublist of the
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  6756
        // target parameter list.
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37343
diff changeset
  6757
        cleanup = dropArgumentsToMatch(cleanup, (rtype == void.class ? 1 : 2), targetParamTypes, 0);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6758
49790
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6759
        // Ensure that the intrinsic type checks the instance thrown by the
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6760
        // target against the first parameter of cleanup
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6761
        cleanup = cleanup.asType(cleanup.type().changeParameterType(0, Throwable.class));
403e2f61f384 8194238: Trying exceptions in MethodHandles
psandoz
parents: 49432
diff changeset
  6762
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  6763
        // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case.
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40810
diff changeset
  6764
        return MethodHandleImpl.makeTryFinally(target.asFixedArity(), cleanup.asFixedArity(), rtype, targetParamTypes);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6765
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6766
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6767
    private static void tryFinallyChecks(MethodHandle target, MethodHandle cleanup) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6768
        Class<?> rtype = target.type().returnType();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6769
        if (rtype != cleanup.type().returnType()) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6770
            throw misMatchedTypes("target and return types", cleanup.type().returnType(), rtype);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6771
        }
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6772
        MethodType cleanupType = cleanup.type();
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6773
        if (!Throwable.class.isAssignableFrom(cleanupType.parameterType(0))) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6774
            throw misMatchedTypes("cleanup first argument and Throwable", cleanup.type(), Throwable.class);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6775
        }
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6776
        if (rtype != void.class && cleanupType.parameterType(1) != rtype) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6777
            throw misMatchedTypes("cleanup second argument and target return type", cleanup.type(), rtype);
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6778
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6779
        // The cleanup parameter list (minus the leading Throwable and result parameters) must be a sublist of the
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6780
        // target parameter list.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6781
        int cleanupArgIndex = rtype == void.class ? 1 : 2;
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  6782
        if (!cleanupType.effectivelyIdenticalParameters(cleanupArgIndex, target.type().parameterList())) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6783
            throw misMatchedTypes("cleanup parameters after (Throwable,result) and target parameter list prefix",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6784
                    cleanup.type(), target.type());
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6785
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6786
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 32649
diff changeset
  6787
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  6788
}