Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/beans/MaximallySpecific.java
41161 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 Szegedi3435Redistribution and use in source and binary forms, with or without36modification, are permitted provided that the following conditions are37met:38* Redistributions of source code must retain the above copyright39notice, this list of conditions and the following disclaimer.40* Redistributions in binary form must reproduce the above copyright41notice, this list of conditions and the following disclaimer in the42documentation and/or other materials provided with the distribution.43* Neither the name of the copyright holder nor the names of44contributors may be used to endorse or promote products derived from45this software without specific prior written permission.4647THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS48IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED49TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A50PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER51BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR52CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF53SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR54BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,55WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR56OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF57ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.58*/5960package jdk.dynalink.beans;6162import java.lang.invoke.MethodHandle;63import java.lang.invoke.MethodType;64import java.util.Iterator;65import java.util.LinkedList;66import java.util.List;67import java.util.function.Function;68import jdk.dynalink.linker.ConversionComparator.Comparison;69import jdk.dynalink.linker.LinkerServices;70import jdk.dynalink.linker.support.TypeUtilities;7172/**73* Utility class that encapsulates the algorithm for choosing the maximally specific methods.74*/75class MaximallySpecific {76/**77* Given a list of methods, returns a list of maximally specific methods.78*79* @param methods the list of methods80* @param varArgs whether to assume the methods are varargs81* @return the list of maximally specific methods.82*/83static List<SingleDynamicMethod> getMaximallySpecificMethods(final List<SingleDynamicMethod> methods, final boolean varArgs) {84return getMaximallySpecificMethods(methods, varArgs, null, null, SingleDynamicMethod::getMethodType);85}8687/**88* Given a list of methods handles, returns a list of maximally specific methods, applying language-runtime89* specific conversion preferences.90*91* @param methods the list of method handles92* @param varArgs whether to assume the method handles are varargs93* @param argTypes concrete argument types for the invocation94* @return the list of maximally specific method handles.95*/96static List<MethodHandle> getMaximallySpecificMethodHandles(final List<MethodHandle> methods, final boolean varArgs,97final Class<?>[] argTypes, final LinkerServices ls) {98return getMaximallySpecificMethods(methods, varArgs, argTypes, ls, MethodHandle::type);99}100101/**102* Given a list of methods, returns a list of maximally specific methods, applying language-runtime specific103* conversion preferences.104*105* @param methods the list of methods106* @param varArgs whether to assume the methods are varargs107* @param argTypes concrete argument types for the invocation108* @return the list of maximally specific methods.109*/110private static <T> List<T> getMaximallySpecificMethods(final List<T> methods, final boolean varArgs,111final Class<?>[] argTypes, final LinkerServices ls, final Function<T, MethodType> methodTypeGetter) {112if(methods.size() < 2) {113return methods;114}115final LinkedList<T> maximals = new LinkedList<>();116for(final T m: methods) {117final MethodType methodType = methodTypeGetter.apply(m);118boolean lessSpecific = false;119for(final Iterator<T> maximal = maximals.iterator(); maximal.hasNext();) {120final T max = maximal.next();121switch(isMoreSpecific(methodType, methodTypeGetter.apply(max), varArgs, argTypes, ls)) {122case TYPE_1_BETTER: {123maximal.remove();124break;125}126case TYPE_2_BETTER: {127lessSpecific = true;128break;129}130case INDETERMINATE: {131// do nothing132break;133}134default: {135throw new AssertionError();136}137}138}139if(!lessSpecific) {140maximals.addLast(m);141}142}143return maximals;144}145146private static Comparison isMoreSpecific(final MethodType t1, final MethodType t2, final boolean varArgs, final Class<?>[] argTypes,147final LinkerServices ls) {148final int pc1 = t1.parameterCount();149final int pc2 = t2.parameterCount();150assert varArgs || (pc1 == pc2) && (argTypes == null || argTypes.length == pc1);151assert (argTypes == null) == (ls == null);152final int maxPc = Math.max(Math.max(pc1, pc2), argTypes == null ? 0 : argTypes.length);153boolean t1MoreSpecific = false;154boolean t2MoreSpecific = false;155// NOTE: Starting from 1 as overloaded method resolution doesn't depend on 0th element, which is the type of156// 'this'. We're only dealing with instance methods here, not static methods. Actually, static methods will have157// a fake 'this' of type StaticClass.158for(int i = 1; i < maxPc; ++i) {159final Class<?> c1 = getParameterClass(t1, pc1, i, varArgs);160final Class<?> c2 = getParameterClass(t2, pc2, i, varArgs);161if(c1 != c2) {162final Comparison cmp = compare(c1, c2, argTypes, i, ls);163if(cmp == Comparison.TYPE_1_BETTER && !t1MoreSpecific) {164t1MoreSpecific = true;165if(t2MoreSpecific) {166return Comparison.INDETERMINATE;167}168}169if(cmp == Comparison.TYPE_2_BETTER && !t2MoreSpecific) {170t2MoreSpecific = true;171if(t1MoreSpecific) {172return Comparison.INDETERMINATE;173}174}175}176}177if(t1MoreSpecific) {178return Comparison.TYPE_1_BETTER;179} else if(t2MoreSpecific) {180return Comparison.TYPE_2_BETTER;181}182return Comparison.INDETERMINATE;183}184185private static Comparison compare(final Class<?> c1, final Class<?> c2, final Class<?>[] argTypes, final int i, final LinkerServices cmp) {186if(cmp != null) {187final Comparison c = cmp.compareConversion(argTypes[i], c1, c2);188if(c != Comparison.INDETERMINATE) {189return c;190}191}192if(TypeUtilities.isSubtype(c1, c2)) {193return Comparison.TYPE_1_BETTER;194} if(TypeUtilities.isSubtype(c2, c1)) {195return Comparison.TYPE_2_BETTER;196}197return Comparison.INDETERMINATE;198}199200private static Class<?> getParameterClass(final MethodType t, final int l, final int i, final boolean varArgs) {201return varArgs && i >= l - 1 ? t.parameterType(l - 1).getComponentType() : t.parameterType(i);202}203}204205206