rt/emul/compact/src/main/java/java/lang/invoke/ConstantCallSite.java
branchjdk8
changeset 1674 eca8e9c3ec3e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/invoke/ConstantCallSite.java	Sun Aug 17 20:09:05 2014 +0200
     1.3 @@ -0,0 +1,120 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.invoke;
    1.30 +
    1.31 +/**
    1.32 + * A {@code ConstantCallSite} is a {@link CallSite} whose target is permanent, and can never be changed.
    1.33 + * An {@code invokedynamic} instruction linked to a {@code ConstantCallSite} is permanently
    1.34 + * bound to the call site's target.
    1.35 + * @author John Rose, JSR 292 EG
    1.36 + */
    1.37 +public class ConstantCallSite extends CallSite {
    1.38 +    private final boolean isFrozen;
    1.39 +
    1.40 +    /**
    1.41 +     * Creates a call site with a permanent target.
    1.42 +     * @param target the target to be permanently associated with this call site
    1.43 +     * @throws NullPointerException if the proposed target is null
    1.44 +     */
    1.45 +    public ConstantCallSite(MethodHandle target) {
    1.46 +        super(target);
    1.47 +        isFrozen = true;
    1.48 +    }
    1.49 +
    1.50 +    /**
    1.51 +     * Creates a call site with a permanent target, possibly bound to the call site itself.
    1.52 +     * <p>
    1.53 +     * During construction of the call site, the {@code createTargetHook} is invoked to
    1.54 +     * produce the actual target, as if by a call of the form
    1.55 +     * {@code (MethodHandle) createTargetHook.invoke(this)}.
    1.56 +     * <p>
    1.57 +     * Note that user code cannot perform such an action directly in a subclass constructor,
    1.58 +     * since the target must be fixed before the {@code ConstantCallSite} constructor returns.
    1.59 +     * <p>
    1.60 +     * The hook is said to bind the call site to a target method handle,
    1.61 +     * and a typical action would be {@code someTarget.bindTo(this)}.
    1.62 +     * However, the hook is free to take any action whatever,
    1.63 +     * including ignoring the call site and returning a constant target.
    1.64 +     * <p>
    1.65 +     * The result returned by the hook must be a method handle of exactly
    1.66 +     * the same type as the call site.
    1.67 +     * <p>
    1.68 +     * While the hook is being called, the new {@code ConstantCallSite}
    1.69 +     * object is in a partially constructed state.
    1.70 +     * In this state,
    1.71 +     * a call to {@code getTarget}, or any other attempt to use the target,
    1.72 +     * will result in an {@code IllegalStateException}.
    1.73 +     * It is legal at all times to obtain the call site's type using the {@code type} method.
    1.74 +     *
    1.75 +     * @param targetType the type of the method handle to be permanently associated with this call site
    1.76 +     * @param createTargetHook a method handle to invoke (on the call site) to produce the call site's target
    1.77 +     * @throws WrongMethodTypeException if the hook cannot be invoked on the required arguments,
    1.78 +     *         or if the target returned by the hook is not of the given {@code targetType}
    1.79 +     * @throws NullPointerException if the hook returns a null value
    1.80 +     * @throws ClassCastException if the hook returns something other than a {@code MethodHandle}
    1.81 +     * @throws Throwable anything else thrown by the hook function
    1.82 +     */
    1.83 +    protected ConstantCallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable {
    1.84 +        super(targetType, createTargetHook);
    1.85 +        isFrozen = true;
    1.86 +    }
    1.87 +
    1.88 +    /**
    1.89 +     * Returns the target method of the call site, which behaves
    1.90 +     * like a {@code final} field of the {@code ConstantCallSite}.
    1.91 +     * That is, the target is always the original value passed
    1.92 +     * to the constructor call which created this instance.
    1.93 +     *
    1.94 +     * @return the immutable linkage state of this call site, a constant method handle
    1.95 +     * @throws IllegalStateException if the {@code ConstantCallSite} constructor has not completed
    1.96 +     */
    1.97 +    @Override public final MethodHandle getTarget() {
    1.98 +        if (!isFrozen)  throw new IllegalStateException();
    1.99 +        return target;
   1.100 +    }
   1.101 +
   1.102 +    /**
   1.103 +     * Always throws an {@link UnsupportedOperationException}.
   1.104 +     * This kind of call site cannot change its target.
   1.105 +     * @param ignore a new target proposed for the call site, which is ignored
   1.106 +     * @throws UnsupportedOperationException because this kind of call site cannot change its target
   1.107 +     */
   1.108 +    @Override public final void setTarget(MethodHandle ignore) {
   1.109 +        throw new UnsupportedOperationException();
   1.110 +    }
   1.111 +
   1.112 +    /**
   1.113 +     * Returns this call site's permanent target.
   1.114 +     * Since that target will never change, this is a correct implementation
   1.115 +     * of {@link CallSite#dynamicInvoker CallSite.dynamicInvoker}.
   1.116 +     * @return the immutable linkage state of this call site, a constant method handle
   1.117 +     * @throws IllegalStateException if the {@code ConstantCallSite} constructor has not completed
   1.118 +     */
   1.119 +    @Override
   1.120 +    public final MethodHandle dynamicInvoker() {
   1.121 +        return getTarget();
   1.122 +    }
   1.123 +}