Path: blob/jdk8u272-b10-aarch32-20201026/nashorn/src/jdk/internal/dynalink/beans/AbstractJavaLinker.java
48797 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file, and Oracle licenses the original version of this file under the BSD30* license:31*/32/*33Copyright 2009-2013 Attila Szegedi3435Licensed under both the Apache License, Version 2.0 (the "Apache License")36and the BSD License (the "BSD License"), with licensee being free to37choose either of the two at their discretion.3839You may not use this file except in compliance with either the Apache40License or the BSD License.4142If you choose to use this file in compliance with the Apache License, the43following notice applies to you:4445You may obtain a copy of the Apache License at4647http://www.apache.org/licenses/LICENSE-2.04849Unless required by applicable law or agreed to in writing, software50distributed under the License is distributed on an "AS IS" BASIS,51WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or52implied. See the License for the specific language governing53permissions and limitations under the License.5455If you choose to use this file in compliance with the BSD License, the56following notice applies to you:5758Redistribution and use in source and binary forms, with or without59modification, are permitted provided that the following conditions are60met:61* Redistributions of source code must retain the above copyright62notice, this list of conditions and the following disclaimer.63* Redistributions in binary form must reproduce the above copyright64notice, this list of conditions and the following disclaimer in the65documentation and/or other materials provided with the distribution.66* Neither the name of the copyright holder nor the names of67contributors may be used to endorse or promote products derived from68this software without specific prior written permission.6970THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS71IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED72TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A73PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER74BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR75CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF76SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR77BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,78WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR79OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF80ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.81*/8283package jdk.internal.dynalink.beans;8485import java.lang.invoke.MethodHandle;86import java.lang.invoke.MethodHandles;87import java.lang.invoke.MethodType;88import java.lang.reflect.AccessibleObject;89import java.lang.reflect.Constructor;90import java.lang.reflect.Field;91import java.lang.reflect.Member;92import java.lang.reflect.Method;93import java.lang.reflect.Modifier;94import java.util.Collection;95import java.util.Collections;96import java.util.HashMap;97import java.util.List;98import java.util.Map;99import jdk.internal.dynalink.CallSiteDescriptor;100import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType;101import jdk.internal.dynalink.linker.GuardedInvocation;102import jdk.internal.dynalink.linker.GuardingDynamicLinker;103import jdk.internal.dynalink.linker.LinkRequest;104import jdk.internal.dynalink.linker.LinkerServices;105import jdk.internal.dynalink.support.CallSiteDescriptorFactory;106import jdk.internal.dynalink.support.Guards;107import jdk.internal.dynalink.support.Lookup;108import jdk.internal.dynalink.support.TypeUtilities;109110/**111* A base class for both {@link StaticClassLinker} and {@link BeanLinker}. Deals with common aspects of property112* exposure and method calls for both static and instance facets of a class.113*114* @author Attila Szegedi115*/116abstract class AbstractJavaLinker implements GuardingDynamicLinker {117118final Class<?> clazz;119private final MethodHandle classGuard;120private final MethodHandle assignableGuard;121private final Map<String, AnnotatedDynamicMethod> propertyGetters = new HashMap<>();122private final Map<String, DynamicMethod> propertySetters = new HashMap<>();123private final Map<String, DynamicMethod> methods = new HashMap<>();124125AbstractJavaLinker(final Class<?> clazz, final MethodHandle classGuard) {126this(clazz, classGuard, classGuard);127}128129AbstractJavaLinker(final Class<?> clazz, final MethodHandle classGuard, final MethodHandle assignableGuard) {130this.clazz = clazz;131this.classGuard = classGuard;132this.assignableGuard = assignableGuard;133134final FacetIntrospector introspector = createFacetIntrospector();135// Add methods and properties136for(final Method method: introspector.getMethods()) {137final String name = method.getName();138// Add method139addMember(name, method, methods);140// Add the method as a property getter and/or setter141if(name.startsWith("get") && name.length() > 3 && method.getParameterTypes().length == 0) {142// Property getter143setPropertyGetter(method, 3);144} else if(name.startsWith("is") && name.length() > 2 && method.getParameterTypes().length == 0 &&145method.getReturnType() == boolean.class) {146// Boolean property getter147setPropertyGetter(method, 2);148} else if(name.startsWith("set") && name.length() > 3 && method.getParameterTypes().length == 1) {149// Property setter150addMember(decapitalize(name.substring(3)), method, propertySetters);151}152}153154// Add field getter/setters as property getters/setters.155for(final Field field: introspector.getFields()) {156final String name = field.getName();157// Only add a property getter when one is not defined already as a getXxx()/isXxx() method.158if(!propertyGetters.containsKey(name)) {159setPropertyGetter(name, introspector.unreflectGetter(field), ValidationType.EXACT_CLASS);160}161if(!(Modifier.isFinal(field.getModifiers()) || propertySetters.containsKey(name))) {162addMember(name, new SimpleDynamicMethod(introspector.unreflectSetter(field), clazz, name),163propertySetters);164}165}166167// Add inner classes, but only those for which we don't hide a property with it168for(final Map.Entry<String, MethodHandle> innerClassSpec: introspector.getInnerClassGetters().entrySet()) {169final String name = innerClassSpec.getKey();170if(!propertyGetters.containsKey(name)) {171setPropertyGetter(name, innerClassSpec.getValue(), ValidationType.EXACT_CLASS);172}173}174}175176private static String decapitalize(final String str) {177assert str != null;178if(str.isEmpty()) {179return str;180}181182final char c0 = str.charAt(0);183if(Character.isLowerCase(c0)) {184return str;185}186187// If it has two consecutive upper-case characters, i.e. "URL", don't decapitalize188if(str.length() > 1 && Character.isUpperCase(str.charAt(1))) {189return str;190}191192final char c[] = str.toCharArray();193c[0] = Character.toLowerCase(c0);194return new String(c);195}196197abstract FacetIntrospector createFacetIntrospector();198199Collection<String> getReadablePropertyNames() {200return getUnmodifiableKeys(propertyGetters);201}202203Collection<String> getWritablePropertyNames() {204return getUnmodifiableKeys(propertySetters);205}206207Collection<String> getMethodNames() {208return getUnmodifiableKeys(methods);209}210211private static Collection<String> getUnmodifiableKeys(final Map<String, ?> m) {212return Collections.unmodifiableCollection(m.keySet());213}214215/**216* Sets the specified dynamic method to be the property getter for the specified property. Note that you can only217* use this when you're certain that the method handle does not belong to a caller-sensitive method. For properties218* that are caller-sensitive, you must use {@link #setPropertyGetter(String, SingleDynamicMethod, ValidationType)}219* instead.220* @param name name of the property221* @param handle the method handle that implements the property getter222* @param validationType the validation type for the property223*/224private void setPropertyGetter(final String name, final SingleDynamicMethod handle, final ValidationType validationType) {225propertyGetters.put(name, new AnnotatedDynamicMethod(handle, validationType));226}227228/**229* Sets the specified reflective method to be the property getter for the specified property.230* @param getter the getter method231* @param prefixLen the getter prefix in the method name; should be 3 for getter names starting with "get" and 2 for232* names starting with "is".233*/234private void setPropertyGetter(final Method getter, final int prefixLen) {235setPropertyGetter(decapitalize(getter.getName().substring(prefixLen)), createDynamicMethod(236getMostGenericGetter(getter)), ValidationType.INSTANCE_OF);237}238239/**240* Sets the specified method handle to be the property getter for the specified property. Note that you can only241* use this when you're certain that the method handle does not belong to a caller-sensitive method. For properties242* that are caller-sensitive, you must use {@link #setPropertyGetter(String, SingleDynamicMethod, ValidationType)}243* instead.244* @param name name of the property245* @param handle the method handle that implements the property getter246* @param validationType the validation type for the property247*/248void setPropertyGetter(final String name, final MethodHandle handle, final ValidationType validationType) {249setPropertyGetter(name, new SimpleDynamicMethod(handle, clazz, name), validationType);250}251252private void addMember(final String name, final AccessibleObject ao, final Map<String, DynamicMethod> methodMap) {253addMember(name, createDynamicMethod(ao), methodMap);254}255256private void addMember(final String name, final SingleDynamicMethod method, final Map<String, DynamicMethod> methodMap) {257final DynamicMethod existingMethod = methodMap.get(name);258final DynamicMethod newMethod = mergeMethods(method, existingMethod, clazz, name);259if(newMethod != existingMethod) {260methodMap.put(name, newMethod);261}262}263264/**265* Given one or more reflective methods or constructors, creates a dynamic method that represents them all. The266* methods should represent all overloads of the same name (or all constructors of the class).267* @param members the reflective members268* @param clazz the class declaring the reflective members269* @param name the common name of the reflective members.270* @return a dynamic method representing all the specified reflective members.271*/272static DynamicMethod createDynamicMethod(final Iterable<? extends AccessibleObject> members, final Class<?> clazz, final String name) {273DynamicMethod dynMethod = null;274for(final AccessibleObject method: members) {275dynMethod = mergeMethods(createDynamicMethod(method), dynMethod, clazz, name);276}277return dynMethod;278}279280/**281* Given a reflective method or a constructor, creates a dynamic method that represents it. This method will282* distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive283* dynamic method when needed.284* @param m the reflective member285* @return the single dynamic method representing the reflective member286*/287private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) {288if(CallerSensitiveDetector.isCallerSensitive(m)) {289// Method has @CallerSensitive annotation290return new CallerSensitiveDynamicMethod(m);291}292// Method has no @CallerSensitive annotation293final MethodHandle mh;294try {295mh = unreflectSafely(m);296} catch (final IllegalAccessError e) {297// java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't298// marked with the annotation. In this case, we'll fall back to treating it as caller sensitive.299return new CallerSensitiveDynamicMethod(m);300}301// Proceed with non-caller sensitive302final Member member = (Member)m;303return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor);304}305306/**307* Unreflects a method handle from a Method or a Constructor using safe (zero-privilege) unreflection. Should be308* only used for methods and constructors that are not caller sensitive. If a caller sensitive method were309* unreflected through this mechanism, it would not be a security issue, but would be bound to the zero-privilege310* unreflector as its caller, and thus completely useless.311* @param m the method or constructor312* @return the method handle313*/314private static MethodHandle unreflectSafely(final AccessibleObject m) {315if(m instanceof Method) {316final Method reflMethod = (Method)m;317final MethodHandle handle = Lookup.PUBLIC.unreflect(reflMethod);318if(Modifier.isStatic(reflMethod.getModifiers())) {319return StaticClassIntrospector.editStaticMethodHandle(handle);320}321return handle;322}323return StaticClassIntrospector.editConstructorMethodHandle(Lookup.PUBLIC.unreflectConstructor((Constructor<?>)m));324}325326private static DynamicMethod mergeMethods(final SingleDynamicMethod method, final DynamicMethod existing, final Class<?> clazz, final String name) {327if(existing == null) {328return method;329} else if(existing.contains(method)) {330return existing;331} else if(existing instanceof SingleDynamicMethod) {332final OverloadedDynamicMethod odm = new OverloadedDynamicMethod(clazz, name);333odm.addMethod(((SingleDynamicMethod)existing));334odm.addMethod(method);335return odm;336} else if(existing instanceof OverloadedDynamicMethod) {337((OverloadedDynamicMethod)existing).addMethod(method);338return existing;339}340throw new AssertionError();341}342343@Override344public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)345throws Exception {346final LinkRequest ncrequest = request.withoutRuntimeContext();347// BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn".348final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor();349final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR);350// Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]).351if("callMethod" == op) {352return getCallPropWithThis(callSiteDescriptor, linkerServices);353}354List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor);355while(!operations.isEmpty()) {356final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices,357operations);358if(gic != null) {359return gic.getGuardedInvocation();360}361operations = pop(operations);362}363return null;364}365366protected GuardedInvocationComponent getGuardedInvocationComponent(final CallSiteDescriptor callSiteDescriptor,367final LinkerServices linkerServices, final List<String> operations) throws Exception {368if(operations.isEmpty()) {369return null;370}371final String op = operations.get(0);372// Either dyn:getProp:name(this) or dyn:getProp(this, name)373if("getProp".equals(op)) {374return getPropertyGetter(callSiteDescriptor, linkerServices, pop(operations));375}376// Either dyn:setProp:name(this, value) or dyn:setProp(this, name, value)377if("setProp".equals(op)) {378return getPropertySetter(callSiteDescriptor, linkerServices, pop(operations));379}380// Either dyn:getMethod:name(this), or dyn:getMethod(this, name)381if("getMethod".equals(op)) {382return getMethodGetter(callSiteDescriptor, linkerServices, pop(operations));383}384return null;385}386387static final <T> List<T> pop(final List<T> l) {388return l.subList(1, l.size());389}390391MethodHandle getClassGuard(final CallSiteDescriptor desc) {392return getClassGuard(desc.getMethodType());393}394395MethodHandle getClassGuard(final MethodType type) {396return Guards.asType(classGuard, type);397}398399GuardedInvocationComponent getClassGuardedInvocationComponent(final MethodHandle invocation, final MethodType type) {400return new GuardedInvocationComponent(invocation, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);401}402403SingleDynamicMethod getConstructorMethod(final String signature) {404return null;405}406407private MethodHandle getAssignableGuard(final MethodType type) {408return Guards.asType(assignableGuard, type);409}410411private GuardedInvocation getCallPropWithThis(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) {412switch(callSiteDescriptor.getNameTokenCount()) {413case 3: {414return createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,415callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), methods);416}417default: {418return null;419}420}421}422423private GuardedInvocation createGuardedDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor,424final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap){425final MethodHandle inv = getDynamicMethodInvocation(callSiteDescriptor, linkerServices, methodName, methodMap);426return inv == null ? null : new GuardedInvocation(inv, getClassGuard(callSiteDescriptor.getMethodType()));427}428429private MethodHandle getDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor,430final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap) {431final DynamicMethod dynaMethod = getDynamicMethod(methodName, methodMap);432return dynaMethod != null ? dynaMethod.getInvocation(callSiteDescriptor, linkerServices) : null;433}434435private DynamicMethod getDynamicMethod(final String methodName, final Map<String, DynamicMethod> methodMap) {436final DynamicMethod dynaMethod = methodMap.get(methodName);437return dynaMethod != null ? dynaMethod : getExplicitSignatureDynamicMethod(methodName, methodMap);438}439440private SingleDynamicMethod getExplicitSignatureDynamicMethod(final String fullName,441final Map<String, DynamicMethod> methodsMap) {442// What's below is meant to support the "name(type, type, ...)" syntax that programmers can use in a method name443// to manually pin down an exact overloaded variant. This is not usually required, as the overloaded method444// resolution works correctly in almost every situation. However, in presence of many language-specific445// conversions with a radically dynamic language, most overloaded methods will end up being constantly selected446// at invocation time, so a programmer knowledgeable of the situation might choose to pin down an exact overload447// for performance reasons.448449// Is the method name lexically of the form "name(types)"?450final int lastChar = fullName.length() - 1;451if(fullName.charAt(lastChar) != ')') {452return null;453}454final int openBrace = fullName.indexOf('(');455if(openBrace == -1) {456return null;457}458459final String name = fullName.substring(0, openBrace);460final String signature = fullName.substring(openBrace + 1, lastChar);461462// Find an existing method for the "name" part463final DynamicMethod simpleNamedMethod = methodsMap.get(name);464if(simpleNamedMethod == null) {465// explicit signature constructor access466// Java.type("java.awt.Color")["(int,int,int)"]467// will get Color(int,int,int) constructor of Color class.468if (name.isEmpty()) {469return getConstructorMethod(signature);470}471472return null;473}474475// Try to get a narrowed dynamic method for the explicit parameter types.476return simpleNamedMethod.getMethodForExactParamTypes(signature);477}478479private static final MethodHandle IS_METHOD_HANDLE_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType(480boolean.class, MethodHandle.class));481private static final MethodHandle CONSTANT_NULL_DROP_METHOD_HANDLE = MethodHandles.dropArguments(482MethodHandles.constant(Object.class, null), 0, MethodHandle.class);483484private GuardedInvocationComponent getPropertySetter(final CallSiteDescriptor callSiteDescriptor,485final LinkerServices linkerServices, final List<String> operations) throws Exception {486switch(callSiteDescriptor.getNameTokenCount()) {487case 2: {488// Must have three arguments: target object, property name, and property value.489assertParameterCount(callSiteDescriptor, 3);490491// We want setters that conform to "Object(O, V)". Note, we aren't doing "R(O, V)" as it might not be492// valid for us to convert return values proactively. Also, since we don't know what setters will be493// invoked, we'll conservatively presume Object return type. The one exception is void return.494final MethodType origType = callSiteDescriptor.getMethodType();495final MethodType type = origType.returnType() == void.class ? origType : origType.changeReturnType(Object.class);496497// What's below is basically:498// foldArguments(guardWithTest(isNotNull, invoke, null|nextComponent.invocation),499// get_setter_handle(type, linkerServices))500// only with a bunch of method signature adjustments. Basically, retrieve method setter501// MethodHandle; if it is non-null, invoke it, otherwise either return null, or delegate to next502// component's invocation.503504// Call site type is "ret_type(object_type,property_name_type,property_value_type)", which we'll505// abbreviate to R(O, N, V) going forward, although we don't really use R here (see above about using506// Object return type).507final MethodType setterType = type.dropParameterTypes(1, 2);508// Bind property setter handle to the expected setter type and linker services. Type is509// MethodHandle(Object, String, Object)510final MethodHandle boundGetter = MethodHandles.insertArguments(getPropertySetterHandle, 0,511callSiteDescriptor.changeMethodType(setterType), linkerServices);512513// Cast getter to MethodHandle(O, N, V)514final MethodHandle typedGetter = linkerServices.asType(boundGetter, type.changeReturnType(515MethodHandle.class));516517// Handle to invoke the setter R(MethodHandle, O, V)518final MethodHandle invokeHandle = MethodHandles.exactInvoker(setterType);519// Handle to invoke the setter, dropping unnecessary fold arguments R(MethodHandle, O, N, V)520final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandle, 2, type.parameterType(5211));522final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,523linkerServices, operations);524525final MethodHandle fallbackFolded;526if(nextComponent == null) {527// Object(MethodHandle)->Object(MethodHandle, O, N, V); returns constant null528fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_METHOD_HANDLE, 1,529type.parameterList()).asType(type.insertParameterTypes(0, MethodHandle.class));530} else {531// Object(O, N, V)->Object(MethodHandle, O, N, V); adapts the next component's invocation to drop the532// extra argument resulting from fold533fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(),5340, MethodHandle.class);535}536537// fold(R(MethodHandle, O, N, V), MethodHandle(O, N, V))538final MethodHandle compositeSetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(539IS_METHOD_HANDLE_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);540if(nextComponent == null) {541return getClassGuardedInvocationComponent(compositeSetter, type);542}543return nextComponent.compose(compositeSetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);544}545case 3: {546// Must have two arguments: target object and property value547assertParameterCount(callSiteDescriptor, 2);548final GuardedInvocation gi = createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,549callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), propertySetters);550// If we have a property setter with this name, this composite operation will always stop here551if(gi != null) {552return new GuardedInvocationComponent(gi, clazz, ValidationType.EXACT_CLASS);553}554// If we don't have a property setter with this name, always fall back to the next operation in the555// composite (if any)556return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, operations);557}558default: {559// More than two name components; don't know what to do with it.560return null;561}562}563}564565private static final Lookup privateLookup = new Lookup(MethodHandles.lookup());566567private static final MethodHandle IS_ANNOTATED_METHOD_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType(568boolean.class, AnnotatedDynamicMethod.class));569private static final MethodHandle CONSTANT_NULL_DROP_ANNOTATED_METHOD = MethodHandles.dropArguments(570MethodHandles.constant(Object.class, null), 0, AnnotatedDynamicMethod.class);571private static final MethodHandle GET_ANNOTATED_METHOD = privateLookup.findVirtual(AnnotatedDynamicMethod.class,572"getTarget", MethodType.methodType(MethodHandle.class, MethodHandles.Lookup.class, LinkerServices.class));573private static final MethodHandle GETTER_INVOKER = MethodHandles.invoker(MethodType.methodType(Object.class, Object.class));574575private GuardedInvocationComponent getPropertyGetter(final CallSiteDescriptor callSiteDescriptor,576final LinkerServices linkerServices, final List<String> ops) throws Exception {577switch(callSiteDescriptor.getNameTokenCount()) {578case 2: {579// Since we can't know what kind of a getter we'll get back on different invocations, we'll just580// conservatively presume Object. Note we can't just coerce to a narrower call site type as the linking581// runtime might not allow coercing at that call site.582final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);583// Must have exactly two arguments: receiver and name584assertParameterCount(callSiteDescriptor, 2);585586// What's below is basically:587// foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle)588// only with a bunch of method signature adjustments. Basically, retrieve method getter589// AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null,590// or delegate to next component's invocation.591592final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType(593AnnotatedDynamicMethod.class));594final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments(595GET_ANNOTATED_METHOD, 1, callSiteDescriptor.getLookup(), linkerServices);596final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0,597callSiteBoundMethodGetter);598// Object(AnnotatedDynamicMethod, Object)->Object(AnnotatedDynamicMethod, T0)599final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker,600MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0)));601// Since it's in the target of a fold, drop the unnecessary second argument602// Object(AnnotatedDynamicMethod, T0)->Object(AnnotatedDynamicMethod, T0, T1)603final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2,604type.parameterType(1));605final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,606linkerServices, ops);607608final MethodHandle fallbackFolded;609if(nextComponent == null) {610// Object(AnnotatedDynamicMethod)->Object(AnnotatedDynamicMethod, T0, T1); returns constant null611fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1,612type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class));613} else {614// Object(T0, T1)->Object(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to615// drop the extra argument resulting from fold and to change its return type to Object.616final MethodHandle nextInvocation = nextComponent.getGuardedInvocation().getInvocation();617final MethodType nextType = nextInvocation.type();618fallbackFolded = MethodHandles.dropArguments(nextInvocation.asType(619nextType.changeReturnType(Object.class)), 0, AnnotatedDynamicMethod.class);620}621622// fold(Object(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1))623final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(624IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);625if(nextComponent == null) {626return getClassGuardedInvocationComponent(compositeGetter, type);627}628return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);629}630case 3: {631// Must have exactly one argument: receiver632assertParameterCount(callSiteDescriptor, 1);633// Fixed name634final AnnotatedDynamicMethod annGetter = propertyGetters.get(callSiteDescriptor.getNameToken(635CallSiteDescriptor.NAME_OPERAND));636if(annGetter == null) {637// We have no such property, always delegate to the next component operation638return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);639}640final MethodHandle getter = annGetter.getInvocation(callSiteDescriptor, linkerServices);641// NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being642// overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the643// method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If644// we're linking against a field getter, don't make the assumption.645// NOTE: No delegation to the next component operation if we have a property with this name, even if its646// value is null.647final ValidationType validationType = annGetter.validationType;648// TODO: we aren't using the type that declares the most generic getter here!649return new GuardedInvocationComponent(getter, getGuard(validationType,650callSiteDescriptor.getMethodType()), clazz, validationType);651}652default: {653// Can't do anything with more than 3 name components654return null;655}656}657}658659private MethodHandle getGuard(final ValidationType validationType, final MethodType methodType) {660switch(validationType) {661case EXACT_CLASS: {662return getClassGuard(methodType);663}664case INSTANCE_OF: {665return getAssignableGuard(methodType);666}667case IS_ARRAY: {668return Guards.isArray(0, methodType);669}670case NONE: {671return null;672}673default: {674throw new AssertionError();675}676}677}678679private static final MethodHandle IS_DYNAMIC_METHOD = Guards.isInstance(DynamicMethod.class,680MethodType.methodType(boolean.class, Object.class));681private static final MethodHandle OBJECT_IDENTITY = MethodHandles.identity(Object.class);682683private GuardedInvocationComponent getMethodGetter(final CallSiteDescriptor callSiteDescriptor,684final LinkerServices linkerServices, final List<String> ops) throws Exception {685// The created method handle will always return a DynamicMethod (or null), but since we don't want that type to686// be visible outside of this linker, declare it to return Object.687final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);688switch(callSiteDescriptor.getNameTokenCount()) {689case 2: {690// Must have exactly two arguments: receiver and name691assertParameterCount(callSiteDescriptor, 2);692final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,693linkerServices, ops);694if(nextComponent == null || !TypeUtilities.areAssignable(DynamicMethod.class,695nextComponent.getGuardedInvocation().getInvocation().type().returnType())) {696// No next component operation, or it can never produce a dynamic method; just return a component697// for this operation.698return getClassGuardedInvocationComponent(linkerServices.asType(getDynamicMethod, type), type);699}700701// What's below is basically:702// foldArguments(guardWithTest(isNotNull, identity, nextComponent.invocation), getter) only with a703// bunch of method signature adjustments. Basically, execute method getter; if it returns a non-null704// DynamicMethod, use identity to return it, otherwise delegate to nextComponent's invocation.705706final MethodHandle typedGetter = linkerServices.asType(getDynamicMethod, type);707// Since it is part of the foldArgument() target, it will have extra args that we need to drop.708final MethodHandle returnMethodHandle = linkerServices.asType(MethodHandles.dropArguments(709OBJECT_IDENTITY, 1, type.parameterList()), type.insertParameterTypes(0, Object.class));710final MethodHandle nextComponentInvocation = nextComponent.getGuardedInvocation().getInvocation();711// The assumption is that getGuardedInvocationComponent() already asType()'d it correctly modulo the712// return type.713assert nextComponentInvocation.type().changeReturnType(type.returnType()).equals(type);714// Since it is part of the foldArgument() target, we have to drop an extra arg it receives.715final MethodHandle nextCombinedInvocation = MethodHandles.dropArguments(nextComponentInvocation, 0,716Object.class);717// Assemble it all into a fold(guard(isNotNull, identity, nextInvocation), get)718final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(719IS_DYNAMIC_METHOD, returnMethodHandle, nextCombinedInvocation), typedGetter);720721return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);722}723case 3: {724// Must have exactly one argument: receiver725assertParameterCount(callSiteDescriptor, 1);726final DynamicMethod method = getDynamicMethod(callSiteDescriptor.getNameToken(727CallSiteDescriptor.NAME_OPERAND));728if(method == null) {729// We have no such method, always delegate to the next component730return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);731}732// No delegation to the next component of the composite operation; if we have a method with that name,733// we'll always return it at this point.734return getClassGuardedInvocationComponent(linkerServices.asType(MethodHandles.dropArguments(735MethodHandles.constant(Object.class, method), 0, type.parameterType(0)), type), type);736}737default: {738// Can't do anything with more than 3 name components739return null;740}741}742}743744static class MethodPair {745final MethodHandle method1;746final MethodHandle method2;747748MethodPair(final MethodHandle method1, final MethodHandle method2) {749this.method1 = method1;750this.method2 = method2;751}752753MethodHandle guardWithTest(final MethodHandle test) {754return MethodHandles.guardWithTest(test, method1, method2);755}756}757758static MethodPair matchReturnTypes(final MethodHandle m1, final MethodHandle m2) {759final MethodType type1 = m1.type();760final MethodType type2 = m2.type();761final Class<?> commonRetType = TypeUtilities.getCommonLosslessConversionType(type1.returnType(),762type2.returnType());763return new MethodPair(764m1.asType(type1.changeReturnType(commonRetType)),765m2.asType(type2.changeReturnType(commonRetType)));766}767768private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) {769if(descriptor.getMethodType().parameterCount() != paramCount) {770throw new BootstrapMethodError(descriptor.getName() + " must have exactly " + paramCount + " parameters.");771}772}773774private static MethodHandle GET_PROPERTY_GETTER_HANDLE = MethodHandles.dropArguments(privateLookup.findOwnSpecial(775"getPropertyGetterHandle", Object.class, Object.class), 1, Object.class);776private final MethodHandle getPropertyGetterHandle = GET_PROPERTY_GETTER_HANDLE.bindTo(this);777778/**779* @param id the property ID780* @return the method handle for retrieving the property, or null if the property does not exist781*/782@SuppressWarnings("unused")783private Object getPropertyGetterHandle(final Object id) {784return propertyGetters.get(String.valueOf(id));785}786787// Type is MethodHandle(BeanLinker, MethodType, LinkerServices, Object, String, Object), of which the two "Object"788// args are dropped; this makes handles with first three args conform to "Object, String, Object" though, which is789// a typical property setter with variable name signature (target, name, value).790private static final MethodHandle GET_PROPERTY_SETTER_HANDLE = MethodHandles.dropArguments(MethodHandles.dropArguments(791privateLookup.findOwnSpecial("getPropertySetterHandle", MethodHandle.class, CallSiteDescriptor.class,792LinkerServices.class, Object.class), 3, Object.class), 5, Object.class);793// Type is MethodHandle(MethodType, LinkerServices, Object, String, Object)794private final MethodHandle getPropertySetterHandle = GET_PROPERTY_SETTER_HANDLE.bindTo(this);795796@SuppressWarnings("unused")797private MethodHandle getPropertySetterHandle(final CallSiteDescriptor setterDescriptor, final LinkerServices linkerServices,798final Object id) {799return getDynamicMethodInvocation(setterDescriptor, linkerServices, String.valueOf(id), propertySetters);800}801802private static MethodHandle GET_DYNAMIC_METHOD = MethodHandles.dropArguments(privateLookup.findOwnSpecial(803"getDynamicMethod", Object.class, Object.class), 1, Object.class);804private final MethodHandle getDynamicMethod = GET_DYNAMIC_METHOD.bindTo(this);805806@SuppressWarnings("unused")807// This method is marked to return Object instead of DynamicMethod as it's used as a linking component and we don't808// want to make the DynamicMethod type observable externally (e.g. as the return type of a MethodHandle returned for809// "dyn:getMethod" linking).810private Object getDynamicMethod(final Object name) {811return getDynamicMethod(String.valueOf(name), methods);812}813814/**815* Returns a dynamic method of the specified name.816*817* @param name name of the method818* @return the dynamic method (either {@link SimpleDynamicMethod} or {@link OverloadedDynamicMethod}, or null if the819* method with the specified name does not exist.820*/821DynamicMethod getDynamicMethod(final String name) {822return getDynamicMethod(name, methods);823}824825/**826* Find the most generic superclass that declares this getter. Since getters have zero args (aside from the827* receiver), they can't be overloaded, so we're free to link with an instanceof guard for the most generic one,828* creating more stable call sites.829* @param getter the getter830* @return getter with same name, declared on the most generic superclass/interface of the declaring class831*/832private static Method getMostGenericGetter(final Method getter) {833return getMostGenericGetter(getter.getName(), getter.getReturnType(), getter.getDeclaringClass());834}835836private static Method getMostGenericGetter(final String name, final Class<?> returnType, final Class<?> declaringClass) {837if(declaringClass == null) {838return null;839}840// Prefer interfaces841for(final Class<?> itf: declaringClass.getInterfaces()) {842final Method itfGetter = getMostGenericGetter(name, returnType, itf);843if(itfGetter != null) {844return itfGetter;845}846}847final Method superGetter = getMostGenericGetter(name, returnType, declaringClass.getSuperclass());848if(superGetter != null) {849return superGetter;850}851if(!CheckRestrictedPackage.isRestrictedClass(declaringClass)) {852try {853return declaringClass.getMethod(name);854} catch(final NoSuchMethodException e) {855// Intentionally ignored, meant to fall through856}857}858return null;859}860861private static final class AnnotatedDynamicMethod {862private final SingleDynamicMethod method;863/*private*/ final ValidationType validationType;864865AnnotatedDynamicMethod(final SingleDynamicMethod method, final ValidationType validationType) {866this.method = method;867this.validationType = validationType;868}869870MethodHandle getInvocation(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) {871return method.getInvocation(callSiteDescriptor, linkerServices);872}873874@SuppressWarnings("unused")875MethodHandle getTarget(final MethodHandles.Lookup lookup, final LinkerServices linkerServices) {876final MethodHandle inv = linkerServices.filterInternalObjects(method.getTarget(lookup));877assert inv != null;878return inv;879}880}881}882883884