1 /* 2 * Copyright (c) 2020, 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 27 package java.lang.invoke; 28 29 import jdk.internal.vm.annotation.ForceInline; 30 import jdk.internal.vm.annotation.Stable; 31 32 import java.util.List; 33 import java.util.function.BiFunction; 34 35 /** 36 * An indirect var handle can be thought of as an aggregate of the method handles implementing its supported access modes. 37 * Its varform contains no method name table (given that some of the method handles composing a bound var handle might 38 * not be direct). The set of method handles constituting an inditrect var handle are retrieved lazily, to minimize 39 * code spinning (since not all the access modes will be used anyway). 40 * Indirect var handles are useful when constructing var handle adapters - that is, an adapter var handle 41 * can be constructed by extracting the method handles constituting the target var handle, adapting them 42 * (using the method handle combinator API) and then repackaging the adapted method handles into a new, indirect 43 * var handle. 44 */ 45 /* package */ class IndirectVarHandle extends VarHandle { 46 47 @Stable 48 private final MethodHandle[] handleMap = new MethodHandle[AccessMode.values().length]; 49 private final VarHandle directTarget; // cache, for performance reasons 50 private final VarHandle target; 51 private final BiFunction<AccessMode, MethodHandle, MethodHandle> handleFactory; 52 private final Class<?> value; 53 private final Class<?>[] coordinates; 54 55 IndirectVarHandle(VarHandle target, Class<?> value, Class<?>[] coordinates, BiFunction<AccessMode, MethodHandle, MethodHandle> handleFactory) { 56 super(new VarForm(value, coordinates)); 57 this.handleFactory = handleFactory; 58 this.target = target; 59 this.directTarget = target.asDirect(); 60 this.value = value; 61 this.coordinates = coordinates; 62 } 63 64 @Override 65 public Class<?> varType() { 66 return value; 67 } 68 69 @Override 70 public List<Class<?>> coordinateTypes() { 71 return List.of(coordinates); 72 } 73 74 @Override 75 MethodType accessModeTypeUncached(AccessMode accessMode) { 76 return accessMode.at.accessModeType(directTarget.getClass(), value, coordinates); 77 } 78 79 @Override 80 boolean isDirect() { 81 return false; 82 } 83 84 @Override 85 VarHandle asDirect() { 86 return directTarget; 87 } 88 89 VarHandle target() { 90 return target; 91 } 92 93 @Override 94 @ForceInline 95 MethodHandle getMethodHandle(int mode) { 96 MethodHandle handle = handleMap[mode]; 97 if (handle == null) { 98 MethodHandle targetHandle = target.getMethodHandle(mode); // might throw UOE of access mode is not supported, which is ok 99 handle = handleMap[mode] = handleFactory.apply(AccessMode.values()[mode], targetHandle); 100 } 101 return handle; 102 } 103 104 @Override 105 public MethodHandle toMethodHandle(AccessMode accessMode) { 106 return getMethodHandle(accessMode.ordinal()).bindTo(directTarget); 107 } 108 }