1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/jdk/internal/dynalink/beans/MaximallySpecific.java Wed Apr 27 01:36:41 2016 +0800 1.3 @@ -0,0 +1,262 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/* 1.30 + * This file is available under and governed by the GNU General Public 1.31 + * License version 2 only, as published by the Free Software Foundation. 1.32 + * However, the following notice accompanied the original version of this 1.33 + * file, and Oracle licenses the original version of this file under the BSD 1.34 + * license: 1.35 + */ 1.36 +/* 1.37 + Copyright 2009-2013 Attila Szegedi 1.38 + 1.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 1.40 + and the BSD License (the "BSD License"), with licensee being free to 1.41 + choose either of the two at their discretion. 1.42 + 1.43 + You may not use this file except in compliance with either the Apache 1.44 + License or the BSD License. 1.45 + 1.46 + If you choose to use this file in compliance with the Apache License, the 1.47 + following notice applies to you: 1.48 + 1.49 + You may obtain a copy of the Apache License at 1.50 + 1.51 + http://www.apache.org/licenses/LICENSE-2.0 1.52 + 1.53 + Unless required by applicable law or agreed to in writing, software 1.54 + distributed under the License is distributed on an "AS IS" BASIS, 1.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 1.56 + implied. See the License for the specific language governing 1.57 + permissions and limitations under the License. 1.58 + 1.59 + If you choose to use this file in compliance with the BSD License, the 1.60 + following notice applies to you: 1.61 + 1.62 + Redistribution and use in source and binary forms, with or without 1.63 + modification, are permitted provided that the following conditions are 1.64 + met: 1.65 + * Redistributions of source code must retain the above copyright 1.66 + notice, this list of conditions and the following disclaimer. 1.67 + * Redistributions in binary form must reproduce the above copyright 1.68 + notice, this list of conditions and the following disclaimer in the 1.69 + documentation and/or other materials provided with the distribution. 1.70 + * Neither the name of the copyright holder nor the names of 1.71 + contributors may be used to endorse or promote products derived from 1.72 + this software without specific prior written permission. 1.73 + 1.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 1.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 1.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 1.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 1.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 1.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 1.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.85 +*/ 1.86 + 1.87 +package jdk.internal.dynalink.beans; 1.88 + 1.89 +import java.lang.invoke.MethodHandle; 1.90 +import java.lang.invoke.MethodType; 1.91 +import java.util.Iterator; 1.92 +import java.util.LinkedList; 1.93 +import java.util.List; 1.94 +import jdk.internal.dynalink.linker.ConversionComparator.Comparison; 1.95 +import jdk.internal.dynalink.linker.LinkerServices; 1.96 +import jdk.internal.dynalink.support.TypeUtilities; 1.97 + 1.98 +/** 1.99 + * Utility class that encapsulates the algorithm for choosing the maximally specific methods. 1.100 + * 1.101 + * @author Attila Szegedi 1.102 + */ 1.103 +class MaximallySpecific { 1.104 + /** 1.105 + * Given a list of methods, returns a list of maximally specific methods. 1.106 + * 1.107 + * @param methods the list of methods 1.108 + * @param varArgs whether to assume the methods are varargs 1.109 + * @return the list of maximally specific methods. 1.110 + */ 1.111 + static List<SingleDynamicMethod> getMaximallySpecificMethods(List<SingleDynamicMethod> methods, boolean varArgs) { 1.112 + return getMaximallySpecificSingleDynamicMethods(methods, varArgs, null, null); 1.113 + } 1.114 + 1.115 + private abstract static class MethodTypeGetter<T> { 1.116 + abstract MethodType getMethodType(T t); 1.117 + } 1.118 + 1.119 + private static final MethodTypeGetter<MethodHandle> METHOD_HANDLE_TYPE_GETTER = 1.120 + new MethodTypeGetter<MethodHandle>() { 1.121 + @Override 1.122 + MethodType getMethodType(MethodHandle t) { 1.123 + return t.type(); 1.124 + } 1.125 + }; 1.126 + 1.127 + private static final MethodTypeGetter<SingleDynamicMethod> DYNAMIC_METHOD_TYPE_GETTER = 1.128 + new MethodTypeGetter<SingleDynamicMethod>() { 1.129 + @Override 1.130 + MethodType getMethodType(SingleDynamicMethod t) { 1.131 + return t.getMethodType(); 1.132 + } 1.133 + }; 1.134 + 1.135 + /** 1.136 + * Given a list of methods handles, returns a list of maximally specific methods, applying language-runtime 1.137 + * specific conversion preferences. 1.138 + * 1.139 + * @param methods the list of method handles 1.140 + * @param varArgs whether to assume the method handles are varargs 1.141 + * @param argTypes concrete argument types for the invocation 1.142 + * @return the list of maximally specific method handles. 1.143 + */ 1.144 + static List<MethodHandle> getMaximallySpecificMethodHandles(List<MethodHandle> methods, boolean varArgs, 1.145 + Class<?>[] argTypes, LinkerServices ls) { 1.146 + return getMaximallySpecificMethods(methods, varArgs, argTypes, ls, METHOD_HANDLE_TYPE_GETTER); 1.147 + } 1.148 + 1.149 + /** 1.150 + * Given a list of methods, returns a list of maximally specific methods, applying language-runtime specific 1.151 + * conversion preferences. 1.152 + * 1.153 + * @param methods the list of methods 1.154 + * @param varArgs whether to assume the methods are varargs 1.155 + * @param argTypes concrete argument types for the invocation 1.156 + * @return the list of maximally specific methods. 1.157 + */ 1.158 + static List<SingleDynamicMethod> getMaximallySpecificSingleDynamicMethods(List<SingleDynamicMethod> methods, 1.159 + boolean varArgs, Class<?>[] argTypes, LinkerServices ls) { 1.160 + return getMaximallySpecificMethods(methods, varArgs, argTypes, ls, DYNAMIC_METHOD_TYPE_GETTER); 1.161 + } 1.162 + 1.163 + /** 1.164 + * Given a list of methods, returns a list of maximally specific methods, applying language-runtime specific 1.165 + * conversion preferences. 1.166 + * 1.167 + * @param methods the list of methods 1.168 + * @param varArgs whether to assume the methods are varargs 1.169 + * @param argTypes concrete argument types for the invocation 1.170 + * @return the list of maximally specific methods. 1.171 + */ 1.172 + private static <T> List<T> getMaximallySpecificMethods(List<T> methods, boolean varArgs, 1.173 + Class<?>[] argTypes, LinkerServices ls, MethodTypeGetter<T> methodTypeGetter) { 1.174 + if(methods.size() < 2) { 1.175 + return methods; 1.176 + } 1.177 + final LinkedList<T> maximals = new LinkedList<>(); 1.178 + for(T m: methods) { 1.179 + final MethodType methodType = methodTypeGetter.getMethodType(m); 1.180 + boolean lessSpecific = false; 1.181 + for(Iterator<T> maximal = maximals.iterator(); maximal.hasNext();) { 1.182 + final T max = maximal.next(); 1.183 + switch(isMoreSpecific(methodType, methodTypeGetter.getMethodType(max), varArgs, argTypes, ls)) { 1.184 + case TYPE_1_BETTER: { 1.185 + maximal.remove(); 1.186 + break; 1.187 + } 1.188 + case TYPE_2_BETTER: { 1.189 + lessSpecific = true; 1.190 + break; 1.191 + } 1.192 + case INDETERMINATE: { 1.193 + // do nothing 1.194 + break; 1.195 + } 1.196 + default: { 1.197 + throw new AssertionError(); 1.198 + } 1.199 + } 1.200 + } 1.201 + if(!lessSpecific) { 1.202 + maximals.addLast(m); 1.203 + } 1.204 + } 1.205 + return maximals; 1.206 + } 1.207 + 1.208 + private static Comparison isMoreSpecific(MethodType t1, MethodType t2, boolean varArgs, Class<?>[] argTypes, 1.209 + LinkerServices ls) { 1.210 + final int pc1 = t1.parameterCount(); 1.211 + final int pc2 = t2.parameterCount(); 1.212 + assert varArgs || (pc1 == pc2) && (argTypes == null || argTypes.length == pc1); 1.213 + assert (argTypes == null) == (ls == null); 1.214 + final int maxPc = Math.max(Math.max(pc1, pc2), argTypes == null ? 0 : argTypes.length); 1.215 + boolean t1MoreSpecific = false; 1.216 + boolean t2MoreSpecific = false; 1.217 + // NOTE: Starting from 1 as overloaded method resolution doesn't depend on 0th element, which is the type of 1.218 + // 'this'. We're only dealing with instance methods here, not static methods. Actually, static methods will have 1.219 + // a fake 'this' of type StaticClass. 1.220 + for(int i = 1; i < maxPc; ++i) { 1.221 + final Class<?> c1 = getParameterClass(t1, pc1, i, varArgs); 1.222 + final Class<?> c2 = getParameterClass(t2, pc2, i, varArgs); 1.223 + if(c1 != c2) { 1.224 + final Comparison cmp = compare(c1, c2, argTypes, i, ls); 1.225 + if(cmp == Comparison.TYPE_1_BETTER && !t1MoreSpecific) { 1.226 + t1MoreSpecific = true; 1.227 + if(t2MoreSpecific) { 1.228 + return Comparison.INDETERMINATE; 1.229 + } 1.230 + } 1.231 + if(cmp == Comparison.TYPE_2_BETTER && !t2MoreSpecific) { 1.232 + t2MoreSpecific = true; 1.233 + if(t1MoreSpecific) { 1.234 + return Comparison.INDETERMINATE; 1.235 + } 1.236 + } 1.237 + } 1.238 + } 1.239 + if(t1MoreSpecific) { 1.240 + return Comparison.TYPE_1_BETTER; 1.241 + } else if(t2MoreSpecific) { 1.242 + return Comparison.TYPE_2_BETTER; 1.243 + } 1.244 + return Comparison.INDETERMINATE; 1.245 + } 1.246 + 1.247 + private static Comparison compare(Class<?> c1, Class<?> c2, Class<?>[] argTypes, int i, LinkerServices cmp) { 1.248 + if(cmp != null) { 1.249 + final Comparison c = cmp.compareConversion(argTypes[i], c1, c2); 1.250 + if(c != Comparison.INDETERMINATE) { 1.251 + return c; 1.252 + } 1.253 + } 1.254 + if(TypeUtilities.isSubtype(c1, c2)) { 1.255 + return Comparison.TYPE_1_BETTER; 1.256 + } if(TypeUtilities.isSubtype(c2, c1)) { 1.257 + return Comparison.TYPE_2_BETTER; 1.258 + } 1.259 + return Comparison.INDETERMINATE; 1.260 + } 1.261 + 1.262 + private static Class<?> getParameterClass(MethodType t, int l, int i, boolean varArgs) { 1.263 + return varArgs && i >= l - 1 ? t.parameterType(l - 1).getComponentType() : t.parameterType(i); 1.264 + } 1.265 +}