Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/beans/ClassString.java
41161 views
/*1* Copyright (c) 2010, 2021, 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.security.AccessControlContext;64import java.security.AccessController;65import java.security.PrivilegedAction;66import java.util.Arrays;67import java.util.LinkedList;68import java.util.List;69import jdk.dynalink.internal.AccessControlContextFactory;70import jdk.dynalink.internal.InternalTypeUtilities;71import jdk.dynalink.linker.LinkerServices;72import jdk.dynalink.linker.support.TypeUtilities;7374/**75* Represents a sequence of {@link Class} objects, useful for representing method signatures. Provides value76* semantics for using them as map keys, as well as specificity calculations and applicability checks as per77* JLS.78*/79final class ClassString {80@SuppressWarnings("removal")81private static final AccessControlContext GET_CLASS_LOADER_CONTEXT =82AccessControlContextFactory.createAccessControlContext("getClassLoader");8384/**85* An anonymous inner class used solely to represent the "type" of null values for method applicability checking.86*/87static final Class<?> NULL_CLASS = (new Object() { /* Intentionally empty */ }).getClass();8889private final Class<?>[] classes;90private int hashCode;9192ClassString(final Class<?>[] classes) {93this.classes = classes;94}9596@Override97public boolean equals(final Object other) {98if(!(other instanceof ClassString)) {99return false;100}101final Class<?>[] otherClasses = ((ClassString)other).classes;102if(otherClasses.length != classes.length) {103return false;104}105for(int i = 0; i < otherClasses.length; ++i) {106if(otherClasses[i] != classes[i]) {107return false;108}109}110return true;111}112113@Override114public int hashCode() {115if(hashCode == 0) {116int h = 0;117for(final Class<?> cls: classes) {118h ^= cls.hashCode();119}120hashCode = h;121}122return hashCode;123}124125@Override126public String toString() {127return "ClassString[" + Arrays.toString(classes) + "]";128}129130@SuppressWarnings("removal")131boolean isVisibleFrom(final ClassLoader classLoader) {132return AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {133for(final Class<?> clazz: classes) {134if(!InternalTypeUtilities.canReferenceDirectly(classLoader, clazz.getClassLoader())) {135return false;136}137}138return true;139}, GET_CLASS_LOADER_CONTEXT);140}141142List<MethodHandle> getMaximallySpecifics(final List<MethodHandle> methods, final LinkerServices linkerServices, final boolean varArg) {143return MaximallySpecific.getMaximallySpecificMethodHandles(getApplicables(methods, linkerServices, varArg),144varArg, classes, linkerServices);145}146147/**148* Returns all methods that are applicable to actual parameter classes represented by this ClassString object.149*/150LinkedList<MethodHandle> getApplicables(final List<MethodHandle> methods, final LinkerServices linkerServices, final boolean varArg) {151final LinkedList<MethodHandle> list = new LinkedList<>();152for(final MethodHandle member: methods) {153if(isApplicable(member, linkerServices, varArg)) {154list.add(member);155}156}157return list;158}159160/**161* Returns true if the supplied method is applicable to actual parameter classes represented by this ClassString162* object.163*164*/165private boolean isApplicable(final MethodHandle method, final LinkerServices linkerServices, final boolean varArg) {166final Class<?>[] formalTypes = method.type().parameterArray();167final int cl = classes.length;168final int fl = formalTypes.length - (varArg ? 1 : 0);169if(varArg) {170if(cl < fl) {171return false;172}173} else {174if(cl != fl) {175return false;176}177}178// Starting from 1 as we ignore the receiver type179for(int i = 1; i < fl; ++i) {180if(!canConvert(linkerServices, classes[i], formalTypes[i])) {181return false;182}183}184if(varArg) {185final Class<?> varArgType = formalTypes[fl].getComponentType();186for(int i = fl; i < cl; ++i) {187if(!canConvert(linkerServices, classes[i], varArgType)) {188return false;189}190}191}192return true;193}194195private static boolean canConvert(final LinkerServices ls, final Class<?> from, final Class<?> to) {196if(from == NULL_CLASS) {197return !to.isPrimitive();198}199return ls == null ? TypeUtilities.isMethodInvocationConvertible(from, to) : ls.canConvert(from, to);200}201}202203204