Path: blob/jdk8u272-b10-aarch32-20201026/nashorn/src/jdk/internal/dynalink/beans/ApplicableOverloadedMethods.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.MethodType;86import java.util.LinkedList;87import java.util.List;88import jdk.internal.dynalink.support.TypeUtilities;8990/**91* Represents overloaded methods applicable to a specific call site signature.92*93* @author Attila Szegedi94*/95class ApplicableOverloadedMethods {96private final List<SingleDynamicMethod> methods;97private final boolean varArgs;9899/**100* Creates a new ApplicableOverloadedMethods instance101*102* @param methods a list of all overloaded methods with the same name for a class.103* @param callSiteType the type of the call site104* @param test applicability test. One of {@link #APPLICABLE_BY_SUBTYPING},105* {@link #APPLICABLE_BY_METHOD_INVOCATION_CONVERSION}, or {@link #APPLICABLE_BY_VARIABLE_ARITY}.106*/107ApplicableOverloadedMethods(final List<SingleDynamicMethod> methods, final MethodType callSiteType,108final ApplicabilityTest test) {109this.methods = new LinkedList<>();110for(final SingleDynamicMethod m: methods) {111if(test.isApplicable(callSiteType, m)) {112this.methods.add(m);113}114}115varArgs = test == APPLICABLE_BY_VARIABLE_ARITY;116}117118/**119* Retrieves all the methods this object holds.120*121* @return list of all methods.122*/123List<SingleDynamicMethod> getMethods() {124return methods;125}126127/**128* Returns a list of all methods in this objects that are maximally specific.129*130* @return a list of maximally specific methods.131*/132List<SingleDynamicMethod> findMaximallySpecificMethods() {133return MaximallySpecific.getMaximallySpecificMethods(methods, varArgs);134}135136abstract static class ApplicabilityTest {137abstract boolean isApplicable(MethodType callSiteType, SingleDynamicMethod method);138}139140/**141* Implements the applicability-by-subtyping test from JLS 15.12.2.2.142*/143static final ApplicabilityTest APPLICABLE_BY_SUBTYPING = new ApplicabilityTest() {144@Override145boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {146final MethodType methodType = method.getMethodType();147final int methodArity = methodType.parameterCount();148if(methodArity != callSiteType.parameterCount()) {149return false;150}151// 0th arg is receiver; it doesn't matter for overload152// resolution.153for(int i = 1; i < methodArity; ++i) {154if(!TypeUtilities.isSubtype(callSiteType.parameterType(i), methodType.parameterType(i))) {155return false;156}157}158return true;159}160};161162/**163* Implements the applicability-by-method-invocation-conversion test from JLS 15.12.2.3.164*/165static final ApplicabilityTest APPLICABLE_BY_METHOD_INVOCATION_CONVERSION = new ApplicabilityTest() {166@Override167boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {168final MethodType methodType = method.getMethodType();169final int methodArity = methodType.parameterCount();170if(methodArity != callSiteType.parameterCount()) {171return false;172}173// 0th arg is receiver; it doesn't matter for overload174// resolution.175for(int i = 1; i < methodArity; ++i) {176if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i),177methodType.parameterType(i))) {178return false;179}180}181return true;182}183};184185/**186* Implements the applicability-by-variable-arity test from JLS 15.12.2.4.187*/188static final ApplicabilityTest APPLICABLE_BY_VARIABLE_ARITY = new ApplicabilityTest() {189@Override190boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {191if(!method.isVarArgs()) {192return false;193}194final MethodType methodType = method.getMethodType();195final int methodArity = methodType.parameterCount();196final int fixArity = methodArity - 1;197final int callSiteArity = callSiteType.parameterCount();198if(fixArity > callSiteArity) {199return false;200}201// 0th arg is receiver; it doesn't matter for overload202// resolution.203for(int i = 1; i < fixArity; ++i) {204if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i),205methodType.parameterType(i))) {206return false;207}208}209final Class<?> varArgType = methodType.parameterType(fixArity).getComponentType();210for(int i = fixArity; i < callSiteArity; ++i) {211if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), varArgType)) {212return false;213}214}215return true;216}217};218}219220221