src/java.base/share/classes/java/lang/invoke/CallSite.java
author jboes
Thu, 21 Nov 2019 09:10:21 +0000
changeset 59201 b24f4caa1411
parent 52220 9c260a6b6471
child 59275 a6e25566cb56
permissions -rw-r--r--
8234335: Remove line break in class declaration in java.base Summary: Remove line break in class declarations where applicable Reviewed-by: rriggs, lancea
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
/*
42103
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
     2
 * Copyright (c) 2008, 2016, 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
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
    28
import static java.lang.invoke.MethodHandleStatics.*;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    29
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    30
43194
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
    31
import jdk.internal.vm.annotation.Stable;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
    32
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    33
/**
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    34
 * A {@code CallSite} is a holder for a variable {@link MethodHandle},
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    35
 * which is called its {@code target}.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    36
 * An {@code invokedynamic} instruction linked to a {@code CallSite} delegates
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    37
 * all calls to the site's current target.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    38
 * A {@code CallSite} may be associated with several {@code invokedynamic}
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    39
 * instructions, or it may be "free floating", associated with none.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    40
 * In any case, it may be invoked through an associated method handle
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    41
 * called its {@linkplain #dynamicInvoker dynamic invoker}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    42
 * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    43
 * {@code CallSite} is an abstract class which does not allow
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    44
 * direct subclassing by users.  It has three immediate,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    45
 * concrete subclasses that may be either instantiated or subclassed.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    46
 * <ul>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    47
 * <li>If a mutable target is not required, an {@code invokedynamic} instruction
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    48
 * may be permanently bound by means of a {@linkplain ConstantCallSite constant call site}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    49
 * <li>If a mutable target is required which has volatile variable semantics,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    50
 * because updates to the target must be immediately and reliably witnessed by other threads,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    51
 * a {@linkplain VolatileCallSite volatile call site} may be used.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    52
 * <li>Otherwise, if a mutable target is required,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    53
 * a {@linkplain MutableCallSite mutable call site} may be used.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    54
 * </ul>
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
    55
 * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    56
 * A non-constant call site may be <em>relinked</em> by changing its target.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    57
 * The new target must have the same {@linkplain MethodHandle#type() type}
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    58
 * as the previous target.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    59
 * Thus, though a call site can be relinked to a series of
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    60
 * successive targets, it cannot change its type.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    61
 * <p>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    62
 * Here is a sample use of call sites and bootstrap methods which links every
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    63
 * dynamic call site to print its arguments:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
    64
<blockquote><pre>{@code
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    65
static void test() throws Throwable {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    66
    // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    67
    InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14);
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    68
}
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    69
private static void printArgs(Object... args) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    70
  System.out.println(java.util.Arrays.deepToString(args));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    71
}
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    72
private static final MethodHandle printArgs;
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    73
static {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    74
  MethodHandles.Lookup lookup = MethodHandles.lookup();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    75
  Class thisClass = lookup.lookupClass();  // (who am I?)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    76
  printArgs = lookup.findStatic(thisClass,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    77
      "printArgs", MethodType.methodType(void.class, Object[].class));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    78
}
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    79
private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    80
  // ignore caller and name, but match the type:
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    81
  return new ConstantCallSite(printArgs.asType(type));
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    82
}
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
    83
}</pre></blockquote>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    84
 * @author John Rose, JSR 292 EG
44255
515cf13d7791 8176566: @since value errors in types of java.base module
mli
parents: 43194
diff changeset
    85
 * @since 1.7
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    86
 */
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    87
abstract
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    88
public class CallSite {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    89
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    90
    // The actual payload of this call site:
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    91
    /*package-private*/
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
    92
    MethodHandle target;    // Note: This field is known to the JVM.  Do not change.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    93
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    94
    /**
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    95
     * Make a blank call site object with the given method type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    96
     * An initial target method is supplied which will throw
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    97
     * an {@link IllegalStateException} if called.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    98
     * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    99
     * Before this {@code CallSite} object is returned from a bootstrap method,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   100
     * it is usually provided with a more useful target method,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   101
     * via a call to {@link CallSite#setTarget(MethodHandle) setTarget}.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   102
     * @throws NullPointerException if the proposed type is null
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   103
     */
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   104
    /*package-private*/
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   105
    CallSite(MethodType type) {
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   106
        target = makeUninitializedCallSite(type);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   107
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   108
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
   109
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   110
     * Make a call site object equipped with an initial target method handle.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   111
     * @param target the method handle which will be the initial target of the call site
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   112
     * @throws NullPointerException if the proposed target is null
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
   113
     */
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   114
    /*package-private*/
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   115
    CallSite(MethodHandle target) {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   116
        target.type();  // null check
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   117
        this.target = target;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   118
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   119
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   120
    /**
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   121
     * Make a call site object equipped with an initial target method handle.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   122
     * @param targetType the desired type of the call site
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   123
     * @param createTargetHook a hook which will bind the call site to the target method handle
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   124
     * @throws WrongMethodTypeException if the hook cannot be invoked on the required arguments,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   125
     *         or if the target returned by the hook is not of the given {@code targetType}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   126
     * @throws NullPointerException if the hook returns a null value
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   127
     * @throws ClassCastException if the hook returns something other than a {@code MethodHandle}
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20535
diff changeset
   128
     * @throws Throwable anything else thrown by the hook function
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   129
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   130
    /*package-private*/
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   131
    CallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   132
        this(targetType);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   133
        ConstantCallSite selfCCS = (ConstantCallSite) this;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   134
        MethodHandle boundTarget = (MethodHandle) createTargetHook.invokeWithArguments(selfCCS);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   135
        checkTargetChange(this.target, boundTarget);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   136
        this.target = boundTarget;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   137
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   138
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   139
    /**
30362
2b921e0eb3e0 8057967: CallSite dependency tracking scales devastatingly poorly
vlivanov
parents: 26470
diff changeset
   140
     * {@code CallSite} dependency context.
31070
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
   141
     * JVM uses CallSite.context to store nmethod dependencies on the call site target.
30362
2b921e0eb3e0 8057967: CallSite dependency tracking scales devastatingly poorly
vlivanov
parents: 26470
diff changeset
   142
     */
31070
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
   143
    private final MethodHandleNatives.CallSiteContext context = MethodHandleNatives.CallSiteContext.make(this);
30362
2b921e0eb3e0 8057967: CallSite dependency tracking scales devastatingly poorly
vlivanov
parents: 26470
diff changeset
   144
2b921e0eb3e0 8057967: CallSite dependency tracking scales devastatingly poorly
vlivanov
parents: 26470
diff changeset
   145
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   146
     * Returns the type of this call site's target.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   147
     * Although targets may change, any call site's type is permanent, and can never change to an unequal type.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   148
     * The {@code setTarget} method enforces this invariant by refusing any new target that does
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   149
     * not have the previous target's type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   150
     * @return the type of the current target, which is also the type of any future target
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   151
     */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   152
    public MethodType type() {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   153
        // warning:  do not call getTarget here, because CCS.getTarget can throw IllegalStateException
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   154
        return target.type();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   155
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   156
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   157
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   158
     * Returns the target method of the call site, according to the
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   159
     * behavior defined by this call site's specific class.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   160
     * The immediate subclasses of {@code CallSite} document the
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   161
     * class-specific behaviors of this method.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   162
     *
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   163
     * @return the current linkage state of the call site, its target method handle
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   164
     * @see ConstantCallSite
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   165
     * @see VolatileCallSite
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   166
     * @see #setTarget
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   167
     * @see ConstantCallSite#getTarget
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   168
     * @see MutableCallSite#getTarget
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   169
     * @see VolatileCallSite#getTarget
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   170
     */
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   171
    public abstract MethodHandle getTarget();
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   172
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   173
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   174
     * Updates the target method of this call site, according to the
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   175
     * behavior defined by this call site's specific class.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   176
     * The immediate subclasses of {@code CallSite} document the
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   177
     * class-specific behaviors of this method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   178
     * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   179
     * The type of the new target must be {@linkplain MethodType#equals equal to}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   180
     * the type of the old target.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   181
     *
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   182
     * @param newTarget the new target
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
   183
     * @throws NullPointerException if the proposed new target is null
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   184
     * @throws WrongMethodTypeException if the proposed new target
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   185
     *         has a method type that differs from the previous target
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   186
     * @see CallSite#getTarget
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   187
     * @see ConstantCallSite#setTarget
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   188
     * @see MutableCallSite#setTarget
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   189
     * @see VolatileCallSite#setTarget
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   190
     */
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   191
    public abstract void setTarget(MethodHandle newTarget);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   192
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   193
    void checkTargetChange(MethodHandle oldTarget, MethodHandle newTarget) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   194
        MethodType oldType = oldTarget.type();
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   195
        MethodType newType = newTarget.type();  // null check!
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   196
        if (!newType.equals(oldType))
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   197
            throw wrongTargetType(newTarget, oldType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   198
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   199
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   200
    private static WrongMethodTypeException wrongTargetType(MethodHandle target, MethodType type) {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   201
        return new WrongMethodTypeException(String.valueOf(target)+" should be of type "+type);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   202
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   203
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   204
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   205
     * Produces a method handle equivalent to an invokedynamic instruction
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   206
     * which has been linked to this call site.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   207
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   208
     * This method is equivalent to the following code:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
   209
     * <blockquote><pre>{@code
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   210
     * MethodHandle getTarget, invoker, result;
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   211
     * getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   212
     * invoker = MethodHandles.exactInvoker(this.type());
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   213
     * result = MethodHandles.foldArguments(invoker, getTarget)
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
   214
     * }</pre></blockquote>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   215
     *
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   216
     * @return a method handle which always invokes this call site's current target
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   217
     */
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   218
    public abstract MethodHandle dynamicInvoker();
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   219
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 52220
diff changeset
   220
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 52220
diff changeset
   221
    MethodHandle makeDynamicInvoker() {
43194
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   222
        MethodHandle getTarget = getTargetHandle().bindArgumentL(0, this);
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   223
        MethodHandle invoker = MethodHandles.exactInvoker(this.type());
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   224
        return MethodHandles.foldArguments(invoker, getTarget);
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   225
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   226
43194
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   227
    private static @Stable MethodHandle GET_TARGET;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   228
    private static MethodHandle getTargetHandle() {
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   229
        MethodHandle handle = GET_TARGET;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   230
        if (handle != null) {
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   231
            return handle;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   232
        }
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   233
        try {
43194
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   234
            return GET_TARGET = IMPL_LOOKUP.
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   235
                    findVirtual(CallSite.class, "getTarget",
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   236
                                MethodType.methodType(MethodHandle.class));
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   237
        } catch (ReflectiveOperationException e) {
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   238
            throw newInternalError(e);
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   239
        }
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   240
    }
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   241
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   242
    private static @Stable MethodHandle THROW_UCS;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   243
    private static MethodHandle uninitializedCallSiteHandle() {
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   244
        MethodHandle handle = THROW_UCS;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   245
        if (handle != null) {
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   246
            return handle;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   247
        }
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   248
        try {
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   249
            return THROW_UCS = IMPL_LOOKUP.
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   250
                findStatic(CallSite.class, "uninitializedCallSite",
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   251
                           MethodType.methodType(Object.class, Object[].class));
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9752
diff changeset
   252
        } catch (ReflectiveOperationException e) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13423
diff changeset
   253
            throw newInternalError(e);
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   254
        }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 7051
diff changeset
   255
    }
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   256
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   257
    /** This guy is rolled into the default target if a MethodType is supplied to the constructor. */
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   258
    private static Object uninitializedCallSite(Object... ignore) {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   259
        throw new IllegalStateException("uninitialized call site");
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   260
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   261
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   262
    private MethodHandle makeUninitializedCallSite(MethodType targetType) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   263
        MethodType basicType = targetType.basicType();
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   264
        MethodHandle invoker = basicType.form().cachedMethodHandle(MethodTypeForm.MH_UNINIT_CS);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   265
        if (invoker == null) {
43194
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   266
            invoker = uninitializedCallSiteHandle().asType(basicType);
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   267
            invoker = basicType.form().setCachedMethodHandle(MethodTypeForm.MH_UNINIT_CS, invoker);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   268
        }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   269
        // unchecked view is OK since no values will be received or returned
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   270
        return invoker.viewAsType(targetType, false);
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   271
    }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   272
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   273
    // unsafe stuff:
43194
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   274
    private static @Stable long TARGET_OFFSET;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   275
    private static long getTargetOffset() {
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   276
        long offset = TARGET_OFFSET;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   277
        if (offset > 0) {
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   278
            return offset;
3c8c3c61ae92 8172905: Minor startup cleanup of CallSite and MethodType
redestad
parents: 42103
diff changeset
   279
        }
46873
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 44255
diff changeset
   280
        offset = TARGET_OFFSET = UNSAFE.objectFieldOffset(CallSite.class, "target");
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 44255
diff changeset
   281
        assert(offset > 0);
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 44255
diff changeset
   282
        return offset;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   283
    }
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   284
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   285
    /*package-private*/
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   286
    void setTargetNormal(MethodHandle newTarget) {
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents: 10419
diff changeset
   287
        MethodHandleNatives.setCallSiteTargetNormal(this, newTarget);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   288
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   289
    /*package-private*/
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   290
    MethodHandle getTargetVolatile() {
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 48826
diff changeset
   291
        return (MethodHandle) UNSAFE.getReferenceVolatile(this, getTargetOffset());
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   292
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   293
    /*package-private*/
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   294
    void setTargetVolatile(MethodHandle newTarget) {
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents: 10419
diff changeset
   295
        MethodHandleNatives.setCallSiteTargetVolatile(this, newTarget);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   296
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   297
42103
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   298
    // this implements the upcall from the JVM, MethodHandleNatives.linkCallSite:
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   299
    static CallSite makeSite(MethodHandle bootstrapMethod,
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   300
                             // Callee information:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   301
                             String name, MethodType type,
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   302
                             // Extra arguments for BSM, if any:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   303
                             Object info,
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   304
                             // Caller information:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   305
                             Class<?> callerClass) {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   306
        CallSite site;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   307
        try {
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   308
            Object binding = BootstrapMethodInvoker.invoke(
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   309
                    CallSite.class, bootstrapMethod, name, type, info, callerClass);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   310
            if (binding instanceof CallSite) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   311
                site = (CallSite) binding;
42103
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   312
            } else {
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   313
                // See the "Linking Exceptions" section for the invokedynamic
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   314
                // instruction in JVMS 6.5.
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   315
                // Throws a runtime exception defining the cause that is then
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   316
                // in the "catch (Throwable ex)" a few lines below wrapped in
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   317
                // BootstrapMethodError
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   318
                throw new ClassCastException("CallSite bootstrap method failed to produce an instance of CallSite");
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   319
            }
42103
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   320
            if (!site.getTarget().type().equals(type)) {
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   321
                // See the "Linking Exceptions" section for the invokedynamic
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   322
                // instruction in JVMS 6.5.
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   323
                // Throws a runtime exception defining the cause that is then
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   324
                // in the "catch (Throwable ex)" a few lines below wrapped in
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   325
                // BootstrapMethodError
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   326
                throw wrongTargetType(site.getTarget(), type);
42103
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   327
            }
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   328
        } catch (Error e) {
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   329
            // Pass through an Error, including BootstrapMethodError, any other
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   330
            // form of linkage error, such as IllegalAccessError if the bootstrap
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   331
            // method is inaccessible, or say ThreadDeath/OutOfMemoryError
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   332
            // See the "Linking Exceptions" section for the invokedynamic
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   333
            // instruction in JVMS 6.5.
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   334
            throw e;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   335
        } catch (Throwable ex) {
42103
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 34720
diff changeset
   336
            // Wrap anything else in BootstrapMethodError
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   337
            throw new BootstrapMethodError("CallSite bootstrap method initialization exception", ex);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   338
        }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   339
        return site;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   340
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   341
}