jdk/src/share/classes/java/dyn/InvokeDynamic.java
author jrose
Wed, 08 Sep 2010 18:40:11 -0700
changeset 7051 1c545d70a157
parent 5725 16c1792b2ee6
child 7563 7ba4fe073384
permissions -rw-r--r--
6964498: JSR 292 invokedynamic sites need local bootstrap methods Summary: Add JVM_CONSTANT_InvokeDynamic records to constant pool to determine per-instruction BSMs; add MethodHandleProvider. Reviewed-by: twisti
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
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     2
 * Copyright (c) 2008, 2009, 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
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    26
package java.dyn;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    28
/**
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    29
 * {@code InvokeDynamic} is a class with neither methods nor instances,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    30
 * which serves only as a syntactic marker in Java source code for
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    31
 * an {@code invokedynamic} instruction.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    32
 * (See <a href="package-summary.html#jvm_mods">the package information</a> for specifics on this instruction.)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    33
 * <p>
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
    34
 * The {@code invokedynamic} instruction is incomplete without a target method.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    35
 * The target method is a property of the reified {@linkplain CallSite call site object}
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    36
 * which is linked to each active {@code invokedynamic} instruction.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    37
 * The call site object is initially produced by a
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    38
 * {@linkplain BootstrapMethod bootstrap method}
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    39
 * associated with the class whose bytecodes include the dynamic call site.
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
    40
 * <p>
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
    41
 * The type {@code InvokeDynamic} has no particular meaning as a
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
    42
 * class or interface supertype, or an object type; it can never be instantiated.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    43
 * Logically, it denotes a source of all dynamically typed methods.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    44
 * It may be viewed as a pure syntactic marker of static calls.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    45
 * It may be imported for ease of use.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    46
 * <p>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    47
 * Here are some examples:
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    48
<blockquote><pre><!-- see indy-demo/src/JavaDocExamples.java -->
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    49
&#064;BootstrapMethod(value=Here.class, name="bootstrapDynamic")
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    50
static void example() throws Throwable {
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    51
    Object x; String s; int i;
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    52
    x = InvokeDynamic.greet("world"); // greet(Ljava/lang/String;)Ljava/lang/Object;
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    53
    s = (String) InvokeDynamic.hail(x); // hail(Ljava/lang/Object;)Ljava/lang/String;
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    54
    InvokeDynamic.cogito(); // cogito()V
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    55
    i = (int) InvokeDynamic.#"op:+"(2, 3); // "op:+"(II)I
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    56
}
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    57
static MethodHandle bootstrapDynamic(Class caller, String name, MethodType type) { ... }
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    58
</pre></blockquote>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    59
 * Each of the above calls generates a single invokedynamic instruction
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    60
 * with the name-and-type descriptors indicated in the comments.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    61
 * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    62
 * The argument types are taken directly from the actual arguments,
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    63
 * while the return type corresponds to the target of the assignment.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    64
 * (Currently, the return type must be given as a false type parameter.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    65
 * This type parameter is an irregular use of the generic type syntax,
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    66
 * and is likely to change in favor of a convention based on target typing.)
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    67
 * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    68
 * The final example uses a special syntax for uttering non-Java names.
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    69
 * Any name legal to the JVM may be given between the double quotes.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    70
 * <p>
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
    71
 * None of these calls is complete without a bootstrap method,
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    72
 * which must be declared for the enclosing class or method.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    73
 * @author John Rose, JSR 292 EG
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    74
 */
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    75
@MethodHandle.PolymorphicSignature
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    76
public final class InvokeDynamic {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    77
    private InvokeDynamic() { throw new InternalError(); }  // do not instantiate
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    78
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    79
    // no statically defined static methods
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    80
}