src/java.base/share/classes/java/lang/invoke/BootstrapCallInfo.java
author psandoz
Fri, 08 Sep 2017 10:46:46 -0700
changeset 48826 c4d9d1b08e2e
permissions -rw-r--r--
8186209: Tool support for ConstantDynamic 8186046: Minimal ConstantDynamic support 8190972: Ensure that AOT/Graal filters out class files containing CONSTANT_Dynamic ahead of full AOT support Reviewed-by: acorn, coleenp, kvn Contributed-by: lois.foltan@oracle.com, john.r.rose@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     1
/*
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     4
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    10
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    15
 * accompanied this code).
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    16
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    20
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    23
 * questions.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    24
 */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    25
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    26
package java.lang.invoke;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    27
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    28
import java.lang.invoke.MethodHandles.Lookup;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    29
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    30
/**
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    31
 * An interface providing full static information about a particular
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    32
 * call to a
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    33
 * <a href="package-summary.html#bsm">bootstrap method</a> of an
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    34
 * dynamic call site or dynamic constant.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    35
 * This information includes the method itself, the associated
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    36
 * name and type, and any associated static arguments.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    37
 * <p>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    38
 * If a bootstrap method declares exactly two arguments, and is
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    39
 * not of variable arity, then it is fed only two arguments by
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    40
 * the JVM, the {@linkplain Lookup lookup object} and an instance
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    41
 * of {@code BootstrapCallInfo} which supplies the rest of the
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    42
 * information about the call.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    43
 * <p>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    44
 * The API for accessing the static arguments allows the bootstrap
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    45
 * method to reorder the resolution (in the constant pool) of the
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    46
 * static arguments, and to catch errors resulting from the resolution.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    47
 * This mode of evaluation <em>pulls</em> bootstrap parameters from
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    48
 * the JVM under control of the bootstrap method, as opposed to
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    49
 * the JVM <em>pushing</em> parameters to a bootstrap method
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    50
 * by resolving them all before the bootstrap method is called.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    51
 * @apiNote
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    52
 * <p>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    53
 * The {@linkplain Lookup lookup object} is <em>not</em> included in this
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    54
 * bundle of information, so as not to obscure the access control
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    55
 * logic of the program.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    56
 * In cases where there are many thousands of parameters, it may
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    57
 * be preferable to pull their resolved values, either singly or in
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    58
 * batches, rather than wait until all of them have been resolved
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    59
 * before a constant or call site can be used.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    60
 * <p>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    61
 * A push mode bootstrap method can be adapted to a pull mode
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    62
 * bootstrap method, and vice versa.  For example, this generic
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    63
 * adapter pops a push-mode bootstrap method from the beginning
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    64
 * of the static argument list, eagerly resolves all the remaining
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    65
 * static arguments, and invokes the popped method in push mode.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    66
 * The callee has no way of telling that it was not called directly
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    67
 * from the JVM.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    68
 * <blockquote><pre>{@code
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    69
static Object genericBSM(Lookup lookup, BootstrapCallInfo<Object> bsci)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    70
    throws Throwable {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    71
  ArrayList<Object> args = new ArrayList<>();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    72
  args.add(lookup);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    73
  args.add(bsci.invocationName());
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    74
  args.add(bsci.invocationType());
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    75
  MethodHandle bsm = (MethodHandle) bsci.get(0);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    76
  List<Object> restOfArgs = bsci.asList().subList(1, bsci.size();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    77
  // the next line eagerly resolves all remaining static arguments:
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    78
  args.addAll(restOfArgs);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    79
  return bsm.invokeWithArguments(args);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    80
}
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    81
 * }</pre></blockquote>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    82
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    83
 * <p>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    84
 * In the other direction, here is a combinator which pops
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    85
 * a pull-mode bootstrap method from the beginning of a list of
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    86
 * static argument values (already resolved), reformats all of
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    87
 * the arguments into a pair of a lookup and a {@code BootstrapCallInfo},
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    88
 * and invokes the popped method.  Again the callee has no way of
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    89
 * telling it was not called directly by the JVM, except that
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    90
 * all of the constant values will appear as resolved.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    91
 * Put another way, if any constant fails to resolve, the
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    92
 * callee will not be able to catch the resulting error,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    93
 * since the error will be thrown by the JVM before the
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    94
 * bootstrap method is entered.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    95
 * <blockquote><pre>{@code
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    96
static Object genericBSM(Lookup lookup, String name, Object type,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    97
                         MethodHandle bsm, Object... args)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    98
    throws Throwable {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    99
  ConstantGroup cons = ConstantGroup.makeConstantGroup(Arrays.asList(args));
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   100
  BootstrapCallInfo<Object> bsci = makeBootstrapCallInfo(bsm, name, type, cons);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   101
  return bsm.invoke(lookup, bsci);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   102
}
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   103
 * }</pre></blockquote>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   104
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   105
 * @since 1.10
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   106
 */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   107
// public
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   108
interface BootstrapCallInfo<T> extends ConstantGroup {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   109
    /** Returns the bootstrap method for this call.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   110
     * @return the bootstrap method
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   111
     */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   112
    MethodHandle bootstrapMethod();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   113
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   114
    /** Returns the method name or constant name for this call.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   115
     * @return the method name or constant name
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   116
     */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   117
    String invocationName();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   118
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   119
    /** Returns the method type or constant type for this call.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   120
     * @return the method type or constant type
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   121
     */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   122
    T invocationType();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   123
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   124
    /**
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   125
     * Make a new bootstrap call descriptor with the given components.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   126
     * @param bsm bootstrap method
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   127
     * @param name invocation name
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   128
     * @param type invocation type
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   129
     * @param constants the additional static arguments for the bootstrap method
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   130
     * @param <T> the type of the invocation type, either {@link MethodHandle} or {@link Class}
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   131
     * @return a new bootstrap call descriptor with the given components
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   132
     */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   133
    static <T> BootstrapCallInfo<T> makeBootstrapCallInfo(MethodHandle bsm,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   134
                                                          String name,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   135
                                                          T type,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   136
                                                          ConstantGroup constants) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   137
        AbstractConstantGroup.BSCIWithCache<T> bsci = new AbstractConstantGroup.BSCIWithCache<>(bsm, name, type, constants.size());
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   138
        final Object NP = AbstractConstantGroup.BSCIWithCache.NOT_PRESENT;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   139
        bsci.initializeCache(constants.asList(NP), NP);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   140
        return bsci;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   141
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   142
}