src/jdk/internal/dynalink/beans/AbstractJavaLinker.java

Mon, 03 Nov 2014 07:29:46 +0100

author
attila
date
Mon, 03 Nov 2014 07:29:46 +0100
changeset 1081
a54684572f14
parent 963
e2497b11a021
child 1183
6ed91931b5a7
permissions
-rw-r--r--

8062050: A method is considered caller sensitive, but it doesn't have the CallerSensitive annotation
Reviewed-by: hannesw, lagergren

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@963 109 import jdk.internal.dynalink.support.TypeUtilities;
attila@90 110
attila@90 111 /**
attila@90 112 * A base class for both {@link StaticClassLinker} and {@link BeanLinker}. Deals with common aspects of property
attila@90 113 * exposure and method calls for both static and instance facets of a class.
attila@90 114 *
attila@90 115 * @author Attila Szegedi
attila@90 116 */
attila@90 117 abstract class AbstractJavaLinker implements GuardingDynamicLinker {
attila@404 118
attila@90 119 final Class<?> clazz;
attila@90 120 private final MethodHandle classGuard;
attila@90 121 private final MethodHandle assignableGuard;
attila@404 122 private final Map<String, AnnotatedDynamicMethod> propertyGetters = new HashMap<>();
attila@90 123 private final Map<String, DynamicMethod> propertySetters = new HashMap<>();
attila@90 124 private final Map<String, DynamicMethod> methods = new HashMap<>();
attila@90 125
attila@962 126 AbstractJavaLinker(final Class<?> clazz, final MethodHandle classGuard) {
attila@90 127 this(clazz, classGuard, classGuard);
attila@90 128 }
attila@90 129
attila@962 130 AbstractJavaLinker(final Class<?> clazz, final MethodHandle classGuard, final MethodHandle assignableGuard) {
attila@90 131 this.clazz = clazz;
attila@90 132 this.classGuard = classGuard;
attila@90 133 this.assignableGuard = assignableGuard;
attila@90 134
attila@90 135 final FacetIntrospector introspector = createFacetIntrospector();
attila@101 136 // Add methods and properties
attila@962 137 for(final Method method: introspector.getMethods()) {
attila@101 138 final String name = method.getName();
attila@101 139 // Add method
attila@404 140 addMember(name, method, methods);
attila@101 141 // Add the method as a property getter and/or setter
attila@101 142 if(name.startsWith("get") && name.length() > 3 && method.getParameterTypes().length == 0) {
attila@101 143 // Property getter
attila@404 144 setPropertyGetter(method, 3);
attila@101 145 } else if(name.startsWith("is") && name.length() > 2 && method.getParameterTypes().length == 0 &&
attila@101 146 method.getReturnType() == boolean.class) {
attila@101 147 // Boolean property getter
attila@404 148 setPropertyGetter(method, 2);
attila@101 149 } else if(name.startsWith("set") && name.length() > 3 && method.getParameterTypes().length == 1) {
attila@101 150 // Property setter
attila@404 151 addMember(decapitalize(name.substring(3)), method, propertySetters);
attila@90 152 }
attila@101 153 }
attila@90 154
attila@101 155 // Add field getter/setters as property getters/setters.
attila@962 156 for(final Field field: introspector.getFields()) {
attila@101 157 final String name = field.getName();
attila@101 158 // Only add a property getter when one is not defined already as a getXxx()/isXxx() method.
attila@101 159 if(!propertyGetters.containsKey(name)) {
attila@101 160 setPropertyGetter(name, introspector.unreflectGetter(field), ValidationType.EXACT_CLASS);
attila@90 161 }
attila@101 162 if(!(Modifier.isFinal(field.getModifiers()) || propertySetters.containsKey(name))) {
attila@404 163 addMember(name, new SimpleDynamicMethod(introspector.unreflectSetter(field), clazz, name),
attila@404 164 propertySetters);
attila@101 165 }
attila@101 166 }
attila@90 167
attila@101 168 // Add inner classes, but only those for which we don't hide a property with it
attila@962 169 for(final Map.Entry<String, MethodHandle> innerClassSpec: introspector.getInnerClassGetters().entrySet()) {
attila@101 170 final String name = innerClassSpec.getKey();
attila@101 171 if(!propertyGetters.containsKey(name)) {
attila@101 172 setPropertyGetter(name, innerClassSpec.getValue(), ValidationType.EXACT_CLASS);
attila@90 173 }
attila@90 174 }
attila@90 175 }
attila@90 176
attila@962 177 private static String decapitalize(final String str) {
attila@123 178 assert str != null;
attila@123 179 if(str.isEmpty()) {
attila@123 180 return str;
attila@123 181 }
attila@123 182
attila@123 183 final char c0 = str.charAt(0);
attila@123 184 if(Character.isLowerCase(c0)) {
attila@123 185 return str;
attila@123 186 }
attila@123 187
attila@123 188 // If it has two consecutive upper-case characters, i.e. "URL", don't decapitalize
attila@123 189 if(str.length() > 1 && Character.isUpperCase(str.charAt(1))) {
attila@123 190 return str;
attila@123 191 }
attila@123 192
attila@123 193 final char c[] = str.toCharArray();
attila@123 194 c[0] = Character.toLowerCase(c0);
attila@123 195 return new String(c);
attila@123 196 }
attila@123 197
attila@90 198 abstract FacetIntrospector createFacetIntrospector();
attila@90 199
attila@439 200 Collection<String> getReadablePropertyNames() {
attila@439 201 return getUnmodifiableKeys(propertyGetters);
attila@439 202 }
attila@439 203
attila@439 204 Collection<String> getWritablePropertyNames() {
attila@439 205 return getUnmodifiableKeys(propertySetters);
attila@439 206 }
attila@439 207
attila@439 208 Collection<String> getMethodNames() {
attila@439 209 return getUnmodifiableKeys(methods);
attila@439 210 }
attila@439 211
attila@962 212 private static Collection<String> getUnmodifiableKeys(final Map<String, ?> m) {
attila@439 213 return Collections.unmodifiableCollection(m.keySet());
attila@439 214 }
attila@439 215
attila@404 216 /**
attila@404 217 * Sets the specified dynamic method to be the property getter for the specified property. Note that you can only
attila@404 218 * use this when you're certain that the method handle does not belong to a caller-sensitive method. For properties
attila@404 219 * that are caller-sensitive, you must use {@link #setPropertyGetter(String, SingleDynamicMethod, ValidationType)}
attila@404 220 * instead.
attila@404 221 * @param name name of the property
attila@404 222 * @param handle the method handle that implements the property getter
attila@404 223 * @param validationType the validation type for the property
attila@404 224 */
attila@962 225 private void setPropertyGetter(final String name, final SingleDynamicMethod handle, final ValidationType validationType) {
attila@404 226 propertyGetters.put(name, new AnnotatedDynamicMethod(handle, validationType));
attila@90 227 }
attila@90 228
attila@404 229 /**
attila@404 230 * Sets the specified reflective method to be the property getter for the specified property.
attila@404 231 * @param getter the getter method
attila@404 232 * @param prefixLen the getter prefix in the method name; should be 3 for getter names starting with "get" and 2 for
attila@404 233 * names starting with "is".
attila@404 234 */
attila@962 235 private void setPropertyGetter(final Method getter, final int prefixLen) {
attila@404 236 setPropertyGetter(decapitalize(getter.getName().substring(prefixLen)), createDynamicMethod(
attila@404 237 getMostGenericGetter(getter)), ValidationType.INSTANCE_OF);
attila@404 238 }
attila@404 239
attila@404 240 /**
attila@404 241 * Sets the specified method handle to be the property getter for the specified property. Note that you can only
attila@404 242 * use this when you're certain that the method handle does not belong to a caller-sensitive method. For properties
attila@404 243 * that are caller-sensitive, you must use {@link #setPropertyGetter(String, SingleDynamicMethod, ValidationType)}
attila@404 244 * instead.
attila@404 245 * @param name name of the property
attila@404 246 * @param handle the method handle that implements the property getter
attila@404 247 * @param validationType the validation type for the property
attila@404 248 */
attila@962 249 void setPropertyGetter(final String name, final MethodHandle handle, final ValidationType validationType) {
attila@404 250 setPropertyGetter(name, new SimpleDynamicMethod(handle, clazz, name), validationType);
attila@404 251 }
attila@404 252
attila@962 253 private void addMember(final String name, final AccessibleObject ao, final Map<String, DynamicMethod> methodMap) {
attila@404 254 addMember(name, createDynamicMethod(ao), methodMap);
attila@404 255 }
attila@404 256
attila@962 257 private void addMember(final String name, final SingleDynamicMethod method, final Map<String, DynamicMethod> methodMap) {
attila@90 258 final DynamicMethod existingMethod = methodMap.get(name);
attila@404 259 final DynamicMethod newMethod = mergeMethods(method, existingMethod, clazz, name);
attila@90 260 if(newMethod != existingMethod) {
attila@90 261 methodMap.put(name, newMethod);
attila@90 262 }
attila@90 263 }
attila@90 264
attila@404 265 /**
attila@404 266 * Given one or more reflective methods or constructors, creates a dynamic method that represents them all. The
attila@404 267 * methods should represent all overloads of the same name (or all constructors of the class).
attila@404 268 * @param members the reflective members
attila@404 269 * @param clazz the class declaring the reflective members
attila@404 270 * @param name the common name of the reflective members.
attila@404 271 * @return a dynamic method representing all the specified reflective members.
attila@404 272 */
attila@962 273 static DynamicMethod createDynamicMethod(final Iterable<? extends AccessibleObject> members, final Class<?> clazz, final String name) {
attila@90 274 DynamicMethod dynMethod = null;
attila@962 275 for(final AccessibleObject method: members) {
attila@404 276 dynMethod = mergeMethods(createDynamicMethod(method), dynMethod, clazz, name);
attila@90 277 }
attila@90 278 return dynMethod;
attila@90 279 }
attila@90 280
attila@404 281 /**
attila@404 282 * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
attila@404 283 * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
attila@404 284 * dynamic method when needed.
attila@404 285 * @param m the reflective member
attila@404 286 * @return the single dynamic method representing the reflective member
attila@404 287 */
attila@962 288 private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) {
attila@404 289 if(CallerSensitiveDetector.isCallerSensitive(m)) {
attila@1081 290 // Method has @CallerSensitive annotation
attila@404 291 return new CallerSensitiveDynamicMethod(m);
attila@404 292 }
attila@1081 293 // Method has no @CallerSensitive annotation
attila@1081 294 final MethodHandle mh;
attila@1081 295 try {
attila@1081 296 mh = unreflectSafely(m);
attila@1081 297 } catch (final IllegalAccessError e) {
attila@1081 298 // java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't
attila@1081 299 // marked with the annotation. In this case, we'll fall back to treating it as caller sensitive.
attila@1081 300 return new CallerSensitiveDynamicMethod(m);
attila@1081 301 }
attila@1081 302 // Proceed with non-caller sensitive
attila@404 303 final Member member = (Member)m;
attila@1081 304 return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor);
attila@404 305 }
attila@404 306
attila@404 307 /**
attila@404 308 * Unreflects a method handle from a Method or a Constructor using safe (zero-privilege) unreflection. Should be
attila@404 309 * only used for methods and constructors that are not caller sensitive. If a caller sensitive method were
attila@404 310 * unreflected through this mechanism, it would not be a security issue, but would be bound to the zero-privilege
attila@404 311 * unreflector as its caller, and thus completely useless.
attila@404 312 * @param m the method or constructor
attila@404 313 * @return the method handle
attila@404 314 */
attila@962 315 private static MethodHandle unreflectSafely(final AccessibleObject m) {
attila@404 316 if(m instanceof Method) {
attila@404 317 final Method reflMethod = (Method)m;
attila@464 318 final MethodHandle handle = Lookup.PUBLIC.unreflect(reflMethod);
attila@404 319 if(Modifier.isStatic(reflMethod.getModifiers())) {
attila@404 320 return StaticClassIntrospector.editStaticMethodHandle(handle);
attila@404 321 }
attila@404 322 return handle;
attila@404 323 }
attila@464 324 return StaticClassIntrospector.editConstructorMethodHandle(Lookup.PUBLIC.unreflectConstructor((Constructor<?>)m));
attila@404 325 }
attila@404 326
attila@962 327 private static DynamicMethod mergeMethods(final SingleDynamicMethod method, final DynamicMethod existing, final Class<?> clazz, final String name) {
attila@90 328 if(existing == null) {
attila@404 329 return method;
attila@404 330 } else if(existing.contains(method)) {
attila@90 331 return existing;
attila@404 332 } else if(existing instanceof SingleDynamicMethod) {
attila@90 333 final OverloadedDynamicMethod odm = new OverloadedDynamicMethod(clazz, name);
attila@404 334 odm.addMethod(((SingleDynamicMethod)existing));
attila@404 335 odm.addMethod(method);
attila@90 336 return odm;
attila@90 337 } else if(existing instanceof OverloadedDynamicMethod) {
attila@404 338 ((OverloadedDynamicMethod)existing).addMethod(method);
attila@90 339 return existing;
attila@90 340 }
attila@90 341 throw new AssertionError();
attila@90 342 }
attila@90 343
attila@90 344 @Override
attila@962 345 public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
attila@90 346 throws Exception {
attila@90 347 final LinkRequest ncrequest = request.withoutRuntimeContext();
attila@90 348 // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".
attila@90 349 final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();
attila@90 350 final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);
attila@90 351 // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).
attila@90 352 if("callMethod" == op) {
attila@90 353 return getCallPropWithThis(callSiteDescriptor, linkerServices);
attila@90 354 }
attila@90 355 List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);
attila@90 356 while(!operations.isEmpty()) {
attila@90 357 final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,
attila@90 358 operations);
attila@90 359 if(gic != null) {
attila@90 360 return gic.getGuardedInvocation();
attila@90 361 }
attila@90 362 operations = pop(operations);
attila@90 363 }
attila@90 364 return null;
attila@90 365 }
attila@90 366
attila@962 367 protected GuardedInvocationComponent getGuardedInvocationComponent(final CallSiteDescriptor callSiteDescriptor,
attila@962 368 final LinkerServices linkerServices, final List<String> operations) throws Exception {
attila@90 369 if(operations.isEmpty()) {
attila@90 370 return null;
attila@90 371 }
attila@90 372 final String op = operations.get(0);
attila@90 373 // Either dyn:getProp:name(this) or dyn:getProp(this, name)
attila@90 374 if("getProp".equals(op)) {
attila@90 375 return getPropertyGetter(callSiteDescriptor, linkerServices, pop(operations));
attila@90 376 }
attila@90 377 // Either dyn:setProp:name(this, value) or dyn:setProp(this, name, value)
attila@90 378 if("setProp".equals(op)) {
attila@90 379 return getPropertySetter(callSiteDescriptor, linkerServices, pop(operations));
attila@90 380 }
attila@90 381 // Either dyn:getMethod:name(this), or dyn:getMethod(this, name)
attila@90 382 if("getMethod".equals(op)) {
attila@90 383 return getMethodGetter(callSiteDescriptor, linkerServices, pop(operations));
attila@90 384 }
attila@90 385 return null;
attila@90 386 }
attila@90 387
attila@962 388 static final <T> List<T> pop(final List<T> l) {
attila@90 389 return l.subList(1, l.size());
attila@90 390 }
attila@90 391
attila@962 392 MethodHandle getClassGuard(final CallSiteDescriptor desc) {
attila@90 393 return getClassGuard(desc.getMethodType());
attila@90 394 }
attila@90 395
attila@962 396 MethodHandle getClassGuard(final MethodType type) {
attila@90 397 return Guards.asType(classGuard, type);
attila@90 398 }
attila@90 399
attila@962 400 GuardedInvocationComponent getClassGuardedInvocationComponent(final MethodHandle invocation, final MethodType type) {
attila@90 401 return new GuardedInvocationComponent(invocation, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
attila@90 402 }
attila@90 403
attila@963 404 SingleDynamicMethod getConstructorMethod(final String signature) {
attila@963 405 return null;
attila@963 406 }
attila@963 407
attila@962 408 private MethodHandle getAssignableGuard(final MethodType type) {
attila@90 409 return Guards.asType(assignableGuard, type);
attila@90 410 }
attila@90 411
attila@962 412 private GuardedInvocation getCallPropWithThis(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) {
attila@90 413 switch(callSiteDescriptor.getNameTokenCount()) {
attila@90 414 case 3: {
attila@404 415 return createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,
attila@90 416 callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), methods);
attila@90 417 }
attila@90 418 default: {
attila@90 419 return null;
attila@90 420 }
attila@90 421 }
attila@90 422 }
attila@90 423
attila@962 424 private GuardedInvocation createGuardedDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor,
attila@962 425 final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap){
attila@404 426 final MethodHandle inv = getDynamicMethodInvocation(callSiteDescriptor, linkerServices, methodName, methodMap);
attila@404 427 return inv == null ? null : new GuardedInvocation(inv, getClassGuard(callSiteDescriptor.getMethodType()));
attila@90 428 }
attila@90 429
attila@963 430 private MethodHandle getDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor,
attila@962 431 final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap) {
attila@90 432 final DynamicMethod dynaMethod = getDynamicMethod(methodName, methodMap);
attila@404 433 return dynaMethod != null ? dynaMethod.getInvocation(callSiteDescriptor, linkerServices) : null;
attila@90 434 }
attila@90 435
attila@963 436 private DynamicMethod getDynamicMethod(final String methodName, final Map<String, DynamicMethod> methodMap) {
attila@90 437 final DynamicMethod dynaMethod = methodMap.get(methodName);
attila@90 438 return dynaMethod != null ? dynaMethod : getExplicitSignatureDynamicMethod(methodName, methodMap);
attila@90 439 }
attila@90 440
attila@963 441 private SingleDynamicMethod getExplicitSignatureDynamicMethod(final String fullName,
attila@962 442 final Map<String, DynamicMethod> methodsMap) {
attila@90 443 // What's below is meant to support the "name(type, type, ...)" syntax that programmers can use in a method name
attila@90 444 // to manually pin down an exact overloaded variant. This is not usually required, as the overloaded method
attila@90 445 // resolution works correctly in almost every situation. However, in presence of many language-specific
attila@90 446 // conversions with a radically dynamic language, most overloaded methods will end up being constantly selected
attila@404 447 // at invocation time, so a programmer knowledgeable of the situation might choose to pin down an exact overload
attila@90 448 // for performance reasons.
attila@90 449
attila@90 450 // Is the method name lexically of the form "name(types)"?
attila@963 451 final int lastChar = fullName.length() - 1;
attila@963 452 if(fullName.charAt(lastChar) != ')') {
attila@90 453 return null;
attila@90 454 }
attila@963 455 final int openBrace = fullName.indexOf('(');
attila@90 456 if(openBrace == -1) {
attila@90 457 return null;
attila@90 458 }
attila@90 459
attila@963 460 final String name = fullName.substring(0, openBrace);
attila@963 461 final String signature = fullName.substring(openBrace + 1, lastChar);
attila@963 462
attila@90 463 // Find an existing method for the "name" part
attila@963 464 final DynamicMethod simpleNamedMethod = methodsMap.get(name);
attila@90 465 if(simpleNamedMethod == null) {
attila@963 466 // explicit signature constructor access
attila@963 467 // Java.type("java.awt.Color")["(int,int,int)"]
attila@963 468 // will get Color(int,int,int) constructor of Color class.
attila@963 469 if (name.isEmpty()) {
attila@963 470 return getConstructorMethod(signature);
attila@963 471 }
attila@963 472
attila@90 473 return null;
attila@90 474 }
attila@90 475
attila@90 476 // Try to get a narrowed dynamic method for the explicit parameter types.
attila@963 477 return simpleNamedMethod.getMethodForExactParamTypes(signature);
attila@90 478 }
attila@90 479
attila@90 480 private static final MethodHandle IS_METHOD_HANDLE_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType(
attila@90 481 boolean.class, MethodHandle.class));
attila@90 482 private static final MethodHandle CONSTANT_NULL_DROP_METHOD_HANDLE = MethodHandles.dropArguments(
attila@90 483 MethodHandles.constant(Object.class, null), 0, MethodHandle.class);
attila@90 484
attila@962 485 private GuardedInvocationComponent getPropertySetter(final CallSiteDescriptor callSiteDescriptor,
attila@962 486 final LinkerServices linkerServices, final List<String> operations) throws Exception {
attila@90 487 switch(callSiteDescriptor.getNameTokenCount()) {
attila@90 488 case 2: {
attila@90 489 // Must have three arguments: target object, property name, and property value.
attila@90 490 assertParameterCount(callSiteDescriptor, 3);
attila@90 491
attila@963 492 // We want setters that conform to "Object(O, V)". Note, we aren't doing "R(O, V)" as it might not be
attila@963 493 // valid for us to convert return values proactively. Also, since we don't know what setters will be
attila@963 494 // invoked, we'll conservatively presume Object return type.
attila@963 495 final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
attila@963 496
attila@90 497 // What's below is basically:
attila@90 498 // foldArguments(guardWithTest(isNotNull, invoke, null|nextComponent.invocation),
attila@90 499 // get_setter_handle(type, linkerServices))
attila@90 500 // only with a bunch of method signature adjustments. Basically, retrieve method setter
attila@90 501 // MethodHandle; if it is non-null, invoke it, otherwise either return null, or delegate to next
attila@90 502 // component's invocation.
attila@90 503
attila@90 504 // Call site type is "ret_type(object_type,property_name_type,property_value_type)", which we'll
attila@963 505 // abbreviate to R(O, N, V) going forward, although we don't really use R here (see above about using
attila@963 506 // Object return type).
attila@90 507 final MethodType setterType = type.dropParameterTypes(1, 2);
attila@90 508 // Bind property setter handle to the expected setter type and linker services. Type is
attila@90 509 // MethodHandle(Object, String, Object)
attila@404 510 final MethodHandle boundGetter = MethodHandles.insertArguments(getPropertySetterHandle, 0,
attila@404 511 CallSiteDescriptorFactory.dropParameterTypes(callSiteDescriptor, 1, 2), linkerServices);
attila@90 512
attila@90 513 // Cast getter to MethodHandle(O, N, V)
attila@90 514 final MethodHandle typedGetter = linkerServices.asType(boundGetter, type.changeReturnType(
attila@90 515 MethodHandle.class));
attila@90 516
attila@90 517 // Handle to invoke the setter R(MethodHandle, O, V)
attila@90 518 final MethodHandle invokeHandle = MethodHandles.exactInvoker(setterType);
attila@90 519 // Handle to invoke the setter, dropping unnecessary fold arguments R(MethodHandle, O, N, V)
attila@90 520 final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandle, 2, type.parameterType(
attila@90 521 1));
attila@90 522 final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
attila@90 523 linkerServices, operations);
attila@90 524
attila@90 525 final MethodHandle fallbackFolded;
attila@90 526 if(nextComponent == null) {
attila@963 527 // Object(MethodHandle)->Object(MethodHandle, O, N, V); returns constant null
attila@90 528 fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_METHOD_HANDLE, 1,
attila@90 529 type.parameterList()).asType(type.insertParameterTypes(0, MethodHandle.class));
attila@90 530 } else {
attila@963 531 // Object(O, N, V)->Object(MethodHandle, O, N, V); adapts the next component's invocation to drop the
attila@90 532 // extra argument resulting from fold
attila@90 533 fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(),
attila@90 534 0, MethodHandle.class);
attila@90 535 }
attila@90 536
attila@90 537 // fold(R(MethodHandle, O, N, V), MethodHandle(O, N, V))
attila@90 538 final MethodHandle compositeSetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
attila@90 539 IS_METHOD_HANDLE_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
attila@90 540 if(nextComponent == null) {
attila@90 541 return getClassGuardedInvocationComponent(compositeSetter, type);
attila@90 542 }
attila@101 543 return nextComponent.compose(compositeSetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
attila@90 544 }
attila@90 545 case 3: {
attila@90 546 // Must have two arguments: target object and property value
attila@90 547 assertParameterCount(callSiteDescriptor, 2);
attila@404 548 final GuardedInvocation gi = createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,
attila@404 549 callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), propertySetters);
attila@90 550 // If we have a property setter with this name, this composite operation will always stop here
attila@90 551 if(gi != null) {
attila@90 552 return new GuardedInvocationComponent(gi, clazz, ValidationType.EXACT_CLASS);
attila@90 553 }
attila@90 554 // If we don't have a property setter with this name, always fall back to the next operation in the
attila@90 555 // composite (if any)
attila@90 556 return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, operations);
attila@90 557 }
attila@90 558 default: {
attila@90 559 // More than two name components; don't know what to do with it.
attila@90 560 return null;
attila@90 561 }
attila@90 562 }
attila@90 563 }
attila@90 564
attila@90 565 private static final Lookup privateLookup = new Lookup(MethodHandles.lookup());
attila@90 566
attila@404 567 private static final MethodHandle IS_ANNOTATED_METHOD_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType(
attila@404 568 boolean.class, AnnotatedDynamicMethod.class));
attila@404 569 private static final MethodHandle CONSTANT_NULL_DROP_ANNOTATED_METHOD = MethodHandles.dropArguments(
attila@404 570 MethodHandles.constant(Object.class, null), 0, AnnotatedDynamicMethod.class);
attila@404 571 private static final MethodHandle GET_ANNOTATED_METHOD = privateLookup.findVirtual(AnnotatedDynamicMethod.class,
attila@404 572 "getTarget", MethodType.methodType(MethodHandle.class, MethodHandles.Lookup.class));
attila@404 573 private static final MethodHandle GETTER_INVOKER = MethodHandles.invoker(MethodType.methodType(Object.class, Object.class));
attila@90 574
attila@962 575 private GuardedInvocationComponent getPropertyGetter(final CallSiteDescriptor callSiteDescriptor,
attila@962 576 final LinkerServices linkerServices, final List<String> ops) throws Exception {
attila@90 577 switch(callSiteDescriptor.getNameTokenCount()) {
attila@90 578 case 2: {
attila@963 579 // Since we can't know what kind of a getter we'll get back on different invocations, we'll just
attila@963 580 // conservatively presume Object. Note we can't just coerce to a narrower call site type as the linking
attila@963 581 // runtime might not allow coercing at that call site.
attila@963 582 final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
attila@90 583 // Must have exactly two arguments: receiver and name
attila@90 584 assertParameterCount(callSiteDescriptor, 2);
attila@90 585
attila@90 586 // What's below is basically:
attila@90 587 // foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle)
attila@90 588 // only with a bunch of method signature adjustments. Basically, retrieve method getter
attila@404 589 // AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null,
attila@90 590 // or delegate to next component's invocation.
attila@90 591
attila@90 592 final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType(
attila@404 593 AnnotatedDynamicMethod.class));
attila@404 594 final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments(
attila@404 595 GET_ANNOTATED_METHOD, 1, callSiteDescriptor.getLookup());
attila@404 596 final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0,
attila@404 597 callSiteBoundMethodGetter);
attila@963 598 // Object(AnnotatedDynamicMethod, Object)->Object(AnnotatedDynamicMethod, T0)
attila@404 599 final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker,
attila@404 600 MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0)));
attila@90 601 // Since it's in the target of a fold, drop the unnecessary second argument
attila@963 602 // Object(AnnotatedDynamicMethod, T0)->Object(AnnotatedDynamicMethod, T0, T1)
attila@90 603 final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2,
attila@90 604 type.parameterType(1));
attila@90 605 final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
attila@90 606 linkerServices, ops);
attila@90 607
attila@90 608 final MethodHandle fallbackFolded;
attila@90 609 if(nextComponent == null) {
attila@963 610 // Object(AnnotatedDynamicMethod)->Object(AnnotatedDynamicMethod, T0, T1); returns constant null
attila@404 611 fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1,
attila@404 612 type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class));
attila@90 613 } else {
attila@963 614 // Object(T0, T1)->Object(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to
attila@963 615 // drop the extra argument resulting from fold and to change its return type to Object.
attila@963 616 final MethodHandle nextInvocation = nextComponent.getGuardedInvocation().getInvocation();
attila@963 617 final MethodType nextType = nextInvocation.type();
attila@963 618 fallbackFolded = MethodHandles.dropArguments(nextInvocation.asType(
attila@963 619 nextType.changeReturnType(Object.class)), 0, AnnotatedDynamicMethod.class);
attila@90 620 }
attila@90 621
attila@963 622 // fold(Object(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1))
attila@90 623 final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
attila@404 624 IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
attila@90 625 if(nextComponent == null) {
attila@90 626 return getClassGuardedInvocationComponent(compositeGetter, type);
attila@90 627 }
attila@101 628 return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
attila@90 629 }
attila@90 630 case 3: {
attila@90 631 // Must have exactly one argument: receiver
attila@90 632 assertParameterCount(callSiteDescriptor, 1);
attila@90 633 // Fixed name
attila@404 634 final AnnotatedDynamicMethod annGetter = propertyGetters.get(callSiteDescriptor.getNameToken(
attila@90 635 CallSiteDescriptor.NAME_OPERAND));
attila@90 636 if(annGetter == null) {
attila@90 637 // We have no such property, always delegate to the next component operation
attila@90 638 return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
attila@90 639 }
attila@404 640 final MethodHandle getter = annGetter.getInvocation(callSiteDescriptor, linkerServices);
attila@90 641 // NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being
attila@90 642 // overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the
attila@90 643 // method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If
attila@90 644 // we're linking against a field getter, don't make the assumption.
attila@90 645 // NOTE: No delegation to the next component operation if we have a property with this name, even if its
attila@90 646 // value is null.
attila@90 647 final ValidationType validationType = annGetter.validationType;
attila@404 648 // TODO: we aren't using the type that declares the most generic getter here!
attila@963 649 return new GuardedInvocationComponent(getter, getGuard(validationType,
attila@963 650 callSiteDescriptor.getMethodType()), clazz, validationType);
attila@90 651 }
attila@90 652 default: {
attila@90 653 // Can't do anything with more than 3 name components
attila@90 654 return null;
attila@90 655 }
attila@90 656 }
attila@90 657 }
attila@90 658
attila@962 659 private MethodHandle getGuard(final ValidationType validationType, final MethodType methodType) {
attila@90 660 switch(validationType) {
attila@90 661 case EXACT_CLASS: {
attila@90 662 return getClassGuard(methodType);
attila@90 663 }
attila@90 664 case INSTANCE_OF: {
attila@90 665 return getAssignableGuard(methodType);
attila@90 666 }
attila@90 667 case IS_ARRAY: {
attila@90 668 return Guards.isArray(0, methodType);
attila@90 669 }
attila@90 670 case NONE: {
attila@90 671 return null;
attila@90 672 }
attila@101 673 default: {
attila@101 674 throw new AssertionError();
attila@101 675 }
attila@90 676 }
attila@90 677 }
attila@90 678
attila@963 679 private static final MethodHandle IS_DYNAMIC_METHOD = Guards.isInstance(DynamicMethod.class,
attila@963 680 MethodType.methodType(boolean.class, Object.class));
attila@963 681 private static final MethodHandle OBJECT_IDENTITY = MethodHandles.identity(Object.class);
attila@90 682
attila@962 683 private GuardedInvocationComponent getMethodGetter(final CallSiteDescriptor callSiteDescriptor,
attila@962 684 final LinkerServices linkerServices, final List<String> ops) throws Exception {
attila@963 685 // The created method handle will always return a DynamicMethod (or null), but since we don't want that type to
attila@963 686 // be visible outside of this linker, declare it to return Object.
attila@963 687 final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
attila@90 688 switch(callSiteDescriptor.getNameTokenCount()) {
attila@90 689 case 2: {
attila@90 690 // Must have exactly two arguments: receiver and name
attila@90 691 assertParameterCount(callSiteDescriptor, 2);
attila@90 692 final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
attila@90 693 linkerServices, ops);
attila@963 694 if(nextComponent == null || !TypeUtilities.areAssignable(DynamicMethod.class,
attila@963 695 nextComponent.getGuardedInvocation().getInvocation().type().returnType())) {
attila@963 696 // No next component operation, or it can never produce a dynamic method; just return a component
attila@963 697 // for this operation.
attila@90 698 return getClassGuardedInvocationComponent(linkerServices.asType(getDynamicMethod, type), type);
attila@101 699 }
attila@90 700
attila@101 701 // What's below is basically:
attila@101 702 // foldArguments(guardWithTest(isNotNull, identity, nextComponent.invocation), getter) only with a
attila@101 703 // bunch of method signature adjustments. Basically, execute method getter; if it returns a non-null
attila@101 704 // DynamicMethod, use identity to return it, otherwise delegate to nextComponent's invocation.
attila@90 705
attila@963 706 final MethodHandle typedGetter = linkerServices.asType(getDynamicMethod, type);
attila@101 707 // Since it is part of the foldArgument() target, it will have extra args that we need to drop.
attila@101 708 final MethodHandle returnMethodHandle = linkerServices.asType(MethodHandles.dropArguments(
attila@963 709 OBJECT_IDENTITY, 1, type.parameterList()), type.insertParameterTypes(0, Object.class));
attila@101 710 final MethodHandle nextComponentInvocation = nextComponent.getGuardedInvocation().getInvocation();
attila@963 711 // The assumption is that getGuardedInvocationComponent() already asType()'d it correctly modulo the
attila@963 712 // return type.
attila@963 713 assert nextComponentInvocation.type().changeReturnType(type.returnType()).equals(type);
attila@101 714 // Since it is part of the foldArgument() target, we have to drop an extra arg it receives.
attila@101 715 final MethodHandle nextCombinedInvocation = MethodHandles.dropArguments(nextComponentInvocation, 0,
attila@963 716 Object.class);
attila@101 717 // Assemble it all into a fold(guard(isNotNull, identity, nextInvocation), get)
attila@101 718 final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
attila@963 719 IS_DYNAMIC_METHOD, returnMethodHandle, nextCombinedInvocation), typedGetter);
attila@101 720
attila@101 721 return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
attila@90 722 }
attila@90 723 case 3: {
attila@90 724 // Must have exactly one argument: receiver
attila@90 725 assertParameterCount(callSiteDescriptor, 1);
attila@90 726 final DynamicMethod method = getDynamicMethod(callSiteDescriptor.getNameToken(
attila@90 727 CallSiteDescriptor.NAME_OPERAND));
attila@90 728 if(method == null) {
attila@90 729 // We have no such method, always delegate to the next component
attila@90 730 return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
attila@90 731 }
attila@90 732 // No delegation to the next component of the composite operation; if we have a method with that name,
attila@90 733 // we'll always return it at this point.
attila@90 734 return getClassGuardedInvocationComponent(linkerServices.asType(MethodHandles.dropArguments(
attila@963 735 MethodHandles.constant(Object.class, method), 0, type.parameterType(0)), type), type);
attila@90 736 }
attila@90 737 default: {
attila@90 738 // Can't do anything with more than 3 name components
attila@90 739 return null;
attila@90 740 }
attila@90 741 }
attila@90 742 }
attila@90 743
attila@963 744 static class MethodPair {
attila@963 745 final MethodHandle method1;
attila@963 746 final MethodHandle method2;
attila@963 747
attila@963 748 MethodPair(final MethodHandle method1, final MethodHandle method2) {
attila@963 749 this.method1 = method1;
attila@963 750 this.method2 = method2;
attila@963 751 }
attila@963 752
attila@963 753 MethodHandle guardWithTest(final MethodHandle test) {
attila@963 754 return MethodHandles.guardWithTest(test, method1, method2);
attila@963 755 }
attila@963 756 }
attila@963 757
attila@963 758 static MethodPair matchReturnTypes(final MethodHandle m1, final MethodHandle m2) {
attila@963 759 final MethodType type1 = m1.type();
attila@963 760 final MethodType type2 = m2.type();
attila@963 761 final Class<?> commonRetType = TypeUtilities.getCommonLosslessConversionType(type1.returnType(),
attila@963 762 type2.returnType());
attila@963 763 return new MethodPair(
attila@963 764 m1.asType(type1.changeReturnType(commonRetType)),
attila@963 765 m2.asType(type2.changeReturnType(commonRetType)));
attila@963 766 }
attila@963 767
attila@962 768 private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) {
attila@90 769 if(descriptor.getMethodType().parameterCount() != paramCount) {
attila@90 770 throw new BootstrapMethodError(descriptor.getName() + " must have exactly " + paramCount + " parameters.");
attila@90 771 }
attila@90 772 }
attila@90 773
attila@90 774 private static MethodHandle GET_PROPERTY_GETTER_HANDLE = MethodHandles.dropArguments(privateLookup.findOwnSpecial(
attila@90 775 "getPropertyGetterHandle", Object.class, Object.class), 1, Object.class);
attila@90 776 private final MethodHandle getPropertyGetterHandle = GET_PROPERTY_GETTER_HANDLE.bindTo(this);
attila@90 777
attila@90 778 /**
attila@90 779 * @param id the property ID
attila@90 780 * @return the method handle for retrieving the property, or null if the property does not exist
attila@90 781 */
attila@90 782 @SuppressWarnings("unused")
attila@962 783 private Object getPropertyGetterHandle(final Object id) {
attila@90 784 return propertyGetters.get(id);
attila@90 785 }
attila@90 786
attila@90 787 // Type is MethodHandle(BeanLinker, MethodType, LinkerServices, Object, String, Object), of which the two "Object"
attila@90 788 // args are dropped; this makes handles with first three args conform to "Object, String, Object" though, which is
attila@90 789 // a typical property setter with variable name signature (target, name, value).
attila@90 790 private static final MethodHandle GET_PROPERTY_SETTER_HANDLE = MethodHandles.dropArguments(MethodHandles.dropArguments(
attila@404 791 privateLookup.findOwnSpecial("getPropertySetterHandle", MethodHandle.class, CallSiteDescriptor.class,
attila@90 792 LinkerServices.class, Object.class), 3, Object.class), 5, Object.class);
attila@90 793 // Type is MethodHandle(MethodType, LinkerServices, Object, String, Object)
attila@90 794 private final MethodHandle getPropertySetterHandle = GET_PROPERTY_SETTER_HANDLE.bindTo(this);
attila@90 795
attila@90 796 @SuppressWarnings("unused")
attila@962 797 private MethodHandle getPropertySetterHandle(final CallSiteDescriptor setterDescriptor, final LinkerServices linkerServices,
attila@962 798 final Object id) {
attila@404 799 return getDynamicMethodInvocation(setterDescriptor, linkerServices, String.valueOf(id), propertySetters);
attila@90 800 }
attila@90 801
attila@90 802 private static MethodHandle GET_DYNAMIC_METHOD = MethodHandles.dropArguments(privateLookup.findOwnSpecial(
attila@963 803 "getDynamicMethod", Object.class, Object.class), 1, Object.class);
attila@90 804 private final MethodHandle getDynamicMethod = GET_DYNAMIC_METHOD.bindTo(this);
attila@90 805
attila@90 806 @SuppressWarnings("unused")
attila@963 807 // This method is marked to return Object instead of DynamicMethod as it's used as a linking component and we don't
attila@963 808 // want to make the DynamicMethod type observable externally (e.g. as the return type of a MethodHandle returned for
attila@963 809 // "dyn:getMethod" linking).
attila@963 810 private Object getDynamicMethod(final Object name) {
attila@90 811 return getDynamicMethod(String.valueOf(name), methods);
attila@90 812 }
attila@90 813
attila@90 814 /**
attila@90 815 * Returns a dynamic method of the specified name.
attila@90 816 *
attila@90 817 * @param name name of the method
attila@90 818 * @return the dynamic method (either {@link SimpleDynamicMethod} or {@link OverloadedDynamicMethod}, or null if the
attila@90 819 * method with the specified name does not exist.
attila@90 820 */
attila@962 821 DynamicMethod getDynamicMethod(final String name) {
attila@90 822 return getDynamicMethod(name, methods);
attila@90 823 }
attila@90 824
attila@90 825 /**
attila@90 826 * Find the most generic superclass that declares this getter. Since getters have zero args (aside from the
attila@90 827 * receiver), they can't be overloaded, so we're free to link with an instanceof guard for the most generic one,
attila@90 828 * creating more stable call sites.
attila@90 829 * @param getter the getter
attila@90 830 * @return getter with same name, declared on the most generic superclass/interface of the declaring class
attila@90 831 */
attila@962 832 private static Method getMostGenericGetter(final Method getter) {
attila@90 833 return getMostGenericGetter(getter.getName(), getter.getReturnType(), getter.getDeclaringClass());
attila@90 834 }
attila@90 835
attila@962 836 private static Method getMostGenericGetter(final String name, final Class<?> returnType, final Class<?> declaringClass) {
attila@90 837 if(declaringClass == null) {
attila@90 838 return null;
attila@90 839 }
attila@90 840 // Prefer interfaces
attila@962 841 for(final Class<?> itf: declaringClass.getInterfaces()) {
attila@90 842 final Method itfGetter = getMostGenericGetter(name, returnType, itf);
attila@90 843 if(itfGetter != null) {
attila@90 844 return itfGetter;
attila@90 845 }
attila@90 846 }
attila@90 847 final Method superGetter = getMostGenericGetter(name, returnType, declaringClass.getSuperclass());
attila@90 848 if(superGetter != null) {
attila@90 849 return superGetter;
attila@90 850 }
attila@90 851 if(!CheckRestrictedPackage.isRestrictedClass(declaringClass)) {
attila@90 852 try {
attila@90 853 return declaringClass.getMethod(name);
attila@962 854 } catch(final NoSuchMethodException e) {
attila@90 855 // Intentionally ignored, meant to fall through
attila@90 856 }
attila@90 857 }
attila@90 858 return null;
attila@90 859 }
attila@90 860
attila@404 861 private static final class AnnotatedDynamicMethod {
attila@404 862 private final SingleDynamicMethod method;
attila@90 863 /*private*/ final ValidationType validationType;
attila@90 864
attila@962 865 AnnotatedDynamicMethod(final SingleDynamicMethod method, final ValidationType validationType) {
attila@404 866 this.method = method;
attila@90 867 this.validationType = validationType;
attila@90 868 }
attila@404 869
attila@962 870 MethodHandle getInvocation(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) {
attila@404 871 return method.getInvocation(callSiteDescriptor, linkerServices);
attila@404 872 }
attila@404 873
attila@404 874 @SuppressWarnings("unused")
attila@962 875 MethodHandle getTarget(final MethodHandles.Lookup lookup) {
attila@962 876 final MethodHandle inv = method.getTarget(lookup);
attila@404 877 assert inv != null;
attila@404 878 return inv;
attila@404 879 }
attila@90 880 }
attila@101 881 }

mercurial