1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/jdk/internal/dynalink/beans/DynamicMethod.java Thu Feb 14 13:22:26 2013 +0100 1.3 @@ -0,0 +1,169 @@ 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.StringTokenizer; 1.92 +import jdk.internal.dynalink.linker.LinkerServices; 1.93 + 1.94 + 1.95 +/** 1.96 + * Represents a single dynamic method. A "dynamic" method can be bound to a single Java method, or can be bound to all 1.97 + * overloaded methods of the same name on a class. Getting an invocation of a dynamic method bound to multiple 1.98 + * overloaded methods will perform overload resolution (actually, it will perform partial overloaded resolution at link 1.99 + * time, but if that fails to identify exactly one target method, it will generate a method handle that will perform the 1.100 + * rest of the overload resolution at invocation time for actual argument types). 1.101 + * 1.102 + * @author Attila Szegedi 1.103 + */ 1.104 +abstract class DynamicMethod { 1.105 + private final String name; 1.106 + 1.107 + DynamicMethod(String name) { 1.108 + this.name = name; 1.109 + } 1.110 + 1.111 + String getName() { 1.112 + return name; 1.113 + } 1.114 + 1.115 + /** 1.116 + * Creates an invocation for the dynamic method. If the method is overloaded, it will perform overloaded method 1.117 + * resolution based on the specified method type. The resulting resolution can either identify a single method to be 1.118 + * invoked among the overloads, or it can identify multiple ones. In the latter case, the returned method handle 1.119 + * will perform further overload resolution among these candidates at every invocation. If the method to be invoked 1.120 + * is a variable arguments (vararg) method, it will pack the extra arguments in an array before the invocation of 1.121 + * the underlying method if it is not already done. 1.122 + * 1.123 + * @param callSiteType the method type at a call site 1.124 + * @param linkerServices linker services. Used for language-specific type conversions. 1.125 + * @return an invocation suitable for calling the method from the specified call site. 1.126 + */ 1.127 + abstract MethodHandle getInvocation(MethodType callSiteType, LinkerServices linkerServices); 1.128 + 1.129 + /** 1.130 + * Returns a simple dynamic method representing a single underlying Java method (possibly selected among several 1.131 + * overloads) with formal parameter types exactly matching the passed signature. 1.132 + * @param paramTypes the comma-separated list of requested parameter type names. The names will match both 1.133 + * qualified and unqualified type names. 1.134 + * @return a simple dynamic method representing a single underlying Java method, or null if none of the Java methods 1.135 + * behind this dynamic method exactly match the requested parameter types. 1.136 + */ 1.137 + abstract SimpleDynamicMethod getMethodForExactParamTypes(String paramTypes); 1.138 + 1.139 + /** 1.140 + * True if this dynamic method already contains a method handle with an identical signature as the passed in method 1.141 + * handle. 1.142 + * @param mh the method handle to check 1.143 + * @return true if it already contains an equivalent method handle. 1.144 + */ 1.145 + abstract boolean contains(MethodHandle mh); 1.146 + 1.147 + static boolean typeMatchesDescription(String paramTypes, MethodType type) { 1.148 + final StringTokenizer tok = new StringTokenizer(paramTypes, ", "); 1.149 + for(int i = 1; i < type.parameterCount(); ++i) { // i = 1 as we ignore the receiver 1.150 + if(!(tok.hasMoreTokens() && typeNameMatches(tok.nextToken(), type.parameterType(i)))) { 1.151 + return false; 1.152 + } 1.153 + } 1.154 + return !tok.hasMoreTokens(); 1.155 + } 1.156 + 1.157 + private static boolean typeNameMatches(String typeName, Class<?> type) { 1.158 + final int lastDot = typeName.lastIndexOf('.'); 1.159 + final String fullTypeName = type.getCanonicalName(); 1.160 + return lastDot != -1 && fullTypeName.endsWith(typeName.substring(lastDot)) || typeName.equals(fullTypeName); 1.161 + } 1.162 + 1.163 + static String getClassAndMethodName(Class<?> clazz, String name) { 1.164 + final String clazzName = clazz.getCanonicalName(); 1.165 + return (clazzName == null ? clazz.getName() : clazzName) + "." + name; 1.166 + } 1.167 + 1.168 + @Override 1.169 + public String toString() { 1.170 + return "[" + getClass().getName() + " " + getName() + "]"; 1.171 + } 1.172 +} 1.173 \ No newline at end of file