rt/emul/compact/src/main/java/java/lang/invoke/ConstantCallSite.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Aug 2014 11:11:13 +0200
branchjdk8-b132
changeset 1646 c880a8a8803b
permissions -rw-r--r--
Batch of classes necessary to implement invoke dynamic interfaces. Taken from JDK8 build 132
jaroslav@1646
     1
/*
jaroslav@1646
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jaroslav@1646
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1646
     4
 *
jaroslav@1646
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1646
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1646
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1646
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1646
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1646
    10
 *
jaroslav@1646
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1646
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1646
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1646
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1646
    15
 * accompanied this code).
jaroslav@1646
    16
 *
jaroslav@1646
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1646
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1646
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1646
    20
 *
jaroslav@1646
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1646
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1646
    23
 * questions.
jaroslav@1646
    24
 */
jaroslav@1646
    25
jaroslav@1646
    26
package java.lang.invoke;
jaroslav@1646
    27
jaroslav@1646
    28
/**
jaroslav@1646
    29
 * A {@code ConstantCallSite} is a {@link CallSite} whose target is permanent, and can never be changed.
jaroslav@1646
    30
 * An {@code invokedynamic} instruction linked to a {@code ConstantCallSite} is permanently
jaroslav@1646
    31
 * bound to the call site's target.
jaroslav@1646
    32
 * @author John Rose, JSR 292 EG
jaroslav@1646
    33
 */
jaroslav@1646
    34
public class ConstantCallSite extends CallSite {
jaroslav@1646
    35
    private final boolean isFrozen;
jaroslav@1646
    36
jaroslav@1646
    37
    /**
jaroslav@1646
    38
     * Creates a call site with a permanent target.
jaroslav@1646
    39
     * @param target the target to be permanently associated with this call site
jaroslav@1646
    40
     * @throws NullPointerException if the proposed target is null
jaroslav@1646
    41
     */
jaroslav@1646
    42
    public ConstantCallSite(MethodHandle target) {
jaroslav@1646
    43
        super(target);
jaroslav@1646
    44
        isFrozen = true;
jaroslav@1646
    45
    }
jaroslav@1646
    46
jaroslav@1646
    47
    /**
jaroslav@1646
    48
     * Creates a call site with a permanent target, possibly bound to the call site itself.
jaroslav@1646
    49
     * <p>
jaroslav@1646
    50
     * During construction of the call site, the {@code createTargetHook} is invoked to
jaroslav@1646
    51
     * produce the actual target, as if by a call of the form
jaroslav@1646
    52
     * {@code (MethodHandle) createTargetHook.invoke(this)}.
jaroslav@1646
    53
     * <p>
jaroslav@1646
    54
     * Note that user code cannot perform such an action directly in a subclass constructor,
jaroslav@1646
    55
     * since the target must be fixed before the {@code ConstantCallSite} constructor returns.
jaroslav@1646
    56
     * <p>
jaroslav@1646
    57
     * The hook is said to bind the call site to a target method handle,
jaroslav@1646
    58
     * and a typical action would be {@code someTarget.bindTo(this)}.
jaroslav@1646
    59
     * However, the hook is free to take any action whatever,
jaroslav@1646
    60
     * including ignoring the call site and returning a constant target.
jaroslav@1646
    61
     * <p>
jaroslav@1646
    62
     * The result returned by the hook must be a method handle of exactly
jaroslav@1646
    63
     * the same type as the call site.
jaroslav@1646
    64
     * <p>
jaroslav@1646
    65
     * While the hook is being called, the new {@code ConstantCallSite}
jaroslav@1646
    66
     * object is in a partially constructed state.
jaroslav@1646
    67
     * In this state,
jaroslav@1646
    68
     * a call to {@code getTarget}, or any other attempt to use the target,
jaroslav@1646
    69
     * will result in an {@code IllegalStateException}.
jaroslav@1646
    70
     * It is legal at all times to obtain the call site's type using the {@code type} method.
jaroslav@1646
    71
     *
jaroslav@1646
    72
     * @param targetType the type of the method handle to be permanently associated with this call site
jaroslav@1646
    73
     * @param createTargetHook a method handle to invoke (on the call site) to produce the call site's target
jaroslav@1646
    74
     * @throws WrongMethodTypeException if the hook cannot be invoked on the required arguments,
jaroslav@1646
    75
     *         or if the target returned by the hook is not of the given {@code targetType}
jaroslav@1646
    76
     * @throws NullPointerException if the hook returns a null value
jaroslav@1646
    77
     * @throws ClassCastException if the hook returns something other than a {@code MethodHandle}
jaroslav@1646
    78
     * @throws Throwable anything else thrown by the hook function
jaroslav@1646
    79
     */
jaroslav@1646
    80
    protected ConstantCallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable {
jaroslav@1646
    81
        super(targetType, createTargetHook);
jaroslav@1646
    82
        isFrozen = true;
jaroslav@1646
    83
    }
jaroslav@1646
    84
jaroslav@1646
    85
    /**
jaroslav@1646
    86
     * Returns the target method of the call site, which behaves
jaroslav@1646
    87
     * like a {@code final} field of the {@code ConstantCallSite}.
jaroslav@1646
    88
     * That is, the target is always the original value passed
jaroslav@1646
    89
     * to the constructor call which created this instance.
jaroslav@1646
    90
     *
jaroslav@1646
    91
     * @return the immutable linkage state of this call site, a constant method handle
jaroslav@1646
    92
     * @throws IllegalStateException if the {@code ConstantCallSite} constructor has not completed
jaroslav@1646
    93
     */
jaroslav@1646
    94
    @Override public final MethodHandle getTarget() {
jaroslav@1646
    95
        if (!isFrozen)  throw new IllegalStateException();
jaroslav@1646
    96
        return target;
jaroslav@1646
    97
    }
jaroslav@1646
    98
jaroslav@1646
    99
    /**
jaroslav@1646
   100
     * Always throws an {@link UnsupportedOperationException}.
jaroslav@1646
   101
     * This kind of call site cannot change its target.
jaroslav@1646
   102
     * @param ignore a new target proposed for the call site, which is ignored
jaroslav@1646
   103
     * @throws UnsupportedOperationException because this kind of call site cannot change its target
jaroslav@1646
   104
     */
jaroslav@1646
   105
    @Override public final void setTarget(MethodHandle ignore) {
jaroslav@1646
   106
        throw new UnsupportedOperationException();
jaroslav@1646
   107
    }
jaroslav@1646
   108
jaroslav@1646
   109
    /**
jaroslav@1646
   110
     * Returns this call site's permanent target.
jaroslav@1646
   111
     * Since that target will never change, this is a correct implementation
jaroslav@1646
   112
     * of {@link CallSite#dynamicInvoker CallSite.dynamicInvoker}.
jaroslav@1646
   113
     * @return the immutable linkage state of this call site, a constant method handle
jaroslav@1646
   114
     * @throws IllegalStateException if the {@code ConstantCallSite} constructor has not completed
jaroslav@1646
   115
     */
jaroslav@1646
   116
    @Override
jaroslav@1646
   117
    public final MethodHandle dynamicInvoker() {
jaroslav@1646
   118
        return getTarget();
jaroslav@1646
   119
    }
jaroslav@1646
   120
}