Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/src/jdk/internal/dynalink/beans/MaximallySpecific.java
48579 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.MethodType;87import java.util.Iterator;88import java.util.LinkedList;89import java.util.List;90import jdk.internal.dynalink.linker.ConversionComparator.Comparison;91import jdk.internal.dynalink.linker.LinkerServices;92import jdk.internal.dynalink.support.TypeUtilities;9394/**95* Utility class that encapsulates the algorithm for choosing the maximally specific methods.96*97* @author Attila Szegedi98*/99class MaximallySpecific {100/**101* Given a list of methods, returns a list of maximally specific methods.102*103* @param methods the list of methods104* @param varArgs whether to assume the methods are varargs105* @return the list of maximally specific methods.106*/107static List<SingleDynamicMethod> getMaximallySpecificMethods(final List<SingleDynamicMethod> methods, final boolean varArgs) {108return getMaximallySpecificSingleDynamicMethods(methods, varArgs, null, null);109}110111private abstract static class MethodTypeGetter<T> {112abstract MethodType getMethodType(T t);113}114115private static final MethodTypeGetter<MethodHandle> METHOD_HANDLE_TYPE_GETTER =116new MethodTypeGetter<MethodHandle>() {117@Override118MethodType getMethodType(final MethodHandle t) {119return t.type();120}121};122123private static final MethodTypeGetter<SingleDynamicMethod> DYNAMIC_METHOD_TYPE_GETTER =124new MethodTypeGetter<SingleDynamicMethod>() {125@Override126MethodType getMethodType(final SingleDynamicMethod t) {127return t.getMethodType();128}129};130131/**132* Given a list of methods handles, returns a list of maximally specific methods, applying language-runtime133* specific conversion preferences.134*135* @param methods the list of method handles136* @param varArgs whether to assume the method handles are varargs137* @param argTypes concrete argument types for the invocation138* @return the list of maximally specific method handles.139*/140static List<MethodHandle> getMaximallySpecificMethodHandles(final List<MethodHandle> methods, final boolean varArgs,141final Class<?>[] argTypes, final LinkerServices ls) {142return getMaximallySpecificMethods(methods, varArgs, argTypes, ls, METHOD_HANDLE_TYPE_GETTER);143}144145/**146* Given a list of methods, returns a list of maximally specific methods, applying language-runtime specific147* conversion preferences.148*149* @param methods the list of methods150* @param varArgs whether to assume the methods are varargs151* @param argTypes concrete argument types for the invocation152* @return the list of maximally specific methods.153*/154static List<SingleDynamicMethod> getMaximallySpecificSingleDynamicMethods(final List<SingleDynamicMethod> methods,155final boolean varArgs, final Class<?>[] argTypes, final LinkerServices ls) {156return getMaximallySpecificMethods(methods, varArgs, argTypes, ls, DYNAMIC_METHOD_TYPE_GETTER);157}158159/**160* Given a list of methods, returns a list of maximally specific methods, applying language-runtime specific161* conversion preferences.162*163* @param methods the list of methods164* @param varArgs whether to assume the methods are varargs165* @param argTypes concrete argument types for the invocation166* @return the list of maximally specific methods.167*/168private static <T> List<T> getMaximallySpecificMethods(final List<T> methods, final boolean varArgs,169final Class<?>[] argTypes, final LinkerServices ls, final MethodTypeGetter<T> methodTypeGetter) {170if(methods.size() < 2) {171return methods;172}173final LinkedList<T> maximals = new LinkedList<>();174for(final T m: methods) {175final MethodType methodType = methodTypeGetter.getMethodType(m);176boolean lessSpecific = false;177for(final Iterator<T> maximal = maximals.iterator(); maximal.hasNext();) {178final T max = maximal.next();179switch(isMoreSpecific(methodType, methodTypeGetter.getMethodType(max), varArgs, argTypes, ls)) {180case TYPE_1_BETTER: {181maximal.remove();182break;183}184case TYPE_2_BETTER: {185lessSpecific = true;186break;187}188case INDETERMINATE: {189// do nothing190break;191}192default: {193throw new AssertionError();194}195}196}197if(!lessSpecific) {198maximals.addLast(m);199}200}201return maximals;202}203204private static Comparison isMoreSpecific(final MethodType t1, final MethodType t2, final boolean varArgs, final Class<?>[] argTypes,205final LinkerServices ls) {206final int pc1 = t1.parameterCount();207final int pc2 = t2.parameterCount();208assert varArgs || (pc1 == pc2) && (argTypes == null || argTypes.length == pc1);209assert (argTypes == null) == (ls == null);210final int maxPc = Math.max(Math.max(pc1, pc2), argTypes == null ? 0 : argTypes.length);211boolean t1MoreSpecific = false;212boolean t2MoreSpecific = false;213// NOTE: Starting from 1 as overloaded method resolution doesn't depend on 0th element, which is the type of214// 'this'. We're only dealing with instance methods here, not static methods. Actually, static methods will have215// a fake 'this' of type StaticClass.216for(int i = 1; i < maxPc; ++i) {217final Class<?> c1 = getParameterClass(t1, pc1, i, varArgs);218final Class<?> c2 = getParameterClass(t2, pc2, i, varArgs);219if(c1 != c2) {220final Comparison cmp = compare(c1, c2, argTypes, i, ls);221if(cmp == Comparison.TYPE_1_BETTER && !t1MoreSpecific) {222t1MoreSpecific = true;223if(t2MoreSpecific) {224return Comparison.INDETERMINATE;225}226}227if(cmp == Comparison.TYPE_2_BETTER && !t2MoreSpecific) {228t2MoreSpecific = true;229if(t1MoreSpecific) {230return Comparison.INDETERMINATE;231}232}233}234}235if(t1MoreSpecific) {236return Comparison.TYPE_1_BETTER;237} else if(t2MoreSpecific) {238return Comparison.TYPE_2_BETTER;239}240return Comparison.INDETERMINATE;241}242243private static Comparison compare(final Class<?> c1, final Class<?> c2, final Class<?>[] argTypes, final int i, final LinkerServices cmp) {244if(cmp != null) {245final Comparison c = cmp.compareConversion(argTypes[i], c1, c2);246if(c != Comparison.INDETERMINATE) {247return c;248}249}250if(TypeUtilities.isSubtype(c1, c2)) {251return Comparison.TYPE_1_BETTER;252} if(TypeUtilities.isSubtype(c2, c1)) {253return Comparison.TYPE_2_BETTER;254}255return Comparison.INDETERMINATE;256}257258private static Class<?> getParameterClass(final MethodType t, final int l, final int i, final boolean varArgs) {259return varArgs && i >= l - 1 ? t.parameterType(l - 1).getComponentType() : t.parameterType(i);260}261}262263264