src/jdk/internal/dynalink/beans/OverloadedDynamicMethod.java

Thu, 24 May 2018 16:39:31 +0800

author
aoqi
date
Thu, 24 May 2018 16:39:31 +0800
changeset 1959
61ffdd1b89f2
parent 1539
684d430470f6
parent 1205
4112748288bb
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 /*
aoqi@0 27 * This file is available under and governed by the GNU General Public
aoqi@0 28 * License version 2 only, as published by the Free Software Foundation.
aoqi@0 29 * However, the following notice accompanied the original version of this
aoqi@0 30 * file, and Oracle licenses the original version of this file under the BSD
aoqi@0 31 * license:
aoqi@0 32 */
aoqi@0 33 /*
aoqi@0 34 Copyright 2009-2013 Attila Szegedi
aoqi@0 35
aoqi@0 36 Licensed under both the Apache License, Version 2.0 (the "Apache License")
aoqi@0 37 and the BSD License (the "BSD License"), with licensee being free to
aoqi@0 38 choose either of the two at their discretion.
aoqi@0 39
aoqi@0 40 You may not use this file except in compliance with either the Apache
aoqi@0 41 License or the BSD License.
aoqi@0 42
aoqi@0 43 If you choose to use this file in compliance with the Apache License, the
aoqi@0 44 following notice applies to you:
aoqi@0 45
aoqi@0 46 You may obtain a copy of the Apache License at
aoqi@0 47
aoqi@0 48 http://www.apache.org/licenses/LICENSE-2.0
aoqi@0 49
aoqi@0 50 Unless required by applicable law or agreed to in writing, software
aoqi@0 51 distributed under the License is distributed on an "AS IS" BASIS,
aoqi@0 52 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
aoqi@0 53 implied. See the License for the specific language governing
aoqi@0 54 permissions and limitations under the License.
aoqi@0 55
aoqi@0 56 If you choose to use this file in compliance with the BSD License, the
aoqi@0 57 following notice applies to you:
aoqi@0 58
aoqi@0 59 Redistribution and use in source and binary forms, with or without
aoqi@0 60 modification, are permitted provided that the following conditions are
aoqi@0 61 met:
aoqi@0 62 * Redistributions of source code must retain the above copyright
aoqi@0 63 notice, this list of conditions and the following disclaimer.
aoqi@0 64 * Redistributions in binary form must reproduce the above copyright
aoqi@0 65 notice, this list of conditions and the following disclaimer in the
aoqi@0 66 documentation and/or other materials provided with the distribution.
aoqi@0 67 * Neither the name of the copyright holder nor the names of
aoqi@0 68 contributors may be used to endorse or promote products derived from
aoqi@0 69 this software without specific prior written permission.
aoqi@0 70
aoqi@0 71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
aoqi@0 72 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
aoqi@0 73 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
aoqi@0 74 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
aoqi@0 75 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
aoqi@0 76 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
aoqi@0 77 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
aoqi@0 78 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
aoqi@0 79 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
aoqi@0 80 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
aoqi@0 81 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
aoqi@0 82 */
aoqi@0 83
aoqi@0 84 package jdk.internal.dynalink.beans;
aoqi@0 85
aoqi@0 86 import java.lang.invoke.MethodHandle;
aoqi@0 87 import java.lang.invoke.MethodHandles;
aoqi@0 88 import java.lang.invoke.MethodType;
attila@1539 89 import java.text.Collator;
aoqi@0 90 import java.util.ArrayList;
attila@1539 91 import java.util.Collections;
aoqi@0 92 import java.util.Iterator;
aoqi@0 93 import java.util.LinkedList;
aoqi@0 94 import java.util.List;
aoqi@0 95 import jdk.internal.dynalink.CallSiteDescriptor;
aoqi@0 96 import jdk.internal.dynalink.beans.ApplicableOverloadedMethods.ApplicabilityTest;
aoqi@0 97 import jdk.internal.dynalink.linker.LinkerServices;
aoqi@0 98 import jdk.internal.dynalink.support.TypeUtilities;
aoqi@0 99
aoqi@0 100 /**
aoqi@0 101 * Represents a group of {@link SingleDynamicMethod} objects that represents all overloads of a particular name (or all
aoqi@0 102 * constructors) for a particular class. Correctly handles overload resolution, variable arity methods, and caller
aoqi@0 103 * sensitive methods within the overloads.
aoqi@0 104 *
aoqi@0 105 * @author Attila Szegedi
aoqi@0 106 */
aoqi@0 107 class OverloadedDynamicMethod extends DynamicMethod {
aoqi@0 108 /**
aoqi@0 109 * Holds a list of all methods.
aoqi@0 110 */
aoqi@0 111 private final LinkedList<SingleDynamicMethod> methods;
aoqi@0 112 private final ClassLoader classLoader;
aoqi@0 113
aoqi@0 114 /**
aoqi@0 115 * Creates a new overloaded dynamic method.
aoqi@0 116 *
aoqi@0 117 * @param clazz the class this method belongs to
aoqi@0 118 * @param name the name of the method
aoqi@0 119 */
attila@962 120 OverloadedDynamicMethod(final Class<?> clazz, final String name) {
aoqi@0 121 this(new LinkedList<SingleDynamicMethod>(), clazz.getClassLoader(), getClassAndMethodName(clazz, name));
aoqi@0 122 }
aoqi@0 123
attila@962 124 private OverloadedDynamicMethod(final LinkedList<SingleDynamicMethod> methods, final ClassLoader classLoader, final String name) {
aoqi@0 125 super(name);
aoqi@0 126 this.methods = methods;
aoqi@0 127 this.classLoader = classLoader;
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 @Override
attila@962 131 SingleDynamicMethod getMethodForExactParamTypes(final String paramTypes) {
aoqi@0 132 final LinkedList<SingleDynamicMethod> matchingMethods = new LinkedList<>();
attila@962 133 for(final SingleDynamicMethod method: methods) {
aoqi@0 134 final SingleDynamicMethod matchingMethod = method.getMethodForExactParamTypes(paramTypes);
aoqi@0 135 if(matchingMethod != null) {
aoqi@0 136 matchingMethods.add(matchingMethod);
aoqi@0 137 }
aoqi@0 138 }
aoqi@0 139 switch(matchingMethods.size()) {
aoqi@0 140 case 0: {
aoqi@0 141 return null;
aoqi@0 142 }
aoqi@0 143 case 1: {
aoqi@0 144 return matchingMethods.getFirst();
aoqi@0 145 }
aoqi@0 146 default: {
aoqi@0 147 throw new BootstrapMethodError("Can't choose among " + matchingMethods + " for argument types "
aoqi@0 148 + paramTypes + " for method " + getName());
aoqi@0 149 }
aoqi@0 150 }
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 @Override
aoqi@0 154 public MethodHandle getInvocation(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) {
aoqi@0 155 final MethodType callSiteType = callSiteDescriptor.getMethodType();
aoqi@0 156 // First, find all methods applicable to the call site by subtyping (JLS 15.12.2.2)
aoqi@0 157 final ApplicableOverloadedMethods subtypingApplicables = getApplicables(callSiteType,
aoqi@0 158 ApplicableOverloadedMethods.APPLICABLE_BY_SUBTYPING);
aoqi@0 159 // Next, find all methods applicable by method invocation conversion to the call site (JLS 15.12.2.3).
aoqi@0 160 final ApplicableOverloadedMethods methodInvocationApplicables = getApplicables(callSiteType,
aoqi@0 161 ApplicableOverloadedMethods.APPLICABLE_BY_METHOD_INVOCATION_CONVERSION);
aoqi@0 162 // Finally, find all methods applicable by variable arity invocation. (JLS 15.12.2.4).
aoqi@0 163 final ApplicableOverloadedMethods variableArityApplicables = getApplicables(callSiteType,
aoqi@0 164 ApplicableOverloadedMethods.APPLICABLE_BY_VARIABLE_ARITY);
aoqi@0 165
aoqi@0 166 // Find the methods that are maximally specific based on the call site signature
aoqi@0 167 List<SingleDynamicMethod> maximallySpecifics = subtypingApplicables.findMaximallySpecificMethods();
aoqi@0 168 if(maximallySpecifics.isEmpty()) {
aoqi@0 169 maximallySpecifics = methodInvocationApplicables.findMaximallySpecificMethods();
aoqi@0 170 if(maximallySpecifics.isEmpty()) {
aoqi@0 171 maximallySpecifics = variableArityApplicables.findMaximallySpecificMethods();
aoqi@0 172 }
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 // Now, get a list of the rest of the methods; those that are *not* applicable to the call site signature based
aoqi@0 176 // on JLS rules. As paradoxical as that might sound, we have to consider these for dynamic invocation, as they
aoqi@0 177 // might match more concrete types passed in invocations. That's why we provisionally call them "invokables".
aoqi@0 178 // This is typical for very generic signatures at call sites. Typical example: call site specifies
aoqi@0 179 // (Object, Object), and we have a method whose parameter types are (String, int). None of the JLS applicability
aoqi@0 180 // rules will trigger, but we must consider the method, as it can be the right match for a concrete invocation.
aoqi@0 181 @SuppressWarnings({ "unchecked", "rawtypes" })
aoqi@0 182 final List<SingleDynamicMethod> invokables = (List)methods.clone();
aoqi@0 183 invokables.removeAll(subtypingApplicables.getMethods());
aoqi@0 184 invokables.removeAll(methodInvocationApplicables.getMethods());
aoqi@0 185 invokables.removeAll(variableArityApplicables.getMethods());
aoqi@0 186 for(final Iterator<SingleDynamicMethod> it = invokables.iterator(); it.hasNext();) {
aoqi@0 187 final SingleDynamicMethod m = it.next();
aoqi@0 188 if(!isApplicableDynamically(linkerServices, callSiteType, m)) {
aoqi@0 189 it.remove();
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 // If no additional methods can apply at invocation time, and there's more than one maximally specific method
aoqi@0 194 // based on call site signature, that is a link-time ambiguity. In a static scenario, javac would report an
aoqi@0 195 // ambiguity error.
aoqi@0 196 if(invokables.isEmpty() && maximallySpecifics.size() > 1) {
aoqi@0 197 throw new BootstrapMethodError("Can't choose among " + maximallySpecifics + " for argument types "
aoqi@0 198 + callSiteType);
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 // Merge them all.
aoqi@0 202 invokables.addAll(maximallySpecifics);
aoqi@0 203 switch(invokables.size()) {
aoqi@0 204 case 0: {
aoqi@0 205 // No overloads can ever match the call site type
aoqi@0 206 return null;
aoqi@0 207 }
aoqi@0 208 case 1: {
aoqi@0 209 // Very lucky, we ended up with a single candidate method handle based on the call site signature; we
aoqi@0 210 // can link it very simply by delegating to the SingleDynamicMethod.
attila@963 211 return invokables.iterator().next().getInvocation(callSiteDescriptor, linkerServices);
aoqi@0 212 }
aoqi@0 213 default: {
aoqi@0 214 // We have more than one candidate. We have no choice but to link to a method that resolves overloads on
aoqi@0 215 // every invocation (alternatively, we could opportunistically link the one method that resolves for the
aoqi@0 216 // current arguments, but we'd need to install a fairly complex guard for that and when it'd fail, we'd
aoqi@0 217 // go back all the way to candidate selection. Note that we're resolving any potential caller sensitive
aoqi@0 218 // methods here to their handles, as the OverloadedMethod instance is specific to a call site, so it
aoqi@0 219 // has an already determined Lookup.
aoqi@0 220 final List<MethodHandle> methodHandles = new ArrayList<>(invokables.size());
aoqi@0 221 final MethodHandles.Lookup lookup = callSiteDescriptor.getLookup();
attila@962 222 for(final SingleDynamicMethod method: invokables) {
aoqi@0 223 methodHandles.add(method.getTarget(lookup));
aoqi@0 224 }
aoqi@0 225 return new OverloadedMethod(methodHandles, this, callSiteType, linkerServices).getInvoker();
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 @Override
attila@962 232 public boolean contains(final SingleDynamicMethod m) {
attila@962 233 for(final SingleDynamicMethod method: methods) {
aoqi@0 234 if(method.contains(m)) {
aoqi@0 235 return true;
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238 return false;
aoqi@0 239 }
aoqi@0 240
attila@963 241 @Override
attila@963 242 public boolean isConstructor() {
attila@963 243 assert !methods.isEmpty();
attila@963 244 return methods.getFirst().isConstructor();
attila@963 245 }
attila@963 246
attila@1539 247 @Override
attila@1539 248 public String toString() {
attila@1539 249 // First gather the names and sort them. This makes it consistent and easier to read.
attila@1539 250 final List<String> names = new ArrayList<>(methods.size());
attila@1539 251 int len = 0;
attila@1539 252 for (final SingleDynamicMethod m: methods) {
attila@1539 253 final String name = m.getName();
attila@1539 254 len += name.length();
attila@1539 255 names.add(name);
attila@1539 256 }
attila@1539 257 // Case insensitive sorting, so e.g. "Object" doesn't come before "boolean".
attila@1539 258 final Collator collator = Collator.getInstance();
attila@1539 259 collator.setStrength(Collator.SECONDARY);
attila@1539 260 Collections.sort(names, collator);
attila@1539 261
attila@1539 262 final String className = getClass().getName();
attila@1539 263 // Class name length + length of signatures + 2 chars/per signature for indentation and newline +
attila@1539 264 // 3 for brackets and initial newline
attila@1539 265 final int totalLength = className.length() + len + 2 * names.size() + 3;
attila@1539 266 final StringBuilder b = new StringBuilder(totalLength);
attila@1539 267 b.append('[').append(className).append('\n');
attila@1539 268 for(final String name: names) {
attila@1539 269 b.append(' ').append(name).append('\n');
attila@1539 270 }
attila@1539 271 b.append(']');
attila@1539 272 assert b.length() == totalLength;
attila@1539 273 return b.toString();
attila@1539 274 };
attila@1539 275
aoqi@0 276 ClassLoader getClassLoader() {
aoqi@0 277 return classLoader;
aoqi@0 278 }
aoqi@0 279
attila@962 280 private static boolean isApplicableDynamically(final LinkerServices linkerServices, final MethodType callSiteType,
attila@962 281 final SingleDynamicMethod m) {
aoqi@0 282 final MethodType methodType = m.getMethodType();
aoqi@0 283 final boolean varArgs = m.isVarArgs();
aoqi@0 284 final int fixedArgLen = methodType.parameterCount() - (varArgs ? 1 : 0);
aoqi@0 285 final int callSiteArgLen = callSiteType.parameterCount();
aoqi@0 286
aoqi@0 287 // Arity checks
aoqi@0 288 if(varArgs) {
aoqi@0 289 if(callSiteArgLen < fixedArgLen) {
aoqi@0 290 return false;
aoqi@0 291 }
aoqi@0 292 } else if(callSiteArgLen != fixedArgLen) {
aoqi@0 293 return false;
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 // Fixed arguments type checks, starting from 1, as receiver type doesn't participate
aoqi@0 297 for(int i = 1; i < fixedArgLen; ++i) {
aoqi@0 298 if(!isApplicableDynamically(linkerServices, callSiteType.parameterType(i), methodType.parameterType(i))) {
aoqi@0 299 return false;
aoqi@0 300 }
aoqi@0 301 }
aoqi@0 302 if(!varArgs) {
aoqi@0 303 // Not vararg; both arity and types matched.
aoqi@0 304 return true;
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 final Class<?> varArgArrayType = methodType.parameterType(fixedArgLen);
aoqi@0 308 final Class<?> varArgType = varArgArrayType.getComponentType();
aoqi@0 309
aoqi@0 310 if(fixedArgLen == callSiteArgLen - 1) {
aoqi@0 311 // Exactly one vararg; check both array type matching and array component type matching.
aoqi@0 312 final Class<?> callSiteArgType = callSiteType.parameterType(fixedArgLen);
aoqi@0 313 return isApplicableDynamically(linkerServices, callSiteArgType, varArgArrayType)
aoqi@0 314 || isApplicableDynamically(linkerServices, callSiteArgType, varArgType);
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 // Either zero, or more than one vararg; check if all actual vararg types match the vararg array component type.
aoqi@0 318 for(int i = fixedArgLen; i < callSiteArgLen; ++i) {
aoqi@0 319 if(!isApplicableDynamically(linkerServices, callSiteType.parameterType(i), varArgType)) {
aoqi@0 320 return false;
aoqi@0 321 }
aoqi@0 322 }
aoqi@0 323
aoqi@0 324 return true;
aoqi@0 325 }
aoqi@0 326
attila@962 327 private static boolean isApplicableDynamically(final LinkerServices linkerServices, final Class<?> callSiteType,
attila@962 328 final Class<?> methodType) {
aoqi@0 329 return TypeUtilities.isPotentiallyConvertible(callSiteType, methodType)
aoqi@0 330 || linkerServices.canConvert(callSiteType, methodType);
aoqi@0 331 }
aoqi@0 332
attila@962 333 private ApplicableOverloadedMethods getApplicables(final MethodType callSiteType, final ApplicabilityTest test) {
aoqi@0 334 return new ApplicableOverloadedMethods(methods, callSiteType, test);
aoqi@0 335 }
aoqi@0 336
aoqi@0 337 /**
aoqi@0 338 * Add a method to this overloaded method's set.
aoqi@0 339 *
aoqi@0 340 * @param method a method to add
aoqi@0 341 */
attila@962 342 public void addMethod(final SingleDynamicMethod method) {
attila@963 343 assert constructorFlagConsistent(method);
aoqi@0 344 methods.add(method);
aoqi@0 345 }
attila@963 346
attila@963 347 private boolean constructorFlagConsistent(final SingleDynamicMethod method) {
attila@963 348 return methods.isEmpty()? true : (methods.getFirst().isConstructor() == method.isConstructor());
attila@963 349 }
aoqi@0 350 }

mercurial