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