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