Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 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 | /* |
| 27 | * This file is available under and governed by the GNU General Public |
| 28 | * License version 2 only, as published by the Free Software Foundation. |
| 29 | * However, the following notice accompanied the original version of this |
| 30 | * file, and Oracle licenses the original version of this file under the BSD |
| 31 | * license: |
| 32 | */ |
| 33 | /* |
| 34 | Copyright 2009-2013 Attila Szegedi |
| 35 | |
| 36 | Licensed under both the Apache License, Version 2.0 (the "Apache License") |
| 37 | and the BSD License (the "BSD License"), with licensee being free to |
| 38 | choose either of the two at their discretion. |
| 39 | |
| 40 | You may not use this file except in compliance with either the Apache |
| 41 | License or the BSD License. |
| 42 | |
| 43 | If you choose to use this file in compliance with the Apache License, the |
| 44 | following notice applies to you: |
| 45 | |
| 46 | You may obtain a copy of the Apache License at |
| 47 | |
| 48 | http://www.apache.org/licenses/LICENSE-2.0 |
| 49 | |
| 50 | Unless required by applicable law or agreed to in writing, software |
| 51 | distributed under the License is distributed on an "AS IS" BASIS, |
| 52 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 53 | implied. See the License for the specific language governing |
| 54 | permissions and limitations under the License. |
| 55 | |
| 56 | If you choose to use this file in compliance with the BSD License, the |
| 57 | following notice applies to you: |
| 58 | |
| 59 | Redistribution and use in source and binary forms, with or without |
| 60 | modification, are permitted provided that the following conditions are |
| 61 | met: |
| 62 | * Redistributions of source code must retain the above copyright |
| 63 | notice, this list of conditions and the following disclaimer. |
| 64 | * Redistributions in binary form must reproduce the above copyright |
| 65 | notice, this list of conditions and the following disclaimer in the |
| 66 | documentation and/or other materials provided with the distribution. |
| 67 | * Neither the name of the copyright holder nor the names of |
| 68 | contributors may be used to endorse or promote products derived from |
| 69 | this software without specific prior written permission. |
| 70 | |
| 71 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 72 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 73 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 74 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER |
| 75 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 76 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 77 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 78 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 79 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 80 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 81 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 82 | */ |
| 83 | |
Attila Szegedi | 505e509 | 2015-11-24 10:19:34 +0100 | [diff] [blame] | 84 | package jdk.dynalink.beans; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 85 | |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 86 | import java.lang.invoke.MethodHandle; |
| 87 | import java.lang.invoke.MethodHandles; |
| 88 | import java.lang.invoke.MethodType; |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 89 | import java.lang.reflect.AccessibleObject; |
| 90 | import java.lang.reflect.Constructor; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 91 | import java.lang.reflect.Field; |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 92 | import java.lang.reflect.Member; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 93 | import java.lang.reflect.Method; |
| 94 | import java.lang.reflect.Modifier; |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 95 | import java.util.Arrays; |
Attila Szegedi | e11a9b1 | 2013-07-15 12:33:48 +0200 | [diff] [blame] | 96 | import java.util.Collections; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 97 | import java.util.HashMap; |
| 98 | import java.util.List; |
| 99 | import java.util.Map; |
Attila Szegedi | 534ff3d | 2015-11-11 14:54:09 +0100 | [diff] [blame] | 100 | import java.util.Set; |
Attila Szegedi | 505e509 | 2015-11-24 10:19:34 +0100 | [diff] [blame] | 101 | import jdk.dynalink.CallSiteDescriptor; |
| 102 | import jdk.dynalink.CompositeOperation; |
| 103 | import jdk.dynalink.NamedOperation; |
| 104 | import jdk.dynalink.Operation; |
| 105 | import jdk.dynalink.StandardOperation; |
| 106 | import jdk.dynalink.beans.GuardedInvocationComponent.ValidationType; |
| 107 | import jdk.dynalink.internal.InternalTypeUtilities; |
| 108 | import jdk.dynalink.linker.GuardedInvocation; |
| 109 | import jdk.dynalink.linker.GuardingDynamicLinker; |
| 110 | import jdk.dynalink.linker.LinkRequest; |
| 111 | import jdk.dynalink.linker.LinkerServices; |
| 112 | import jdk.dynalink.linker.support.Guards; |
| 113 | import jdk.dynalink.linker.support.Lookup; |
Chris Hegarty | 9ec1590 | 2016-04-15 16:19:15 +0100 | [diff] [blame^] | 114 | import jdk.internal.reflect.CallerSensitive; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 115 | |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 116 | /** |
| 117 | * A base class for both {@link StaticClassLinker} and {@link BeanLinker}. Deals with common aspects of property |
| 118 | * exposure and method calls for both static and instance facets of a class. |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 119 | */ |
| 120 | abstract class AbstractJavaLinker implements GuardingDynamicLinker { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 121 | |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 122 | final Class<?> clazz; |
| 123 | private final MethodHandle classGuard; |
| 124 | private final MethodHandle assignableGuard; |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 125 | private final Map<String, AnnotatedDynamicMethod> propertyGetters = new HashMap<>(); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 126 | private final Map<String, DynamicMethod> propertySetters = new HashMap<>(); |
| 127 | private final Map<String, DynamicMethod> methods = new HashMap<>(); |
| 128 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 129 | AbstractJavaLinker(final Class<?> clazz, final MethodHandle classGuard) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 130 | this(clazz, classGuard, classGuard); |
| 131 | } |
| 132 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 133 | AbstractJavaLinker(final Class<?> clazz, final MethodHandle classGuard, final MethodHandle assignableGuard) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 134 | this.clazz = clazz; |
| 135 | this.classGuard = classGuard; |
| 136 | this.assignableGuard = assignableGuard; |
| 137 | |
| 138 | final FacetIntrospector introspector = createFacetIntrospector(); |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 139 | // Add methods and properties |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 140 | for(final Method method: introspector.getMethods()) { |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 141 | final String name = method.getName(); |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 142 | // Add method |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 143 | addMember(name, method, methods); |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 144 | // Add the method as a property getter and/or setter |
| 145 | if(name.startsWith("get") && name.length() > 3 && method.getParameterTypes().length == 0) { |
| 146 | // Property getter |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 147 | setPropertyGetter(method, 3); |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 148 | } else if(name.startsWith("is") && name.length() > 2 && method.getParameterTypes().length == 0 && |
| 149 | method.getReturnType() == boolean.class) { |
| 150 | // Boolean property getter |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 151 | setPropertyGetter(method, 2); |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 152 | } else if(name.startsWith("set") && name.length() > 3 && method.getParameterTypes().length == 1) { |
| 153 | // Property setter |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 154 | addMember(decapitalize(name.substring(3)), method, propertySetters); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 155 | } |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 156 | } |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 157 | |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 158 | // Add field getter/setters as property getters/setters. |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 159 | for(final Field field: introspector.getFields()) { |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 160 | final String name = field.getName(); |
| 161 | // Only add a property getter when one is not defined already as a getXxx()/isXxx() method. |
| 162 | if(!propertyGetters.containsKey(name)) { |
| 163 | setPropertyGetter(name, introspector.unreflectGetter(field), ValidationType.EXACT_CLASS); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 164 | } |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 165 | if(!(Modifier.isFinal(field.getModifiers()) || propertySetters.containsKey(name))) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 166 | addMember(name, new SimpleDynamicMethod(introspector.unreflectSetter(field), clazz, name), |
| 167 | propertySetters); |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 170 | |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 171 | // Add inner classes, but only those for which we don't hide a property with it |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 172 | for(final Map.Entry<String, MethodHandle> innerClassSpec: introspector.getInnerClassGetters().entrySet()) { |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 173 | final String name = innerClassSpec.getKey(); |
| 174 | if(!propertyGetters.containsKey(name)) { |
| 175 | setPropertyGetter(name, innerClassSpec.getValue(), ValidationType.EXACT_CLASS); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 176 | } |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 180 | private static String decapitalize(final String str) { |
Attila Szegedi | a1ade22 | 2013-02-27 15:20:26 +0100 | [diff] [blame] | 181 | assert str != null; |
| 182 | if(str.isEmpty()) { |
| 183 | return str; |
| 184 | } |
| 185 | |
| 186 | final char c0 = str.charAt(0); |
| 187 | if(Character.isLowerCase(c0)) { |
| 188 | return str; |
| 189 | } |
| 190 | |
| 191 | // If it has two consecutive upper-case characters, i.e. "URL", don't decapitalize |
| 192 | if(str.length() > 1 && Character.isUpperCase(str.charAt(1))) { |
| 193 | return str; |
| 194 | } |
| 195 | |
| 196 | final char c[] = str.toCharArray(); |
| 197 | c[0] = Character.toLowerCase(c0); |
| 198 | return new String(c); |
| 199 | } |
| 200 | |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 201 | abstract FacetIntrospector createFacetIntrospector(); |
| 202 | |
Attila Szegedi | 534ff3d | 2015-11-11 14:54:09 +0100 | [diff] [blame] | 203 | Set<String> getReadablePropertyNames() { |
Attila Szegedi | e11a9b1 | 2013-07-15 12:33:48 +0200 | [diff] [blame] | 204 | return getUnmodifiableKeys(propertyGetters); |
| 205 | } |
| 206 | |
Attila Szegedi | 534ff3d | 2015-11-11 14:54:09 +0100 | [diff] [blame] | 207 | Set<String> getWritablePropertyNames() { |
Attila Szegedi | e11a9b1 | 2013-07-15 12:33:48 +0200 | [diff] [blame] | 208 | return getUnmodifiableKeys(propertySetters); |
| 209 | } |
| 210 | |
Attila Szegedi | 534ff3d | 2015-11-11 14:54:09 +0100 | [diff] [blame] | 211 | Set<String> getMethodNames() { |
Attila Szegedi | e11a9b1 | 2013-07-15 12:33:48 +0200 | [diff] [blame] | 212 | return getUnmodifiableKeys(methods); |
| 213 | } |
| 214 | |
Attila Szegedi | 534ff3d | 2015-11-11 14:54:09 +0100 | [diff] [blame] | 215 | private static Set<String> getUnmodifiableKeys(final Map<String, ?> m) { |
| 216 | return Collections.unmodifiableSet(m.keySet()); |
Attila Szegedi | e11a9b1 | 2013-07-15 12:33:48 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 219 | /** |
| 220 | * Sets the specified dynamic method to be the property getter for the specified property. Note that you can only |
| 221 | * use this when you're certain that the method handle does not belong to a caller-sensitive method. For properties |
| 222 | * that are caller-sensitive, you must use {@link #setPropertyGetter(String, SingleDynamicMethod, ValidationType)} |
| 223 | * instead. |
| 224 | * @param name name of the property |
| 225 | * @param handle the method handle that implements the property getter |
| 226 | * @param validationType the validation type for the property |
| 227 | */ |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 228 | private void setPropertyGetter(final String name, final SingleDynamicMethod handle, final ValidationType validationType) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 229 | propertyGetters.put(name, new AnnotatedDynamicMethod(handle, validationType)); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 232 | /** |
| 233 | * Sets the specified reflective method to be the property getter for the specified property. |
| 234 | * @param getter the getter method |
| 235 | * @param prefixLen the getter prefix in the method name; should be 3 for getter names starting with "get" and 2 for |
| 236 | * names starting with "is". |
| 237 | */ |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 238 | private void setPropertyGetter(final Method getter, final int prefixLen) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 239 | setPropertyGetter(decapitalize(getter.getName().substring(prefixLen)), createDynamicMethod( |
| 240 | getMostGenericGetter(getter)), ValidationType.INSTANCE_OF); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Sets the specified method handle to be the property getter for the specified property. Note that you can only |
| 245 | * use this when you're certain that the method handle does not belong to a caller-sensitive method. For properties |
| 246 | * that are caller-sensitive, you must use {@link #setPropertyGetter(String, SingleDynamicMethod, ValidationType)} |
| 247 | * instead. |
| 248 | * @param name name of the property |
| 249 | * @param handle the method handle that implements the property getter |
| 250 | * @param validationType the validation type for the property |
| 251 | */ |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 252 | void setPropertyGetter(final String name, final MethodHandle handle, final ValidationType validationType) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 253 | setPropertyGetter(name, new SimpleDynamicMethod(handle, clazz, name), validationType); |
| 254 | } |
| 255 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 256 | private void addMember(final String name, final AccessibleObject ao, final Map<String, DynamicMethod> methodMap) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 257 | addMember(name, createDynamicMethod(ao), methodMap); |
| 258 | } |
| 259 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 260 | private void addMember(final String name, final SingleDynamicMethod method, final Map<String, DynamicMethod> methodMap) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 261 | final DynamicMethod existingMethod = methodMap.get(name); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 262 | final DynamicMethod newMethod = mergeMethods(method, existingMethod, clazz, name); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 263 | if(newMethod != existingMethod) { |
| 264 | methodMap.put(name, newMethod); |
| 265 | } |
| 266 | } |
| 267 | |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 268 | /** |
| 269 | * Given one or more reflective methods or constructors, creates a dynamic method that represents them all. The |
| 270 | * methods should represent all overloads of the same name (or all constructors of the class). |
| 271 | * @param members the reflective members |
| 272 | * @param clazz the class declaring the reflective members |
| 273 | * @param name the common name of the reflective members. |
| 274 | * @return a dynamic method representing all the specified reflective members. |
| 275 | */ |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 276 | static DynamicMethod createDynamicMethod(final Iterable<? extends AccessibleObject> members, final Class<?> clazz, final String name) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 277 | DynamicMethod dynMethod = null; |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 278 | for(final AccessibleObject method: members) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 279 | dynMethod = mergeMethods(createDynamicMethod(method), dynMethod, clazz, name); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 280 | } |
| 281 | return dynMethod; |
| 282 | } |
| 283 | |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 284 | /** |
| 285 | * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will |
| 286 | * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive |
| 287 | * dynamic method when needed. |
| 288 | * @param m the reflective member |
| 289 | * @return the single dynamic method representing the reflective member |
| 290 | */ |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 291 | private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) { |
Attila Szegedi | eba3009 | 2015-10-19 08:23:03 +0200 | [diff] [blame] | 292 | if (m.isAnnotationPresent(CallerSensitive.class)) { |
Attila Szegedi | 10ebc44 | 2014-11-03 07:29:46 +0100 | [diff] [blame] | 293 | // Method has @CallerSensitive annotation |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 294 | return new CallerSensitiveDynamicMethod(m); |
| 295 | } |
Attila Szegedi | 10ebc44 | 2014-11-03 07:29:46 +0100 | [diff] [blame] | 296 | // Method has no @CallerSensitive annotation |
| 297 | final MethodHandle mh; |
| 298 | try { |
| 299 | mh = unreflectSafely(m); |
| 300 | } catch (final IllegalAccessError e) { |
| 301 | // java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't |
| 302 | // marked with the annotation. In this case, we'll fall back to treating it as caller sensitive. |
| 303 | return new CallerSensitiveDynamicMethod(m); |
| 304 | } |
| 305 | // Proceed with non-caller sensitive |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 306 | final Member member = (Member)m; |
Attila Szegedi | 10ebc44 | 2014-11-03 07:29:46 +0100 | [diff] [blame] | 307 | return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Unreflects a method handle from a Method or a Constructor using safe (zero-privilege) unreflection. Should be |
| 312 | * only used for methods and constructors that are not caller sensitive. If a caller sensitive method were |
| 313 | * unreflected through this mechanism, it would not be a security issue, but would be bound to the zero-privilege |
| 314 | * unreflector as its caller, and thus completely useless. |
| 315 | * @param m the method or constructor |
| 316 | * @return the method handle |
| 317 | */ |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 318 | private static MethodHandle unreflectSafely(final AccessibleObject m) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 319 | if(m instanceof Method) { |
| 320 | final Method reflMethod = (Method)m; |
Attila Szegedi | 91e5fbd | 2013-07-24 11:13:24 +0200 | [diff] [blame] | 321 | final MethodHandle handle = Lookup.PUBLIC.unreflect(reflMethod); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 322 | if(Modifier.isStatic(reflMethod.getModifiers())) { |
| 323 | return StaticClassIntrospector.editStaticMethodHandle(handle); |
| 324 | } |
| 325 | return handle; |
| 326 | } |
Attila Szegedi | 91e5fbd | 2013-07-24 11:13:24 +0200 | [diff] [blame] | 327 | return StaticClassIntrospector.editConstructorMethodHandle(Lookup.PUBLIC.unreflectConstructor((Constructor<?>)m)); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 330 | private static DynamicMethod mergeMethods(final SingleDynamicMethod method, final DynamicMethod existing, final Class<?> clazz, final String name) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 331 | if(existing == null) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 332 | return method; |
| 333 | } else if(existing.contains(method)) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 334 | return existing; |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 335 | } else if(existing instanceof SingleDynamicMethod) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 336 | final OverloadedDynamicMethod odm = new OverloadedDynamicMethod(clazz, name); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 337 | odm.addMethod(((SingleDynamicMethod)existing)); |
| 338 | odm.addMethod(method); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 339 | return odm; |
| 340 | } else if(existing instanceof OverloadedDynamicMethod) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 341 | ((OverloadedDynamicMethod)existing).addMethod(method); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 342 | return existing; |
| 343 | } |
| 344 | throw new AssertionError(); |
| 345 | } |
| 346 | |
| 347 | @Override |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 348 | public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 349 | throws Exception { |
Attila Szegedi | 57a2ec8 | 2015-10-19 08:39:06 +0200 | [diff] [blame] | 350 | final CallSiteDescriptor callSiteDescriptor = request.getCallSiteDescriptor(); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 351 | |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 352 | final MissingMemberHandlerFactory missingMemberHandlerFactory; |
| 353 | final LinkerServices directLinkerServices; |
| 354 | if (linkerServices instanceof LinkerServicesWithMissingMemberHandlerFactory) { |
| 355 | final LinkerServicesWithMissingMemberHandlerFactory lswmmhf = ((LinkerServicesWithMissingMemberHandlerFactory)linkerServices); |
| 356 | missingMemberHandlerFactory = lswmmhf.missingMemberHandlerFactory; |
| 357 | directLinkerServices = lswmmhf.linkerServices; |
| 358 | } else { |
| 359 | missingMemberHandlerFactory = null; |
| 360 | directLinkerServices = linkerServices; |
| 361 | } |
| 362 | |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 363 | // Handle NamedOperation(CALL_METHOD, name) separately |
| 364 | final Operation operation = callSiteDescriptor.getOperation(); |
| 365 | if (operation instanceof NamedOperation) { |
| 366 | final NamedOperation namedOperation = (NamedOperation)operation; |
| 367 | if (namedOperation.getBaseOperation() == StandardOperation.CALL_METHOD) { |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 368 | final GuardedInvocation inv = |
| 369 | createGuardedDynamicMethodInvocation(callSiteDescriptor, |
| 370 | directLinkerServices, namedOperation.getName().toString(), methods); |
| 371 | if (inv == null) { |
| 372 | return createNoSuchMemberHandler(missingMemberHandlerFactory, |
| 373 | request, directLinkerServices).getGuardedInvocation(); |
| 374 | } |
| 375 | return inv; |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 376 | } |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 377 | } |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 378 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 379 | final GuardedInvocationComponent gic = getGuardedInvocationComponent( |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 380 | new ComponentLinkRequest(request, directLinkerServices, |
| 381 | missingMemberHandlerFactory)); |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 382 | return gic != null ? gic.getGuardedInvocation() : null; |
| 383 | } |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 384 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 385 | static final class ComponentLinkRequest { |
| 386 | final LinkRequest linkRequest; |
| 387 | final LinkerServices linkerServices; |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 388 | final MissingMemberHandlerFactory missingMemberHandlerFactory; |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 389 | final List<Operation> operations; |
| 390 | final Object name; |
| 391 | |
| 392 | ComponentLinkRequest(final LinkRequest linkRequest, |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 393 | final LinkerServices linkerServices, |
| 394 | final MissingMemberHandlerFactory missingMemberHandlerFactory) { |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 395 | this.linkRequest = linkRequest; |
| 396 | this.linkerServices = linkerServices; |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 397 | this.missingMemberHandlerFactory = missingMemberHandlerFactory; |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 398 | final Operation operation = linkRequest.getCallSiteDescriptor().getOperation(); |
| 399 | this.operations = Arrays.asList( |
| 400 | CompositeOperation.getOperations( |
| 401 | NamedOperation.getBaseOperation(operation))); |
| 402 | this.name = NamedOperation.getName(operation); |
| 403 | } |
| 404 | |
| 405 | private ComponentLinkRequest(final LinkRequest linkRequest, |
| 406 | final LinkerServices linkerServices, |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 407 | final MissingMemberHandlerFactory missingMemberHandlerFactory, |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 408 | final List<Operation> operations, final Object name) { |
| 409 | this.linkRequest = linkRequest; |
| 410 | this.linkerServices = linkerServices; |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 411 | this.missingMemberHandlerFactory = missingMemberHandlerFactory; |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 412 | this.operations = operations; |
| 413 | this.name = name; |
| 414 | } |
| 415 | |
| 416 | CallSiteDescriptor getDescriptor() { |
| 417 | return linkRequest.getCallSiteDescriptor(); |
| 418 | } |
| 419 | |
| 420 | ComponentLinkRequest popOperations() { |
| 421 | return new ComponentLinkRequest(linkRequest, linkerServices, |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 422 | missingMemberHandlerFactory, |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 423 | operations.subList(1, operations.size()), name); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | protected GuardedInvocationComponent getGuardedInvocationComponent(final ComponentLinkRequest req) |
| 428 | throws Exception { |
| 429 | final Operation op = req.operations.get(0); |
| 430 | if (op instanceof StandardOperation) { |
| 431 | switch((StandardOperation)op) { |
| 432 | case GET_PROPERTY: return getPropertyGetter(req.popOperations()); |
| 433 | case SET_PROPERTY: return getPropertySetter(req.popOperations()); |
| 434 | case GET_METHOD: return getMethodGetter(req.popOperations()); |
| 435 | default: |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 436 | } |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 437 | } |
| 438 | return null; |
| 439 | } |
| 440 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 441 | GuardedInvocationComponent getNextComponent(final ComponentLinkRequest req) throws Exception { |
| 442 | if (req.operations.isEmpty()) { |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 443 | return createNoSuchMemberHandler(req.missingMemberHandlerFactory, |
| 444 | req.linkRequest, req.linkerServices); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 445 | } |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 446 | final GuardedInvocationComponent gic = getGuardedInvocationComponent(req); |
| 447 | if (gic != null) { |
| 448 | return gic; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 449 | } |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 450 | return getNextComponent(req.popOperations()); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Attila Szegedi | df1606c | 2016-01-14 13:24:03 +0100 | [diff] [blame] | 453 | private GuardedInvocationComponent createNoSuchMemberHandler( |
| 454 | final MissingMemberHandlerFactory missingMemberHandlerFactory, |
| 455 | final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { |
| 456 | if (missingMemberHandlerFactory == null) { |
| 457 | return null; |
| 458 | } |
| 459 | final MethodHandle handler = missingMemberHandlerFactory.createMissingMemberHandler(linkRequest, linkerServices); |
| 460 | if (handler == null) { |
| 461 | return null; |
| 462 | } |
| 463 | final MethodType type = linkRequest.getCallSiteDescriptor().getMethodType(); |
| 464 | // The returned handler is allowed to differ in return type. |
| 465 | assert handler.type().changeReturnType(type.returnType()).equals(type); |
| 466 | return getClassGuardedInvocationComponent(handler, type); |
| 467 | } |
| 468 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 469 | static final <T> List<T> pop(final List<T> l) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 470 | return l.subList(1, l.size()); |
| 471 | } |
| 472 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 473 | MethodHandle getClassGuard(final CallSiteDescriptor desc) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 474 | return getClassGuard(desc.getMethodType()); |
| 475 | } |
| 476 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 477 | MethodHandle getClassGuard(final MethodType type) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 478 | return Guards.asType(classGuard, type); |
| 479 | } |
| 480 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 481 | GuardedInvocationComponent getClassGuardedInvocationComponent(final MethodHandle invocation, final MethodType type) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 482 | return new GuardedInvocationComponent(invocation, getClassGuard(type), clazz, ValidationType.EXACT_CLASS); |
| 483 | } |
| 484 | |
Attila Szegedi | 534ff3d | 2015-11-11 14:54:09 +0100 | [diff] [blame] | 485 | abstract SingleDynamicMethod getConstructorMethod(final String signature); |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 486 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 487 | private MethodHandle getAssignableGuard(final MethodType type) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 488 | return Guards.asType(assignableGuard, type); |
| 489 | } |
| 490 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 491 | private GuardedInvocation createGuardedDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor, |
| 492 | final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap){ |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 493 | final MethodHandle inv = getDynamicMethodInvocation(callSiteDescriptor, linkerServices, methodName, methodMap); |
| 494 | return inv == null ? null : new GuardedInvocation(inv, getClassGuard(callSiteDescriptor.getMethodType())); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 497 | private MethodHandle getDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor, |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 498 | final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 499 | final DynamicMethod dynaMethod = getDynamicMethod(methodName, methodMap); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 500 | return dynaMethod != null ? dynaMethod.getInvocation(callSiteDescriptor, linkerServices) : null; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 503 | private DynamicMethod getDynamicMethod(final String methodName, final Map<String, DynamicMethod> methodMap) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 504 | final DynamicMethod dynaMethod = methodMap.get(methodName); |
| 505 | return dynaMethod != null ? dynaMethod : getExplicitSignatureDynamicMethod(methodName, methodMap); |
| 506 | } |
| 507 | |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 508 | private SingleDynamicMethod getExplicitSignatureDynamicMethod(final String fullName, |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 509 | final Map<String, DynamicMethod> methodsMap) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 510 | // What's below is meant to support the "name(type, type, ...)" syntax that programmers can use in a method name |
| 511 | // to manually pin down an exact overloaded variant. This is not usually required, as the overloaded method |
| 512 | // resolution works correctly in almost every situation. However, in presence of many language-specific |
| 513 | // conversions with a radically dynamic language, most overloaded methods will end up being constantly selected |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 514 | // at invocation time, so a programmer knowledgeable of the situation might choose to pin down an exact overload |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 515 | // for performance reasons. |
| 516 | |
| 517 | // Is the method name lexically of the form "name(types)"? |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 518 | final int lastChar = fullName.length() - 1; |
| 519 | if(fullName.charAt(lastChar) != ')') { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 520 | return null; |
| 521 | } |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 522 | final int openBrace = fullName.indexOf('('); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 523 | if(openBrace == -1) { |
| 524 | return null; |
| 525 | } |
| 526 | |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 527 | final String name = fullName.substring(0, openBrace); |
| 528 | final String signature = fullName.substring(openBrace + 1, lastChar); |
| 529 | |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 530 | // Find an existing method for the "name" part |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 531 | final DynamicMethod simpleNamedMethod = methodsMap.get(name); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 532 | if(simpleNamedMethod == null) { |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 533 | // explicit signature constructor access |
| 534 | // Java.type("java.awt.Color")["(int,int,int)"] |
| 535 | // will get Color(int,int,int) constructor of Color class. |
| 536 | if (name.isEmpty()) { |
| 537 | return getConstructorMethod(signature); |
| 538 | } |
| 539 | |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 540 | return null; |
| 541 | } |
| 542 | |
| 543 | // Try to get a narrowed dynamic method for the explicit parameter types. |
Athijegannathan Sundararajan | 14eb01c | 2014-07-03 23:03:37 +0530 | [diff] [blame] | 544 | return simpleNamedMethod.getMethodForExactParamTypes(signature); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | private static final MethodHandle IS_METHOD_HANDLE_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType( |
| 548 | boolean.class, MethodHandle.class)); |
| 549 | private static final MethodHandle CONSTANT_NULL_DROP_METHOD_HANDLE = MethodHandles.dropArguments( |
| 550 | MethodHandles.constant(Object.class, null), 0, MethodHandle.class); |
| 551 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 552 | private GuardedInvocationComponent getPropertySetter(final ComponentLinkRequest req) throws Exception { |
| 553 | if (req.name == null) { |
| 554 | return getUnnamedPropertySetter(req); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 555 | } |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 556 | return getNamedPropertySetter(req); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 559 | private GuardedInvocationComponent getUnnamedPropertySetter(final ComponentLinkRequest req) throws Exception { |
| 560 | final CallSiteDescriptor callSiteDescriptor = req.getDescriptor(); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 561 | // Must have three arguments: target object, property name, and property value. |
| 562 | assertParameterCount(callSiteDescriptor, 3); |
| 563 | |
| 564 | // We want setters that conform to "Object(O, V)". Note, we aren't doing "R(O, V)" as it might not be |
| 565 | // valid for us to convert return values proactively. Also, since we don't know what setters will be |
| 566 | // invoked, we'll conservatively presume Object return type. The one exception is void return. |
| 567 | final MethodType origType = callSiteDescriptor.getMethodType(); |
| 568 | final MethodType type = origType.returnType() == void.class ? origType : origType.changeReturnType(Object.class); |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 569 | final LinkerServices linkerServices = req.linkerServices; |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 570 | |
| 571 | // What's below is basically: |
| 572 | // foldArguments(guardWithTest(isNotNull, invoke, null|nextComponent.invocation), |
| 573 | // get_setter_handle(type, linkerServices)) |
| 574 | // only with a bunch of method signature adjustments. Basically, retrieve method setter |
| 575 | // MethodHandle; if it is non-null, invoke it, otherwise either return null, or delegate to next |
| 576 | // component's invocation. |
| 577 | |
| 578 | // Call site type is "ret_type(object_type,property_name_type,property_value_type)", which we'll |
| 579 | // abbreviate to R(O, N, V) going forward, although we don't really use R here (see above about using |
| 580 | // Object return type). |
| 581 | final MethodType setterType = type.dropParameterTypes(1, 2); |
| 582 | // Bind property setter handle to the expected setter type and linker services. Type is |
| 583 | // MethodHandle(Object, String, Object) |
| 584 | final MethodHandle boundGetter = MethodHandles.insertArguments(getPropertySetterHandle, 0, |
| 585 | callSiteDescriptor.changeMethodType(setterType), linkerServices); |
| 586 | |
| 587 | // Cast getter to MethodHandle(O, N, V) |
| 588 | final MethodHandle typedGetter = linkerServices.asType(boundGetter, type.changeReturnType( |
| 589 | MethodHandle.class)); |
| 590 | |
| 591 | // Handle to invoke the setter R(MethodHandle, O, V) |
| 592 | final MethodHandle invokeHandle = MethodHandles.exactInvoker(setterType); |
| 593 | // Handle to invoke the setter, dropping unnecessary fold arguments R(MethodHandle, O, N, V) |
| 594 | final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandle, 2, type.parameterType( |
| 595 | 1)); |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 596 | final GuardedInvocationComponent nextComponent = getNextComponent(req); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 597 | |
| 598 | final MethodHandle fallbackFolded; |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 599 | if (nextComponent == null) { |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 600 | // Object(MethodHandle)->Object(MethodHandle, O, N, V); returns constant null |
| 601 | fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_METHOD_HANDLE, 1, |
| 602 | type.parameterList()).asType(type.insertParameterTypes(0, MethodHandle.class)); |
| 603 | } else { |
| 604 | // Object(O, N, V)->Object(MethodHandle, O, N, V); adapts the next component's invocation to drop the |
| 605 | // extra argument resulting from fold |
| 606 | fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(), |
| 607 | 0, MethodHandle.class); |
| 608 | } |
| 609 | |
| 610 | // fold(R(MethodHandle, O, N, V), MethodHandle(O, N, V)) |
| 611 | final MethodHandle compositeSetter = MethodHandles.foldArguments(MethodHandles.guardWithTest( |
| 612 | IS_METHOD_HANDLE_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter); |
| 613 | if(nextComponent == null) { |
| 614 | return getClassGuardedInvocationComponent(compositeSetter, type); |
| 615 | } |
| 616 | return nextComponent.compose(compositeSetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS); |
| 617 | } |
| 618 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 619 | private GuardedInvocationComponent getNamedPropertySetter(final ComponentLinkRequest req) throws Exception { |
| 620 | final CallSiteDescriptor callSiteDescriptor = req.getDescriptor(); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 621 | // Must have two arguments: target object and property value |
| 622 | assertParameterCount(callSiteDescriptor, 2); |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 623 | final GuardedInvocation gi = createGuardedDynamicMethodInvocation(callSiteDescriptor, req.linkerServices, |
| 624 | req.name.toString(), propertySetters); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 625 | // If we have a property setter with this name, this composite operation will always stop here |
| 626 | if(gi != null) { |
| 627 | return new GuardedInvocationComponent(gi, clazz, ValidationType.EXACT_CLASS); |
| 628 | } |
| 629 | // If we don't have a property setter with this name, always fall back to the next operation in the |
| 630 | // composite (if any) |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 631 | return getNextComponent(req); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | private static final Lookup privateLookup = new Lookup(MethodHandles.lookup()); |
| 635 | |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 636 | private static final MethodHandle IS_ANNOTATED_METHOD_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType( |
| 637 | boolean.class, AnnotatedDynamicMethod.class)); |
| 638 | private static final MethodHandle CONSTANT_NULL_DROP_ANNOTATED_METHOD = MethodHandles.dropArguments( |
| 639 | MethodHandles.constant(Object.class, null), 0, AnnotatedDynamicMethod.class); |
| 640 | private static final MethodHandle GET_ANNOTATED_METHOD = privateLookup.findVirtual(AnnotatedDynamicMethod.class, |
Attila Szegedi | 8f8c3ea | 2015-10-19 08:30:03 +0200 | [diff] [blame] | 641 | "getTarget", MethodType.methodType(MethodHandle.class, CallSiteDescriptor.class, LinkerServices.class)); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 642 | private static final MethodHandle GETTER_INVOKER = MethodHandles.invoker(MethodType.methodType(Object.class, Object.class)); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 643 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 644 | private GuardedInvocationComponent getPropertyGetter(final ComponentLinkRequest req) throws Exception { |
| 645 | if (req.name == null) { |
| 646 | return getUnnamedPropertyGetter(req); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 647 | } |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 648 | return getNamedPropertyGetter(req); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 651 | private GuardedInvocationComponent getUnnamedPropertyGetter(final ComponentLinkRequest req) throws Exception { |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 652 | // Since we can't know what kind of a getter we'll get back on different invocations, we'll just |
| 653 | // conservatively presume Object. Note we can't just coerce to a narrower call site type as the linking |
| 654 | // runtime might not allow coercing at that call site. |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 655 | final CallSiteDescriptor callSiteDescriptor = req.getDescriptor(); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 656 | final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class); |
| 657 | // Must have exactly two arguments: receiver and name |
| 658 | assertParameterCount(callSiteDescriptor, 2); |
| 659 | |
| 660 | // What's below is basically: |
| 661 | // foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle) |
| 662 | // only with a bunch of method signature adjustments. Basically, retrieve method getter |
| 663 | // AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null, |
| 664 | // or delegate to next component's invocation. |
| 665 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 666 | final LinkerServices linkerServices = req.linkerServices; |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 667 | final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType( |
| 668 | AnnotatedDynamicMethod.class)); |
| 669 | final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments( |
| 670 | GET_ANNOTATED_METHOD, 1, callSiteDescriptor, linkerServices); |
| 671 | final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0, |
| 672 | callSiteBoundMethodGetter); |
| 673 | // Object(AnnotatedDynamicMethod, Object)->Object(AnnotatedDynamicMethod, T0) |
| 674 | final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker, |
| 675 | MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0))); |
| 676 | // Since it's in the target of a fold, drop the unnecessary second argument |
| 677 | // Object(AnnotatedDynamicMethod, T0)->Object(AnnotatedDynamicMethod, T0, T1) |
| 678 | final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2, |
| 679 | type.parameterType(1)); |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 680 | final GuardedInvocationComponent nextComponent = getNextComponent(req); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 681 | |
| 682 | final MethodHandle fallbackFolded; |
| 683 | if(nextComponent == null) { |
| 684 | // Object(AnnotatedDynamicMethod)->Object(AnnotatedDynamicMethod, T0, T1); returns constant null |
| 685 | fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1, |
| 686 | type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class)); |
| 687 | } else { |
| 688 | // Object(T0, T1)->Object(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to |
| 689 | // drop the extra argument resulting from fold and to change its return type to Object. |
| 690 | final MethodHandle nextInvocation = nextComponent.getGuardedInvocation().getInvocation(); |
| 691 | final MethodType nextType = nextInvocation.type(); |
| 692 | fallbackFolded = MethodHandles.dropArguments(nextInvocation.asType( |
| 693 | nextType.changeReturnType(Object.class)), 0, AnnotatedDynamicMethod.class); |
| 694 | } |
| 695 | |
| 696 | // fold(Object(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1)) |
| 697 | final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest( |
| 698 | IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter); |
| 699 | if(nextComponent == null) { |
| 700 | return getClassGuardedInvocationComponent(compositeGetter, type); |
| 701 | } |
| 702 | return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS); |
| 703 | } |
| 704 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 705 | private GuardedInvocationComponent getNamedPropertyGetter(final ComponentLinkRequest req) throws Exception { |
| 706 | final CallSiteDescriptor callSiteDescriptor = req.getDescriptor(); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 707 | // Must have exactly one argument: receiver |
| 708 | assertParameterCount(callSiteDescriptor, 1); |
| 709 | // Fixed name |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 710 | final AnnotatedDynamicMethod annGetter = propertyGetters.get(req.name.toString()); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 711 | if(annGetter == null) { |
| 712 | // We have no such property, always delegate to the next component operation |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 713 | return getNextComponent(req); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 714 | } |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 715 | final MethodHandle getter = annGetter.getInvocation(req); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 716 | // NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being |
| 717 | // overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the |
| 718 | // method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If |
| 719 | // we're linking against a field getter, don't make the assumption. |
| 720 | // NOTE: No delegation to the next component operation if we have a property with this name, even if its |
| 721 | // value is null. |
| 722 | final ValidationType validationType = annGetter.validationType; |
| 723 | // TODO: we aren't using the type that declares the most generic getter here! |
| 724 | return new GuardedInvocationComponent(getter, getGuard(validationType, |
| 725 | callSiteDescriptor.getMethodType()), clazz, validationType); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 726 | } |
| 727 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 728 | private MethodHandle getGuard(final ValidationType validationType, final MethodType methodType) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 729 | switch(validationType) { |
| 730 | case EXACT_CLASS: { |
| 731 | return getClassGuard(methodType); |
| 732 | } |
| 733 | case INSTANCE_OF: { |
| 734 | return getAssignableGuard(methodType); |
| 735 | } |
| 736 | case IS_ARRAY: { |
| 737 | return Guards.isArray(0, methodType); |
| 738 | } |
| 739 | case NONE: { |
| 740 | return null; |
| 741 | } |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 742 | default: { |
| 743 | throw new AssertionError(); |
| 744 | } |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 745 | } |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Attila Szegedi | e9e7dd2 | 2014-02-26 13:17:57 +0100 | [diff] [blame] | 748 | private static final MethodHandle IS_DYNAMIC_METHOD = Guards.isInstance(DynamicMethod.class, |
| 749 | MethodType.methodType(boolean.class, Object.class)); |
| 750 | private static final MethodHandle OBJECT_IDENTITY = MethodHandles.identity(Object.class); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 751 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 752 | private GuardedInvocationComponent getMethodGetter(final ComponentLinkRequest req) throws Exception { |
| 753 | if (req.name == null) { |
| 754 | return getUnnamedMethodGetter(req); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 755 | } |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 756 | return getNamedMethodGetter(req); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 759 | private static MethodType getMethodGetterType(final ComponentLinkRequest req) { |
| 760 | // The created method handle will always return a DynamicMethod (or null), but since we don't want that type to |
| 761 | // be visible outside of this linker, declare it to return Object. |
| 762 | return req.getDescriptor().getMethodType().changeReturnType(Object.class); |
| 763 | } |
| 764 | |
| 765 | private GuardedInvocationComponent getUnnamedMethodGetter(final ComponentLinkRequest req) throws Exception { |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 766 | // Must have exactly two arguments: receiver and name |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 767 | assertParameterCount(req.getDescriptor(), 2); |
| 768 | final GuardedInvocationComponent nextComponent = getNextComponent(req); |
| 769 | final LinkerServices linkerServices = req.linkerServices; |
| 770 | final MethodType type = getMethodGetterType(req); |
| 771 | if(nextComponent == null) { |
| 772 | // No next component operation; just return a component for this operation. |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 773 | return getClassGuardedInvocationComponent(linkerServices.asType(getDynamicMethod, type), type); |
| 774 | } |
| 775 | |
| 776 | // What's below is basically: |
| 777 | // foldArguments(guardWithTest(isNotNull, identity, nextComponent.invocation), getter) only with a |
| 778 | // bunch of method signature adjustments. Basically, execute method getter; if it returns a non-null |
| 779 | // DynamicMethod, use identity to return it, otherwise delegate to nextComponent's invocation. |
| 780 | |
| 781 | final MethodHandle typedGetter = linkerServices.asType(getDynamicMethod, type); |
| 782 | // Since it is part of the foldArgument() target, it will have extra args that we need to drop. |
| 783 | final MethodHandle returnMethodHandle = linkerServices.asType(MethodHandles.dropArguments( |
| 784 | OBJECT_IDENTITY, 1, type.parameterList()), type.insertParameterTypes(0, Object.class)); |
| 785 | final MethodHandle nextComponentInvocation = nextComponent.getGuardedInvocation().getInvocation(); |
| 786 | // The assumption is that getGuardedInvocationComponent() already asType()'d it correctly modulo the |
| 787 | // return type. |
| 788 | assert nextComponentInvocation.type().changeReturnType(type.returnType()).equals(type); |
| 789 | // Since it is part of the foldArgument() target, we have to drop an extra arg it receives. |
| 790 | final MethodHandle nextCombinedInvocation = MethodHandles.dropArguments(nextComponentInvocation, 0, |
| 791 | Object.class); |
| 792 | // Assemble it all into a fold(guard(isNotNull, identity, nextInvocation), get) |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 793 | // Note that nextCombinedInvocation needs to have its return type changed to Object |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 794 | final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest( |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 795 | IS_DYNAMIC_METHOD, returnMethodHandle, |
| 796 | nextCombinedInvocation.asType(nextCombinedInvocation.type().changeReturnType(Object.class))), |
| 797 | typedGetter); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 798 | |
| 799 | return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS); |
| 800 | } |
| 801 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 802 | private GuardedInvocationComponent getNamedMethodGetter(final ComponentLinkRequest req) |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 803 | throws Exception { |
| 804 | // Must have exactly one argument: receiver |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 805 | assertParameterCount(req.getDescriptor(), 1); |
| 806 | final DynamicMethod method = getDynamicMethod(req.name.toString()); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 807 | if(method == null) { |
| 808 | // We have no such method, always delegate to the next component |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 809 | return getNextComponent(req); |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 810 | } |
| 811 | // No delegation to the next component of the composite operation; if we have a method with that name, |
| 812 | // we'll always return it at this point. |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 813 | final MethodType type = getMethodGetterType(req); |
| 814 | return getClassGuardedInvocationComponent(req.linkerServices.asType(MethodHandles.dropArguments( |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 815 | MethodHandles.constant(Object.class, method), 0, type.parameterType(0)), type), type); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Attila Szegedi | e9e7dd2 | 2014-02-26 13:17:57 +0100 | [diff] [blame] | 818 | static class MethodPair { |
| 819 | final MethodHandle method1; |
| 820 | final MethodHandle method2; |
| 821 | |
| 822 | MethodPair(final MethodHandle method1, final MethodHandle method2) { |
| 823 | this.method1 = method1; |
| 824 | this.method2 = method2; |
| 825 | } |
| 826 | |
| 827 | MethodHandle guardWithTest(final MethodHandle test) { |
| 828 | return MethodHandles.guardWithTest(test, method1, method2); |
| 829 | } |
| 830 | } |
| 831 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 832 | static MethodPair matchReturnTypes(final MethodHandle m1, final MethodHandle m2) { |
Attila Szegedi | e9e7dd2 | 2014-02-26 13:17:57 +0100 | [diff] [blame] | 833 | final MethodType type1 = m1.type(); |
| 834 | final MethodType type2 = m2.type(); |
Attila Szegedi | 4b535d3 | 2015-10-20 23:33:39 +0200 | [diff] [blame] | 835 | final Class<?> commonRetType = InternalTypeUtilities.getCommonLosslessConversionType(type1.returnType(), |
Attila Szegedi | e9e7dd2 | 2014-02-26 13:17:57 +0100 | [diff] [blame] | 836 | type2.returnType()); |
| 837 | return new MethodPair( |
| 838 | m1.asType(type1.changeReturnType(commonRetType)), |
| 839 | m2.asType(type2.changeReturnType(commonRetType))); |
| 840 | } |
| 841 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 842 | private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 843 | if(descriptor.getMethodType().parameterCount() != paramCount) { |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 844 | throw new BootstrapMethodError(descriptor.getOperation() + " must have exactly " + paramCount + " parameters."); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
| 848 | private static MethodHandle GET_PROPERTY_GETTER_HANDLE = MethodHandles.dropArguments(privateLookup.findOwnSpecial( |
| 849 | "getPropertyGetterHandle", Object.class, Object.class), 1, Object.class); |
| 850 | private final MethodHandle getPropertyGetterHandle = GET_PROPERTY_GETTER_HANDLE.bindTo(this); |
| 851 | |
| 852 | /** |
| 853 | * @param id the property ID |
| 854 | * @return the method handle for retrieving the property, or null if the property does not exist |
| 855 | */ |
| 856 | @SuppressWarnings("unused") |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 857 | private Object getPropertyGetterHandle(final Object id) { |
Athijegannathan Sundararajan | 9f0dcb1 | 2015-12-24 16:52:06 +0530 | [diff] [blame] | 858 | return propertyGetters.get(String.valueOf(id)); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | // Type is MethodHandle(BeanLinker, MethodType, LinkerServices, Object, String, Object), of which the two "Object" |
| 862 | // args are dropped; this makes handles with first three args conform to "Object, String, Object" though, which is |
| 863 | // a typical property setter with variable name signature (target, name, value). |
| 864 | private static final MethodHandle GET_PROPERTY_SETTER_HANDLE = MethodHandles.dropArguments(MethodHandles.dropArguments( |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 865 | privateLookup.findOwnSpecial("getPropertySetterHandle", MethodHandle.class, CallSiteDescriptor.class, |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 866 | LinkerServices.class, Object.class), 3, Object.class), 5, Object.class); |
| 867 | // Type is MethodHandle(MethodType, LinkerServices, Object, String, Object) |
| 868 | private final MethodHandle getPropertySetterHandle = GET_PROPERTY_SETTER_HANDLE.bindTo(this); |
| 869 | |
| 870 | @SuppressWarnings("unused") |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 871 | private MethodHandle getPropertySetterHandle(final CallSiteDescriptor setterDescriptor, final LinkerServices linkerServices, |
| 872 | final Object id) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 873 | return getDynamicMethodInvocation(setterDescriptor, linkerServices, String.valueOf(id), propertySetters); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | private static MethodHandle GET_DYNAMIC_METHOD = MethodHandles.dropArguments(privateLookup.findOwnSpecial( |
Attila Szegedi | e9e7dd2 | 2014-02-26 13:17:57 +0100 | [diff] [blame] | 877 | "getDynamicMethod", Object.class, Object.class), 1, Object.class); |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 878 | private final MethodHandle getDynamicMethod = GET_DYNAMIC_METHOD.bindTo(this); |
| 879 | |
| 880 | @SuppressWarnings("unused") |
Attila Szegedi | e9e7dd2 | 2014-02-26 13:17:57 +0100 | [diff] [blame] | 881 | // This method is marked to return Object instead of DynamicMethod as it's used as a linking component and we don't |
| 882 | // want to make the DynamicMethod type observable externally (e.g. as the return type of a MethodHandle returned for |
Attila Szegedi | fe13034 | 2015-10-21 19:33:58 +0200 | [diff] [blame] | 883 | // GET_METHOD linking). |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 884 | private Object getDynamicMethod(final Object name) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 885 | return getDynamicMethod(String.valueOf(name), methods); |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Returns a dynamic method of the specified name. |
| 890 | * |
| 891 | * @param name name of the method |
| 892 | * @return the dynamic method (either {@link SimpleDynamicMethod} or {@link OverloadedDynamicMethod}, or null if the |
| 893 | * method with the specified name does not exist. |
| 894 | */ |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 895 | DynamicMethod getDynamicMethod(final String name) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 896 | return getDynamicMethod(name, methods); |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * Find the most generic superclass that declares this getter. Since getters have zero args (aside from the |
| 901 | * receiver), they can't be overloaded, so we're free to link with an instanceof guard for the most generic one, |
| 902 | * creating more stable call sites. |
| 903 | * @param getter the getter |
| 904 | * @return getter with same name, declared on the most generic superclass/interface of the declaring class |
| 905 | */ |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 906 | private static Method getMostGenericGetter(final Method getter) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 907 | return getMostGenericGetter(getter.getName(), getter.getReturnType(), getter.getDeclaringClass()); |
| 908 | } |
| 909 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 910 | private static Method getMostGenericGetter(final String name, final Class<?> returnType, final Class<?> declaringClass) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 911 | if(declaringClass == null) { |
| 912 | return null; |
| 913 | } |
| 914 | // Prefer interfaces |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 915 | for(final Class<?> itf: declaringClass.getInterfaces()) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 916 | final Method itfGetter = getMostGenericGetter(name, returnType, itf); |
| 917 | if(itfGetter != null) { |
| 918 | return itfGetter; |
| 919 | } |
| 920 | } |
| 921 | final Method superGetter = getMostGenericGetter(name, returnType, declaringClass.getSuperclass()); |
| 922 | if(superGetter != null) { |
| 923 | return superGetter; |
| 924 | } |
| 925 | if(!CheckRestrictedPackage.isRestrictedClass(declaringClass)) { |
| 926 | try { |
| 927 | return declaringClass.getMethod(name); |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 928 | } catch(final NoSuchMethodException e) { |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 929 | // Intentionally ignored, meant to fall through |
| 930 | } |
| 931 | } |
| 932 | return null; |
| 933 | } |
| 934 | |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 935 | private static final class AnnotatedDynamicMethod { |
| 936 | private final SingleDynamicMethod method; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 937 | /*private*/ final ValidationType validationType; |
| 938 | |
Attila Szegedi | 5ec014a | 2014-06-04 13:08:57 +0200 | [diff] [blame] | 939 | AnnotatedDynamicMethod(final SingleDynamicMethod method, final ValidationType validationType) { |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 940 | this.method = method; |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 941 | this.validationType = validationType; |
| 942 | } |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 943 | |
Attila Szegedi | 0beda5b | 2016-01-14 13:22:58 +0100 | [diff] [blame] | 944 | MethodHandle getInvocation(final ComponentLinkRequest req) { |
| 945 | return method.getInvocation(req.getDescriptor(), req.linkerServices); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | @SuppressWarnings("unused") |
Attila Szegedi | 8f8c3ea | 2015-10-19 08:30:03 +0200 | [diff] [blame] | 949 | MethodHandle getTarget(final CallSiteDescriptor desc, final LinkerServices linkerServices) { |
| 950 | final MethodHandle inv = linkerServices.filterInternalObjects(method.getTarget(desc)); |
Attila Szegedi | 92bcfea | 2013-07-03 12:39:28 +0200 | [diff] [blame] | 951 | assert inv != null; |
| 952 | return inv; |
| 953 | } |
Attila Szegedi | 82f8cef | 2013-02-14 13:22:26 +0100 | [diff] [blame] | 954 | } |
Attila Szegedi | ccce3fa | 2013-02-18 16:00:15 +0100 | [diff] [blame] | 955 | } |