Thu, 14 Feb 2013 13:22:26 +0100
8008085: Integrate Dynalink source code into Nashorn codebase
Reviewed-by: jlaskey, lagergren, sundar
1.1 --- a/THIRD_PARTY_README Thu Feb 14 13:01:52 2013 +0100 1.2 +++ b/THIRD_PARTY_README Thu Feb 14 13:22:26 2013 +0100 1.3 @@ -67,3 +67,34 @@ 1.4 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 1.5 OF SUCH DAMAGE. 1.6 --- end of LICENSE --- 1.7 + 1.8 +%% This notice is provided with respect to Dynalink library which is included 1.9 +with the Nashorn technology. 1.10 + 1.11 +--- begin of LICENSE --- 1.12 +Copyright (c) 2009-2013, Attila Szegedi 1.13 + 1.14 +Redistribution and use in source and binary forms, with or without 1.15 +modification, are permitted provided that the following conditions are 1.16 +met: 1.17 +* Redistributions of source code must retain the above copyright 1.18 + notice, this list of conditions and the following disclaimer. 1.19 +* Redistributions in binary form must reproduce the above copyright 1.20 + notice, this list of conditions and the following disclaimer in the 1.21 + documentation and/or other materials provided with the distribution. 1.22 +* Neither the name of the copyright holder nor the names of 1.23 + contributors may be used to endorse or promote products derived from 1.24 + this software without specific prior written permission. 1.25 + 1.26 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.27 +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1.28 +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 1.29 +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 1.30 +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.31 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.32 +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 1.33 +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 1.34 +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 1.35 +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 1.36 +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.37 +--- end of LICENSE ---
2.1 --- a/make/build.xml Thu Feb 14 13:01:52 2013 +0100 2.2 +++ b/make/build.xml Thu Feb 14 13:22:26 2013 +0100 2.3 @@ -54,19 +54,11 @@ 2.4 2.5 <target name="clean" depends="init, clean-nasgen"> 2.6 <delete includeemptydirs="true"> 2.7 - <fileset dir="${build.dir}" excludes="${dynalink.dir.name}/**/*" erroronmissingdir="false"/> 2.8 + <fileset dir="${build.dir}" erroronmissingdir="false"/> 2.9 </delete> 2.10 <delete dir="${dist.dir}"/> 2.11 </target> 2.12 2.13 - <target name="clean-dynalink"> 2.14 - <delete dir="${dynalink.dir}"/> 2.15 - </target> 2.16 - 2.17 - <target name="clean-all" depends="clean-dynalink, clean"> 2.18 - <delete dir="${build.dir}"/> 2.19 - </target> 2.20 - 2.21 <!-- do it only if ASM is not available --> 2.22 <target name="compile-asm" depends="prepare" unless="asm.available"> 2.23 <javac srcdir="${jdk.asm.src.dir}" 2.24 @@ -79,29 +71,7 @@ 2.25 includeantruntime="false"/> 2.26 </target> 2.27 2.28 - <target name="check-dynalink-uptodate" depends="init"> 2.29 - <property name="versioned.dynalink.jar" value="${dynalink.dir}/dynalink-${dynalink.jar.version}.jar"/> 2.30 - <condition property="dynalink.uptodate"> 2.31 - <and> 2.32 - <available file="${versioned.dynalink.jar}"/> 2.33 - <filesmatch file1="${versioned.dynalink.jar}" file2="${dynalink.jar}"/> 2.34 - </and> 2.35 - </condition> 2.36 - </target> 2.37 - 2.38 - <target name="get-dynalink" depends="check-dynalink-uptodate" unless="dynalink.uptodate"> 2.39 - <mkdir dir="${dynalink.dir}"/> 2.40 - <!-- Delete previous snapshots, if any --> 2.41 - <delete> 2.42 - <fileset dir="${dynalink.dir}" includes="*"/> 2.43 - </delete> 2.44 - <property name="dynalink.download.base.url" value="http://oss.sonatype.org/content/repositories/${dynalink.version.type}s/org/dynalang/dynalink/${dynalink.version}/dynalink-${dynalink.jar.version}"/> 2.45 - <get src="${dynalink.download.base.url}.jar" dest="${versioned.dynalink.jar}" usetimestamp="true"/> 2.46 - <get src="${dynalink.download.base.url}-sources.jar" dest="${dynalink.dir}/dynalink-sources.jar" usetimestamp="true"/> 2.47 - <copy file="${versioned.dynalink.jar}" tofile="${dynalink.jar}" overwrite="true"/> 2.48 - </target> 2.49 - 2.50 - <target name="compile" depends="compile-asm, get-dynalink" description="Compiles nashorn"> 2.51 + <target name="compile" depends="compile-asm" description="Compiles nashorn"> 2.52 <javac srcdir="${src.dir}" 2.53 destdir="${build.classes.dir}" 2.54 classpath="${javac.classpath}" 2.55 @@ -126,6 +96,8 @@ 2.56 <copy todir="${build.classes.dir}/jdk/nashorn/tools/resources"> 2.57 <fileset dir="${src.dir}/jdk/nashorn/tools/resources/"/> 2.58 </copy> 2.59 + <copy file="${src.dir}/jdk/internal/dynalink/support/messages.properties" todir="${build.classes.dir}/jdk/internal/dynalink/support"/> 2.60 + 2.61 <echo message="full=${nashorn.fullversion}" file="${build.classes.dir}/jdk/nashorn/internal/runtime/resources/version.properties"/> 2.62 <echo file="${build.classes.dir}/jdk/nashorn/internal/runtime/resources/version.properties" append="true">${line.separator}</echo> 2.63 <echo message="release=${nashorn.version}" file="${build.classes.dir}/jdk/nashorn/internal/runtime/resources/version.properties" append="true"/> 2.64 @@ -134,7 +106,6 @@ 2.65 <target name="jar" depends="compile, run-nasgen" description="Creates nashorn.jar"> 2.66 <jar jarfile="${dist.jar}" manifest="${meta.inf.dir}/MANIFEST.MF" index="true" filesetmanifest="merge"> 2.67 <fileset dir="${build.classes.dir}"/> 2.68 - <zipfileset src="${dynalink.jar}"/> 2.69 <manifest> 2.70 <attribute name="Archiver-Version" value="n/a"/> 2.71 <attribute name="Build-Jdk" value="${java.runtime.version}"/> 2.72 @@ -152,12 +123,10 @@ 2.73 <javadoc destdir="${dist.javadoc.dir}" use="yes" overview="src/overview.html" windowtitle="${nashorn.product.name} ${nashorn.version}" additionalparam="-quiet" failonerror="true"> 2.74 <classpath> 2.75 <pathelement location="${build.classes.dir}"/> 2.76 - <pathelement location="${dynalink.jar}"/> 2.77 </classpath> 2.78 <fileset dir="${src.dir}" includes="**/*.java"/> 2.79 <fileset dir="${jdk.asm.src.dir}" includes="**/*.java"/> 2.80 <link href="http://docs.oracle.com/javase/7/docs/api/"/> 2.81 - <link href="http://szegedi.github.com/dynalink/0.4/javadoc/"/> 2.82 <!-- The following tags are used only in ASM sources - just ignore these --> 2.83 <tag name="label" description="label tag in ASM sources" enabled="false"/> 2.84 <tag name="linked" description="linked tag in ASM sources" enabled="false"/>
3.1 --- a/make/nbproject/project.xml Thu Feb 14 13:01:52 2013 +0100 3.2 +++ b/make/nbproject/project.xml Thu Feb 14 13:22:26 2013 +0100 3.3 @@ -164,7 +164,6 @@ 3.4 </compilation-unit> 3.5 <compilation-unit> 3.6 <package-root>../src</package-root> 3.7 - <classpath mode="compile">../build/dynalink/dynalink.jar</classpath> 3.8 <source-level>1.7</source-level> 3.9 </compilation-unit> 3.10 <compilation-unit>
4.1 --- a/make/project.properties Thu Feb 14 13:01:52 2013 +0100 4.2 +++ b/make/project.properties Thu Feb 14 13:22:26 2013 +0100 4.3 @@ -87,23 +87,10 @@ 4.4 org.testng.reporters.EmailableReporter, \ 4.5 jdk.nashorn.internal.test.framework.JSJUnitReportReporter 4.6 4.7 -# Define the version of Dynalink that is used. Version types are either 4.8 -# 'snapshot' or 'release'. When it is 'snapshot', the version must have 4.9 -# "-SNAPSHOT" suffix and the jar version will have a timestamp in it. When 4.10 -# it's 'release', the version has no suffix, and the jar version is 4.11 -# identical to version - fun with Maven central. 4.12 -dynalink.version=0.5-SNAPSHOT 4.13 -dynalink.version.type=snapshot 4.14 -dynalink.jar.version=0.5-20130109.113843-12 4.15 -dynalink.dir.name=dynalink 4.16 -dynalink.dir=build/${dynalink.dir.name} 4.17 -dynalink.jar=${dynalink.dir}/dynalink.jar 4.18 - 4.19 javac.debug=true 4.20 javac.encoding=ascii 4.21 javac.classpath=\ 4.22 - ${build.classes.dir}:\ 4.23 - ${dynalink.jar} 4.24 + ${build.classes.dir} 4.25 javac.test.classpath=\ 4.26 ${build.classes.dir}:\ 4.27 ${build.test.classes.dir}:\
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/jdk/internal/dynalink/CallSiteDescriptor.java Thu Feb 14 13:22:26 2013 +0100 5.3 @@ -0,0 +1,175 @@ 5.4 +/* 5.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. Oracle designates this 5.11 + * particular file as subject to the "Classpath" exception as provided 5.12 + * by Oracle in the LICENSE file that accompanied this code. 5.13 + * 5.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.17 + * version 2 for more details (a copy is included in the LICENSE file that 5.18 + * accompanied this code). 5.19 + * 5.20 + * You should have received a copy of the GNU General Public License version 5.21 + * 2 along with this work; if not, write to the Free Software Foundation, 5.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.23 + * 5.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.25 + * or visit www.oracle.com if you need additional information or have any 5.26 + * questions. 5.27 + */ 5.28 + 5.29 +/* 5.30 + * This file is available under and governed by the GNU General Public 5.31 + * License version 2 only, as published by the Free Software Foundation. 5.32 + * However, the following notice accompanied the original version of this 5.33 + * file, and Oracle licenses the original version of this file under the BSD 5.34 + * license: 5.35 + */ 5.36 +/* 5.37 + Copyright 2009-2013 Attila Szegedi 5.38 + 5.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 5.40 + and the BSD License (the "BSD License"), with licensee being free to 5.41 + choose either of the two at their discretion. 5.42 + 5.43 + You may not use this file except in compliance with either the Apache 5.44 + License or the BSD License. 5.45 + 5.46 + If you choose to use this file in compliance with the Apache License, the 5.47 + following notice applies to you: 5.48 + 5.49 + You may obtain a copy of the Apache License at 5.50 + 5.51 + http://www.apache.org/licenses/LICENSE-2.0 5.52 + 5.53 + Unless required by applicable law or agreed to in writing, software 5.54 + distributed under the License is distributed on an "AS IS" BASIS, 5.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 5.56 + implied. See the License for the specific language governing 5.57 + permissions and limitations under the License. 5.58 + 5.59 + If you choose to use this file in compliance with the BSD License, the 5.60 + following notice applies to you: 5.61 + 5.62 + Redistribution and use in source and binary forms, with or without 5.63 + modification, are permitted provided that the following conditions are 5.64 + met: 5.65 + * Redistributions of source code must retain the above copyright 5.66 + notice, this list of conditions and the following disclaimer. 5.67 + * Redistributions in binary form must reproduce the above copyright 5.68 + notice, this list of conditions and the following disclaimer in the 5.69 + documentation and/or other materials provided with the distribution. 5.70 + * Neither the name of the copyright holder nor the names of 5.71 + contributors may be used to endorse or promote products derived from 5.72 + this software without specific prior written permission. 5.73 + 5.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 5.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 5.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 5.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 5.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 5.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 5.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 5.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 5.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 5.85 +*/ 5.86 + 5.87 +package jdk.internal.dynalink; 5.88 + 5.89 +import java.lang.invoke.MethodHandles.Lookup; 5.90 +import java.lang.invoke.MethodType; 5.91 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 5.92 + 5.93 + 5.94 +/** 5.95 + * An immutable descriptor of a call site. It is an immutable object that contains all the information about a call 5.96 + * site: the class performing the lookups, the name of the method being invoked, and the method signature. The library 5.97 + * has a default {@link CallSiteDescriptorFactory} for descriptors that you can use, or you can create your own 5.98 + * descriptor classes, especially if you need to add further information (values passed in additional parameters to the 5.99 + * bootstrap method) to them. Call site descriptors are used in this library in place of passing a real call site to 5.100 + * guarding linkers so they aren't tempted to directly manipulate the call sites. The constructors of built-in 5.101 + * {@link RelinkableCallSite} implementations all need a call site descriptor. Even if you create your own call site 5.102 + * descriptors consider using {@link CallSiteDescriptorFactory#tokenizeName(String)} in your implementation. 5.103 + * 5.104 + * @author Attila Szegedi 5.105 + */ 5.106 +public interface CallSiteDescriptor { 5.107 + /** 5.108 + * The index of the name token that will carry the operation scheme prefix (usually, "dyn"). 5.109 + */ 5.110 + public static final int SCHEME = 0; 5.111 + /** 5.112 + * The index of the name token that will usually carry the operation name. 5.113 + */ 5.114 + 5.115 + public static final int OPERATOR=1; 5.116 + /** 5.117 + * The index of the name token that will usually carry a name of an operand (of a property, method, etc.) 5.118 + */ 5.119 + 5.120 + public static final int NAME_OPERAND=2; 5.121 + 5.122 + /** 5.123 + * Character used to delimit tokens in an call site name. 5.124 + */ 5.125 + public static final String TOKEN_DELIMITER = ":"; 5.126 + 5.127 + /** 5.128 + * Character used to delimit operation names in a composite operation specification. 5.129 + */ 5.130 + public static final String OPERATOR_DELIMITER = "|"; 5.131 + 5.132 + /** 5.133 + * Returns the number of tokens in the name of the method at the call site. Method names are tokenized with the 5.134 + * colon ":" character, i.e. "dyn:getProp:color" would be the name used to describe a method that retrieves the 5.135 + * property named "color" on the object it is invoked on. 5.136 + * @return the number of tokens in the name of the method at the call site. 5.137 + */ 5.138 + public int getNameTokenCount(); 5.139 + 5.140 + /** 5.141 + * Returns the <i>i<sup>th</sup></i> token in the method name at the call site. Method names are tokenized with the 5.142 + * colon ":" character. 5.143 + * @param i the index of the token. Must be between 0 (inclusive) and {@link #getNameTokenCount()} (exclusive) 5.144 + * @throws IllegalArgumentException if the index is outside the allowed range. 5.145 + * @return the <i>i<sup>th</sup></i> token in the method name at the call site. The returned strings are interned. 5.146 + */ 5.147 + public String getNameToken(int i); 5.148 + 5.149 + /** 5.150 + * Returns the name of the method at the call site. Note that the object internally only stores the tokenized name, 5.151 + * and has to reconstruct the full name from tokens on each invocation. 5.152 + * @return the name of the method at the call site. 5.153 + */ 5.154 + public String getName(); 5.155 + 5.156 + /** 5.157 + * The type of the method at the call site. 5.158 + * 5.159 + * @return type of the method at the call site. 5.160 + */ 5.161 + public MethodType getMethodType(); 5.162 + 5.163 + /** 5.164 + * Returns the lookup passed to the bootstrap method. 5.165 + * @return the lookup passed to the bootstrap method. 5.166 + */ 5.167 + public Lookup getLookup(); 5.168 + 5.169 + /** 5.170 + * Creates a new call site descriptor from this descriptor, which is identical to this, except it changes the method 5.171 + * type. 5.172 + * 5.173 + * @param newMethodType the new method type 5.174 + * @return a new call site descriptor, with the method type changed. 5.175 + */ 5.176 + public CallSiteDescriptor changeMethodType(MethodType newMethodType); 5.177 + 5.178 +} 5.179 \ No newline at end of file
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/jdk/internal/dynalink/ChainedCallSite.java Thu Feb 14 13:22:26 2013 +0100 6.3 @@ -0,0 +1,212 @@ 6.4 +/* 6.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. Oracle designates this 6.11 + * particular file as subject to the "Classpath" exception as provided 6.12 + * by Oracle in the LICENSE file that accompanied this code. 6.13 + * 6.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.17 + * version 2 for more details (a copy is included in the LICENSE file that 6.18 + * accompanied this code). 6.19 + * 6.20 + * You should have received a copy of the GNU General Public License version 6.21 + * 2 along with this work; if not, write to the Free Software Foundation, 6.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.23 + * 6.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.25 + * or visit www.oracle.com if you need additional information or have any 6.26 + * questions. 6.27 + */ 6.28 + 6.29 +/* 6.30 + * This file is available under and governed by the GNU General Public 6.31 + * License version 2 only, as published by the Free Software Foundation. 6.32 + * However, the following notice accompanied the original version of this 6.33 + * file, and Oracle licenses the original version of this file under the BSD 6.34 + * license: 6.35 + */ 6.36 +/* 6.37 + Copyright 2009-2013 Attila Szegedi 6.38 + 6.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 6.40 + and the BSD License (the "BSD License"), with licensee being free to 6.41 + choose either of the two at their discretion. 6.42 + 6.43 + You may not use this file except in compliance with either the Apache 6.44 + License or the BSD License. 6.45 + 6.46 + If you choose to use this file in compliance with the Apache License, the 6.47 + following notice applies to you: 6.48 + 6.49 + You may obtain a copy of the Apache License at 6.50 + 6.51 + http://www.apache.org/licenses/LICENSE-2.0 6.52 + 6.53 + Unless required by applicable law or agreed to in writing, software 6.54 + distributed under the License is distributed on an "AS IS" BASIS, 6.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 6.56 + implied. See the License for the specific language governing 6.57 + permissions and limitations under the License. 6.58 + 6.59 + If you choose to use this file in compliance with the BSD License, the 6.60 + following notice applies to you: 6.61 + 6.62 + Redistribution and use in source and binary forms, with or without 6.63 + modification, are permitted provided that the following conditions are 6.64 + met: 6.65 + * Redistributions of source code must retain the above copyright 6.66 + notice, this list of conditions and the following disclaimer. 6.67 + * Redistributions in binary form must reproduce the above copyright 6.68 + notice, this list of conditions and the following disclaimer in the 6.69 + documentation and/or other materials provided with the distribution. 6.70 + * Neither the name of the copyright holder nor the names of 6.71 + contributors may be used to endorse or promote products derived from 6.72 + this software without specific prior written permission. 6.73 + 6.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 6.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 6.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 6.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 6.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 6.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 6.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 6.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 6.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 6.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 6.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 6.85 +*/ 6.86 + 6.87 +package jdk.internal.dynalink; 6.88 + 6.89 +import java.lang.invoke.MethodHandle; 6.90 +import java.lang.invoke.MethodHandles; 6.91 +import java.lang.invoke.MethodType; 6.92 +import java.util.Iterator; 6.93 +import java.util.LinkedList; 6.94 +import java.util.concurrent.atomic.AtomicReference; 6.95 +import jdk.internal.dynalink.linker.GuardedInvocation; 6.96 +import jdk.internal.dynalink.support.AbstractRelinkableCallSite; 6.97 + 6.98 + 6.99 +/** 6.100 + * A relinkable call site that maintains a chain of linked method handles. In the default implementation, up to 8 method 6.101 + * handles can be chained, cascading from one to the other through 6.102 + * {@link MethodHandles#guardWithTest(MethodHandle, MethodHandle, MethodHandle)}. When this call site has to link a new 6.103 + * method handle and the length of the chain is already at the maximum, it will throw away the oldest method handle. 6.104 + * Switchpoint-invalidated handles in the chain are removed eagerly (on each linking request, and whenever a 6.105 + * switchpoint-invalidated method handle is traversed during invocation). There is currently no profiling 6.106 + * attached to the handles in the chain, so they are never reordered based on usage; the most recently linked method 6.107 + * handle is always at the start of the chain. 6.108 + */ 6.109 +public class ChainedCallSite extends AbstractRelinkableCallSite { 6.110 + private final AtomicReference<LinkedList<GuardedInvocation>> invocations = new AtomicReference<>(); 6.111 + 6.112 + /** 6.113 + * Creates a new chained call site. 6.114 + * @param descriptor the descriptor for the call site. 6.115 + */ 6.116 + public ChainedCallSite(CallSiteDescriptor descriptor) { 6.117 + super(descriptor); 6.118 + } 6.119 + 6.120 + /** 6.121 + * The maximum number of method handles in the chain. Defaults to 8. You can override it in a subclass if you need 6.122 + * to change the value. If your override returns a value less than 1, the code will break. 6.123 + * @return the maximum number of method handles in the chain. 6.124 + */ 6.125 + @SuppressWarnings("static-method") 6.126 + protected int getMaxChainLength() { 6.127 + return 8; 6.128 + } 6.129 + 6.130 + @Override 6.131 + public void relink(GuardedInvocation guardedInvocation, MethodHandle fallback) { 6.132 + relinkInternal(guardedInvocation, fallback, false); 6.133 + } 6.134 + 6.135 + @Override 6.136 + public void resetAndRelink(GuardedInvocation guardedInvocation, MethodHandle fallback) { 6.137 + relinkInternal(guardedInvocation, fallback, true); 6.138 + } 6.139 + 6.140 + private MethodHandle relinkInternal(GuardedInvocation invocation, MethodHandle relink, boolean reset) { 6.141 + final LinkedList<GuardedInvocation> currentInvocations = invocations.get(); 6.142 + @SuppressWarnings({ "unchecked", "rawtypes" }) 6.143 + final LinkedList<GuardedInvocation> newInvocations = 6.144 + currentInvocations == null || reset ? new LinkedList<>() : (LinkedList)currentInvocations.clone(); 6.145 + 6.146 + // First, prune the chain of invalidated switchpoints. 6.147 + for(Iterator<GuardedInvocation> it = newInvocations.iterator(); it.hasNext();) { 6.148 + if(it.next().hasBeenInvalidated()) { 6.149 + it.remove(); 6.150 + } 6.151 + } 6.152 + 6.153 + // prune() is allowed to invoke this method with invocation == null meaning we're just pruning the chain and not 6.154 + // adding any new invocations to it. 6.155 + if(invocation != null) { 6.156 + // Remove oldest entry if we're at max length 6.157 + if(newInvocations.size() == getMaxChainLength()) { 6.158 + newInvocations.removeFirst(); 6.159 + } 6.160 + newInvocations.addLast(invocation); 6.161 + } 6.162 + 6.163 + // prune-and-invoke is used as the fallback for invalidated switchpoints. If a switchpoint gets invalidated, we 6.164 + // rebuild the chain and get rid of all invalidated switchpoints instead of letting them linger. 6.165 + final MethodHandle pruneAndInvoke = makePruneAndInvokeMethod(relink); 6.166 + 6.167 + // Fold the new chain 6.168 + MethodHandle target = relink; 6.169 + for(GuardedInvocation inv: newInvocations) { 6.170 + target = inv.compose(pruneAndInvoke, target); 6.171 + } 6.172 + 6.173 + // If nobody else updated the call site while we were rebuilding the chain, set the target to our chain. In case 6.174 + // we lost the race for multithreaded update, just do nothing. Either the other thread installed the same thing 6.175 + // we wanted to install, or otherwise, we'll be asked to relink again. 6.176 + if(invocations.compareAndSet(currentInvocations, newInvocations)) { 6.177 + setTarget(target); 6.178 + } 6.179 + return target; 6.180 + } 6.181 + 6.182 + /** 6.183 + * Creates a method that rebuilds our call chain, pruning it of any invalidated switchpoints, and then invokes that 6.184 + * chain. 6.185 + * @param relink the ultimate fallback for the chain (the {@code DynamicLinker}'s relink). 6.186 + * @return a method handle for prune-and-invoke 6.187 + */ 6.188 + private MethodHandle makePruneAndInvokeMethod(MethodHandle relink) { 6.189 + // Bind prune to (this, relink) 6.190 + final MethodHandle boundPrune = MethodHandles.insertArguments(PRUNE, 0, this, relink); 6.191 + // Make it ignore all incoming arguments 6.192 + final MethodHandle ignoreArgsPrune = MethodHandles.dropArguments(boundPrune, 0, type().parameterList()); 6.193 + // Invoke prune, then invoke the call site target with original arguments 6.194 + return MethodHandles.foldArguments(MethodHandles.exactInvoker(type()), ignoreArgsPrune); 6.195 + } 6.196 + 6.197 + @SuppressWarnings("unused") 6.198 + private MethodHandle prune(MethodHandle relink) { 6.199 + return relinkInternal(null, relink, false); 6.200 + } 6.201 + 6.202 + private static final MethodHandle PRUNE; 6.203 + static { 6.204 + try { 6.205 + PRUNE = MethodHandles.lookup().findSpecial(ChainedCallSite.class, "prune", MethodType.methodType( 6.206 + MethodHandle.class, MethodHandle.class), ChainedCallSite.class); 6.207 + // NOTE: using two catch blocks so we don't introduce a reference to 1.7 ReflectiveOperationException, allowing 6.208 + // Dynalink to be used on 1.6 JVMs with Remi's backport library. 6.209 + } catch(IllegalAccessException e) { 6.210 + throw new AssertionError(e.getMessage(), e); // Can not happen 6.211 + } catch(NoSuchMethodException e) { 6.212 + throw new AssertionError(e.getMessage(), e); // Can not happen 6.213 + } 6.214 + } 6.215 +} 6.216 \ No newline at end of file
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/jdk/internal/dynalink/DefaultBootstrapper.java Thu Feb 14 13:22:26 2013 +0100 7.3 @@ -0,0 +1,144 @@ 7.4 +/* 7.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. Oracle designates this 7.11 + * particular file as subject to the "Classpath" exception as provided 7.12 + * by Oracle in the LICENSE file that accompanied this code. 7.13 + * 7.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.17 + * version 2 for more details (a copy is included in the LICENSE file that 7.18 + * accompanied this code). 7.19 + * 7.20 + * You should have received a copy of the GNU General Public License version 7.21 + * 2 along with this work; if not, write to the Free Software Foundation, 7.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.23 + * 7.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.25 + * or visit www.oracle.com if you need additional information or have any 7.26 + * questions. 7.27 + */ 7.28 + 7.29 +/* 7.30 + * This file is available under and governed by the GNU General Public 7.31 + * License version 2 only, as published by the Free Software Foundation. 7.32 + * However, the following notice accompanied the original version of this 7.33 + * file, and Oracle licenses the original version of this file under the BSD 7.34 + * license: 7.35 + */ 7.36 +/* 7.37 + Copyright 2009-2013 Attila Szegedi 7.38 + 7.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 7.40 + and the BSD License (the "BSD License"), with licensee being free to 7.41 + choose either of the two at their discretion. 7.42 + 7.43 + You may not use this file except in compliance with either the Apache 7.44 + License or the BSD License. 7.45 + 7.46 + If you choose to use this file in compliance with the Apache License, the 7.47 + following notice applies to you: 7.48 + 7.49 + You may obtain a copy of the Apache License at 7.50 + 7.51 + http://www.apache.org/licenses/LICENSE-2.0 7.52 + 7.53 + Unless required by applicable law or agreed to in writing, software 7.54 + distributed under the License is distributed on an "AS IS" BASIS, 7.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 7.56 + implied. See the License for the specific language governing 7.57 + permissions and limitations under the License. 7.58 + 7.59 + If you choose to use this file in compliance with the BSD License, the 7.60 + following notice applies to you: 7.61 + 7.62 + Redistribution and use in source and binary forms, with or without 7.63 + modification, are permitted provided that the following conditions are 7.64 + met: 7.65 + * Redistributions of source code must retain the above copyright 7.66 + notice, this list of conditions and the following disclaimer. 7.67 + * Redistributions in binary form must reproduce the above copyright 7.68 + notice, this list of conditions and the following disclaimer in the 7.69 + documentation and/or other materials provided with the distribution. 7.70 + * Neither the name of the copyright holder nor the names of 7.71 + contributors may be used to endorse or promote products derived from 7.72 + this software without specific prior written permission. 7.73 + 7.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 7.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 7.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 7.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 7.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 7.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 7.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 7.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 7.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 7.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 7.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 7.85 +*/ 7.86 + 7.87 +package jdk.internal.dynalink; 7.88 + 7.89 +import java.lang.invoke.CallSite; 7.90 +import java.lang.invoke.MethodHandles; 7.91 +import java.lang.invoke.MethodType; 7.92 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 7.93 + 7.94 + 7.95 +/** 7.96 + * A convenience default bootstrapper that exposes static bootstrap methods which language runtimes that need the very 7.97 + * default behavior can use with minimal setup. When first referenced, it will create a dynamic linker with default 7.98 + * settings for the {@link DynamicLinkerFactory}, and its bootstrap methods will create {@link MonomorphicCallSite} for 7.99 + * all call sites. It has two bootstrap methods: one creates call sites that use the 7.100 + * {@link MethodHandles#publicLookup()} as the lookup for all call sites and disregard the one passed in as the caller, 7.101 + * and one that just uses the passed caller as the lookup scope. Using the public lookup one is advised if your language 7.102 + * runtime has no concept of interacting with Java visibility scopes, as it results in a more lightweight runtime 7.103 + * information. 7.104 + * 7.105 + * @author Attila Szegedi 7.106 + */ 7.107 +public class DefaultBootstrapper { 7.108 + private static final DynamicLinker dynamicLinker = new DynamicLinkerFactory().createLinker(); 7.109 + 7.110 + private DefaultBootstrapper() { 7.111 + } 7.112 + 7.113 + /** 7.114 + * Use this method as your bootstrap method (see the documentation of the java.lang.invoke package for how to do 7.115 + * this). In case your language runtime doesn't have a concept of interaction with Java access scopes, you might 7.116 + * want to consider using 7.117 + * {@link #publicBootstrap(java.lang.invoke.MethodHandles.Lookup, String, MethodType)} instead. 7.118 + * 7.119 + * @param caller the caller's lookup 7.120 + * @param name the name of the method at the call site 7.121 + * @param type the method signature at the call site 7.122 + * @return a new {@link MonomorphicCallSite} linked with the default dynamic linker. 7.123 + */ 7.124 + public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type) { 7.125 + return bootstrapInternal(caller, name, type); 7.126 + } 7.127 + 7.128 + /** 7.129 + * Use this method as your bootstrap method (see the documentation of the java.lang.invoke package for how to do 7.130 + * this) when your language runtime doesn't have a concept of interaction with Java access scopes. If you need to 7.131 + * preserve the different caller Lookup objects in the call sites, use 7.132 + * {@link #bootstrap(java.lang.invoke.MethodHandles.Lookup, String, MethodType)} instead 7.133 + * 7.134 + * @param caller the caller's lookup. It is ignored as the call sites will be created with 7.135 + * {@link MethodHandles#publicLookup()} instead. 7.136 + * @param name the name of the method at the call site 7.137 + * @param type the method signature at the call site 7.138 + * @return a new {@link MonomorphicCallSite} linked with the default dynamic linker. 7.139 + */ 7.140 + public static CallSite publicBootstrap(@SuppressWarnings("unused") MethodHandles.Lookup caller, String name, MethodType type) { 7.141 + return bootstrapInternal(MethodHandles.publicLookup(), name, type); 7.142 + } 7.143 + 7.144 + private static CallSite bootstrapInternal(MethodHandles.Lookup caller, String name, MethodType type) { 7.145 + return dynamicLinker.link(new MonomorphicCallSite(CallSiteDescriptorFactory.create(caller, name, type))); 7.146 + } 7.147 +} 7.148 \ No newline at end of file
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/src/jdk/internal/dynalink/DynamicLinker.java Thu Feb 14 13:22:26 2013 +0100 8.3 @@ -0,0 +1,281 @@ 8.4 +/* 8.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. Oracle designates this 8.11 + * particular file as subject to the "Classpath" exception as provided 8.12 + * by Oracle in the LICENSE file that accompanied this code. 8.13 + * 8.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.17 + * version 2 for more details (a copy is included in the LICENSE file that 8.18 + * accompanied this code). 8.19 + * 8.20 + * You should have received a copy of the GNU General Public License version 8.21 + * 2 along with this work; if not, write to the Free Software Foundation, 8.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.23 + * 8.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.25 + * or visit www.oracle.com if you need additional information or have any 8.26 + * questions. 8.27 + */ 8.28 + 8.29 +/* 8.30 + * This file is available under and governed by the GNU General Public 8.31 + * License version 2 only, as published by the Free Software Foundation. 8.32 + * However, the following notice accompanied the original version of this 8.33 + * file, and Oracle licenses the original version of this file under the BSD 8.34 + * license: 8.35 + */ 8.36 +/* 8.37 + Copyright 2009-2013 Attila Szegedi 8.38 + 8.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 8.40 + and the BSD License (the "BSD License"), with licensee being free to 8.41 + choose either of the two at their discretion. 8.42 + 8.43 + You may not use this file except in compliance with either the Apache 8.44 + License or the BSD License. 8.45 + 8.46 + If you choose to use this file in compliance with the Apache License, the 8.47 + following notice applies to you: 8.48 + 8.49 + You may obtain a copy of the Apache License at 8.50 + 8.51 + http://www.apache.org/licenses/LICENSE-2.0 8.52 + 8.53 + Unless required by applicable law or agreed to in writing, software 8.54 + distributed under the License is distributed on an "AS IS" BASIS, 8.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 8.56 + implied. See the License for the specific language governing 8.57 + permissions and limitations under the License. 8.58 + 8.59 + If you choose to use this file in compliance with the BSD License, the 8.60 + following notice applies to you: 8.61 + 8.62 + Redistribution and use in source and binary forms, with or without 8.63 + modification, are permitted provided that the following conditions are 8.64 + met: 8.65 + * Redistributions of source code must retain the above copyright 8.66 + notice, this list of conditions and the following disclaimer. 8.67 + * Redistributions in binary form must reproduce the above copyright 8.68 + notice, this list of conditions and the following disclaimer in the 8.69 + documentation and/or other materials provided with the distribution. 8.70 + * Neither the name of the copyright holder nor the names of 8.71 + contributors may be used to endorse or promote products derived from 8.72 + this software without specific prior written permission. 8.73 + 8.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 8.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 8.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 8.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 8.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 8.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 8.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 8.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 8.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 8.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 8.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 8.85 +*/ 8.86 + 8.87 +package jdk.internal.dynalink; 8.88 + 8.89 +import java.lang.invoke.MethodHandle; 8.90 +import java.lang.invoke.MethodHandles; 8.91 +import java.lang.invoke.MethodType; 8.92 +import java.lang.invoke.MutableCallSite; 8.93 +import java.util.List; 8.94 +import jdk.internal.dynalink.linker.GuardedInvocation; 8.95 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 8.96 +import jdk.internal.dynalink.linker.LinkRequest; 8.97 +import jdk.internal.dynalink.linker.LinkerServices; 8.98 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 8.99 +import jdk.internal.dynalink.support.LinkRequestImpl; 8.100 +import jdk.internal.dynalink.support.Lookup; 8.101 +import jdk.internal.dynalink.support.RuntimeContextLinkRequestImpl; 8.102 + 8.103 + 8.104 +/** 8.105 + * The linker for {@link RelinkableCallSite} objects. Users of it (scripting frameworks and language runtimes) have to 8.106 + * create a linker using the {@link DynamicLinkerFactory} and invoke its link method from the invokedynamic bootstrap 8.107 + * methods to set the target of all the call sites in the code they generate. Usual usage would be to create one class 8.108 + * per language runtime to contain one linker instance as: 8.109 + * 8.110 + * <pre> 8.111 + * class MyLanguageRuntime { 8.112 + * private static final GuardingDynamicLinker myLanguageLinker = new MyLanguageLinker(); 8.113 + * private static final DynamicLinker dynamicLinker = createDynamicLinker(); 8.114 + * 8.115 + * private static DynamicLinker createDynamicLinker() { 8.116 + * final DynamicLinkerFactory factory = new DynamicLinkerFactory(); 8.117 + * factory.setPrioritizedLinker(myLanguageLinker); 8.118 + * return factory.createLinker(); 8.119 + * } 8.120 + * 8.121 + * public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type) { 8.122 + * return dynamicLinker.link(new MonomorphicCallSite(CallSiteDescriptorFactory.create(lookup, name, type))); 8.123 + * } 8.124 + * } 8.125 + * </pre> 8.126 + * 8.127 + * Note how there are three components you will need to provide here: 8.128 + * <ul> 8.129 + * <li>You're expected to provide a {@link GuardingDynamicLinker} for your own language. If your runtime doesn't 8.130 + * have its own language and/or object model (i.e. it's a generic scripting shell), you don't need to implement a 8.131 + * dynamic linker; you would simply not invoke the {@code setPrioritizedLinker} method on the factory, or even better, 8.132 + * simply use {@link DefaultBootstrapper}.</li> 8.133 + * <li>The performance of the programs can depend on your choice of the class to represent call sites. The above 8.134 + * example used {@link MonomorphicCallSite}, but you might want to use {@link ChainedCallSite} instead. You'll need to 8.135 + * experiment and decide what fits your language runtime the best. You can subclass either of these or roll your own if 8.136 + * you need to.</li> 8.137 + * <li>You also need to provide {@link CallSiteDescriptor}s to your call sites. They are immutable objects that contain 8.138 + * all the information about the call site: the class performing the lookups, the name of the method being invoked, and 8.139 + * the method signature. The library has a default {@link CallSiteDescriptorFactory} for descriptors that you can use, 8.140 + * or you can create your own descriptor classes, especially if you need to add further information (values passed in 8.141 + * additional parameters to the bootstrap method) to them.</li> 8.142 + * </ul> 8.143 + * 8.144 + * @author Attila Szegedi 8.145 + */ 8.146 +public class DynamicLinker { 8.147 + 8.148 + private static final String CLASS_NAME = DynamicLinker.class.getName(); 8.149 + private static final String RELINK_METHOD_NAME = "relink"; 8.150 + 8.151 + private final LinkerServices linkerServices; 8.152 + private final int runtimeContextArgCount; 8.153 + private final boolean syncOnRelink; 8.154 + private final int unstableRelinkThreshold; 8.155 + 8.156 + /** 8.157 + * Creates a new dynamic linker. 8.158 + * 8.159 + * @param linkerServices the linkerServices used by the linker, created by the factory. 8.160 + * @param runtimeContextArgCount see {@link DynamicLinkerFactory#setRuntimeContextArgCount(int)} 8.161 + */ 8.162 + DynamicLinker(LinkerServices linkerServices, int runtimeContextArgCount, boolean syncOnRelink, 8.163 + int unstableRelinkThreshold) { 8.164 + if(runtimeContextArgCount < 0) { 8.165 + throw new IllegalArgumentException("runtimeContextArgCount < 0"); 8.166 + } 8.167 + if(unstableRelinkThreshold < 0) { 8.168 + throw new IllegalArgumentException("unstableRelinkThreshold < 0"); 8.169 + } 8.170 + this.runtimeContextArgCount = runtimeContextArgCount; 8.171 + this.linkerServices = linkerServices; 8.172 + this.syncOnRelink = syncOnRelink; 8.173 + this.unstableRelinkThreshold = unstableRelinkThreshold; 8.174 + } 8.175 + 8.176 + /** 8.177 + * Links an invokedynamic call site. It will install a method handle into the call site that invokes the relinking 8.178 + * mechanism of this linker. Next time the call site is invoked, it will be linked for the actual arguments it was 8.179 + * invoked with. 8.180 + * 8.181 + * @param callSite the call site to link. 8.182 + * @return the callSite, for easy call chaining. 8.183 + */ 8.184 + public <T extends RelinkableCallSite> T link(final T callSite) { 8.185 + callSite.initialize(createRelinkAndInvokeMethod(callSite, 0)); 8.186 + return callSite; 8.187 + } 8.188 + 8.189 + /** 8.190 + * Returns the object representing the lower level linker services of this class that are normally exposed to 8.191 + * individual language-specific linkers. While as a user of this class you normally only care about the 8.192 + * {@link #link(RelinkableCallSite)} method, in certain circumstances you might want to use the lower level services 8.193 + * directly; either to lookup specific method handles, to access the type converters, and so on. 8.194 + * @return the object representing the linker services of this class. 8.195 + */ 8.196 + public LinkerServices getLinkerServices() { 8.197 + return linkerServices; 8.198 + } 8.199 + 8.200 + private static final MethodHandle RELINK = Lookup.findOwnSpecial(MethodHandles.lookup(), RELINK_METHOD_NAME, 8.201 + MethodHandle.class, RelinkableCallSite.class, int.class, Object[].class); 8.202 + 8.203 + private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, int relinkCount) { 8.204 + // Make a bound MH of invoke() for this linker and call site 8.205 + final MethodHandle boundRelinker = MethodHandles.insertArguments(RELINK, 0, this, callSite, Integer.valueOf( 8.206 + relinkCount)); 8.207 + // Make a MH that gathers all arguments to the invocation into an Object[] 8.208 + final MethodType type = callSite.getDescriptor().getMethodType(); 8.209 + final MethodHandle collectingRelinker = boundRelinker.asCollector(Object[].class, type.parameterCount()); 8.210 + return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), collectingRelinker.asType( 8.211 + type.changeReturnType(MethodHandle.class))); 8.212 + } 8.213 + 8.214 + /** 8.215 + * Relinks a call site conforming to the invocation arguments. 8.216 + * 8.217 + * @param callSite the call site itself 8.218 + * @param arguments arguments to the invocation 8.219 + * @return return the method handle for the invocation 8.220 + * @throws Exception rethrows any exception thrown by the linkers 8.221 + */ 8.222 + @SuppressWarnings("unused") 8.223 + private MethodHandle relink(RelinkableCallSite callSite, int relinkCount, Object... arguments) throws Exception { 8.224 + final CallSiteDescriptor callSiteDescriptor = callSite.getDescriptor(); 8.225 + final boolean unstableDetectionEnabled = unstableRelinkThreshold > 0; 8.226 + final boolean callSiteUnstable = unstableDetectionEnabled && relinkCount >= unstableRelinkThreshold; 8.227 + final LinkRequest linkRequest = 8.228 + runtimeContextArgCount == 0 ? new LinkRequestImpl(callSiteDescriptor, callSiteUnstable, arguments) 8.229 + : new RuntimeContextLinkRequestImpl(callSiteDescriptor, callSiteUnstable, arguments, 8.230 + runtimeContextArgCount); 8.231 + 8.232 + // Find a suitable method handle with a guard 8.233 + GuardedInvocation guardedInvocation = linkerServices.getGuardedInvocation(linkRequest); 8.234 + 8.235 + // None found - throw an exception 8.236 + if(guardedInvocation == null) { 8.237 + throw new NoSuchDynamicMethodException(callSiteDescriptor.toString()); 8.238 + } 8.239 + 8.240 + // If our call sites have a runtime context, and the linker produced a context-stripped invocation, adapt the 8.241 + // produced invocation into contextual invocation (by dropping the context...) 8.242 + if(runtimeContextArgCount > 0) { 8.243 + final MethodType origType = callSiteDescriptor.getMethodType(); 8.244 + final MethodHandle invocation = guardedInvocation.getInvocation(); 8.245 + if(invocation.type().parameterCount() == origType.parameterCount() - runtimeContextArgCount) { 8.246 + final List<Class<?>> prefix = origType.parameterList().subList(1, runtimeContextArgCount + 1); 8.247 + final MethodHandle guard = guardedInvocation.getGuard(); 8.248 + guardedInvocation = guardedInvocation.dropArguments(1, prefix); 8.249 + } 8.250 + } 8.251 + 8.252 + if(unstableDetectionEnabled && relinkCount <= unstableRelinkThreshold && relinkCount++ == unstableRelinkThreshold) { 8.253 + // Note that we'll increase the relinkCount until threshold+1 and not increase it beyond that. Threshold+1 8.254 + // is treated as a special value to signal that resetAndRelink has already executed once for the unstable 8.255 + // call site; we only want the call site to throw away its current linkage once, when it transitions to 8.256 + // unstable. 8.257 + callSite.resetAndRelink(guardedInvocation, createRelinkAndInvokeMethod(callSite, relinkCount)); 8.258 + } else { 8.259 + callSite.relink(guardedInvocation, createRelinkAndInvokeMethod(callSite, relinkCount)); 8.260 + } 8.261 + if(syncOnRelink) { 8.262 + MutableCallSite.syncAll(new MutableCallSite[] { (MutableCallSite)callSite }); 8.263 + } 8.264 + return guardedInvocation.getInvocation(); 8.265 + } 8.266 + 8.267 + /** 8.268 + * Returns a stack trace element describing the location of the call site currently being relinked on the current 8.269 + * thread. The operation internally creates a Throwable object and inspects its stack trace, so it's potentially 8.270 + * expensive. The recommended usage for it is in writing diagnostics code. 8.271 + * @return a stack trace element describing the location of the call site currently being relinked, or null if it is 8.272 + * not invoked while a call site is being relinked. 8.273 + */ 8.274 + public static StackTraceElement getRelinkedCallSiteLocation() { 8.275 + final StackTraceElement[] trace = new Throwable().getStackTrace(); 8.276 + for(int i = 0; i < trace.length - 1; ++i) { 8.277 + final StackTraceElement frame = trace[i]; 8.278 + if(RELINK_METHOD_NAME.equals(frame.getMethodName()) && CLASS_NAME.equals(frame.getClassName())) { 8.279 + return trace[i + 1]; 8.280 + } 8.281 + } 8.282 + return null; 8.283 + } 8.284 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/src/jdk/internal/dynalink/DynamicLinkerFactory.java Thu Feb 14 13:22:26 2013 +0100 9.3 @@ -0,0 +1,311 @@ 9.4 +/* 9.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.7 + * 9.8 + * This code is free software; you can redistribute it and/or modify it 9.9 + * under the terms of the GNU General Public License version 2 only, as 9.10 + * published by the Free Software Foundation. Oracle designates this 9.11 + * particular file as subject to the "Classpath" exception as provided 9.12 + * by Oracle in the LICENSE file that accompanied this code. 9.13 + * 9.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 9.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 9.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 9.17 + * version 2 for more details (a copy is included in the LICENSE file that 9.18 + * accompanied this code). 9.19 + * 9.20 + * You should have received a copy of the GNU General Public License version 9.21 + * 2 along with this work; if not, write to the Free Software Foundation, 9.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 9.23 + * 9.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 9.25 + * or visit www.oracle.com if you need additional information or have any 9.26 + * questions. 9.27 + */ 9.28 + 9.29 +/* 9.30 + * This file is available under and governed by the GNU General Public 9.31 + * License version 2 only, as published by the Free Software Foundation. 9.32 + * However, the following notice accompanied the original version of this 9.33 + * file, and Oracle licenses the original version of this file under the BSD 9.34 + * license: 9.35 + */ 9.36 +/* 9.37 + Copyright 2009-2013 Attila Szegedi 9.38 + 9.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 9.40 + and the BSD License (the "BSD License"), with licensee being free to 9.41 + choose either of the two at their discretion. 9.42 + 9.43 + You may not use this file except in compliance with either the Apache 9.44 + License or the BSD License. 9.45 + 9.46 + If you choose to use this file in compliance with the Apache License, the 9.47 + following notice applies to you: 9.48 + 9.49 + You may obtain a copy of the Apache License at 9.50 + 9.51 + http://www.apache.org/licenses/LICENSE-2.0 9.52 + 9.53 + Unless required by applicable law or agreed to in writing, software 9.54 + distributed under the License is distributed on an "AS IS" BASIS, 9.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 9.56 + implied. See the License for the specific language governing 9.57 + permissions and limitations under the License. 9.58 + 9.59 + If you choose to use this file in compliance with the BSD License, the 9.60 + following notice applies to you: 9.61 + 9.62 + Redistribution and use in source and binary forms, with or without 9.63 + modification, are permitted provided that the following conditions are 9.64 + met: 9.65 + * Redistributions of source code must retain the above copyright 9.66 + notice, this list of conditions and the following disclaimer. 9.67 + * Redistributions in binary form must reproduce the above copyright 9.68 + notice, this list of conditions and the following disclaimer in the 9.69 + documentation and/or other materials provided with the distribution. 9.70 + * Neither the name of the copyright holder nor the names of 9.71 + contributors may be used to endorse or promote products derived from 9.72 + this software without specific prior written permission. 9.73 + 9.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 9.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 9.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 9.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 9.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 9.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 9.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 9.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 9.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 9.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 9.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 9.85 +*/ 9.86 + 9.87 +package jdk.internal.dynalink; 9.88 + 9.89 +import java.lang.invoke.MutableCallSite; 9.90 +import java.util.ArrayList; 9.91 +import java.util.Arrays; 9.92 +import java.util.Collections; 9.93 +import java.util.HashSet; 9.94 +import java.util.LinkedList; 9.95 +import java.util.List; 9.96 +import java.util.Set; 9.97 +import jdk.internal.dynalink.beans.BeansLinker; 9.98 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 9.99 +import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; 9.100 +import jdk.internal.dynalink.linker.LinkRequest; 9.101 +import jdk.internal.dynalink.support.AutoDiscovery; 9.102 +import jdk.internal.dynalink.support.BottomGuardingDynamicLinker; 9.103 +import jdk.internal.dynalink.support.CompositeGuardingDynamicLinker; 9.104 +import jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker; 9.105 +import jdk.internal.dynalink.support.LinkerServicesImpl; 9.106 +import jdk.internal.dynalink.support.TypeConverterFactory; 9.107 + 9.108 + 9.109 +/** 9.110 + * A factory class for creating {@link DynamicLinker}s. The most usual dynamic linker is a linker that is a composition 9.111 + * of all {@link GuardingDynamicLinker}s known and pre-created by the caller as well as any 9.112 + * {@link AutoDiscovery automatically discovered} guarding linkers and the standard fallback {@link BeansLinker}. See 9.113 + * {@link DynamicLinker} documentation for tips on how to use this class. 9.114 + * 9.115 + * @author Attila Szegedi 9.116 + */ 9.117 +public class DynamicLinkerFactory { 9.118 + 9.119 + /** 9.120 + * Default value for {@link #setUnstableRelinkThreshold(int) unstable relink threshold}. 9.121 + */ 9.122 + public static final int DEFAULT_UNSTABLE_RELINK_THRESHOLD = 8; 9.123 + 9.124 + private ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 9.125 + private List<? extends GuardingDynamicLinker> prioritizedLinkers; 9.126 + private List<? extends GuardingDynamicLinker> fallbackLinkers; 9.127 + private int runtimeContextArgCount = 0; 9.128 + private boolean syncOnRelink = false; 9.129 + private int unstableRelinkThreshold = DEFAULT_UNSTABLE_RELINK_THRESHOLD; 9.130 + 9.131 + /** 9.132 + * Sets the class loader for automatic discovery of available linkers. If not set explicitly, then the thread 9.133 + * context class loader at the time of the constructor invocation will be used. 9.134 + * 9.135 + * @param classLoader the class loader used for the autodiscovery of available linkers. 9.136 + */ 9.137 + public void setClassLoader(ClassLoader classLoader) { 9.138 + this.classLoader = classLoader; 9.139 + } 9.140 + 9.141 + /** 9.142 + * Sets the prioritized linkers. Language runtimes using this framework will usually precreate at least the linker 9.143 + * for their own language. These linkers will be consulted first in the resulting dynamic linker, before any 9.144 + * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the prioritized 9.145 + * linkers, it will be ignored and the explicit prioritized instance will be used. 9.146 + * 9.147 + * @param prioritizedLinkers the list of prioritized linkers. Null can be passed to indicate no prioritized linkers 9.148 + * (this is also the default value). 9.149 + */ 9.150 + public void setPrioritizedLinkers(List<? extends GuardingDynamicLinker> prioritizedLinkers) { 9.151 + this.prioritizedLinkers = 9.152 + prioritizedLinkers == null ? null : new ArrayList<>(prioritizedLinkers); 9.153 + } 9.154 + 9.155 + /** 9.156 + * Sets the prioritized linkers. Language runtimes using this framework will usually precreate at least the linker 9.157 + * for their own language. These linkers will be consulted first in the resulting dynamic linker, before any 9.158 + * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the prioritized 9.159 + * linkers, it will be ignored and the explicit prioritized instance will be used. 9.160 + * 9.161 + * @param prioritizedLinkers a list of prioritized linkers. 9.162 + */ 9.163 + public void setPrioritizedLinkers(GuardingDynamicLinker... prioritizedLinkers) { 9.164 + setPrioritizedLinkers(Arrays.asList(prioritizedLinkers)); 9.165 + } 9.166 + 9.167 + /** 9.168 + * Sets a single prioritized linker. Identical to calling {@link #setPrioritizedLinkers(List)} with a single-element 9.169 + * list. 9.170 + * 9.171 + * @param prioritizedLinker the single prioritized linker. Must not be null. 9.172 + * @throws IllegalArgumentException if null is passed. 9.173 + */ 9.174 + public void setPrioritizedLinker(GuardingDynamicLinker prioritizedLinker) { 9.175 + if(prioritizedLinker == null) { 9.176 + throw new IllegalArgumentException("prioritizedLinker == null"); 9.177 + } 9.178 + this.prioritizedLinkers = Collections.singletonList(prioritizedLinker); 9.179 + } 9.180 + 9.181 + /** 9.182 + * Sets the fallback linkers. These linkers will be consulted last in the resulting composite linker, after any 9.183 + * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the fallback 9.184 + * linkers, it will be ignored and the explicit fallback instance will be used. 9.185 + * 9.186 + * @param fallbackLinkers the list of fallback linkers. Can be empty to indicate the caller wishes to set no 9.187 + * fallback linkers. 9.188 + */ 9.189 + public void setFallbackLinkers(List<? extends GuardingDynamicLinker> fallbackLinkers) { 9.190 + this.fallbackLinkers = fallbackLinkers == null ? null : new ArrayList<>(fallbackLinkers); 9.191 + } 9.192 + 9.193 + /** 9.194 + * Sets the fallback linkers. These linkers will be consulted last in the resulting composite linker, after any 9.195 + * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the fallback 9.196 + * linkers, it will be ignored and the explicit fallback instance will be used. 9.197 + * 9.198 + * @param fallbackLinkers the list of fallback linkers. Can be empty to indicate the caller wishes to set no 9.199 + * fallback linkers. If it is left as null, the standard fallback {@link BeansLinker} will be used. 9.200 + */ 9.201 + public void setFallbackLinkers(GuardingDynamicLinker... fallbackLinkers) { 9.202 + setFallbackLinkers(Arrays.asList(fallbackLinkers)); 9.203 + } 9.204 + 9.205 + /** 9.206 + * Sets the number of arguments in the call sites that represent the stack context of the language runtime creating 9.207 + * the linker. If the language runtime uses no context information passed on stack, then it should be zero 9.208 + * (the default value). If it is set to nonzero value, then every dynamic call site emitted by this runtime must 9.209 + * have the argument list of the form: {@code (this, contextArg1[, contextArg2[, ...]], normalArgs)}. It is 9.210 + * advisable to only pass one context-specific argument, though, of an easily recognizable, runtime specific type 9.211 + * encapsulating the runtime thread local state. 9.212 + * 9.213 + * @param runtimeContextArgCount the number of language runtime context arguments in call sites. 9.214 + */ 9.215 + public void setRuntimeContextArgCount(int runtimeContextArgCount) { 9.216 + if(runtimeContextArgCount < 0) { 9.217 + throw new IllegalArgumentException("runtimeContextArgCount < 0"); 9.218 + } 9.219 + this.runtimeContextArgCount = runtimeContextArgCount; 9.220 + } 9.221 + 9.222 + /** 9.223 + * Sets whether the linker created by this factory will invoke {@link MutableCallSite#syncAll(MutableCallSite[])} 9.224 + * after a call site is relinked. Defaults to false. You probably want to set it to true if your runtime supports 9.225 + * multithreaded execution of dynamically linked code. 9.226 + * @param syncOnRelink true for invoking sync on relink, false otherwise. 9.227 + */ 9.228 + public void setSyncOnRelink(boolean syncOnRelink) { 9.229 + this.syncOnRelink = syncOnRelink; 9.230 + } 9.231 + 9.232 + /** 9.233 + * Sets the unstable relink threshold; the number of times a call site is relinked after which it will be 9.234 + * considered unstable, and subsequent link requests for it will indicate this. 9.235 + * @param unstableRelinkThreshold the new threshold. Must not be less than zero. The value of zero means that 9.236 + * call sites will never be considered unstable. 9.237 + * @see LinkRequest#isCallSiteUnstable() 9.238 + */ 9.239 + public void setUnstableRelinkThreshold(int unstableRelinkThreshold) { 9.240 + if(unstableRelinkThreshold < 0) { 9.241 + throw new IllegalArgumentException("unstableRelinkThreshold < 0"); 9.242 + } 9.243 + this.unstableRelinkThreshold = unstableRelinkThreshold; 9.244 + } 9.245 + 9.246 + /** 9.247 + * Creates a new dynamic linker consisting of all the prioritized, autodiscovered, and fallback linkers. 9.248 + * 9.249 + * @return the new dynamic Linker 9.250 + */ 9.251 + public DynamicLinker createLinker() { 9.252 + // Treat nulls appropriately 9.253 + if(prioritizedLinkers == null) { 9.254 + prioritizedLinkers = Collections.emptyList(); 9.255 + } 9.256 + if(fallbackLinkers == null) { 9.257 + fallbackLinkers = Collections.singletonList(new BeansLinker()); 9.258 + } 9.259 + 9.260 + // Gather classes of all precreated (prioritized and fallback) linkers. 9.261 + // We'll filter out any discovered linkers of the same class. 9.262 + final Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses = new HashSet<>(); 9.263 + addClasses(knownLinkerClasses, prioritizedLinkers); 9.264 + addClasses(knownLinkerClasses, fallbackLinkers); 9.265 + 9.266 + final List<GuardingDynamicLinker> discovered = AutoDiscovery.loadLinkers(classLoader); 9.267 + // Now, concatenate ... 9.268 + final List<GuardingDynamicLinker> linkers = new ArrayList<>(prioritizedLinkers.size() + discovered.size() 9.269 + + fallbackLinkers.size()); 9.270 + // ... prioritized linkers, ... 9.271 + linkers.addAll(prioritizedLinkers); 9.272 + // ... filtered discovered linkers, ... 9.273 + for(GuardingDynamicLinker linker: discovered) { 9.274 + if(!knownLinkerClasses.contains(linker.getClass())) { 9.275 + linkers.add(linker); 9.276 + } 9.277 + } 9.278 + // ... and finally fallback linkers. 9.279 + linkers.addAll(fallbackLinkers); 9.280 + final List<GuardingDynamicLinker> optimized = CompositeTypeBasedGuardingDynamicLinker.optimize(linkers); 9.281 + final GuardingDynamicLinker composite; 9.282 + switch(linkers.size()) { 9.283 + case 0: { 9.284 + composite = BottomGuardingDynamicLinker.INSTANCE; 9.285 + break; 9.286 + } 9.287 + case 1: { 9.288 + composite = optimized.get(0); 9.289 + break; 9.290 + } 9.291 + default: { 9.292 + composite = new CompositeGuardingDynamicLinker(optimized); 9.293 + break; 9.294 + } 9.295 + } 9.296 + 9.297 + final List<GuardingTypeConverterFactory> typeConverters = new LinkedList<>(); 9.298 + for(GuardingDynamicLinker linker: linkers) { 9.299 + if(linker instanceof GuardingTypeConverterFactory) { 9.300 + typeConverters.add((GuardingTypeConverterFactory)linker); 9.301 + } 9.302 + } 9.303 + 9.304 + return new DynamicLinker(new LinkerServicesImpl(new TypeConverterFactory(typeConverters), composite), 9.305 + runtimeContextArgCount, syncOnRelink, unstableRelinkThreshold); 9.306 + } 9.307 + 9.308 + private static void addClasses(Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses, 9.309 + List<? extends GuardingDynamicLinker> linkers) { 9.310 + for(GuardingDynamicLinker linker: linkers) { 9.311 + knownLinkerClasses.add(linker.getClass()); 9.312 + } 9.313 + } 9.314 +} 9.315 \ No newline at end of file
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/src/jdk/internal/dynalink/MonomorphicCallSite.java Thu Feb 14 13:22:26 2013 +0100 10.3 @@ -0,0 +1,116 @@ 10.4 +/* 10.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 10.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 10.7 + * 10.8 + * This code is free software; you can redistribute it and/or modify it 10.9 + * under the terms of the GNU General Public License version 2 only, as 10.10 + * published by the Free Software Foundation. Oracle designates this 10.11 + * particular file as subject to the "Classpath" exception as provided 10.12 + * by Oracle in the LICENSE file that accompanied this code. 10.13 + * 10.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 10.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 10.17 + * version 2 for more details (a copy is included in the LICENSE file that 10.18 + * accompanied this code). 10.19 + * 10.20 + * You should have received a copy of the GNU General Public License version 10.21 + * 2 along with this work; if not, write to the Free Software Foundation, 10.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 10.23 + * 10.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 10.25 + * or visit www.oracle.com if you need additional information or have any 10.26 + * questions. 10.27 + */ 10.28 + 10.29 +/* 10.30 + * This file is available under and governed by the GNU General Public 10.31 + * License version 2 only, as published by the Free Software Foundation. 10.32 + * However, the following notice accompanied the original version of this 10.33 + * file, and Oracle licenses the original version of this file under the BSD 10.34 + * license: 10.35 + */ 10.36 +/* 10.37 + Copyright 2009-2013 Attila Szegedi 10.38 + 10.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 10.40 + and the BSD License (the "BSD License"), with licensee being free to 10.41 + choose either of the two at their discretion. 10.42 + 10.43 + You may not use this file except in compliance with either the Apache 10.44 + License or the BSD License. 10.45 + 10.46 + If you choose to use this file in compliance with the Apache License, the 10.47 + following notice applies to you: 10.48 + 10.49 + You may obtain a copy of the Apache License at 10.50 + 10.51 + http://www.apache.org/licenses/LICENSE-2.0 10.52 + 10.53 + Unless required by applicable law or agreed to in writing, software 10.54 + distributed under the License is distributed on an "AS IS" BASIS, 10.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 10.56 + implied. See the License for the specific language governing 10.57 + permissions and limitations under the License. 10.58 + 10.59 + If you choose to use this file in compliance with the BSD License, the 10.60 + following notice applies to you: 10.61 + 10.62 + Redistribution and use in source and binary forms, with or without 10.63 + modification, are permitted provided that the following conditions are 10.64 + met: 10.65 + * Redistributions of source code must retain the above copyright 10.66 + notice, this list of conditions and the following disclaimer. 10.67 + * Redistributions in binary form must reproduce the above copyright 10.68 + notice, this list of conditions and the following disclaimer in the 10.69 + documentation and/or other materials provided with the distribution. 10.70 + * Neither the name of the copyright holder nor the names of 10.71 + contributors may be used to endorse or promote products derived from 10.72 + this software without specific prior written permission. 10.73 + 10.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 10.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 10.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 10.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 10.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 10.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 10.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 10.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 10.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 10.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 10.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 10.85 +*/ 10.86 + 10.87 +package jdk.internal.dynalink; 10.88 + 10.89 +import java.lang.invoke.MethodHandle; 10.90 +import jdk.internal.dynalink.linker.GuardedInvocation; 10.91 +import jdk.internal.dynalink.support.AbstractRelinkableCallSite; 10.92 + 10.93 + 10.94 +/** 10.95 + * A relinkable call site that implements monomorphic inline caching strategy. After it linked a method, it will keep it 10.96 + * until either its guard evaluates to false, or its switchpoint is invalidated, at which time it will throw away the 10.97 + * previous linkage, and trigger relinking with its associated {@link DynamicLinker}. 10.98 + * 10.99 + * @author Attila Szegedi 10.100 + */ 10.101 +public class MonomorphicCallSite extends AbstractRelinkableCallSite { 10.102 + /** 10.103 + * Creates a new call site with monomorphic inline caching strategy. 10.104 + * @param descriptor the descriptor for this call site 10.105 + */ 10.106 + public MonomorphicCallSite(CallSiteDescriptor descriptor) { 10.107 + super(descriptor); 10.108 + } 10.109 + 10.110 + @Override 10.111 + public void relink(GuardedInvocation guardedInvocation, MethodHandle relink) { 10.112 + setTarget(guardedInvocation.compose(relink)); 10.113 + } 10.114 + 10.115 + @Override 10.116 + public void resetAndRelink(GuardedInvocation guardedInvocation, MethodHandle relink) { 10.117 + relink(guardedInvocation, relink); 10.118 + } 10.119 +} 10.120 \ No newline at end of file
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/src/jdk/internal/dynalink/NoSuchDynamicMethodException.java Thu Feb 14 13:22:26 2013 +0100 11.3 @@ -0,0 +1,103 @@ 11.4 +/* 11.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 11.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.7 + * 11.8 + * This code is free software; you can redistribute it and/or modify it 11.9 + * under the terms of the GNU General Public License version 2 only, as 11.10 + * published by the Free Software Foundation. Oracle designates this 11.11 + * particular file as subject to the "Classpath" exception as provided 11.12 + * by Oracle in the LICENSE file that accompanied this code. 11.13 + * 11.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 11.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11.17 + * version 2 for more details (a copy is included in the LICENSE file that 11.18 + * accompanied this code). 11.19 + * 11.20 + * You should have received a copy of the GNU General Public License version 11.21 + * 2 along with this work; if not, write to the Free Software Foundation, 11.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 11.23 + * 11.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 11.25 + * or visit www.oracle.com if you need additional information or have any 11.26 + * questions. 11.27 + */ 11.28 + 11.29 +/* 11.30 + * This file is available under and governed by the GNU General Public 11.31 + * License version 2 only, as published by the Free Software Foundation. 11.32 + * However, the following notice accompanied the original version of this 11.33 + * file, and Oracle licenses the original version of this file under the BSD 11.34 + * license: 11.35 + */ 11.36 +/* 11.37 + Copyright 2009-2013 Attila Szegedi 11.38 + 11.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 11.40 + and the BSD License (the "BSD License"), with licensee being free to 11.41 + choose either of the two at their discretion. 11.42 + 11.43 + You may not use this file except in compliance with either the Apache 11.44 + License or the BSD License. 11.45 + 11.46 + If you choose to use this file in compliance with the Apache License, the 11.47 + following notice applies to you: 11.48 + 11.49 + You may obtain a copy of the Apache License at 11.50 + 11.51 + http://www.apache.org/licenses/LICENSE-2.0 11.52 + 11.53 + Unless required by applicable law or agreed to in writing, software 11.54 + distributed under the License is distributed on an "AS IS" BASIS, 11.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 11.56 + implied. See the License for the specific language governing 11.57 + permissions and limitations under the License. 11.58 + 11.59 + If you choose to use this file in compliance with the BSD License, the 11.60 + following notice applies to you: 11.61 + 11.62 + Redistribution and use in source and binary forms, with or without 11.63 + modification, are permitted provided that the following conditions are 11.64 + met: 11.65 + * Redistributions of source code must retain the above copyright 11.66 + notice, this list of conditions and the following disclaimer. 11.67 + * Redistributions in binary form must reproduce the above copyright 11.68 + notice, this list of conditions and the following disclaimer in the 11.69 + documentation and/or other materials provided with the distribution. 11.70 + * Neither the name of the copyright holder nor the names of 11.71 + contributors may be used to endorse or promote products derived from 11.72 + this software without specific prior written permission. 11.73 + 11.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 11.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 11.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 11.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 11.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 11.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 11.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 11.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 11.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 11.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 11.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 11.85 +*/ 11.86 + 11.87 +package jdk.internal.dynalink; 11.88 + 11.89 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 11.90 + 11.91 +/** 11.92 + * Thrown at the invocation if the call site can not be linked by any available {@link GuardingDynamicLinker}. 11.93 + * 11.94 + * @author Attila Szegedi 11.95 + */ 11.96 +public class NoSuchDynamicMethodException extends RuntimeException { 11.97 + private static final long serialVersionUID = 1L; 11.98 + 11.99 + /** 11.100 + * Creates a new NoSuchDynamicMethodException 11.101 + * @param message the message of the exception. 11.102 + */ 11.103 + public NoSuchDynamicMethodException(String message) { 11.104 + super(message); 11.105 + } 11.106 +} 11.107 \ No newline at end of file
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/src/jdk/internal/dynalink/RelinkableCallSite.java Thu Feb 14 13:22:26 2013 +0100 12.3 @@ -0,0 +1,148 @@ 12.4 +/* 12.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 12.7 + * 12.8 + * This code is free software; you can redistribute it and/or modify it 12.9 + * under the terms of the GNU General Public License version 2 only, as 12.10 + * published by the Free Software Foundation. Oracle designates this 12.11 + * particular file as subject to the "Classpath" exception as provided 12.12 + * by Oracle in the LICENSE file that accompanied this code. 12.13 + * 12.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 12.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12.17 + * version 2 for more details (a copy is included in the LICENSE file that 12.18 + * accompanied this code). 12.19 + * 12.20 + * You should have received a copy of the GNU General Public License version 12.21 + * 2 along with this work; if not, write to the Free Software Foundation, 12.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 12.23 + * 12.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 12.25 + * or visit www.oracle.com if you need additional information or have any 12.26 + * questions. 12.27 + */ 12.28 + 12.29 +/* 12.30 + * This file is available under and governed by the GNU General Public 12.31 + * License version 2 only, as published by the Free Software Foundation. 12.32 + * However, the following notice accompanied the original version of this 12.33 + * file, and Oracle licenses the original version of this file under the BSD 12.34 + * license: 12.35 + */ 12.36 +/* 12.37 + Copyright 2009-2013 Attila Szegedi 12.38 + 12.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 12.40 + and the BSD License (the "BSD License"), with licensee being free to 12.41 + choose either of the two at their discretion. 12.42 + 12.43 + You may not use this file except in compliance with either the Apache 12.44 + License or the BSD License. 12.45 + 12.46 + If you choose to use this file in compliance with the Apache License, the 12.47 + following notice applies to you: 12.48 + 12.49 + You may obtain a copy of the Apache License at 12.50 + 12.51 + http://www.apache.org/licenses/LICENSE-2.0 12.52 + 12.53 + Unless required by applicable law or agreed to in writing, software 12.54 + distributed under the License is distributed on an "AS IS" BASIS, 12.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12.56 + implied. See the License for the specific language governing 12.57 + permissions and limitations under the License. 12.58 + 12.59 + If you choose to use this file in compliance with the BSD License, the 12.60 + following notice applies to you: 12.61 + 12.62 + Redistribution and use in source and binary forms, with or without 12.63 + modification, are permitted provided that the following conditions are 12.64 + met: 12.65 + * Redistributions of source code must retain the above copyright 12.66 + notice, this list of conditions and the following disclaimer. 12.67 + * Redistributions in binary form must reproduce the above copyright 12.68 + notice, this list of conditions and the following disclaimer in the 12.69 + documentation and/or other materials provided with the distribution. 12.70 + * Neither the name of the copyright holder nor the names of 12.71 + contributors may be used to endorse or promote products derived from 12.72 + this software without specific prior written permission. 12.73 + 12.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 12.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 12.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 12.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 12.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 12.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 12.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 12.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 12.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 12.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 12.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12.85 +*/ 12.86 + 12.87 +package jdk.internal.dynalink; 12.88 + 12.89 +import java.lang.invoke.CallSite; 12.90 +import java.lang.invoke.MethodHandle; 12.91 +import java.lang.invoke.MutableCallSite; 12.92 +import java.lang.invoke.VolatileCallSite; 12.93 +import jdk.internal.dynalink.linker.GuardedInvocation; 12.94 + 12.95 + 12.96 +/** 12.97 + * Interface for relinkable call sites. Language runtimes wishing to use this framework must use subclasses of 12.98 + * {@link CallSite} that also implement this interface as their call sites. There is a readily usable 12.99 + * {@link MonomorphicCallSite} subclass that implements monomorphic inline caching strategy as well as a 12.100 + * {@link ChainedCallSite} that retains a chain of already linked method handles. The reason this is defined as an 12.101 + * interface instead of a concrete, albeit abstract class is that it allows independent implementations to choose 12.102 + * between {@link MutableCallSite} and {@link VolatileCallSite} as they see fit. 12.103 + * 12.104 + * @author Attila Szegedi 12.105 + */ 12.106 +public interface RelinkableCallSite { 12.107 + /** 12.108 + * Initializes the relinkable call site by setting a relink-and-invoke method handle. The call site implementation 12.109 + * is supposed to set this method handle as its target. 12.110 + * @param relinkAndInvoke a relink-and-invoke method handle supplied by the {@link DynamicLinker}. 12.111 + */ 12.112 + public void initialize(MethodHandle relinkAndInvoke); 12.113 + 12.114 + /** 12.115 + * Returns the descriptor for this call site. 12.116 + * 12.117 + * @return the descriptor for this call site. 12.118 + */ 12.119 + public CallSiteDescriptor getDescriptor(); 12.120 + 12.121 + /** 12.122 + * This method will be called by the dynamic linker every time the call site is normally relinked. It will be passed 12.123 + * a {@code GuardedInvocation} that the call site should incorporate into its target method handle. When this method 12.124 + * is called, the call site is allowed to keep other non-invalidated invocations around for implementation of 12.125 + * polymorphic inline caches and compose them with this invocation to form its final target. 12.126 + * 12.127 + * @param guardedInvocation the guarded invocation that the call site should incorporate into its target method 12.128 + * handle. 12.129 + * @param fallback the fallback method. This is a method matching the method type of the call site that is supplied 12.130 + * by the {@link DynamicLinker} to be used by this call site as a fallback when it can't invoke its target with the 12.131 + * passed arguments. The fallback method is such that when it's invoked, it'll try to discover the adequate target 12.132 + * for the invocation, subsequently invoke {@link #relink(GuardedInvocation, MethodHandle)} or 12.133 + * {@link #resetAndRelink(GuardedInvocation, MethodHandle)}, and finally invoke the target. 12.134 + */ 12.135 + public void relink(GuardedInvocation guardedInvocation, MethodHandle fallback); 12.136 + 12.137 + /** 12.138 + * This method will be called by the dynamic linker every time the call site is relinked and the linker wishes the 12.139 + * call site to throw away any prior linkage state. It will be passed a {@code GuardedInvocation} that the call site 12.140 + * should use to build its target method handle. When this method is called, the call site is discouraged from 12.141 + * keeping previous state around, and is supposed to only link the current invocation. 12.142 + * 12.143 + * @param guardedInvocation the guarded invocation that the call site should use to build its target method handle. 12.144 + * @param fallback the fallback method. This is a method matching the method type of the call site that is supplied 12.145 + * by the {@link DynamicLinker} to be used by this call site as a fallback when it can't invoke its target with the 12.146 + * passed arguments. The fallback method is such that when it's invoked, it'll try to discover the adequate target 12.147 + * for the invocation, subsequently invoke {@link #relink(GuardedInvocation, MethodHandle)} or 12.148 + * {@link #resetAndRelink(GuardedInvocation, MethodHandle)}, and finally invoke the target. 12.149 + */ 12.150 + public void resetAndRelink(GuardedInvocation guardedInvocation, MethodHandle fallback); 12.151 +} 12.152 \ No newline at end of file
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/src/jdk/internal/dynalink/beans/AbstractJavaLinker.java Thu Feb 14 13:22:26 2013 +0100 13.3 @@ -0,0 +1,690 @@ 13.4 +/* 13.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 13.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 13.7 + * 13.8 + * This code is free software; you can redistribute it and/or modify it 13.9 + * under the terms of the GNU General Public License version 2 only, as 13.10 + * published by the Free Software Foundation. Oracle designates this 13.11 + * particular file as subject to the "Classpath" exception as provided 13.12 + * by Oracle in the LICENSE file that accompanied this code. 13.13 + * 13.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 13.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13.17 + * version 2 for more details (a copy is included in the LICENSE file that 13.18 + * accompanied this code). 13.19 + * 13.20 + * You should have received a copy of the GNU General Public License version 13.21 + * 2 along with this work; if not, write to the Free Software Foundation, 13.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 13.23 + * 13.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 13.25 + * or visit www.oracle.com if you need additional information or have any 13.26 + * questions. 13.27 + */ 13.28 + 13.29 +/* 13.30 + * This file is available under and governed by the GNU General Public 13.31 + * License version 2 only, as published by the Free Software Foundation. 13.32 + * However, the following notice accompanied the original version of this 13.33 + * file, and Oracle licenses the original version of this file under the BSD 13.34 + * license: 13.35 + */ 13.36 +/* 13.37 + Copyright 2009-2013 Attila Szegedi 13.38 + 13.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 13.40 + and the BSD License (the "BSD License"), with licensee being free to 13.41 + choose either of the two at their discretion. 13.42 + 13.43 + You may not use this file except in compliance with either the Apache 13.44 + License or the BSD License. 13.45 + 13.46 + If you choose to use this file in compliance with the Apache License, the 13.47 + following notice applies to you: 13.48 + 13.49 + You may obtain a copy of the Apache License at 13.50 + 13.51 + http://www.apache.org/licenses/LICENSE-2.0 13.52 + 13.53 + Unless required by applicable law or agreed to in writing, software 13.54 + distributed under the License is distributed on an "AS IS" BASIS, 13.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13.56 + implied. See the License for the specific language governing 13.57 + permissions and limitations under the License. 13.58 + 13.59 + If you choose to use this file in compliance with the BSD License, the 13.60 + following notice applies to you: 13.61 + 13.62 + Redistribution and use in source and binary forms, with or without 13.63 + modification, are permitted provided that the following conditions are 13.64 + met: 13.65 + * Redistributions of source code must retain the above copyright 13.66 + notice, this list of conditions and the following disclaimer. 13.67 + * Redistributions in binary form must reproduce the above copyright 13.68 + notice, this list of conditions and the following disclaimer in the 13.69 + documentation and/or other materials provided with the distribution. 13.70 + * Neither the name of the copyright holder nor the names of 13.71 + contributors may be used to endorse or promote products derived from 13.72 + this software without specific prior written permission. 13.73 + 13.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 13.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 13.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 13.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 13.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 13.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 13.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 13.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 13.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 13.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 13.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13.85 +*/ 13.86 + 13.87 +package jdk.internal.dynalink.beans; 13.88 + 13.89 +import java.beans.Introspector; 13.90 +import java.lang.invoke.MethodHandle; 13.91 +import java.lang.invoke.MethodHandles; 13.92 +import java.lang.invoke.MethodType; 13.93 +import java.lang.reflect.Field; 13.94 +import java.lang.reflect.Method; 13.95 +import java.lang.reflect.Modifier; 13.96 +import java.util.HashMap; 13.97 +import java.util.List; 13.98 +import java.util.Map; 13.99 +import jdk.internal.dynalink.CallSiteDescriptor; 13.100 +import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType; 13.101 +import jdk.internal.dynalink.linker.GuardedInvocation; 13.102 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 13.103 +import jdk.internal.dynalink.linker.LinkRequest; 13.104 +import jdk.internal.dynalink.linker.LinkerServices; 13.105 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 13.106 +import jdk.internal.dynalink.support.Guards; 13.107 +import jdk.internal.dynalink.support.Lookup; 13.108 + 13.109 + 13.110 +/** 13.111 + * A base class for both {@link StaticClassLinker} and {@link BeanLinker}. Deals with common aspects of property 13.112 + * exposure and method calls for both static and instance facets of a class. 13.113 + * 13.114 + * @author Attila Szegedi 13.115 + */ 13.116 +abstract class AbstractJavaLinker implements GuardingDynamicLinker { 13.117 + final Class<?> clazz; 13.118 + private final MethodHandle classGuard; 13.119 + private final MethodHandle assignableGuard; 13.120 + private final Map<String, AnnotatedMethodHandle> propertyGetters = new HashMap<>(); 13.121 + private final Map<String, DynamicMethod> propertySetters = new HashMap<>(); 13.122 + private final Map<String, DynamicMethod> methods = new HashMap<>(); 13.123 + 13.124 + AbstractJavaLinker(Class<?> clazz, MethodHandle classGuard) { 13.125 + this(clazz, classGuard, classGuard); 13.126 + } 13.127 + 13.128 + AbstractJavaLinker(Class<?> clazz, MethodHandle classGuard, MethodHandle assignableGuard) { 13.129 + this.clazz = clazz; 13.130 + this.classGuard = classGuard; 13.131 + this.assignableGuard = assignableGuard; 13.132 + 13.133 + final FacetIntrospector introspector = createFacetIntrospector(); 13.134 + try { 13.135 + // Add methods and properties 13.136 + for(Method method: introspector.getMethods()) { 13.137 + final String name = method.getName(); 13.138 + final MethodHandle methodHandle = introspector.unreflect(method); 13.139 + // Add method 13.140 + addMember(name, methodHandle, methods); 13.141 + // Add the method as a property getter and/or setter 13.142 + if(name.startsWith("get") && name.length() > 3 && method.getParameterTypes().length == 0) { 13.143 + // Property getter 13.144 + setPropertyGetter(Introspector.decapitalize(name.substring(3)), introspector.unreflect( 13.145 + getMostGenericGetter(method)), ValidationType.INSTANCE_OF); 13.146 + } else if(name.startsWith("is") && name.length() > 2 && method.getParameterTypes().length == 0 && 13.147 + method.getReturnType() == boolean.class) { 13.148 + // Boolean property getter 13.149 + setPropertyGetter(Introspector.decapitalize(name.substring(2)), introspector.unreflect( 13.150 + getMostGenericGetter(method)), ValidationType.INSTANCE_OF); 13.151 + } else if(name.startsWith("set") && name.length() > 3 && method.getParameterTypes().length == 1) { 13.152 + // Property setter 13.153 + addMember(Introspector.decapitalize(name.substring(3)), methodHandle, propertySetters); 13.154 + } 13.155 + } 13.156 + 13.157 + // Add field getter/setters as property getters/setters. 13.158 + for(Field field: introspector.getFields()) { 13.159 + final String name = field.getName(); 13.160 + // Only add a property getter when one is not defined already as a getXxx()/isXxx() method. 13.161 + if(!propertyGetters.containsKey(name)) { 13.162 + setPropertyGetter(name, introspector.unreflectGetter(field), ValidationType.EXACT_CLASS); 13.163 + } 13.164 + if(!(Modifier.isFinal(field.getModifiers()) || propertySetters.containsKey(name))) { 13.165 + addMember(name, introspector.unreflectSetter(field), propertySetters); 13.166 + } 13.167 + } 13.168 + 13.169 + // Add inner classes, but only those for which we don't hide a property with it 13.170 + for(Map.Entry<String, MethodHandle> innerClassSpec: introspector.getInnerClassGetters().entrySet()) { 13.171 + final String name = innerClassSpec.getKey(); 13.172 + if(!propertyGetters.containsKey(name)) { 13.173 + setPropertyGetter(name, innerClassSpec.getValue(), ValidationType.EXACT_CLASS); 13.174 + } 13.175 + } 13.176 + } finally { 13.177 + introspector.close(); 13.178 + } 13.179 + } 13.180 + 13.181 + abstract FacetIntrospector createFacetIntrospector(); 13.182 + 13.183 + void setPropertyGetter(String name, MethodHandle handle, ValidationType validationType) { 13.184 + propertyGetters.put(name, new AnnotatedMethodHandle(handle, validationType)); 13.185 + } 13.186 + 13.187 + private void addMember(String name, MethodHandle mh, Map<String, DynamicMethod> methodMap) { 13.188 + final DynamicMethod existingMethod = methodMap.get(name); 13.189 + final DynamicMethod newMethod = addMember(mh, existingMethod, clazz, name); 13.190 + if(newMethod != existingMethod) { 13.191 + methodMap.put(name, newMethod); 13.192 + } 13.193 + } 13.194 + 13.195 + static DynamicMethod createDynamicMethod(Iterable<MethodHandle> methodHandles, Class<?> clazz, String name) { 13.196 + DynamicMethod dynMethod = null; 13.197 + for(MethodHandle methodHandle: methodHandles) { 13.198 + dynMethod = addMember(methodHandle, dynMethod, clazz, name); 13.199 + } 13.200 + return dynMethod; 13.201 + } 13.202 + 13.203 + private static DynamicMethod addMember(MethodHandle mh, DynamicMethod existing, Class<?> clazz, String name) { 13.204 + if(existing == null) { 13.205 + return new SimpleDynamicMethod(mh, clazz, name); 13.206 + } else if(existing.contains(mh)) { 13.207 + return existing; 13.208 + } else if(existing instanceof SimpleDynamicMethod) { 13.209 + final OverloadedDynamicMethod odm = new OverloadedDynamicMethod(clazz, name); 13.210 + odm.addMethod(((SimpleDynamicMethod)existing)); 13.211 + odm.addMethod(mh); 13.212 + return odm; 13.213 + } else if(existing instanceof OverloadedDynamicMethod) { 13.214 + ((OverloadedDynamicMethod)existing).addMethod(mh); 13.215 + return existing; 13.216 + } 13.217 + throw new AssertionError(); 13.218 + } 13.219 + 13.220 + @Override 13.221 + public GuardedInvocation getGuardedInvocation(LinkRequest request, final LinkerServices linkerServices) 13.222 + throws Exception { 13.223 + final LinkRequest ncrequest = request.withoutRuntimeContext(); 13.224 + // BeansLinker already checked that the name is at least 2 elements long and the first element is "dyn". 13.225 + final CallSiteDescriptor callSiteDescriptor = ncrequest.getCallSiteDescriptor(); 13.226 + final String op = callSiteDescriptor.getNameToken(CallSiteDescriptor.OPERATOR); 13.227 + // Either dyn:callMethod:name(this[,args]) or dyn:callMethod(this,name[,args]). 13.228 + if("callMethod" == op) { 13.229 + return getCallPropWithThis(callSiteDescriptor, linkerServices); 13.230 + } 13.231 + List<String> operations = CallSiteDescriptorFactory.tokenizeOperators(callSiteDescriptor); 13.232 + while(!operations.isEmpty()) { 13.233 + final GuardedInvocationComponent gic = getGuardedInvocationComponent(callSiteDescriptor, linkerServices, 13.234 + operations); 13.235 + if(gic != null) { 13.236 + return gic.getGuardedInvocation(); 13.237 + } 13.238 + operations = pop(operations); 13.239 + } 13.240 + return null; 13.241 + } 13.242 + 13.243 + protected GuardedInvocationComponent getGuardedInvocationComponent(CallSiteDescriptor callSiteDescriptor, 13.244 + LinkerServices linkerServices, List<String> operations) throws Exception { 13.245 + if(operations.isEmpty()) { 13.246 + return null; 13.247 + } 13.248 + final String op = operations.get(0); 13.249 + // Either dyn:getProp:name(this) or dyn:getProp(this, name) 13.250 + if("getProp".equals(op)) { 13.251 + return getPropertyGetter(callSiteDescriptor, linkerServices, pop(operations)); 13.252 + } 13.253 + // Either dyn:setProp:name(this, value) or dyn:setProp(this, name, value) 13.254 + if("setProp".equals(op)) { 13.255 + return getPropertySetter(callSiteDescriptor, linkerServices, pop(operations)); 13.256 + } 13.257 + // Either dyn:getMethod:name(this), or dyn:getMethod(this, name) 13.258 + if("getMethod".equals(op)) { 13.259 + return getMethodGetter(callSiteDescriptor, linkerServices, pop(operations)); 13.260 + } 13.261 + return null; 13.262 + } 13.263 + 13.264 + static final <T> List<T> pop(List<T> l) { 13.265 + return l.subList(1, l.size()); 13.266 + } 13.267 + 13.268 + MethodHandle getClassGuard(CallSiteDescriptor desc) { 13.269 + return getClassGuard(desc.getMethodType()); 13.270 + } 13.271 + 13.272 + MethodHandle getClassGuard(MethodType type) { 13.273 + return Guards.asType(classGuard, type); 13.274 + } 13.275 + 13.276 + GuardedInvocationComponent getClassGuardedInvocationComponent(MethodHandle invocation, MethodType type) { 13.277 + return new GuardedInvocationComponent(invocation, getClassGuard(type), clazz, ValidationType.EXACT_CLASS); 13.278 + } 13.279 + 13.280 + private MethodHandle getAssignableGuard(MethodType type) { 13.281 + return Guards.asType(assignableGuard, type); 13.282 + } 13.283 + 13.284 + private GuardedInvocation getCallPropWithThis(CallSiteDescriptor callSiteDescriptor, LinkerServices linkerServices) { 13.285 + switch(callSiteDescriptor.getNameTokenCount()) { 13.286 + case 3: { 13.287 + return createGuardedDynamicMethodInvocation(callSiteDescriptor.getMethodType(), linkerServices, 13.288 + callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), methods); 13.289 + } 13.290 + default: { 13.291 + return null; 13.292 + } 13.293 + } 13.294 + } 13.295 + 13.296 + private GuardedInvocation createGuardedDynamicMethodInvocation(MethodType callSiteType, 13.297 + LinkerServices linkerServices, String methodName, Map<String, DynamicMethod> methodMap){ 13.298 + final MethodHandle inv = getDynamicMethodInvocation(callSiteType, linkerServices, methodName, methodMap); 13.299 + return inv == null ? null : new GuardedInvocation(inv, getClassGuard(callSiteType)); 13.300 + } 13.301 + 13.302 + private static MethodHandle getDynamicMethodInvocation(MethodType callSiteType, LinkerServices linkerServices, 13.303 + String methodName, Map<String, DynamicMethod> methodMap) { 13.304 + final DynamicMethod dynaMethod = getDynamicMethod(methodName, methodMap); 13.305 + return dynaMethod != null ? dynaMethod.getInvocation(callSiteType, linkerServices) : null; 13.306 + } 13.307 + 13.308 + private static DynamicMethod getDynamicMethod(String methodName, Map<String, DynamicMethod> methodMap) { 13.309 + final DynamicMethod dynaMethod = methodMap.get(methodName); 13.310 + return dynaMethod != null ? dynaMethod : getExplicitSignatureDynamicMethod(methodName, methodMap); 13.311 + } 13.312 + 13.313 + private static SimpleDynamicMethod getExplicitSignatureDynamicMethod(String methodName, 13.314 + Map<String, DynamicMethod> methodsMap) { 13.315 + // What's below is meant to support the "name(type, type, ...)" syntax that programmers can use in a method name 13.316 + // to manually pin down an exact overloaded variant. This is not usually required, as the overloaded method 13.317 + // resolution works correctly in almost every situation. However, in presence of many language-specific 13.318 + // conversions with a radically dynamic language, most overloaded methods will end up being constantly selected 13.319 + // at invocation time, so a programmer knowledgable of the situation might choose to pin down an exact overload 13.320 + // for performance reasons. 13.321 + 13.322 + // Is the method name lexically of the form "name(types)"? 13.323 + final int lastChar = methodName.length() - 1; 13.324 + if(methodName.charAt(lastChar) != ')') { 13.325 + return null; 13.326 + } 13.327 + final int openBrace = methodName.indexOf('('); 13.328 + if(openBrace == -1) { 13.329 + return null; 13.330 + } 13.331 + 13.332 + // Find an existing method for the "name" part 13.333 + final DynamicMethod simpleNamedMethod = methodsMap.get(methodName.substring(0, openBrace)); 13.334 + if(simpleNamedMethod == null) { 13.335 + return null; 13.336 + } 13.337 + 13.338 + // Try to get a narrowed dynamic method for the explicit parameter types. 13.339 + return simpleNamedMethod.getMethodForExactParamTypes(methodName.substring(openBrace + 1, lastChar)); 13.340 + } 13.341 + 13.342 + private static final MethodHandle IS_METHOD_HANDLE_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType( 13.343 + boolean.class, MethodHandle.class)); 13.344 + private static final MethodHandle CONSTANT_NULL_DROP_METHOD_HANDLE = MethodHandles.dropArguments( 13.345 + MethodHandles.constant(Object.class, null), 0, MethodHandle.class); 13.346 + 13.347 + private GuardedInvocationComponent getPropertySetter(CallSiteDescriptor callSiteDescriptor, 13.348 + LinkerServices linkerServices, List<String> operations) throws Exception { 13.349 + final MethodType type = callSiteDescriptor.getMethodType(); 13.350 + switch(callSiteDescriptor.getNameTokenCount()) { 13.351 + case 2: { 13.352 + // Must have three arguments: target object, property name, and property value. 13.353 + assertParameterCount(callSiteDescriptor, 3); 13.354 + 13.355 + // What's below is basically: 13.356 + // foldArguments(guardWithTest(isNotNull, invoke, null|nextComponent.invocation), 13.357 + // get_setter_handle(type, linkerServices)) 13.358 + // only with a bunch of method signature adjustments. Basically, retrieve method setter 13.359 + // MethodHandle; if it is non-null, invoke it, otherwise either return null, or delegate to next 13.360 + // component's invocation. 13.361 + 13.362 + // Call site type is "ret_type(object_type,property_name_type,property_value_type)", which we'll 13.363 + // abbreviate to R(O, N, V) going forward. 13.364 + // We want setters that conform to "R(O, V)" 13.365 + final MethodType setterType = type.dropParameterTypes(1, 2); 13.366 + // Bind property setter handle to the expected setter type and linker services. Type is 13.367 + // MethodHandle(Object, String, Object) 13.368 + final MethodHandle boundGetter = MethodHandles.insertArguments(getPropertySetterHandle, 0, setterType, 13.369 + linkerServices); 13.370 + 13.371 + // Cast getter to MethodHandle(O, N, V) 13.372 + final MethodHandle typedGetter = linkerServices.asType(boundGetter, type.changeReturnType( 13.373 + MethodHandle.class)); 13.374 + 13.375 + // Handle to invoke the setter R(MethodHandle, O, V) 13.376 + final MethodHandle invokeHandle = MethodHandles.exactInvoker(setterType); 13.377 + // Handle to invoke the setter, dropping unnecessary fold arguments R(MethodHandle, O, N, V) 13.378 + final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandle, 2, type.parameterType( 13.379 + 1)); 13.380 + final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor, 13.381 + linkerServices, operations); 13.382 + 13.383 + final MethodHandle fallbackFolded; 13.384 + if(nextComponent == null) { 13.385 + // Object(MethodHandle)->R(MethodHandle, O, N, V); returns constant null 13.386 + fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_METHOD_HANDLE, 1, 13.387 + type.parameterList()).asType(type.insertParameterTypes(0, MethodHandle.class)); 13.388 + } else { 13.389 + // R(O, N, V)->R(MethodHandle, O, N, V); adapts the next component's invocation to drop the 13.390 + // extra argument resulting from fold 13.391 + fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(), 13.392 + 0, MethodHandle.class); 13.393 + } 13.394 + 13.395 + // fold(R(MethodHandle, O, N, V), MethodHandle(O, N, V)) 13.396 + final MethodHandle compositeSetter = MethodHandles.foldArguments(MethodHandles.guardWithTest( 13.397 + IS_METHOD_HANDLE_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter); 13.398 + if(nextComponent == null) { 13.399 + return getClassGuardedInvocationComponent(compositeSetter, type); 13.400 + } else { 13.401 + return nextComponent.compose(compositeSetter, getClassGuard(type), clazz, 13.402 + ValidationType.EXACT_CLASS); 13.403 + } 13.404 + } 13.405 + case 3: { 13.406 + // Must have two arguments: target object and property value 13.407 + assertParameterCount(callSiteDescriptor, 2); 13.408 + final GuardedInvocation gi = createGuardedDynamicMethodInvocation(callSiteDescriptor.getMethodType(), 13.409 + linkerServices, callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), 13.410 + propertySetters); 13.411 + // If we have a property setter with this name, this composite operation will always stop here 13.412 + if(gi != null) { 13.413 + return new GuardedInvocationComponent(gi, clazz, ValidationType.EXACT_CLASS); 13.414 + } 13.415 + // If we don't have a property setter with this name, always fall back to the next operation in the 13.416 + // composite (if any) 13.417 + return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, operations); 13.418 + } 13.419 + default: { 13.420 + // More than two name components; don't know what to do with it. 13.421 + return null; 13.422 + } 13.423 + } 13.424 + } 13.425 + 13.426 + private static final Lookup privateLookup = new Lookup(MethodHandles.lookup()); 13.427 + 13.428 + private static final MethodHandle IS_ANNOTATED_HANDLE_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType( 13.429 + boolean.class, AnnotatedMethodHandle.class)); 13.430 + private static final MethodHandle CONSTANT_NULL_DROP_ANNOTATED_HANDLE = MethodHandles.dropArguments( 13.431 + MethodHandles.constant(Object.class, null), 0, AnnotatedMethodHandle.class); 13.432 + private static final MethodHandle GET_ANNOTATED_HANDLE = privateLookup.findGetter(AnnotatedMethodHandle.class, 13.433 + "handle", MethodHandle.class); 13.434 + private static final MethodHandle GENERIC_PROPERTY_GETTER_HANDLER_INVOKER = MethodHandles.filterArguments( 13.435 + MethodHandles.invoker(MethodType.methodType(Object.class, Object.class)), 0, GET_ANNOTATED_HANDLE); 13.436 + 13.437 + private GuardedInvocationComponent getPropertyGetter(CallSiteDescriptor callSiteDescriptor, 13.438 + LinkerServices linkerServices, List<String> ops) throws Exception { 13.439 + final MethodType type = callSiteDescriptor.getMethodType(); 13.440 + switch(callSiteDescriptor.getNameTokenCount()) { 13.441 + case 2: { 13.442 + // Must have exactly two arguments: receiver and name 13.443 + assertParameterCount(callSiteDescriptor, 2); 13.444 + 13.445 + // What's below is basically: 13.446 + // foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle) 13.447 + // only with a bunch of method signature adjustments. Basically, retrieve method getter 13.448 + // AnnotatedMethodHandle; if it is non-null, invoke its "handle" field, otherwise either return null, 13.449 + // or delegate to next component's invocation. 13.450 + 13.451 + final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType( 13.452 + AnnotatedMethodHandle.class)); 13.453 + // Object(AnnotatedMethodHandle, Object)->R(AnnotatedMethodHandle, T0) 13.454 + final MethodHandle invokeHandleTyped = linkerServices.asType(GENERIC_PROPERTY_GETTER_HANDLER_INVOKER, 13.455 + MethodType.methodType(type.returnType(), AnnotatedMethodHandle.class, type.parameterType(0))); 13.456 + // Since it's in the target of a fold, drop the unnecessary second argument 13.457 + // R(AnnotatedMethodHandle, T0)->R(AnnotatedMethodHandle, T0, T1) 13.458 + final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2, 13.459 + type.parameterType(1)); 13.460 + final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor, 13.461 + linkerServices, ops); 13.462 + 13.463 + final MethodHandle fallbackFolded; 13.464 + if(nextComponent == null) { 13.465 + // Object(AnnotatedMethodHandle)->R(AnnotatedMethodHandle, T0, T1); returns constant null 13.466 + fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_HANDLE, 1, 13.467 + type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedMethodHandle.class)); 13.468 + } else { 13.469 + // R(T0, T1)->R(AnnotatedMethodHAndle, T0, T1); adapts the next component's invocation to drop the 13.470 + // extra argument resulting from fold 13.471 + fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(), 13.472 + 0, AnnotatedMethodHandle.class); 13.473 + } 13.474 + 13.475 + // fold(R(AnnotatedMethodHandle, T0, T1), AnnotatedMethodHandle(T0, T1)) 13.476 + final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest( 13.477 + IS_ANNOTATED_HANDLE_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter); 13.478 + if(nextComponent == null) { 13.479 + return getClassGuardedInvocationComponent(compositeGetter, type); 13.480 + } else { 13.481 + return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, 13.482 + ValidationType.EXACT_CLASS); 13.483 + } 13.484 + } 13.485 + case 3: { 13.486 + // Must have exactly one argument: receiver 13.487 + assertParameterCount(callSiteDescriptor, 1); 13.488 + // Fixed name 13.489 + final AnnotatedMethodHandle annGetter = propertyGetters.get(callSiteDescriptor.getNameToken( 13.490 + CallSiteDescriptor.NAME_OPERAND)); 13.491 + if(annGetter == null) { 13.492 + // We have no such property, always delegate to the next component operation 13.493 + return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops); 13.494 + } 13.495 + final MethodHandle getter = annGetter.handle; 13.496 + // NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being 13.497 + // overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the 13.498 + // method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If 13.499 + // we're linking against a field getter, don't make the assumption. 13.500 + // NOTE: No delegation to the next component operation if we have a property with this name, even if its 13.501 + // value is null. 13.502 + final ValidationType validationType = annGetter.validationType; 13.503 + return new GuardedInvocationComponent(linkerServices.asType(getter, type), getGuard(validationType, 13.504 + type), clazz, validationType); 13.505 + } 13.506 + default: { 13.507 + // Can't do anything with more than 3 name components 13.508 + return null; 13.509 + } 13.510 + } 13.511 + } 13.512 + 13.513 + private MethodHandle getGuard(ValidationType validationType, MethodType methodType) { 13.514 + switch(validationType) { 13.515 + case EXACT_CLASS: { 13.516 + return getClassGuard(methodType); 13.517 + } 13.518 + case INSTANCE_OF: { 13.519 + return getAssignableGuard(methodType); 13.520 + } 13.521 + case IS_ARRAY: { 13.522 + return Guards.isArray(0, methodType); 13.523 + } 13.524 + case NONE: { 13.525 + return null; 13.526 + } 13.527 + } 13.528 + throw new AssertionError(); 13.529 + } 13.530 + 13.531 + private static final MethodHandle IS_DYNAMIC_METHOD_NOT_NULL = Guards.asType(Guards.isNotNull(), 13.532 + MethodType.methodType(boolean.class, DynamicMethod.class)); 13.533 + private static final MethodHandle DYNAMIC_METHOD_IDENTITY = MethodHandles.identity(DynamicMethod.class); 13.534 + 13.535 + private GuardedInvocationComponent getMethodGetter(CallSiteDescriptor callSiteDescriptor, 13.536 + LinkerServices linkerServices, List<String> ops) throws Exception { 13.537 + final MethodType type = callSiteDescriptor.getMethodType(); 13.538 + switch(callSiteDescriptor.getNameTokenCount()) { 13.539 + case 2: { 13.540 + // Must have exactly two arguments: receiver and name 13.541 + assertParameterCount(callSiteDescriptor, 2); 13.542 + final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor, 13.543 + linkerServices, ops); 13.544 + if(nextComponent == null) { 13.545 + // No next component operation; just return a component for this operation. 13.546 + return getClassGuardedInvocationComponent(linkerServices.asType(getDynamicMethod, type), type); 13.547 + } else { 13.548 + // What's below is basically: 13.549 + // foldArguments(guardWithTest(isNotNull, identity, nextComponent.invocation), getter) 13.550 + // only with a bunch of method signature adjustments. Basically, execute method getter; if 13.551 + // it returns a non-null DynamicMethod, use identity to return it, otherwise delegate to 13.552 + // nextComponent's invocation. 13.553 + 13.554 + final MethodHandle typedGetter = linkerServices.asType(getDynamicMethod, type.changeReturnType( 13.555 + DynamicMethod.class)); 13.556 + // Since it is part of the foldArgument() target, it will have extra args that we need to drop. 13.557 + final MethodHandle returnMethodHandle = linkerServices.asType(MethodHandles.dropArguments( 13.558 + DYNAMIC_METHOD_IDENTITY, 1, type.parameterList()), type.insertParameterTypes(0, 13.559 + DynamicMethod.class)); 13.560 + final MethodHandle nextComponentInvocation = nextComponent.getGuardedInvocation().getInvocation(); 13.561 + // The assumption is that getGuardedInvocationComponent() already asType()'d it correctly 13.562 + assert nextComponentInvocation.type().equals(type); 13.563 + // Since it is part of the foldArgument() target, we have to drop an extra arg it receives. 13.564 + final MethodHandle nextCombinedInvocation = MethodHandles.dropArguments(nextComponentInvocation, 0, 13.565 + DynamicMethod.class); 13.566 + // Assemble it all into a fold(guard(isNotNull, identity, nextInvocation), get) 13.567 + final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest( 13.568 + IS_DYNAMIC_METHOD_NOT_NULL, returnMethodHandle, nextCombinedInvocation), typedGetter); 13.569 + 13.570 + return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, 13.571 + ValidationType.EXACT_CLASS); 13.572 + } 13.573 + } 13.574 + case 3: { 13.575 + // Must have exactly one argument: receiver 13.576 + assertParameterCount(callSiteDescriptor, 1); 13.577 + final DynamicMethod method = getDynamicMethod(callSiteDescriptor.getNameToken( 13.578 + CallSiteDescriptor.NAME_OPERAND)); 13.579 + if(method == null) { 13.580 + // We have no such method, always delegate to the next component 13.581 + return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops); 13.582 + } 13.583 + // No delegation to the next component of the composite operation; if we have a method with that name, 13.584 + // we'll always return it at this point. 13.585 + return getClassGuardedInvocationComponent(linkerServices.asType(MethodHandles.dropArguments( 13.586 + MethodHandles.constant(DynamicMethod.class, method), 0, type.parameterType(0)), type), type); 13.587 + } 13.588 + default: { 13.589 + // Can't do anything with more than 3 name components 13.590 + return null; 13.591 + } 13.592 + } 13.593 + } 13.594 + 13.595 + private static void assertParameterCount(CallSiteDescriptor descriptor, int paramCount) { 13.596 + if(descriptor.getMethodType().parameterCount() != paramCount) { 13.597 + throw new BootstrapMethodError(descriptor.getName() + " must have exactly " + paramCount + " parameters."); 13.598 + } 13.599 + } 13.600 + 13.601 + private static MethodHandle GET_PROPERTY_GETTER_HANDLE = MethodHandles.dropArguments(privateLookup.findOwnSpecial( 13.602 + "getPropertyGetterHandle", Object.class, Object.class), 1, Object.class); 13.603 + private final MethodHandle getPropertyGetterHandle = GET_PROPERTY_GETTER_HANDLE.bindTo(this); 13.604 + 13.605 + /** 13.606 + * @param id the property ID 13.607 + * @return the method handle for retrieving the property, or null if the property does not exist 13.608 + */ 13.609 + @SuppressWarnings("unused") 13.610 + private Object getPropertyGetterHandle(Object id) { 13.611 + return propertyGetters.get(id); 13.612 + } 13.613 + 13.614 + // Type is MethodHandle(BeanLinker, MethodType, LinkerServices, Object, String, Object), of which the two "Object" 13.615 + // args are dropped; this makes handles with first three args conform to "Object, String, Object" though, which is 13.616 + // a typical property setter with variable name signature (target, name, value). 13.617 + private static final MethodHandle GET_PROPERTY_SETTER_HANDLE = MethodHandles.dropArguments(MethodHandles.dropArguments( 13.618 + privateLookup.findOwnSpecial("getPropertySetterHandle", MethodHandle.class, MethodType.class, 13.619 + LinkerServices.class, Object.class), 3, Object.class), 5, Object.class); 13.620 + // Type is MethodHandle(MethodType, LinkerServices, Object, String, Object) 13.621 + private final MethodHandle getPropertySetterHandle = GET_PROPERTY_SETTER_HANDLE.bindTo(this); 13.622 + 13.623 + @SuppressWarnings("unused") 13.624 + private MethodHandle getPropertySetterHandle(MethodType setterType, LinkerServices linkerServices, Object id) { 13.625 + return getDynamicMethodInvocation(setterType, linkerServices, String.valueOf(id), propertySetters); 13.626 + } 13.627 + 13.628 + private static MethodHandle GET_DYNAMIC_METHOD = MethodHandles.dropArguments(privateLookup.findOwnSpecial( 13.629 + "getDynamicMethod", DynamicMethod.class, Object.class), 1, Object.class); 13.630 + private final MethodHandle getDynamicMethod = GET_DYNAMIC_METHOD.bindTo(this); 13.631 + 13.632 + @SuppressWarnings("unused") 13.633 + private DynamicMethod getDynamicMethod(Object name) { 13.634 + return getDynamicMethod(String.valueOf(name), methods); 13.635 + } 13.636 + 13.637 + /** 13.638 + * Returns a dynamic method of the specified name. 13.639 + * 13.640 + * @param name name of the method 13.641 + * @return the dynamic method (either {@link SimpleDynamicMethod} or {@link OverloadedDynamicMethod}, or null if the 13.642 + * method with the specified name does not exist. 13.643 + */ 13.644 + public DynamicMethod getDynamicMethod(String name) { 13.645 + return getDynamicMethod(name, methods); 13.646 + } 13.647 + 13.648 + /** 13.649 + * Find the most generic superclass that declares this getter. Since getters have zero args (aside from the 13.650 + * receiver), they can't be overloaded, so we're free to link with an instanceof guard for the most generic one, 13.651 + * creating more stable call sites. 13.652 + * @param getter the getter 13.653 + * @return getter with same name, declared on the most generic superclass/interface of the declaring class 13.654 + */ 13.655 + private static Method getMostGenericGetter(Method getter) { 13.656 + return getMostGenericGetter(getter.getName(), getter.getReturnType(), getter.getDeclaringClass()); 13.657 + } 13.658 + 13.659 + private static Method getMostGenericGetter(String name, Class<?> returnType, Class<?> declaringClass) { 13.660 + if(declaringClass == null) { 13.661 + return null; 13.662 + } 13.663 + // Prefer interfaces 13.664 + for(Class<?> itf: declaringClass.getInterfaces()) { 13.665 + final Method itfGetter = getMostGenericGetter(name, returnType, itf); 13.666 + if(itfGetter != null) { 13.667 + return itfGetter; 13.668 + } 13.669 + } 13.670 + final Method superGetter = getMostGenericGetter(name, returnType, declaringClass.getSuperclass()); 13.671 + if(superGetter != null) { 13.672 + return superGetter; 13.673 + } 13.674 + if(!CheckRestrictedPackage.isRestrictedClass(declaringClass)) { 13.675 + try { 13.676 + return declaringClass.getMethod(name); 13.677 + } catch(NoSuchMethodException e) { 13.678 + // Intentionally ignored, meant to fall through 13.679 + } 13.680 + } 13.681 + return null; 13.682 + } 13.683 + 13.684 + private static final class AnnotatedMethodHandle { 13.685 + final MethodHandle handle; 13.686 + /*private*/ final ValidationType validationType; 13.687 + 13.688 + AnnotatedMethodHandle(MethodHandle handle, ValidationType validationType) { 13.689 + this.handle = handle; 13.690 + this.validationType = validationType; 13.691 + } 13.692 + } 13.693 +} 13.694 \ No newline at end of file
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/src/jdk/internal/dynalink/beans/AccessibleMembersLookup.java Thu Feb 14 13:22:26 2013 +0100 14.3 @@ -0,0 +1,262 @@ 14.4 +/* 14.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 14.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 14.7 + * 14.8 + * This code is free software; you can redistribute it and/or modify it 14.9 + * under the terms of the GNU General Public License version 2 only, as 14.10 + * published by the Free Software Foundation. Oracle designates this 14.11 + * particular file as subject to the "Classpath" exception as provided 14.12 + * by Oracle in the LICENSE file that accompanied this code. 14.13 + * 14.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 14.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14.17 + * version 2 for more details (a copy is included in the LICENSE file that 14.18 + * accompanied this code). 14.19 + * 14.20 + * You should have received a copy of the GNU General Public License version 14.21 + * 2 along with this work; if not, write to the Free Software Foundation, 14.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 14.23 + * 14.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 14.25 + * or visit www.oracle.com if you need additional information or have any 14.26 + * questions. 14.27 + */ 14.28 + 14.29 +/* 14.30 + * This file is available under and governed by the GNU General Public 14.31 + * License version 2 only, as published by the Free Software Foundation. 14.32 + * However, the following notice accompanied the original version of this 14.33 + * file, and Oracle licenses the original version of this file under the BSD 14.34 + * license: 14.35 + */ 14.36 +/* 14.37 + Copyright 2009-2013 Attila Szegedi 14.38 + 14.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 14.40 + and the BSD License (the "BSD License"), with licensee being free to 14.41 + choose either of the two at their discretion. 14.42 + 14.43 + You may not use this file except in compliance with either the Apache 14.44 + License or the BSD License. 14.45 + 14.46 + If you choose to use this file in compliance with the Apache License, the 14.47 + following notice applies to you: 14.48 + 14.49 + You may obtain a copy of the Apache License at 14.50 + 14.51 + http://www.apache.org/licenses/LICENSE-2.0 14.52 + 14.53 + Unless required by applicable law or agreed to in writing, software 14.54 + distributed under the License is distributed on an "AS IS" BASIS, 14.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 14.56 + implied. See the License for the specific language governing 14.57 + permissions and limitations under the License. 14.58 + 14.59 + If you choose to use this file in compliance with the BSD License, the 14.60 + following notice applies to you: 14.61 + 14.62 + Redistribution and use in source and binary forms, with or without 14.63 + modification, are permitted provided that the following conditions are 14.64 + met: 14.65 + * Redistributions of source code must retain the above copyright 14.66 + notice, this list of conditions and the following disclaimer. 14.67 + * Redistributions in binary form must reproduce the above copyright 14.68 + notice, this list of conditions and the following disclaimer in the 14.69 + documentation and/or other materials provided with the distribution. 14.70 + * Neither the name of the copyright holder nor the names of 14.71 + contributors may be used to endorse or promote products derived from 14.72 + this software without specific prior written permission. 14.73 + 14.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 14.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 14.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 14.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 14.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 14.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 14.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 14.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 14.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 14.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 14.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14.85 +*/ 14.86 + 14.87 +package jdk.internal.dynalink.beans; 14.88 + 14.89 +import java.lang.reflect.Method; 14.90 +import java.lang.reflect.Modifier; 14.91 +import java.util.Arrays; 14.92 +import java.util.Collection; 14.93 +import java.util.HashMap; 14.94 +import java.util.LinkedHashSet; 14.95 +import java.util.Map; 14.96 +import java.util.Set; 14.97 + 14.98 +/** 14.99 + * Utility class for discovering accessible methods and inner classes. Normally, a public member declared on a class is 14.100 + * accessible (that is, it can be invoked from anywhere). However, this is not the case if the class itself is not 14.101 + * public, or belongs to a restricted-access package. In that case, it is required to lookup a member in a publicly 14.102 + * accessible superclass or implemented interface of the class, and use it instead of the member discovered on the 14.103 + * class. 14.104 + * 14.105 + * @author Attila Szegedi 14.106 + */ 14.107 +class AccessibleMembersLookup { 14.108 + private final Map<MethodSignature, Method> methods; 14.109 + private final Set<Class<?>> innerClasses; 14.110 + private boolean instance; 14.111 + 14.112 + /** 14.113 + * Creates a mapping for all accessible methods and inner classes on a class. 14.114 + * 14.115 + * @param clazz the inspected class 14.116 + * @param instance true to inspect instance methods, false to inspect static methods. 14.117 + */ 14.118 + AccessibleMembersLookup(final Class<?> clazz, boolean instance) { 14.119 + this.methods = new HashMap<>(); 14.120 + this.innerClasses = new LinkedHashSet<>(); 14.121 + this.instance = instance; 14.122 + lookupAccessibleMembers(clazz); 14.123 + } 14.124 + 14.125 + /** 14.126 + * Returns an accessible method equivalent of a method. 14.127 + * 14.128 + * @param m the method whose accessible equivalent is requested. 14.129 + * @return the accessible equivalent for the method (can be the same as the passed in method), or null if there is 14.130 + * no accessible method equivalent. 14.131 + */ 14.132 + Method getAccessibleMethod(final Method m) { 14.133 + return m == null ? null : methods.get(new MethodSignature(m)); 14.134 + } 14.135 + 14.136 + Collection<Method> getMethods() { 14.137 + return methods.values(); 14.138 + } 14.139 + 14.140 + Class<?>[] getInnerClasses() { 14.141 + return innerClasses.toArray(new Class<?>[innerClasses.size()]); 14.142 + } 14.143 + 14.144 + /** 14.145 + * A helper class that represents a method signature - name and argument types. 14.146 + * 14.147 + * @author Attila Szegedi 14.148 + */ 14.149 + static final class MethodSignature { 14.150 + private final String name; 14.151 + private final Class<?>[] args; 14.152 + 14.153 + /** 14.154 + * Creates a new method signature from arbitrary data. 14.155 + * 14.156 + * @param name the name of the method this signature represents. 14.157 + * @param args the argument types of the method. 14.158 + */ 14.159 + MethodSignature(String name, Class<?>[] args) { 14.160 + this.name = name; 14.161 + this.args = args; 14.162 + } 14.163 + 14.164 + /** 14.165 + * Creates a signature for the given method. 14.166 + * 14.167 + * @param method the method for which a signature is created. 14.168 + */ 14.169 + MethodSignature(final Method method) { 14.170 + this(method.getName(), method.getParameterTypes()); 14.171 + } 14.172 + 14.173 + /** 14.174 + * Compares this object to another object 14.175 + * 14.176 + * @param o the other object 14.177 + * @return true if the other object is also a method signature with the same name, same number of arguments, and 14.178 + * same types of arguments. 14.179 + */ 14.180 + @Override 14.181 + public boolean equals(final Object o) { 14.182 + if(o instanceof MethodSignature) { 14.183 + final MethodSignature ms = (MethodSignature)o; 14.184 + return ms.name.equals(name) && Arrays.equals(args, ms.args); 14.185 + } 14.186 + return false; 14.187 + } 14.188 + 14.189 + /** 14.190 + * Returns a hash code, consistent with the overridden {@link #equals(Object)}. 14.191 + */ 14.192 + @Override 14.193 + public int hashCode() { 14.194 + return name.hashCode() ^ Arrays.hashCode(args); 14.195 + } 14.196 + 14.197 + @Override 14.198 + public String toString() { 14.199 + final StringBuilder b = new StringBuilder(); 14.200 + b.append("[MethodSignature ").append(name).append('('); 14.201 + if(args.length > 0) { 14.202 + b.append(args[0].getCanonicalName()); 14.203 + for(int i = 1; i < args.length; ++i) { 14.204 + b.append(", ").append(args[i].getCanonicalName()); 14.205 + } 14.206 + } 14.207 + return b.append(")]").toString(); 14.208 + } 14.209 + } 14.210 + 14.211 + private void lookupAccessibleMembers(final Class<?> clazz) { 14.212 + boolean searchSuperTypes; 14.213 + 14.214 + if(!CheckRestrictedPackage.isRestrictedClass(clazz)) { 14.215 + searchSuperTypes = false; 14.216 + for(Method method: clazz.getMethods()) { 14.217 + if(instance != Modifier.isStatic(method.getModifiers())) { 14.218 + final MethodSignature sig = new MethodSignature(method); 14.219 + if(!methods.containsKey(sig)) { 14.220 + final Class<?> declaringClass = method.getDeclaringClass(); 14.221 + if(declaringClass != clazz && CheckRestrictedPackage.isRestrictedClass(declaringClass)) { 14.222 + //Sometimes, the declaring class of a method (Method.getDeclaringClass()) 14.223 + //retrieved through Class.getMethods() for a public class will be a 14.224 + //non-public superclass. For such a method, we need to find a method with 14.225 + //the same name and signature in a public superclass or implemented 14.226 + //interface. 14.227 + //This typically doesn't happen with classes emitted by a reasonably modern 14.228 + //javac, as it'll create synthetic delegator methods in all public 14.229 + //immediate subclasses of the non-public class. We have, however, observed 14.230 + //this in the wild with class files compiled with older javac that doesn't 14.231 + //generate the said synthetic delegators. 14.232 + searchSuperTypes = true; 14.233 + } else { 14.234 + methods.put(sig, method); 14.235 + } 14.236 + } 14.237 + } 14.238 + } 14.239 + for(Class<?> innerClass: clazz.getClasses()) { 14.240 + // Add both static and non-static classes, regardless of instance flag. StaticClassLinker will just 14.241 + // expose non-static classes with explicit constructor outer class argument. 14.242 + // NOTE: getting inner class objects through getClasses() does not resolve them, so if those classes 14.243 + // were not yet loaded, they'll only get loaded in a non-resolved state; no static initializers for 14.244 + // them will trigger just by doing this. 14.245 + innerClasses.add(innerClass); 14.246 + } 14.247 + } else { 14.248 + searchSuperTypes = true; 14.249 + } 14.250 + 14.251 + if(searchSuperTypes) { 14.252 + // If we reach here, the class is either not public, or it is in a restricted package. Alternatively, it is 14.253 + // public, but some of its methods claim that their declaring class is non-public. We'll try superclasses 14.254 + // and implemented interfaces then looking for public ones. 14.255 + final Class<?>[] interfaces = clazz.getInterfaces(); 14.256 + for(int i = 0; i < interfaces.length; i++) { 14.257 + lookupAccessibleMembers(interfaces[i]); 14.258 + } 14.259 + final Class<?> superclass = clazz.getSuperclass(); 14.260 + if(superclass != null) { 14.261 + lookupAccessibleMembers(superclass); 14.262 + } 14.263 + } 14.264 + } 14.265 +}
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/src/jdk/internal/dynalink/beans/ApplicableOverloadedMethods.java Thu Feb 14 13:22:26 2013 +0100 15.3 @@ -0,0 +1,221 @@ 15.4 +/* 15.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 15.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 15.7 + * 15.8 + * This code is free software; you can redistribute it and/or modify it 15.9 + * under the terms of the GNU General Public License version 2 only, as 15.10 + * published by the Free Software Foundation. Oracle designates this 15.11 + * particular file as subject to the "Classpath" exception as provided 15.12 + * by Oracle in the LICENSE file that accompanied this code. 15.13 + * 15.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 15.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15.17 + * version 2 for more details (a copy is included in the LICENSE file that 15.18 + * accompanied this code). 15.19 + * 15.20 + * You should have received a copy of the GNU General Public License version 15.21 + * 2 along with this work; if not, write to the Free Software Foundation, 15.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 15.23 + * 15.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 15.25 + * or visit www.oracle.com if you need additional information or have any 15.26 + * questions. 15.27 + */ 15.28 + 15.29 +/* 15.30 + * This file is available under and governed by the GNU General Public 15.31 + * License version 2 only, as published by the Free Software Foundation. 15.32 + * However, the following notice accompanied the original version of this 15.33 + * file, and Oracle licenses the original version of this file under the BSD 15.34 + * license: 15.35 + */ 15.36 +/* 15.37 + Copyright 2009-2013 Attila Szegedi 15.38 + 15.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 15.40 + and the BSD License (the "BSD License"), with licensee being free to 15.41 + choose either of the two at their discretion. 15.42 + 15.43 + You may not use this file except in compliance with either the Apache 15.44 + License or the BSD License. 15.45 + 15.46 + If you choose to use this file in compliance with the Apache License, the 15.47 + following notice applies to you: 15.48 + 15.49 + You may obtain a copy of the Apache License at 15.50 + 15.51 + http://www.apache.org/licenses/LICENSE-2.0 15.52 + 15.53 + Unless required by applicable law or agreed to in writing, software 15.54 + distributed under the License is distributed on an "AS IS" BASIS, 15.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15.56 + implied. See the License for the specific language governing 15.57 + permissions and limitations under the License. 15.58 + 15.59 + If you choose to use this file in compliance with the BSD License, the 15.60 + following notice applies to you: 15.61 + 15.62 + Redistribution and use in source and binary forms, with or without 15.63 + modification, are permitted provided that the following conditions are 15.64 + met: 15.65 + * Redistributions of source code must retain the above copyright 15.66 + notice, this list of conditions and the following disclaimer. 15.67 + * Redistributions in binary form must reproduce the above copyright 15.68 + notice, this list of conditions and the following disclaimer in the 15.69 + documentation and/or other materials provided with the distribution. 15.70 + * Neither the name of the copyright holder nor the names of 15.71 + contributors may be used to endorse or promote products derived from 15.72 + this software without specific prior written permission. 15.73 + 15.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 15.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 15.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 15.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 15.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 15.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 15.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 15.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 15.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 15.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 15.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15.85 +*/ 15.86 + 15.87 +package jdk.internal.dynalink.beans; 15.88 + 15.89 +import java.lang.invoke.MethodHandle; 15.90 +import java.lang.invoke.MethodType; 15.91 +import java.util.LinkedList; 15.92 +import java.util.List; 15.93 +import jdk.internal.dynalink.support.TypeUtilities; 15.94 + 15.95 + 15.96 +/** 15.97 + * Represents overloaded methods applicable to a specific call site signature. 15.98 + * 15.99 + * @author Attila Szegedi 15.100 + */ 15.101 +class ApplicableOverloadedMethods { 15.102 + private final List<MethodHandle> methods; 15.103 + private final boolean varArgs; 15.104 + 15.105 + /** 15.106 + * Creates a new ApplicableOverloadedMethods instance 15.107 + * 15.108 + * @param methods a list of all overloaded methods with the same name for a class. 15.109 + * @param callSiteType the type of the call site 15.110 + * @param test applicability test. One of {@link #APPLICABLE_BY_SUBTYPING}, 15.111 + * {@link #APPLICABLE_BY_METHOD_INVOCATION_CONVERSION}, or {@link #APPLICABLE_BY_VARIABLE_ARITY}. 15.112 + */ 15.113 + ApplicableOverloadedMethods(final List<MethodHandle> methods, final MethodType callSiteType, 15.114 + final ApplicabilityTest test) { 15.115 + this.methods = new LinkedList<>(); 15.116 + for(MethodHandle m: methods) { 15.117 + if(test.isApplicable(callSiteType, m)) { 15.118 + this.methods.add(m); 15.119 + } 15.120 + } 15.121 + varArgs = test == APPLICABLE_BY_VARIABLE_ARITY; 15.122 + } 15.123 + 15.124 + /** 15.125 + * Retrieves all the methods this object holds. 15.126 + * 15.127 + * @return list of all methods. 15.128 + */ 15.129 + List<MethodHandle> getMethods() { 15.130 + return methods; 15.131 + } 15.132 + 15.133 + /** 15.134 + * Returns a list of all methods in this objects that are maximally specific. 15.135 + * 15.136 + * @return a list of maximally specific methods. 15.137 + */ 15.138 + List<MethodHandle> findMaximallySpecificMethods() { 15.139 + return MaximallySpecific.getMaximallySpecificMethods(methods, varArgs); 15.140 + } 15.141 + 15.142 + abstract static class ApplicabilityTest { 15.143 + abstract boolean isApplicable(MethodType callSiteType, MethodHandle method); 15.144 + } 15.145 + 15.146 + /** 15.147 + * Implements the applicability-by-subtyping test from JLS 15.12.2.2. 15.148 + */ 15.149 + static final ApplicabilityTest APPLICABLE_BY_SUBTYPING = new ApplicabilityTest() { 15.150 + @Override 15.151 + boolean isApplicable(MethodType callSiteType, MethodHandle method) { 15.152 + final MethodType methodType = method.type(); 15.153 + final int methodArity = methodType.parameterCount(); 15.154 + if(methodArity != callSiteType.parameterCount()) { 15.155 + return false; 15.156 + } 15.157 + // 0th arg is receiver; it doesn't matter for overload 15.158 + // resolution. 15.159 + for(int i = 1; i < methodArity; ++i) { 15.160 + if(!TypeUtilities.isSubtype(callSiteType.parameterType(i), methodType.parameterType(i))) { 15.161 + return false; 15.162 + } 15.163 + } 15.164 + return true; 15.165 + } 15.166 + }; 15.167 + 15.168 + /** 15.169 + * Implements the applicability-by-method-invocation-conversion test from JLS 15.12.2.3. 15.170 + */ 15.171 + static final ApplicabilityTest APPLICABLE_BY_METHOD_INVOCATION_CONVERSION = new ApplicabilityTest() { 15.172 + @Override 15.173 + boolean isApplicable(MethodType callSiteType, MethodHandle method) { 15.174 + final MethodType methodType = method.type(); 15.175 + final int methodArity = methodType.parameterCount(); 15.176 + if(methodArity != callSiteType.parameterCount()) { 15.177 + return false; 15.178 + } 15.179 + // 0th arg is receiver; it doesn't matter for overload 15.180 + // resolution. 15.181 + for(int i = 1; i < methodArity; ++i) { 15.182 + if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), 15.183 + methodType.parameterType(i))) { 15.184 + return false; 15.185 + } 15.186 + } 15.187 + return true; 15.188 + } 15.189 + }; 15.190 + 15.191 + /** 15.192 + * Implements the applicability-by-variable-arity test from JLS 15.12.2.4. 15.193 + */ 15.194 + static final ApplicabilityTest APPLICABLE_BY_VARIABLE_ARITY = new ApplicabilityTest() { 15.195 + @Override 15.196 + boolean isApplicable(MethodType callSiteType, MethodHandle method) { 15.197 + if(!method.isVarargsCollector()) { 15.198 + return false; 15.199 + } 15.200 + final MethodType methodType = method.type(); 15.201 + final int methodArity = methodType.parameterCount(); 15.202 + final int fixArity = methodArity - 1; 15.203 + final int callSiteArity = callSiteType.parameterCount(); 15.204 + if(fixArity > callSiteArity) { 15.205 + return false; 15.206 + } 15.207 + // 0th arg is receiver; it doesn't matter for overload 15.208 + // resolution. 15.209 + for(int i = 1; i < fixArity; ++i) { 15.210 + if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), 15.211 + methodType.parameterType(i))) { 15.212 + return false; 15.213 + } 15.214 + } 15.215 + final Class<?> varArgType = methodType.parameterType(fixArity).getComponentType(); 15.216 + for(int i = fixArity; i < callSiteArity; ++i) { 15.217 + if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), varArgType)) { 15.218 + return false; 15.219 + } 15.220 + } 15.221 + return true; 15.222 + } 15.223 + }; 15.224 +}
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/src/jdk/internal/dynalink/beans/BeanIntrospector.java Thu Feb 14 13:22:26 2013 +0100 16.3 @@ -0,0 +1,104 @@ 16.4 +/* 16.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 16.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 16.7 + * 16.8 + * This code is free software; you can redistribute it and/or modify it 16.9 + * under the terms of the GNU General Public License version 2 only, as 16.10 + * published by the Free Software Foundation. Oracle designates this 16.11 + * particular file as subject to the "Classpath" exception as provided 16.12 + * by Oracle in the LICENSE file that accompanied this code. 16.13 + * 16.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 16.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16.17 + * version 2 for more details (a copy is included in the LICENSE file that 16.18 + * accompanied this code). 16.19 + * 16.20 + * You should have received a copy of the GNU General Public License version 16.21 + * 2 along with this work; if not, write to the Free Software Foundation, 16.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 16.23 + * 16.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 16.25 + * or visit www.oracle.com if you need additional information or have any 16.26 + * questions. 16.27 + */ 16.28 + 16.29 +/* 16.30 + * This file is available under and governed by the GNU General Public 16.31 + * License version 2 only, as published by the Free Software Foundation. 16.32 + * However, the following notice accompanied the original version of this 16.33 + * file, and Oracle licenses the original version of this file under the BSD 16.34 + * license: 16.35 + */ 16.36 +/* 16.37 + Copyright 2009-2013 Attila Szegedi 16.38 + 16.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 16.40 + and the BSD License (the "BSD License"), with licensee being free to 16.41 + choose either of the two at their discretion. 16.42 + 16.43 + You may not use this file except in compliance with either the Apache 16.44 + License or the BSD License. 16.45 + 16.46 + If you choose to use this file in compliance with the Apache License, the 16.47 + following notice applies to you: 16.48 + 16.49 + You may obtain a copy of the Apache License at 16.50 + 16.51 + http://www.apache.org/licenses/LICENSE-2.0 16.52 + 16.53 + Unless required by applicable law or agreed to in writing, software 16.54 + distributed under the License is distributed on an "AS IS" BASIS, 16.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16.56 + implied. See the License for the specific language governing 16.57 + permissions and limitations under the License. 16.58 + 16.59 + If you choose to use this file in compliance with the BSD License, the 16.60 + following notice applies to you: 16.61 + 16.62 + Redistribution and use in source and binary forms, with or without 16.63 + modification, are permitted provided that the following conditions are 16.64 + met: 16.65 + * Redistributions of source code must retain the above copyright 16.66 + notice, this list of conditions and the following disclaimer. 16.67 + * Redistributions in binary form must reproduce the above copyright 16.68 + notice, this list of conditions and the following disclaimer in the 16.69 + documentation and/or other materials provided with the distribution. 16.70 + * Neither the name of the copyright holder nor the names of 16.71 + contributors may be used to endorse or promote products derived from 16.72 + this software without specific prior written permission. 16.73 + 16.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 16.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 16.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 16.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 16.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 16.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 16.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 16.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 16.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 16.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 16.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16.85 +*/ 16.86 + 16.87 +package jdk.internal.dynalink.beans; 16.88 + 16.89 +import java.lang.invoke.MethodHandle; 16.90 +import java.util.Collections; 16.91 +import java.util.Map; 16.92 + 16.93 +class BeanIntrospector extends FacetIntrospector { 16.94 + BeanIntrospector(Class<?> clazz) { 16.95 + super(clazz, true); 16.96 + } 16.97 + 16.98 + @Override 16.99 + Map<String, MethodHandle> getInnerClassGetters() { 16.100 + return Collections.emptyMap(); // NOTE: non-static inner classes are also on StaticClassIntrospector. 16.101 + } 16.102 + 16.103 + @Override 16.104 + MethodHandle editMethodHandle(MethodHandle mh) { 16.105 + return mh; 16.106 + } 16.107 +}
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/src/jdk/internal/dynalink/beans/BeanLinker.java Thu Feb 14 13:22:26 2013 +0100 17.3 @@ -0,0 +1,493 @@ 17.4 +/* 17.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 17.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 17.7 + * 17.8 + * This code is free software; you can redistribute it and/or modify it 17.9 + * under the terms of the GNU General Public License version 2 only, as 17.10 + * published by the Free Software Foundation. Oracle designates this 17.11 + * particular file as subject to the "Classpath" exception as provided 17.12 + * by Oracle in the LICENSE file that accompanied this code. 17.13 + * 17.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 17.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17.17 + * version 2 for more details (a copy is included in the LICENSE file that 17.18 + * accompanied this code). 17.19 + * 17.20 + * You should have received a copy of the GNU General Public License version 17.21 + * 2 along with this work; if not, write to the Free Software Foundation, 17.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17.23 + * 17.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 17.25 + * or visit www.oracle.com if you need additional information or have any 17.26 + * questions. 17.27 + */ 17.28 + 17.29 +/* 17.30 + * This file is available under and governed by the GNU General Public 17.31 + * License version 2 only, as published by the Free Software Foundation. 17.32 + * However, the following notice accompanied the original version of this 17.33 + * file, and Oracle licenses the original version of this file under the BSD 17.34 + * license: 17.35 + */ 17.36 +/* 17.37 + Copyright 2009-2013 Attila Szegedi 17.38 + 17.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 17.40 + and the BSD License (the "BSD License"), with licensee being free to 17.41 + choose either of the two at their discretion. 17.42 + 17.43 + You may not use this file except in compliance with either the Apache 17.44 + License or the BSD License. 17.45 + 17.46 + If you choose to use this file in compliance with the Apache License, the 17.47 + following notice applies to you: 17.48 + 17.49 + You may obtain a copy of the Apache License at 17.50 + 17.51 + http://www.apache.org/licenses/LICENSE-2.0 17.52 + 17.53 + Unless required by applicable law or agreed to in writing, software 17.54 + distributed under the License is distributed on an "AS IS" BASIS, 17.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 17.56 + implied. See the License for the specific language governing 17.57 + permissions and limitations under the License. 17.58 + 17.59 + If you choose to use this file in compliance with the BSD License, the 17.60 + following notice applies to you: 17.61 + 17.62 + Redistribution and use in source and binary forms, with or without 17.63 + modification, are permitted provided that the following conditions are 17.64 + met: 17.65 + * Redistributions of source code must retain the above copyright 17.66 + notice, this list of conditions and the following disclaimer. 17.67 + * Redistributions in binary form must reproduce the above copyright 17.68 + notice, this list of conditions and the following disclaimer in the 17.69 + documentation and/or other materials provided with the distribution. 17.70 + * Neither the name of the copyright holder nor the names of 17.71 + contributors may be used to endorse or promote products derived from 17.72 + this software without specific prior written permission. 17.73 + 17.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 17.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 17.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 17.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 17.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 17.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 17.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 17.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 17.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 17.85 +*/ 17.86 + 17.87 +package jdk.internal.dynalink.beans; 17.88 + 17.89 +import java.lang.invoke.MethodHandle; 17.90 +import java.lang.invoke.MethodHandles; 17.91 +import java.lang.invoke.MethodType; 17.92 +import java.lang.reflect.Array; 17.93 +import java.util.Collection; 17.94 +import java.util.List; 17.95 +import java.util.Map; 17.96 +import jdk.internal.dynalink.CallSiteDescriptor; 17.97 +import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType; 17.98 +import jdk.internal.dynalink.linker.GuardedInvocation; 17.99 +import jdk.internal.dynalink.linker.LinkerServices; 17.100 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 17.101 +import jdk.internal.dynalink.support.Guards; 17.102 +import jdk.internal.dynalink.support.Lookup; 17.103 +import jdk.internal.dynalink.support.TypeUtilities; 17.104 + 17.105 + 17.106 +/** 17.107 + * A class that provides linking capabilities for a single POJO class. Normally not used directly, but managed by 17.108 + * {@link BeansLinker}. 17.109 + * 17.110 + * @author Attila Szegedi 17.111 + */ 17.112 +class BeanLinker extends AbstractJavaLinker implements TypeBasedGuardingDynamicLinker { 17.113 + BeanLinker(Class<?> clazz) { 17.114 + super(clazz, Guards.getClassGuard(clazz), Guards.getInstanceOfGuard(clazz)); 17.115 + if(clazz.isArray()) { 17.116 + // Some languages won't have a notion of manipulating collections. Exposing "length" on arrays as an 17.117 + // explicit property is beneficial for them. 17.118 + // REVISIT: is it maybe a code smell that "dyn:getLength" is not needed? 17.119 + setPropertyGetter("length", GET_ARRAY_LENGTH, ValidationType.IS_ARRAY); 17.120 + } 17.121 + } 17.122 + 17.123 + @Override 17.124 + public boolean canLinkType(Class<?> type) { 17.125 + return type == clazz; 17.126 + } 17.127 + 17.128 + @Override 17.129 + FacetIntrospector createFacetIntrospector() { 17.130 + return new BeanIntrospector(clazz); 17.131 + } 17.132 + 17.133 + @Override 17.134 + protected GuardedInvocationComponent getGuardedInvocationComponent(CallSiteDescriptor callSiteDescriptor, 17.135 + LinkerServices linkerServices, List<String> operations) throws Exception { 17.136 + final GuardedInvocationComponent superGic = super.getGuardedInvocationComponent(callSiteDescriptor, 17.137 + linkerServices, operations); 17.138 + if(superGic != null) { 17.139 + return superGic; 17.140 + } 17.141 + if(operations.isEmpty()) { 17.142 + return null; 17.143 + } 17.144 + final String op = operations.get(0); 17.145 + // dyn:getElem(this, id) 17.146 + // id is typically either an int (for arrays and lists) or an object (for maps). linkerServices can provide 17.147 + // conversion from call site argument type though. 17.148 + if("getElem".equals(op)) { 17.149 + return getElementGetter(callSiteDescriptor, linkerServices, pop(operations)); 17.150 + } 17.151 + if("setElem".equals(op)) { 17.152 + return getElementSetter(callSiteDescriptor, linkerServices, pop(operations)); 17.153 + } 17.154 + // dyn:getLength(this) (works on Java arrays, collections, and maps) 17.155 + if("getLength".equals(op)) { 17.156 + return getLengthGetter(callSiteDescriptor); 17.157 + } 17.158 + return null; 17.159 + } 17.160 + 17.161 + private static MethodHandle GET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "get", 17.162 + MethodType.methodType(Object.class, int.class)); 17.163 + 17.164 + private static MethodHandle GET_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "get", 17.165 + MethodType.methodType(Object.class, Object.class)); 17.166 + 17.167 + private static MethodHandle LIST_GUARD = Guards.getInstanceOfGuard(List.class); 17.168 + private static MethodHandle MAP_GUARD = Guards.getInstanceOfGuard(Map.class); 17.169 + 17.170 + private GuardedInvocationComponent getElementGetter(final CallSiteDescriptor callSiteDescriptor, 17.171 + final LinkerServices linkerServices, List<String> operations) throws Exception { 17.172 + final MethodType callSiteType = callSiteDescriptor.getMethodType(); 17.173 + final Class<?> declaredType = callSiteType.parameterType(0); 17.174 + final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor, 17.175 + linkerServices, operations); 17.176 + 17.177 + // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing 17.178 + // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're 17.179 + // dealing with an array, or a list or map, but hey... 17.180 + // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers 17.181 + // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices. 17.182 + final GuardedInvocationComponent gic;; 17.183 + final boolean isMap; 17.184 + if(declaredType.isArray()) { 17.185 + gic = new GuardedInvocationComponent(MethodHandles.arrayElementGetter(declaredType)); 17.186 + isMap = false; 17.187 + } else if(List.class.isAssignableFrom(declaredType)) { 17.188 + gic = new GuardedInvocationComponent(GET_LIST_ELEMENT); 17.189 + isMap = false; 17.190 + } else if(Map.class.isAssignableFrom(declaredType)) { 17.191 + gic = new GuardedInvocationComponent(GET_MAP_ELEMENT); 17.192 + isMap = true; 17.193 + } else if(clazz.isArray()) { 17.194 + gic = getClassGuardedInvocationComponent(MethodHandles.arrayElementGetter(clazz), callSiteType); 17.195 + isMap = false; 17.196 + } else if(List.class.isAssignableFrom(clazz)) { 17.197 + gic = new GuardedInvocationComponent(GET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class, 17.198 + ValidationType.INSTANCE_OF); 17.199 + isMap = false; 17.200 + } else if(Map.class.isAssignableFrom(clazz)) { 17.201 + gic = new GuardedInvocationComponent(GET_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class, 17.202 + ValidationType.INSTANCE_OF); 17.203 + isMap = true; 17.204 + } else { 17.205 + // Can't retrieve elements for objects that are neither arrays, nor list, nor maps. 17.206 + return nextComponent; 17.207 + } 17.208 + 17.209 + // We can have "dyn:getElem:foo", especially in composites, i.e. "dyn:getElem|getProp|getMethod:foo" 17.210 + final String fixedKey = getFixedKey(callSiteDescriptor); 17.211 + // Convert the key to a number if we're working with a list or array 17.212 + final Object typedFixedKey; 17.213 + if(!isMap && fixedKey != null) { 17.214 + typedFixedKey = convertKeyToInteger(fixedKey, linkerServices); 17.215 + if(typedFixedKey == null) { 17.216 + // key is not numeric, it can never succeed 17.217 + return nextComponent; 17.218 + } 17.219 + } else { 17.220 + typedFixedKey = fixedKey; 17.221 + } 17.222 + 17.223 + final GuardedInvocation gi = gic.getGuardedInvocation(); 17.224 + final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey); 17.225 + final MethodHandle invocation = gi.getInvocation(); 17.226 + 17.227 + if(nextComponent == null) { 17.228 + return gic.replaceInvocation(binder.bind(invocation)); 17.229 + } else { 17.230 + final MethodHandle checkGuard; 17.231 + if(invocation == GET_LIST_ELEMENT) { 17.232 + checkGuard = convertArgToInt(RANGE_CHECK_LIST, linkerServices, callSiteDescriptor); 17.233 + } else if(invocation == GET_MAP_ELEMENT) { 17.234 + // TODO: A more complex solution could be devised for maps, one where we do a get() first, and fold it 17.235 + // into a GWT that tests if it returned null, and if it did, do another GWT with containsKey() 17.236 + // that returns constant null (on true), or falls back to next component (on false) 17.237 + checkGuard = CONTAINS_MAP; 17.238 + } else { 17.239 + checkGuard = convertArgToInt(RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor); 17.240 + } 17.241 + return nextComponent.compose(MethodHandles.guardWithTest(binder.bindTest(checkGuard), 17.242 + binder.bind(invocation), nextComponent.getGuardedInvocation().getInvocation()), gi.getGuard(), 17.243 + gic.getValidatorClass(), gic.getValidationType()); 17.244 + } 17.245 + } 17.246 + 17.247 + private static String getFixedKey(final CallSiteDescriptor callSiteDescriptor) { 17.248 + return callSiteDescriptor.getNameTokenCount() == 2 ? null : callSiteDescriptor.getNameToken( 17.249 + CallSiteDescriptor.NAME_OPERAND); 17.250 + } 17.251 + 17.252 + private static Object convertKeyToInteger(String fixedKey, LinkerServices linkerServices) throws Exception { 17.253 + try { 17.254 + if(linkerServices.canConvert(String.class, Number.class)) { 17.255 + try { 17.256 + final Object val = linkerServices.getTypeConverter(String.class, Number.class).invoke(fixedKey); 17.257 + if(!(val instanceof Number)) { 17.258 + return null; // not a number 17.259 + } 17.260 + final Number n = (Number)val; 17.261 + if(n instanceof Integer) { 17.262 + return n; 17.263 + } 17.264 + final int intIndex = n.intValue(); 17.265 + final double doubleValue = n.doubleValue(); 17.266 + if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinites trigger IOOBE 17.267 + return null; // not an exact integer 17.268 + } 17.269 + return Integer.valueOf(intIndex); 17.270 + } catch(Exception|Error e) { 17.271 + throw e; 17.272 + } catch(Throwable t) { 17.273 + throw new RuntimeException(t); 17.274 + } 17.275 + } 17.276 + return Integer.valueOf(fixedKey); 17.277 + } catch(NumberFormatException e) { 17.278 + // key is not a number 17.279 + return null; 17.280 + } 17.281 + } 17.282 + 17.283 + private static MethodHandle convertArgToInt(MethodHandle mh, LinkerServices ls, CallSiteDescriptor desc) { 17.284 + final Class<?> sourceType = desc.getMethodType().parameterType(1); 17.285 + if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) { 17.286 + return mh; 17.287 + } else if(ls.canConvert(sourceType, Number.class)) { 17.288 + final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class); 17.289 + return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType( 17.290 + mh.type().parameterType(1)))); 17.291 + } 17.292 + return mh; 17.293 + } 17.294 + 17.295 + /** 17.296 + * Contains methods to adapt an item getter/setter method handle to the requested type, optionally binding it to a 17.297 + * fixed key first. 17.298 + * @author Attila Szegedi 17.299 + * @version $Id: $ 17.300 + */ 17.301 + private static class Binder { 17.302 + private final LinkerServices linkerServices; 17.303 + private final MethodType methodType; 17.304 + private final Object fixedKey; 17.305 + 17.306 + Binder(LinkerServices linkerServices, MethodType methodType, Object fixedKey) { 17.307 + this.linkerServices = linkerServices; 17.308 + this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass()); 17.309 + this.fixedKey = fixedKey; 17.310 + } 17.311 + 17.312 + /*private*/ MethodHandle bind(MethodHandle handle) { 17.313 + return bindToFixedKey(linkerServices.asType(handle, methodType)); 17.314 + } 17.315 + 17.316 + /*private*/ MethodHandle bindTest(MethodHandle handle) { 17.317 + return bindToFixedKey(Guards.asType(handle, methodType)); 17.318 + } 17.319 + 17.320 + private MethodHandle bindToFixedKey(MethodHandle handle) { 17.321 + return fixedKey == null ? handle : MethodHandles.insertArguments(handle, 1, fixedKey); 17.322 + } 17.323 + } 17.324 + 17.325 + private static MethodHandle RANGE_CHECK_ARRAY = findRangeCheck(Object.class); 17.326 + private static MethodHandle RANGE_CHECK_LIST = findRangeCheck(List.class); 17.327 + private static MethodHandle CONTAINS_MAP = Lookup.PUBLIC.findVirtual(Map.class, "containsKey", 17.328 + MethodType.methodType(boolean.class, Object.class)); 17.329 + 17.330 + private static MethodHandle findRangeCheck(Class<?> collectionType) { 17.331 + return Lookup.findOwnStatic(MethodHandles.lookup(), "rangeCheck", boolean.class, collectionType, Object.class); 17.332 + } 17.333 + 17.334 + @SuppressWarnings("unused") 17.335 + private static final boolean rangeCheck(Object array, Object index) { 17.336 + if(!(index instanceof Number)) { 17.337 + return false; 17.338 + } 17.339 + final Number n = (Number)index; 17.340 + final int intIndex = n.intValue(); 17.341 + final double doubleValue = n.doubleValue(); 17.342 + if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE 17.343 + return false; 17.344 + } 17.345 + if(0 <= intIndex && intIndex < Array.getLength(array)) { 17.346 + return true; 17.347 + } 17.348 + throw new ArrayIndexOutOfBoundsException("Array index out of range: " + n); 17.349 + } 17.350 + 17.351 + @SuppressWarnings("unused") 17.352 + private static final boolean rangeCheck(List<?> list, Object index) { 17.353 + if(!(index instanceof Number)) { 17.354 + return false; 17.355 + } 17.356 + final Number n = (Number)index; 17.357 + final int intIndex = n.intValue(); 17.358 + final double doubleValue = n.doubleValue(); 17.359 + if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE 17.360 + return false; 17.361 + } 17.362 + if(0 <= intIndex && intIndex < list.size()) { 17.363 + return true; 17.364 + } 17.365 + throw new IndexOutOfBoundsException("Index: " + n + ", Size: " + list.size()); 17.366 + } 17.367 + 17.368 + private static MethodHandle SET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "set", 17.369 + MethodType.methodType(Object.class, int.class, Object.class)); 17.370 + 17.371 + private static MethodHandle PUT_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "put", 17.372 + MethodType.methodType(Object.class, Object.class, Object.class)); 17.373 + 17.374 + private GuardedInvocationComponent getElementSetter(CallSiteDescriptor callSiteDescriptor, 17.375 + LinkerServices linkerServices, List<String> operations) throws Exception { 17.376 + final MethodType callSiteType = callSiteDescriptor.getMethodType(); 17.377 + final Class<?> declaredType = callSiteType.parameterType(0); 17.378 + 17.379 + final GuardedInvocationComponent gic; 17.380 + // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing 17.381 + // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're 17.382 + // dealing with an array, or a list or map, but hey... 17.383 + // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers 17.384 + // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices. 17.385 + final boolean isMap; 17.386 + if(declaredType.isArray()) { 17.387 + gic = new GuardedInvocationComponent(MethodHandles.arrayElementSetter(declaredType)); 17.388 + isMap = false; 17.389 + } else if(List.class.isAssignableFrom(declaredType)) { 17.390 + gic = new GuardedInvocationComponent(SET_LIST_ELEMENT); 17.391 + isMap = false; 17.392 + } else if(Map.class.isAssignableFrom(declaredType)) { 17.393 + gic = new GuardedInvocationComponent(PUT_MAP_ELEMENT); 17.394 + isMap = true; 17.395 + } else if(clazz.isArray()) { 17.396 + gic = getClassGuardedInvocationComponent(MethodHandles.arrayElementSetter(clazz), callSiteType); 17.397 + isMap = false; 17.398 + } else if(List.class.isAssignableFrom(clazz)) { 17.399 + gic = new GuardedInvocationComponent(SET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class, 17.400 + ValidationType.INSTANCE_OF); 17.401 + isMap = false; 17.402 + } else if(Map.class.isAssignableFrom(clazz)) { 17.403 + gic = new GuardedInvocationComponent(PUT_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class, 17.404 + ValidationType.INSTANCE_OF); 17.405 + isMap = true; 17.406 + } else { 17.407 + // Can't set elements for objects that are neither arrays, nor list, nor maps. 17.408 + gic = null; 17.409 + isMap = false; 17.410 + } 17.411 + 17.412 + // In contrast to, say, getElementGetter, we only compute the nextComponent if the target object is not a map, 17.413 + // as maps will always succeed in setting the element and will never need to fall back to the next component 17.414 + // operation. 17.415 + final GuardedInvocationComponent nextComponent = isMap ? null : getGuardedInvocationComponent( 17.416 + callSiteDescriptor, linkerServices, operations); 17.417 + if(gic == null) { 17.418 + return nextComponent; 17.419 + } 17.420 + 17.421 + // We can have "dyn:setElem:foo", especially in composites, i.e. "dyn:setElem|setProp:foo" 17.422 + final String fixedKey = getFixedKey(callSiteDescriptor); 17.423 + // Convert the key to a number if we're working with a list or array 17.424 + final Object typedFixedKey; 17.425 + if(!isMap && fixedKey != null) { 17.426 + typedFixedKey = convertKeyToInteger(fixedKey, linkerServices); 17.427 + if(typedFixedKey == null) { 17.428 + // key is not numeric, it can never succeed 17.429 + return nextComponent; 17.430 + } 17.431 + } else { 17.432 + typedFixedKey = fixedKey; 17.433 + } 17.434 + 17.435 + final GuardedInvocation gi = gic.getGuardedInvocation(); 17.436 + final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey); 17.437 + final MethodHandle invocation = gi.getInvocation(); 17.438 + 17.439 + if(nextComponent == null) { 17.440 + return gic.replaceInvocation(binder.bind(invocation)); 17.441 + } else { 17.442 + final MethodHandle checkGuard = convertArgToInt(invocation == SET_LIST_ELEMENT ? RANGE_CHECK_LIST : 17.443 + RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor); 17.444 + return nextComponent.compose(MethodHandles.guardWithTest(binder.bindTest(checkGuard), 17.445 + binder.bind(invocation), nextComponent.getGuardedInvocation().getInvocation()), gi.getGuard(), 17.446 + gic.getValidatorClass(), gic.getValidationType()); 17.447 + } 17.448 + } 17.449 + 17.450 + private static MethodHandle GET_ARRAY_LENGTH = Lookup.PUBLIC.findStatic(Array.class, "getLength", 17.451 + MethodType.methodType(int.class, Object.class)); 17.452 + 17.453 + private static MethodHandle GET_COLLECTION_LENGTH = Lookup.PUBLIC.findVirtual(Collection.class, "size", 17.454 + MethodType.methodType(int.class)); 17.455 + 17.456 + private static MethodHandle GET_MAP_LENGTH = Lookup.PUBLIC.findVirtual(Map.class, "size", 17.457 + MethodType.methodType(int.class)); 17.458 + 17.459 + private static MethodHandle COLLECTION_GUARD = Guards.getInstanceOfGuard(Collection.class); 17.460 + 17.461 + private GuardedInvocationComponent getLengthGetter(CallSiteDescriptor callSiteDescriptor) { 17.462 + assertParameterCount(callSiteDescriptor, 1); 17.463 + final MethodType callSiteType = callSiteDescriptor.getMethodType(); 17.464 + final Class<?> declaredType = callSiteType.parameterType(0); 17.465 + // If declared type of receiver at the call site is already an array, collection, or map, bind without guard. 17.466 + // Thing is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance 17.467 + // they're dealing with an array, collection, or map, but hey... 17.468 + if(declaredType.isArray()) { 17.469 + return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType)); 17.470 + } else if(Collection.class.isAssignableFrom(declaredType)) { 17.471 + return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType)); 17.472 + } else if(Map.class.isAssignableFrom(declaredType)) { 17.473 + return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType)); 17.474 + } 17.475 + 17.476 + // Otherwise, create a binding based on the actual type of the argument with an appropriate guard. 17.477 + if(clazz.isArray()) { 17.478 + return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType), Guards.isArray(0, 17.479 + callSiteType), ValidationType.IS_ARRAY); 17.480 + } if(Collection.class.isAssignableFrom(clazz)) { 17.481 + return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType), Guards.asType( 17.482 + COLLECTION_GUARD, callSiteType), Collection.class, ValidationType.INSTANCE_OF); 17.483 + } if(Map.class.isAssignableFrom(clazz)) { 17.484 + return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType), Guards.asType(MAP_GUARD, 17.485 + callSiteType), Map.class, ValidationType.INSTANCE_OF); 17.486 + } 17.487 + // Can't retrieve length for objects that are neither arrays, nor collections, nor maps. 17.488 + return null; 17.489 + } 17.490 + 17.491 + private static void assertParameterCount(CallSiteDescriptor descriptor, int paramCount) { 17.492 + if(descriptor.getMethodType().parameterCount() != paramCount) { 17.493 + throw new BootstrapMethodError(descriptor.getName() + " must have exactly " + paramCount + " parameters."); 17.494 + } 17.495 + } 17.496 +} 17.497 \ No newline at end of file
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/src/jdk/internal/dynalink/beans/BeansLinker.java Thu Feb 14 13:22:26 2013 +0100 18.3 @@ -0,0 +1,180 @@ 18.4 +/* 18.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 18.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 18.7 + * 18.8 + * This code is free software; you can redistribute it and/or modify it 18.9 + * under the terms of the GNU General Public License version 2 only, as 18.10 + * published by the Free Software Foundation. Oracle designates this 18.11 + * particular file as subject to the "Classpath" exception as provided 18.12 + * by Oracle in the LICENSE file that accompanied this code. 18.13 + * 18.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 18.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 18.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 18.17 + * version 2 for more details (a copy is included in the LICENSE file that 18.18 + * accompanied this code). 18.19 + * 18.20 + * You should have received a copy of the GNU General Public License version 18.21 + * 2 along with this work; if not, write to the Free Software Foundation, 18.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18.23 + * 18.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 18.25 + * or visit www.oracle.com if you need additional information or have any 18.26 + * questions. 18.27 + */ 18.28 + 18.29 +/* 18.30 + * This file is available under and governed by the GNU General Public 18.31 + * License version 2 only, as published by the Free Software Foundation. 18.32 + * However, the following notice accompanied the original version of this 18.33 + * file, and Oracle licenses the original version of this file under the BSD 18.34 + * license: 18.35 + */ 18.36 +/* 18.37 + Copyright 2009-2013 Attila Szegedi 18.38 + 18.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 18.40 + and the BSD License (the "BSD License"), with licensee being free to 18.41 + choose either of the two at their discretion. 18.42 + 18.43 + You may not use this file except in compliance with either the Apache 18.44 + License or the BSD License. 18.45 + 18.46 + If you choose to use this file in compliance with the Apache License, the 18.47 + following notice applies to you: 18.48 + 18.49 + You may obtain a copy of the Apache License at 18.50 + 18.51 + http://www.apache.org/licenses/LICENSE-2.0 18.52 + 18.53 + Unless required by applicable law or agreed to in writing, software 18.54 + distributed under the License is distributed on an "AS IS" BASIS, 18.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 18.56 + implied. See the License for the specific language governing 18.57 + permissions and limitations under the License. 18.58 + 18.59 + If you choose to use this file in compliance with the BSD License, the 18.60 + following notice applies to you: 18.61 + 18.62 + Redistribution and use in source and binary forms, with or without 18.63 + modification, are permitted provided that the following conditions are 18.64 + met: 18.65 + * Redistributions of source code must retain the above copyright 18.66 + notice, this list of conditions and the following disclaimer. 18.67 + * Redistributions in binary form must reproduce the above copyright 18.68 + notice, this list of conditions and the following disclaimer in the 18.69 + documentation and/or other materials provided with the distribution. 18.70 + * Neither the name of the copyright holder nor the names of 18.71 + contributors may be used to endorse or promote products derived from 18.72 + this software without specific prior written permission. 18.73 + 18.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 18.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 18.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 18.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 18.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 18.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 18.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 18.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 18.85 +*/ 18.86 + 18.87 +package jdk.internal.dynalink.beans; 18.88 + 18.89 +import java.beans.BeanInfo; 18.90 +import java.lang.invoke.MethodHandles; 18.91 +import jdk.internal.dynalink.CallSiteDescriptor; 18.92 +import jdk.internal.dynalink.DynamicLinkerFactory; 18.93 +import jdk.internal.dynalink.linker.GuardedInvocation; 18.94 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 18.95 +import jdk.internal.dynalink.linker.LinkRequest; 18.96 +import jdk.internal.dynalink.linker.LinkerServices; 18.97 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 18.98 + 18.99 + 18.100 +/** 18.101 + * A linker for POJOs. Normally used as the ultimate fallback linker by the {@link DynamicLinkerFactory} so it is given 18.102 + * the chance to link calls to all objects that no other language runtime recognizes. Specifically, this linker will: 18.103 + * <ul> 18.104 + * <li>expose all public methods of form {@code setXxx()}, {@code getXxx()}, and {@code isXxx()} as property setters and 18.105 + * getters for {@code dyn:setProp} and {@code dyn:getProp} operations;</li> 18.106 + * <li>expose all property getters and setters declared by the class' {@link BeanInfo};</li> 18.107 + * <li>expose all public methods and methods declared by the class' {@link BeanInfo} for invocation through 18.108 + * {@code dyn:callMethod} operation;</li> 18.109 + * <li>expose all public methods and methods declared by the class' {@link BeanInfo} for retrieval for 18.110 + * {@code dyn:getMethod} operation; the methods thus retrieved can then be invoked using {@code dyn:call};</li> 18.111 + * <li>expose all public fields as properties, unless there are getters or setters for the properties of the same name;</li> 18.112 + * <li>expose {@code dyn:getLength}, {@code dyn:getElem} and {@code dyn:setElem} on native Java arrays, as well as 18.113 + * {@link java.util.List} and {@link java.util.Map} objects; ({@code dyn:getLength} works on any 18.114 + * {@link java.util.Collection});</li> 18.115 + * <li>expose a virtual property named {@code length} on Java arrays;</li> 18.116 + * <li>expose {@code dyn:new} on instances of {@link StaticClass} as calls to constructors, including those static class 18.117 + * objects that represent Java arrays (their constructors take a single {@code int} parameter representing the length of 18.118 + * the array to create);</li> 18.119 + * <li>expose static methods, fields, and properties of classes in a similar manner to how instance method, fields, and 18.120 + * properties are exposed, on {@link StaticClass} objects.</li> 18.121 + * <li>expose a virtual property named {@code static} on instances of {@link java.lang.Class} to access their 18.122 + * {@link StaticClass}.</li> 18.123 + * </ul> 18.124 + * <p><strong>Overloaded method resolution</strong> is performed automatically for property setters, methods, and 18.125 + * constructors. Additionally, manual overloaded method selection is supported by having a call site specify a name for 18.126 + * a method that contains an explicit signature, i.e. {@code dyn:getMethod:parseInt(String,int)}. You can use 18.127 + * non-qualified class names in such signatures regardless of those classes' packages, they will match any class with 18.128 + * the same non-qualified name. You only have to use a fully qualified class name in case non-qualified class names 18.129 + * would cause selection ambiguity (that is extremely rare).</p> 18.130 + * <p><strong>Variable argument invocation</strong> is handled for both methods and constructors.</p> 18.131 + * <p>Currently, only public fields and methods are supported. Any Lookup objects passed in the 18.132 + * {@link LinkRequest}s are ignored and {@link MethodHandles#publicLookup()} is used instead.</p> 18.133 + * 18.134 + * @author Attila Szegedi 18.135 + */ 18.136 +public class BeansLinker implements GuardingDynamicLinker { 18.137 + private static final ClassValue<TypeBasedGuardingDynamicLinker> linkers = new ClassValue<TypeBasedGuardingDynamicLinker>() { 18.138 + @Override 18.139 + protected TypeBasedGuardingDynamicLinker computeValue(Class<?> clazz) { 18.140 + // If ClassValue.put() were public, we could just pre-populate with these known mappings... 18.141 + return 18.142 + clazz == Class.class ? new ClassLinker() : 18.143 + clazz == StaticClass.class ? new StaticClassLinker() : 18.144 + DynamicMethod.class.isAssignableFrom(clazz) ? new DynamicMethodLinker() : 18.145 + new BeanLinker(clazz); 18.146 + } 18.147 + }; 18.148 + 18.149 + /** 18.150 + * Creates a new POJO linker. 18.151 + */ 18.152 + public BeansLinker() { 18.153 + } 18.154 + 18.155 + /** 18.156 + * Returns a bean linker for a particular single class. Useful when you need to override or extend the behavior of 18.157 + * linking for some classes in your language runtime's linker, but still want to delegate to the default behavior in 18.158 + * some cases. 18.159 + * @param clazz the class 18.160 + * @return a bean linker for that class 18.161 + */ 18.162 + public static TypeBasedGuardingDynamicLinker getLinkerForClass(Class<?> clazz) { 18.163 + return linkers.get(clazz); 18.164 + } 18.165 + 18.166 + @Override 18.167 + public GuardedInvocation getGuardedInvocation(LinkRequest request, final LinkerServices linkerServices) 18.168 + throws Exception { 18.169 + final CallSiteDescriptor callSiteDescriptor = request.getCallSiteDescriptor(); 18.170 + final int l = callSiteDescriptor.getNameTokenCount(); 18.171 + // All names conforming to the dynalang MOP should have at least two tokens, the first one being "dyn" 18.172 + if(l < 2 || "dyn" != callSiteDescriptor.getNameToken(CallSiteDescriptor.SCHEME)) { 18.173 + return null; 18.174 + } 18.175 + 18.176 + final Object receiver = request.getReceiver(); 18.177 + if(receiver == null) { 18.178 + // Can't operate on null 18.179 + return null; 18.180 + } 18.181 + return getLinkerForClass(receiver.getClass()).getGuardedInvocation(request, linkerServices); 18.182 + } 18.183 +} 18.184 \ No newline at end of file
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/src/jdk/internal/dynalink/beans/CheckRestrictedPackage.java Thu Feb 14 13:22:26 2013 +0100 19.3 @@ -0,0 +1,109 @@ 19.4 +/* 19.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 19.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 19.7 + * 19.8 + * This code is free software; you can redistribute it and/or modify it 19.9 + * under the terms of the GNU General Public License version 2 only, as 19.10 + * published by the Free Software Foundation. Oracle designates this 19.11 + * particular file as subject to the "Classpath" exception as provided 19.12 + * by Oracle in the LICENSE file that accompanied this code. 19.13 + * 19.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 19.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 19.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 19.17 + * version 2 for more details (a copy is included in the LICENSE file that 19.18 + * accompanied this code). 19.19 + * 19.20 + * You should have received a copy of the GNU General Public License version 19.21 + * 2 along with this work; if not, write to the Free Software Foundation, 19.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19.23 + * 19.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19.25 + * or visit www.oracle.com if you need additional information or have any 19.26 + * questions. 19.27 + */ 19.28 + 19.29 +/* 19.30 + * This file is available under and governed by the GNU General Public 19.31 + * License version 2 only, as published by the Free Software Foundation. 19.32 + * However, the following notice accompanied the original version of this 19.33 + * file, and Oracle licenses the original version of this file under the BSD 19.34 + * license: 19.35 + */ 19.36 +/* 19.37 + Copyright 2009-2013 Attila Szegedi 19.38 + 19.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 19.40 + and the BSD License (the "BSD License"), with licensee being free to 19.41 + choose either of the two at their discretion. 19.42 + 19.43 + You may not use this file except in compliance with either the Apache 19.44 + License or the BSD License. 19.45 + 19.46 + If you choose to use this file in compliance with the Apache License, the 19.47 + following notice applies to you: 19.48 + 19.49 + You may obtain a copy of the Apache License at 19.50 + 19.51 + http://www.apache.org/licenses/LICENSE-2.0 19.52 + 19.53 + Unless required by applicable law or agreed to in writing, software 19.54 + distributed under the License is distributed on an "AS IS" BASIS, 19.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 19.56 + implied. See the License for the specific language governing 19.57 + permissions and limitations under the License. 19.58 + 19.59 + If you choose to use this file in compliance with the BSD License, the 19.60 + following notice applies to you: 19.61 + 19.62 + Redistribution and use in source and binary forms, with or without 19.63 + modification, are permitted provided that the following conditions are 19.64 + met: 19.65 + * Redistributions of source code must retain the above copyright 19.66 + notice, this list of conditions and the following disclaimer. 19.67 + * Redistributions in binary form must reproduce the above copyright 19.68 + notice, this list of conditions and the following disclaimer in the 19.69 + documentation and/or other materials provided with the distribution. 19.70 + * Neither the name of the copyright holder nor the names of 19.71 + contributors may be used to endorse or promote products derived from 19.72 + this software without specific prior written permission. 19.73 + 19.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 19.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 19.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 19.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 19.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 19.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 19.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 19.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 19.85 +*/ 19.86 + 19.87 +package jdk.internal.dynalink.beans; 19.88 + 19.89 +import java.lang.reflect.Modifier; 19.90 + 19.91 +/** 19.92 + * A utility class to check whether a given class is in a package with restricted access e.g. "sun.*" etc. See 19.93 + * {@link CheckRestrictedPackageInternal} for implementation details. 19.94 + */ 19.95 +class CheckRestrictedPackage { 19.96 + /** 19.97 + * Returns true if the class is either not public, or it resides in a package with restricted access. 19.98 + * @param clazz the class to test 19.99 + * @return true if the class is either not public, or it resides in a package with restricted access. 19.100 + */ 19.101 + static boolean isRestrictedClass(Class<?> clazz) { 19.102 + return !Modifier.isPublic(clazz.getModifiers()) || 19.103 + (System.getSecurityManager() != null && isRestrictedPackage(clazz.getPackage())); 19.104 + } 19.105 + 19.106 + private static boolean isRestrictedPackage(Package pkg) { 19.107 + // Note: we broke out the actual implementation into CheckRestrictedPackageInternal, so we only load it when 19.108 + // needed - that is, if we need to check a non-public class with a non-null package, in presence of a security 19.109 + // manager. 19.110 + return pkg == null ? false : CheckRestrictedPackageInternal.isRestrictedPackageName(pkg.getName()); 19.111 + } 19.112 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/src/jdk/internal/dynalink/beans/CheckRestrictedPackageInternal.java Thu Feb 14 13:22:26 2013 +0100 20.3 @@ -0,0 +1,252 @@ 20.4 +/* 20.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 20.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 20.7 + * 20.8 + * This code is free software; you can redistribute it and/or modify it 20.9 + * under the terms of the GNU General Public License version 2 only, as 20.10 + * published by the Free Software Foundation. Oracle designates this 20.11 + * particular file as subject to the "Classpath" exception as provided 20.12 + * by Oracle in the LICENSE file that accompanied this code. 20.13 + * 20.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 20.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 20.17 + * version 2 for more details (a copy is included in the LICENSE file that 20.18 + * accompanied this code). 20.19 + * 20.20 + * You should have received a copy of the GNU General Public License version 20.21 + * 2 along with this work; if not, write to the Free Software Foundation, 20.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20.23 + * 20.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20.25 + * or visit www.oracle.com if you need additional information or have any 20.26 + * questions. 20.27 + */ 20.28 + 20.29 +/* 20.30 + * This file is available under and governed by the GNU General Public 20.31 + * License version 2 only, as published by the Free Software Foundation. 20.32 + * However, the following notice accompanied the original version of this 20.33 + * file, and Oracle licenses the original version of this file under the BSD 20.34 + * license: 20.35 + */ 20.36 +/* 20.37 + Copyright 2009-2013 Attila Szegedi 20.38 + 20.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 20.40 + and the BSD License (the "BSD License"), with licensee being free to 20.41 + choose either of the two at their discretion. 20.42 + 20.43 + You may not use this file except in compliance with either the Apache 20.44 + License or the BSD License. 20.45 + 20.46 + If you choose to use this file in compliance with the Apache License, the 20.47 + following notice applies to you: 20.48 + 20.49 + You may obtain a copy of the Apache License at 20.50 + 20.51 + http://www.apache.org/licenses/LICENSE-2.0 20.52 + 20.53 + Unless required by applicable law or agreed to in writing, software 20.54 + distributed under the License is distributed on an "AS IS" BASIS, 20.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 20.56 + implied. See the License for the specific language governing 20.57 + permissions and limitations under the License. 20.58 + 20.59 + If you choose to use this file in compliance with the BSD License, the 20.60 + following notice applies to you: 20.61 + 20.62 + Redistribution and use in source and binary forms, with or without 20.63 + modification, are permitted provided that the following conditions are 20.64 + met: 20.65 + * Redistributions of source code must retain the above copyright 20.66 + notice, this list of conditions and the following disclaimer. 20.67 + * Redistributions in binary form must reproduce the above copyright 20.68 + notice, this list of conditions and the following disclaimer in the 20.69 + documentation and/or other materials provided with the distribution. 20.70 + * Neither the name of the copyright holder nor the names of 20.71 + contributors may be used to endorse or promote products derived from 20.72 + this software without specific prior written permission. 20.73 + 20.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 20.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 20.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 20.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 20.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 20.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 20.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 20.85 +*/ 20.86 + 20.87 +package jdk.internal.dynalink.beans; 20.88 + 20.89 +import java.io.ByteArrayOutputStream; 20.90 +import java.io.IOException; 20.91 +import java.io.InputStream; 20.92 +import java.lang.invoke.MethodHandle; 20.93 +import java.lang.invoke.MethodHandles; 20.94 +import java.lang.reflect.Method; 20.95 +import java.security.AccessController; 20.96 +import java.security.Permissions; 20.97 +import java.security.PrivilegedAction; 20.98 +import java.security.ProtectionDomain; 20.99 +import java.security.SecureClassLoader; 20.100 + 20.101 +/** 20.102 + * A utility class to check whether a given class is in a package with restricted access e.g. "sun.*". These packages 20.103 + * are normally listed in the security property "package.access" for most JRE implementations, although we fortunately 20.104 + * don't rely on it but solely on {@link SecurityManager#checkPackageAccess(String)}). 20.105 + * 20.106 + * This class accomplishes the check in a fashion that works reliably even if Dynalink itself (and all the code on the 20.107 + * stack that led to the invocation) has the permission to access the restricted package. 20.108 + * 20.109 + * If Dynalink has a broad set of privileges (notably, it is loaded from boot or extension class path), then it loads 20.110 + * the {@link RestrictedPackageTester} class into a isolated secure class loader that gives it no permissions 20.111 + * whatsoever, and uses this completely unprivileged class to subsequently invoke 20.112 + * {@link SecurityManager#checkPackageAccess(String)}. This will reliably throw a {@link SecurityException} for every 20.113 + * restricted package even if Dynalink and other code on the stack have the requisite {@code "accessClassInPackage.*"} 20.114 + * {@link RuntimePermission} privilege. 20.115 + * 20.116 + * On the other hand, if Dynalink does not have a broad set of privileges normally granted by the boot or extension 20.117 + * class path, it will probably lack the privilege to create a new secure class loader into which to load the tester 20.118 + * class. In this case, it will invoke {@link SecurityManager#checkPackageAccess(String)} itself with the reasoning that 20.119 + * it will also be sufficient to discover whether a package is restricted or not. 20.120 + * 20.121 + * The rationale for this design is that if Dynalink is running as part of a privileged classpath - boot or extension 20.122 + * class path, it will have all privileges, so a security manager's package access check might succeed if all other code 20.123 + * on the stack when requesting linking with a particular restricted class is also privileged. A subsequent linking 20.124 + * request from less privileged code would then also succeed in requesting methods in privileged package. On the other 20.125 + * hand, if Dynalink is privileged, it will be able to delegate the package access check to the unprivileged class and 20.126 + * narrow the access based on its result. Finally, if Dynalink itself is unprivileged, it will not be able to load the 20.127 + * unprivileged class, but then it will also fail the security manager's package access. 20.128 + * 20.129 + * With this design, Dynalink effectively restrains itself from giving unauthorized access to restricted packages from 20.130 + * classes doing the linking in case it itself has access to those packages. The only way to defeat it would be to 20.131 + * selectively give Dynalink some {@code "accessClassInPackage.*"} permissions while denying it the privilege to 20.132 + * manipulate class loaders. 20.133 + */ 20.134 +class CheckRestrictedPackageInternal { 20.135 + private static final MethodHandle PACKAGE_ACCESS_CHECK = getPackageAccessCheckMethod(); 20.136 + private static final String TESTER_CLASS_NAME = "jdk.internal.dynalink.beans.RestrictedPackageTester"; 20.137 + 20.138 + /** 20.139 + * Returns true if the specified package has restricted access. 20.140 + * @param pkgName the name of the package to check. 20.141 + * @return true if the specified package has restricted access, false otherwise. 20.142 + * @throws NullPointerException if pkgName is null, or if there is {@link System#getSecurityManager()} returns null 20.143 + * as this method is only expected to be invoked in the presence of a security manager. 20.144 + */ 20.145 + static boolean isRestrictedPackageName(String pkgName) { 20.146 + try { 20.147 + if(PACKAGE_ACCESS_CHECK != null) { 20.148 + // If we were able to load our unprivileged tester class, use it to check package access 20.149 + try { 20.150 + PACKAGE_ACCESS_CHECK.invokeExact(pkgName); 20.151 + } catch(Error|RuntimeException e) { 20.152 + throw e; 20.153 + } catch(Throwable t) { 20.154 + throw new RuntimeException(t); 20.155 + } 20.156 + } else { 20.157 + // If we didn't have sufficient permissions to load our unprivileged tester class, we're definitely not 20.158 + // running in a privileged class path, so invoking SecurityManager.checkPackageAccess() directly should 20.159 + // have the same effect as going through an unprivileged tester. 20.160 + System.getSecurityManager().checkPackageAccess(pkgName); 20.161 + } 20.162 + return false; 20.163 + } catch(SecurityException e) { 20.164 + return true; 20.165 + } 20.166 + } 20.167 + 20.168 + private static MethodHandle getPackageAccessCheckMethod() { 20.169 + try { 20.170 + return AccessController.doPrivileged(new PrivilegedAction<MethodHandle>() { 20.171 + @Override 20.172 + public MethodHandle run() { 20.173 + return getPackageAccessCheckMethodInternal(); 20.174 + } 20.175 + }); 20.176 + } catch(SecurityException e) { 20.177 + // We don't have sufficient privileges to load our tester class into a separate protection domain, so just 20.178 + // return null so isRestrictedPackageName() will default to itself invoking 20.179 + // SecurityManager.checkPackageAccess(). 20.180 + return null; 20.181 + } 20.182 + } 20.183 + 20.184 + static MethodHandle getPackageAccessCheckMethodInternal() { 20.185 + try { 20.186 + // Can't use MethodHandles.lookup().findStatic() -- even though both this class and the loaded class are in 20.187 + // the same package, findStatic() will throw an IllegalAccessException since they have different class 20.188 + // loaders. That's why we have to use unreflect with a setAccessible(true)... 20.189 + final Method m = getTesterClass().getDeclaredMethod("checkPackageAccess", String.class); 20.190 + m.setAccessible(true); 20.191 + return MethodHandles.lookup().unreflect(m); 20.192 + } catch(IllegalAccessException|NoSuchMethodException e) { 20.193 + throw new AssertionError(e); 20.194 + } 20.195 + } 20.196 + 20.197 + private static Class<?> getTesterClass() { 20.198 + final ClassLoader loader = getTesterClassLoader(); 20.199 + try { 20.200 + final Class<?> checkerClass = Class.forName(TESTER_CLASS_NAME, true, loader); 20.201 + // Sanity check to ensure we didn't accidentally pick up the class from elsewhere 20.202 + if(checkerClass.getClassLoader() != loader) { 20.203 + throw new AssertionError(TESTER_CLASS_NAME + " was loaded from a different class loader"); 20.204 + } 20.205 + return checkerClass; 20.206 + } catch(ClassNotFoundException e) { 20.207 + throw new AssertionError(e); 20.208 + } 20.209 + } 20.210 + 20.211 + private static ClassLoader getTesterClassLoader() { 20.212 + // We deliberately override loadClass instead of findClass so that we don't give a chance to finding this 20.213 + // class already loaded anywhere else. Not that there's a big possibility for this, especially since the parent 20.214 + // class loader is the bootstrap class loader, but still... 20.215 + return new SecureClassLoader(null) { 20.216 + 20.217 + @Override 20.218 + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { 20.219 + if(name.equals(TESTER_CLASS_NAME)) { 20.220 + final byte[] bytes = getTesterClassBytes(); 20.221 + // Define the class with a protection domain that grants no permissions. 20.222 + Class<?> clazz = defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(null, 20.223 + new Permissions())); 20.224 + if(resolve) { 20.225 + resolveClass(clazz); 20.226 + } 20.227 + return clazz; 20.228 + } else { 20.229 + return super.loadClass(name, resolve); 20.230 + } 20.231 + } 20.232 + }; 20.233 + } 20.234 + 20.235 + private static byte[] getTesterClassBytes() { 20.236 + try { 20.237 + final InputStream in = CheckRestrictedPackage.class.getResourceAsStream("RestrictedPackageTester.class"); 20.238 + try { 20.239 + final ByteArrayOutputStream out = new ByteArrayOutputStream(2048); 20.240 + for(;;) { 20.241 + final int b = in.read(); 20.242 + if(b == -1) { 20.243 + break; 20.244 + } 20.245 + out.write(b); 20.246 + } 20.247 + return out.toByteArray(); 20.248 + } finally { 20.249 + in.close(); 20.250 + } 20.251 + } catch(IOException e) { 20.252 + throw new RuntimeException(e); 20.253 + } 20.254 + } 20.255 +}
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 21.2 +++ b/src/jdk/internal/dynalink/beans/ClassLinker.java Thu Feb 14 13:22:26 2013 +0100 21.3 @@ -0,0 +1,110 @@ 21.4 +/* 21.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 21.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 21.7 + * 21.8 + * This code is free software; you can redistribute it and/or modify it 21.9 + * under the terms of the GNU General Public License version 2 only, as 21.10 + * published by the Free Software Foundation. Oracle designates this 21.11 + * particular file as subject to the "Classpath" exception as provided 21.12 + * by Oracle in the LICENSE file that accompanied this code. 21.13 + * 21.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 21.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 21.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 21.17 + * version 2 for more details (a copy is included in the LICENSE file that 21.18 + * accompanied this code). 21.19 + * 21.20 + * You should have received a copy of the GNU General Public License version 21.21 + * 2 along with this work; if not, write to the Free Software Foundation, 21.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21.23 + * 21.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21.25 + * or visit www.oracle.com if you need additional information or have any 21.26 + * questions. 21.27 + */ 21.28 + 21.29 +/* 21.30 + * This file is available under and governed by the GNU General Public 21.31 + * License version 2 only, as published by the Free Software Foundation. 21.32 + * However, the following notice accompanied the original version of this 21.33 + * file, and Oracle licenses the original version of this file under the BSD 21.34 + * license: 21.35 + */ 21.36 +/* 21.37 + Copyright 2009-2013 Attila Szegedi 21.38 + 21.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 21.40 + and the BSD License (the "BSD License"), with licensee being free to 21.41 + choose either of the two at their discretion. 21.42 + 21.43 + You may not use this file except in compliance with either the Apache 21.44 + License or the BSD License. 21.45 + 21.46 + If you choose to use this file in compliance with the Apache License, the 21.47 + following notice applies to you: 21.48 + 21.49 + You may obtain a copy of the Apache License at 21.50 + 21.51 + http://www.apache.org/licenses/LICENSE-2.0 21.52 + 21.53 + Unless required by applicable law or agreed to in writing, software 21.54 + distributed under the License is distributed on an "AS IS" BASIS, 21.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 21.56 + implied. See the License for the specific language governing 21.57 + permissions and limitations under the License. 21.58 + 21.59 + If you choose to use this file in compliance with the BSD License, the 21.60 + following notice applies to you: 21.61 + 21.62 + Redistribution and use in source and binary forms, with or without 21.63 + modification, are permitted provided that the following conditions are 21.64 + met: 21.65 + * Redistributions of source code must retain the above copyright 21.66 + notice, this list of conditions and the following disclaimer. 21.67 + * Redistributions in binary form must reproduce the above copyright 21.68 + notice, this list of conditions and the following disclaimer in the 21.69 + documentation and/or other materials provided with the distribution. 21.70 + * Neither the name of the copyright holder nor the names of 21.71 + contributors may be used to endorse or promote products derived from 21.72 + this software without specific prior written permission. 21.73 + 21.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 21.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 21.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 21.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 21.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 21.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 21.85 +*/ 21.86 + 21.87 +package jdk.internal.dynalink.beans; 21.88 + 21.89 +import java.lang.invoke.MethodHandle; 21.90 +import java.lang.invoke.MethodHandles; 21.91 +import java.lang.invoke.MethodType; 21.92 +import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType; 21.93 +import jdk.internal.dynalink.support.Lookup; 21.94 + 21.95 + 21.96 +/** 21.97 + * A linker for java.lang.Class objects. Provides a synthetic property "static" that allows access to static fields and 21.98 + * methods on the class (respecting property getter/setter conventions). Note that Class objects are not recognized by 21.99 + * the Dynalink as constructors for the instances of the class, {@link StaticClass} is used for this purpose. 21.100 + * @author Attila Szegedi 21.101 + */ 21.102 +class ClassLinker extends BeanLinker { 21.103 + 21.104 + ClassLinker() { 21.105 + super(Class.class); 21.106 + // Map "classObject.static" to StaticClass.forClass(classObject). Can use EXACT_CLASS since class Class is final. 21.107 + setPropertyGetter("static", FOR_CLASS, ValidationType.EXACT_CLASS); 21.108 + } 21.109 + 21.110 + private static final MethodHandle FOR_CLASS = new Lookup(MethodHandles.lookup()).findStatic(StaticClass.class, 21.111 + "forClass", MethodType.methodType(StaticClass.class, Class.class)); 21.112 + 21.113 +} 21.114 \ No newline at end of file
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 22.2 +++ b/src/jdk/internal/dynalink/beans/ClassString.java Thu Feb 14 13:22:26 2013 +0100 22.3 @@ -0,0 +1,209 @@ 22.4 +/* 22.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 22.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 22.7 + * 22.8 + * This code is free software; you can redistribute it and/or modify it 22.9 + * under the terms of the GNU General Public License version 2 only, as 22.10 + * published by the Free Software Foundation. Oracle designates this 22.11 + * particular file as subject to the "Classpath" exception as provided 22.12 + * by Oracle in the LICENSE file that accompanied this code. 22.13 + * 22.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 22.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 22.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 22.17 + * version 2 for more details (a copy is included in the LICENSE file that 22.18 + * accompanied this code). 22.19 + * 22.20 + * You should have received a copy of the GNU General Public License version 22.21 + * 2 along with this work; if not, write to the Free Software Foundation, 22.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22.23 + * 22.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22.25 + * or visit www.oracle.com if you need additional information or have any 22.26 + * questions. 22.27 + */ 22.28 + 22.29 +/* 22.30 + * This file is available under and governed by the GNU General Public 22.31 + * License version 2 only, as published by the Free Software Foundation. 22.32 + * However, the following notice accompanied the original version of this 22.33 + * file, and Oracle licenses the original version of this file under the BSD 22.34 + * license: 22.35 + */ 22.36 +/* 22.37 + Copyright 2009-2013 Attila Szegedi 22.38 + 22.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 22.40 + and the BSD License (the "BSD License"), with licensee being free to 22.41 + choose either of the two at their discretion. 22.42 + 22.43 + You may not use this file except in compliance with either the Apache 22.44 + License or the BSD License. 22.45 + 22.46 + If you choose to use this file in compliance with the Apache License, the 22.47 + following notice applies to you: 22.48 + 22.49 + You may obtain a copy of the Apache License at 22.50 + 22.51 + http://www.apache.org/licenses/LICENSE-2.0 22.52 + 22.53 + Unless required by applicable law or agreed to in writing, software 22.54 + distributed under the License is distributed on an "AS IS" BASIS, 22.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 22.56 + implied. See the License for the specific language governing 22.57 + permissions and limitations under the License. 22.58 + 22.59 + If you choose to use this file in compliance with the BSD License, the 22.60 + following notice applies to you: 22.61 + 22.62 + Redistribution and use in source and binary forms, with or without 22.63 + modification, are permitted provided that the following conditions are 22.64 + met: 22.65 + * Redistributions of source code must retain the above copyright 22.66 + notice, this list of conditions and the following disclaimer. 22.67 + * Redistributions in binary form must reproduce the above copyright 22.68 + notice, this list of conditions and the following disclaimer in the 22.69 + documentation and/or other materials provided with the distribution. 22.70 + * Neither the name of the copyright holder nor the names of 22.71 + contributors may be used to endorse or promote products derived from 22.72 + this software without specific prior written permission. 22.73 + 22.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 22.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 22.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 22.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 22.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22.85 +*/ 22.86 + 22.87 +package jdk.internal.dynalink.beans; 22.88 + 22.89 +import java.lang.invoke.MethodHandle; 22.90 +import java.lang.invoke.MethodType; 22.91 +import java.util.LinkedList; 22.92 +import java.util.List; 22.93 +import jdk.internal.dynalink.linker.LinkerServices; 22.94 +import jdk.internal.dynalink.support.Guards; 22.95 +import jdk.internal.dynalink.support.TypeUtilities; 22.96 + 22.97 + 22.98 +/** 22.99 + * 22.100 + * @author Attila Szegedi 22.101 + */ 22.102 +final class ClassString { 22.103 + private final Class<?>[] classes; 22.104 + private int hashCode; 22.105 + 22.106 + ClassString(Class<?>[] classes) { 22.107 + this.classes = classes; 22.108 + } 22.109 + 22.110 + ClassString(MethodType type) { 22.111 + this(type.parameterArray()); 22.112 + } 22.113 + 22.114 + Class<?>[] getClasses() { 22.115 + return classes; 22.116 + } 22.117 + 22.118 + @Override 22.119 + public boolean equals(Object other) { 22.120 + if(!(other instanceof ClassString)) { 22.121 + return false; 22.122 + } 22.123 + final Class<?>[] otherClasses = ((ClassString)other).classes; 22.124 + if(otherClasses.length != classes.length) { 22.125 + return false; 22.126 + } 22.127 + for(int i = 0; i < otherClasses.length; ++i) { 22.128 + if(otherClasses[i] != classes[i]) { 22.129 + return false; 22.130 + } 22.131 + } 22.132 + return true; 22.133 + } 22.134 + 22.135 + @Override 22.136 + public int hashCode() { 22.137 + if(hashCode == 0) { 22.138 + int h = 0; 22.139 + for(int i = 0; i < classes.length; ++i) { 22.140 + h ^= classes[i].hashCode(); 22.141 + } 22.142 + hashCode = h; 22.143 + } 22.144 + return hashCode; 22.145 + } 22.146 + 22.147 + boolean isVisibleFrom(final ClassLoader classLoader) { 22.148 + for(int i = 0; i < classes.length; ++i) { 22.149 + if(!Guards.canReferenceDirectly(classLoader, classes[i].getClassLoader())) { 22.150 + return false; 22.151 + } 22.152 + } 22.153 + return true; 22.154 + } 22.155 + 22.156 + List<MethodHandle> getMaximallySpecifics(List<MethodHandle> methods, LinkerServices linkerServices, boolean varArg) { 22.157 + return MaximallySpecific.getMaximallySpecificMethods(getApplicables(methods, linkerServices, varArg), varArg, 22.158 + classes, linkerServices); 22.159 + } 22.160 + 22.161 + /** 22.162 + * Returns all methods that are applicable to actual parameter classes represented by this ClassString object. 22.163 + */ 22.164 + LinkedList<MethodHandle> getApplicables(List<MethodHandle> methods, LinkerServices linkerServices, boolean varArg) { 22.165 + final LinkedList<MethodHandle> list = new LinkedList<>(); 22.166 + for(final MethodHandle member: methods) { 22.167 + if(isApplicable(member, linkerServices, varArg)) { 22.168 + list.add(member); 22.169 + } 22.170 + } 22.171 + return list; 22.172 + } 22.173 + 22.174 + /** 22.175 + * Returns true if the supplied method is applicable to actual parameter classes represented by this ClassString 22.176 + * object. 22.177 + * 22.178 + */ 22.179 + private boolean isApplicable(MethodHandle method, LinkerServices linkerServices, boolean varArg) { 22.180 + final Class<?>[] formalTypes = method.type().parameterArray(); 22.181 + final int cl = classes.length; 22.182 + final int fl = formalTypes.length - (varArg ? 1 : 0); 22.183 + if(varArg) { 22.184 + if(cl < fl) { 22.185 + return false; 22.186 + } 22.187 + } else { 22.188 + if(cl != fl) { 22.189 + return false; 22.190 + } 22.191 + } 22.192 + // Starting from 1 as we ignore the receiver type 22.193 + for(int i = 1; i < fl; ++i) { 22.194 + if(!canConvert(linkerServices, classes[i], formalTypes[i])) { 22.195 + return false; 22.196 + } 22.197 + } 22.198 + if(varArg) { 22.199 + final Class<?> varArgType = formalTypes[fl].getComponentType(); 22.200 + for(int i = fl; i < cl; ++i) { 22.201 + if(!canConvert(linkerServices, classes[i], varArgType)) { 22.202 + return false; 22.203 + } 22.204 + } 22.205 + } 22.206 + return true; 22.207 + } 22.208 + 22.209 + private static boolean canConvert(LinkerServices ls, Class<?> from, Class<?> to) { 22.210 + return ls == null ? TypeUtilities.isMethodInvocationConvertible(from, to) : ls.canConvert(from, to); 22.211 + } 22.212 +} 22.213 \ No newline at end of file
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 23.2 +++ b/src/jdk/internal/dynalink/beans/DynamicMethod.java Thu Feb 14 13:22:26 2013 +0100 23.3 @@ -0,0 +1,169 @@ 23.4 +/* 23.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 23.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 23.7 + * 23.8 + * This code is free software; you can redistribute it and/or modify it 23.9 + * under the terms of the GNU General Public License version 2 only, as 23.10 + * published by the Free Software Foundation. Oracle designates this 23.11 + * particular file as subject to the "Classpath" exception as provided 23.12 + * by Oracle in the LICENSE file that accompanied this code. 23.13 + * 23.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 23.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 23.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 23.17 + * version 2 for more details (a copy is included in the LICENSE file that 23.18 + * accompanied this code). 23.19 + * 23.20 + * You should have received a copy of the GNU General Public License version 23.21 + * 2 along with this work; if not, write to the Free Software Foundation, 23.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 23.23 + * 23.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23.25 + * or visit www.oracle.com if you need additional information or have any 23.26 + * questions. 23.27 + */ 23.28 + 23.29 +/* 23.30 + * This file is available under and governed by the GNU General Public 23.31 + * License version 2 only, as published by the Free Software Foundation. 23.32 + * However, the following notice accompanied the original version of this 23.33 + * file, and Oracle licenses the original version of this file under the BSD 23.34 + * license: 23.35 + */ 23.36 +/* 23.37 + Copyright 2009-2013 Attila Szegedi 23.38 + 23.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 23.40 + and the BSD License (the "BSD License"), with licensee being free to 23.41 + choose either of the two at their discretion. 23.42 + 23.43 + You may not use this file except in compliance with either the Apache 23.44 + License or the BSD License. 23.45 + 23.46 + If you choose to use this file in compliance with the Apache License, the 23.47 + following notice applies to you: 23.48 + 23.49 + You may obtain a copy of the Apache License at 23.50 + 23.51 + http://www.apache.org/licenses/LICENSE-2.0 23.52 + 23.53 + Unless required by applicable law or agreed to in writing, software 23.54 + distributed under the License is distributed on an "AS IS" BASIS, 23.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 23.56 + implied. See the License for the specific language governing 23.57 + permissions and limitations under the License. 23.58 + 23.59 + If you choose to use this file in compliance with the BSD License, the 23.60 + following notice applies to you: 23.61 + 23.62 + Redistribution and use in source and binary forms, with or without 23.63 + modification, are permitted provided that the following conditions are 23.64 + met: 23.65 + * Redistributions of source code must retain the above copyright 23.66 + notice, this list of conditions and the following disclaimer. 23.67 + * Redistributions in binary form must reproduce the above copyright 23.68 + notice, this list of conditions and the following disclaimer in the 23.69 + documentation and/or other materials provided with the distribution. 23.70 + * Neither the name of the copyright holder nor the names of 23.71 + contributors may be used to endorse or promote products derived from 23.72 + this software without specific prior written permission. 23.73 + 23.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 23.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 23.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 23.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23.85 +*/ 23.86 + 23.87 +package jdk.internal.dynalink.beans; 23.88 + 23.89 +import java.lang.invoke.MethodHandle; 23.90 +import java.lang.invoke.MethodType; 23.91 +import java.util.StringTokenizer; 23.92 +import jdk.internal.dynalink.linker.LinkerServices; 23.93 + 23.94 + 23.95 +/** 23.96 + * Represents a single dynamic method. A "dynamic" method can be bound to a single Java method, or can be bound to all 23.97 + * overloaded methods of the same name on a class. Getting an invocation of a dynamic method bound to multiple 23.98 + * overloaded methods will perform overload resolution (actually, it will perform partial overloaded resolution at link 23.99 + * time, but if that fails to identify exactly one target method, it will generate a method handle that will perform the 23.100 + * rest of the overload resolution at invocation time for actual argument types). 23.101 + * 23.102 + * @author Attila Szegedi 23.103 + */ 23.104 +abstract class DynamicMethod { 23.105 + private final String name; 23.106 + 23.107 + DynamicMethod(String name) { 23.108 + this.name = name; 23.109 + } 23.110 + 23.111 + String getName() { 23.112 + return name; 23.113 + } 23.114 + 23.115 + /** 23.116 + * Creates an invocation for the dynamic method. If the method is overloaded, it will perform overloaded method 23.117 + * resolution based on the specified method type. The resulting resolution can either identify a single method to be 23.118 + * invoked among the overloads, or it can identify multiple ones. In the latter case, the returned method handle 23.119 + * will perform further overload resolution among these candidates at every invocation. If the method to be invoked 23.120 + * is a variable arguments (vararg) method, it will pack the extra arguments in an array before the invocation of 23.121 + * the underlying method if it is not already done. 23.122 + * 23.123 + * @param callSiteType the method type at a call site 23.124 + * @param linkerServices linker services. Used for language-specific type conversions. 23.125 + * @return an invocation suitable for calling the method from the specified call site. 23.126 + */ 23.127 + abstract MethodHandle getInvocation(MethodType callSiteType, LinkerServices linkerServices); 23.128 + 23.129 + /** 23.130 + * Returns a simple dynamic method representing a single underlying Java method (possibly selected among several 23.131 + * overloads) with formal parameter types exactly matching the passed signature. 23.132 + * @param paramTypes the comma-separated list of requested parameter type names. The names will match both 23.133 + * qualified and unqualified type names. 23.134 + * @return a simple dynamic method representing a single underlying Java method, or null if none of the Java methods 23.135 + * behind this dynamic method exactly match the requested parameter types. 23.136 + */ 23.137 + abstract SimpleDynamicMethod getMethodForExactParamTypes(String paramTypes); 23.138 + 23.139 + /** 23.140 + * True if this dynamic method already contains a method handle with an identical signature as the passed in method 23.141 + * handle. 23.142 + * @param mh the method handle to check 23.143 + * @return true if it already contains an equivalent method handle. 23.144 + */ 23.145 + abstract boolean contains(MethodHandle mh); 23.146 + 23.147 + static boolean typeMatchesDescription(String paramTypes, MethodType type) { 23.148 + final StringTokenizer tok = new StringTokenizer(paramTypes, ", "); 23.149 + for(int i = 1; i < type.parameterCount(); ++i) { // i = 1 as we ignore the receiver 23.150 + if(!(tok.hasMoreTokens() && typeNameMatches(tok.nextToken(), type.parameterType(i)))) { 23.151 + return false; 23.152 + } 23.153 + } 23.154 + return !tok.hasMoreTokens(); 23.155 + } 23.156 + 23.157 + private static boolean typeNameMatches(String typeName, Class<?> type) { 23.158 + final int lastDot = typeName.lastIndexOf('.'); 23.159 + final String fullTypeName = type.getCanonicalName(); 23.160 + return lastDot != -1 && fullTypeName.endsWith(typeName.substring(lastDot)) || typeName.equals(fullTypeName); 23.161 + } 23.162 + 23.163 + static String getClassAndMethodName(Class<?> clazz, String name) { 23.164 + final String clazzName = clazz.getCanonicalName(); 23.165 + return (clazzName == null ? clazz.getName() : clazzName) + "." + name; 23.166 + } 23.167 + 23.168 + @Override 23.169 + public String toString() { 23.170 + return "[" + getClass().getName() + " " + getName() + "]"; 23.171 + } 23.172 +} 23.173 \ No newline at end of file
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 24.2 +++ b/src/jdk/internal/dynalink/beans/DynamicMethodLinker.java Thu Feb 14 13:22:26 2013 +0100 24.3 @@ -0,0 +1,130 @@ 24.4 +/* 24.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 24.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 24.7 + * 24.8 + * This code is free software; you can redistribute it and/or modify it 24.9 + * under the terms of the GNU General Public License version 2 only, as 24.10 + * published by the Free Software Foundation. Oracle designates this 24.11 + * particular file as subject to the "Classpath" exception as provided 24.12 + * by Oracle in the LICENSE file that accompanied this code. 24.13 + * 24.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 24.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 24.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 24.17 + * version 2 for more details (a copy is included in the LICENSE file that 24.18 + * accompanied this code). 24.19 + * 24.20 + * You should have received a copy of the GNU General Public License version 24.21 + * 2 along with this work; if not, write to the Free Software Foundation, 24.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24.23 + * 24.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24.25 + * or visit www.oracle.com if you need additional information or have any 24.26 + * questions. 24.27 + */ 24.28 + 24.29 +/* 24.30 + * This file is available under and governed by the GNU General Public 24.31 + * License version 2 only, as published by the Free Software Foundation. 24.32 + * However, the following notice accompanied the original version of this 24.33 + * file, and Oracle licenses the original version of this file under the BSD 24.34 + * license: 24.35 + */ 24.36 +/* 24.37 + Copyright 2009-2013 Attila Szegedi 24.38 + 24.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 24.40 + and the BSD License (the "BSD License"), with licensee being free to 24.41 + choose either of the two at their discretion. 24.42 + 24.43 + You may not use this file except in compliance with either the Apache 24.44 + License or the BSD License. 24.45 + 24.46 + If you choose to use this file in compliance with the Apache License, the 24.47 + following notice applies to you: 24.48 + 24.49 + You may obtain a copy of the Apache License at 24.50 + 24.51 + http://www.apache.org/licenses/LICENSE-2.0 24.52 + 24.53 + Unless required by applicable law or agreed to in writing, software 24.54 + distributed under the License is distributed on an "AS IS" BASIS, 24.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 24.56 + implied. See the License for the specific language governing 24.57 + permissions and limitations under the License. 24.58 + 24.59 + If you choose to use this file in compliance with the BSD License, the 24.60 + following notice applies to you: 24.61 + 24.62 + Redistribution and use in source and binary forms, with or without 24.63 + modification, are permitted provided that the following conditions are 24.64 + met: 24.65 + * Redistributions of source code must retain the above copyright 24.66 + notice, this list of conditions and the following disclaimer. 24.67 + * Redistributions in binary form must reproduce the above copyright 24.68 + notice, this list of conditions and the following disclaimer in the 24.69 + documentation and/or other materials provided with the distribution. 24.70 + * Neither the name of the copyright holder nor the names of 24.71 + contributors may be used to endorse or promote products derived from 24.72 + this software without specific prior written permission. 24.73 + 24.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 24.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 24.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 24.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24.85 +*/ 24.86 + 24.87 +package jdk.internal.dynalink.beans; 24.88 + 24.89 +import java.lang.invoke.MethodHandle; 24.90 +import java.lang.invoke.MethodHandles; 24.91 +import java.lang.invoke.MethodType; 24.92 +import jdk.internal.dynalink.CallSiteDescriptor; 24.93 +import jdk.internal.dynalink.linker.GuardedInvocation; 24.94 +import jdk.internal.dynalink.linker.LinkRequest; 24.95 +import jdk.internal.dynalink.linker.LinkerServices; 24.96 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 24.97 +import jdk.internal.dynalink.support.Guards; 24.98 + 24.99 + 24.100 +/** 24.101 + * Simple linker that implements the "dyn:call" operation for {@link DynamicMethod} objects - the objects returned by 24.102 + * "dyn:getMethod" from {@link AbstractJavaLinker}. 24.103 + */ 24.104 +class DynamicMethodLinker implements TypeBasedGuardingDynamicLinker { 24.105 + @Override 24.106 + public boolean canLinkType(Class<?> type) { 24.107 + return DynamicMethod.class.isAssignableFrom(type); 24.108 + }; 24.109 + 24.110 + @Override 24.111 + public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) { 24.112 + final Object receiver = linkRequest.getReceiver(); 24.113 + if(!(receiver instanceof DynamicMethod)) { 24.114 + return null; 24.115 + } 24.116 + final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor(); 24.117 + if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") { 24.118 + return null; 24.119 + } 24.120 + final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR); 24.121 + if(operator == "call") { 24.122 + final MethodType type = desc.getMethodType(); 24.123 + final MethodHandle invocation = ((DynamicMethod)receiver).getInvocation(type.dropParameterTypes(0, 1), 24.124 + linkerServices); 24.125 + if(invocation == null) { 24.126 + return null; 24.127 + } 24.128 + return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0, type.parameterType(0)), 24.129 + Guards.getIdentityGuard(receiver)); 24.130 + } 24.131 + return null; 24.132 + } 24.133 +}
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 25.2 +++ b/src/jdk/internal/dynalink/beans/FacetIntrospector.java Thu Feb 14 13:22:26 2013 +0100 25.3 @@ -0,0 +1,188 @@ 25.4 +/* 25.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 25.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 25.7 + * 25.8 + * This code is free software; you can redistribute it and/or modify it 25.9 + * under the terms of the GNU General Public License version 2 only, as 25.10 + * published by the Free Software Foundation. Oracle designates this 25.11 + * particular file as subject to the "Classpath" exception as provided 25.12 + * by Oracle in the LICENSE file that accompanied this code. 25.13 + * 25.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 25.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 25.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 25.17 + * version 2 for more details (a copy is included in the LICENSE file that 25.18 + * accompanied this code). 25.19 + * 25.20 + * You should have received a copy of the GNU General Public License version 25.21 + * 2 along with this work; if not, write to the Free Software Foundation, 25.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 25.23 + * 25.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 25.25 + * or visit www.oracle.com if you need additional information or have any 25.26 + * questions. 25.27 + */ 25.28 + 25.29 +/* 25.30 + * This file is available under and governed by the GNU General Public 25.31 + * License version 2 only, as published by the Free Software Foundation. 25.32 + * However, the following notice accompanied the original version of this 25.33 + * file, and Oracle licenses the original version of this file under the BSD 25.34 + * license: 25.35 + */ 25.36 +/* 25.37 + Copyright 2009-2013 Attila Szegedi 25.38 + 25.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 25.40 + and the BSD License (the "BSD License"), with licensee being free to 25.41 + choose either of the two at their discretion. 25.42 + 25.43 + You may not use this file except in compliance with either the Apache 25.44 + License or the BSD License. 25.45 + 25.46 + If you choose to use this file in compliance with the Apache License, the 25.47 + following notice applies to you: 25.48 + 25.49 + You may obtain a copy of the Apache License at 25.50 + 25.51 + http://www.apache.org/licenses/LICENSE-2.0 25.52 + 25.53 + Unless required by applicable law or agreed to in writing, software 25.54 + distributed under the License is distributed on an "AS IS" BASIS, 25.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 25.56 + implied. See the License for the specific language governing 25.57 + permissions and limitations under the License. 25.58 + 25.59 + If you choose to use this file in compliance with the BSD License, the 25.60 + following notice applies to you: 25.61 + 25.62 + Redistribution and use in source and binary forms, with or without 25.63 + modification, are permitted provided that the following conditions are 25.64 + met: 25.65 + * Redistributions of source code must retain the above copyright 25.66 + notice, this list of conditions and the following disclaimer. 25.67 + * Redistributions in binary form must reproduce the above copyright 25.68 + notice, this list of conditions and the following disclaimer in the 25.69 + documentation and/or other materials provided with the distribution. 25.70 + * Neither the name of the copyright holder nor the names of 25.71 + contributors may be used to endorse or promote products derived from 25.72 + this software without specific prior written permission. 25.73 + 25.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 25.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 25.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 25.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.85 +*/ 25.86 + 25.87 +package jdk.internal.dynalink.beans; 25.88 + 25.89 +import java.lang.invoke.MethodHandle; 25.90 +import java.lang.reflect.Field; 25.91 +import java.lang.reflect.Member; 25.92 +import java.lang.reflect.Method; 25.93 +import java.lang.reflect.Modifier; 25.94 +import java.util.ArrayList; 25.95 +import java.util.Collection; 25.96 +import java.util.Collections; 25.97 +import java.util.Map; 25.98 +import jdk.internal.dynalink.support.Lookup; 25.99 + 25.100 + 25.101 +/** 25.102 + * Base for classes that expose class field and method information to an {@link AbstractJavaLinker}. There are 25.103 + * subclasses for instance (bean) and static facet of a class. 25.104 + * @author Attila Szegedi 25.105 + */ 25.106 +abstract class FacetIntrospector implements AutoCloseable { 25.107 + private final Class<?> clazz; 25.108 + private final boolean instance; 25.109 + private final boolean isRestricted; 25.110 + 25.111 + protected final AccessibleMembersLookup membersLookup; 25.112 + 25.113 + FacetIntrospector(Class<?> clazz, boolean instance) { 25.114 + this.clazz = clazz; 25.115 + this.instance = instance; 25.116 + isRestricted = CheckRestrictedPackage.isRestrictedClass(clazz); 25.117 + membersLookup = new AccessibleMembersLookup(clazz, instance); 25.118 + } 25.119 + 25.120 + /** 25.121 + * Returns getters for inner classes. 25.122 + * @return getters for inner classes. 25.123 + */ 25.124 + abstract Map<String, MethodHandle> getInnerClassGetters(); 25.125 + 25.126 + /** 25.127 + * Returns the fields for the class facet. 25.128 + * @return the fields for the class facet. 25.129 + */ 25.130 + Collection<Field> getFields() { 25.131 + if(isRestricted) { 25.132 + // NOTE: we can't do anything here. Unlike with methods in AccessibleMethodsLookup, we can't just return 25.133 + // the fields from a public superclass, because this class might define same-named fields which will shadow 25.134 + // the superclass fields, and we have no way to know if they do, since we're denied invocation of 25.135 + // getFields(). Therefore, the only correct course of action is to not expose any public fields from a class 25.136 + // defined in a restricted package. 25.137 + return Collections.emptySet(); 25.138 + } 25.139 + 25.140 + final Field[] fields = clazz.getFields(); 25.141 + final Collection<Field> cfields = new ArrayList<>(fields.length); 25.142 + for(Field field: fields) { 25.143 + if(instance != Modifier.isStatic(field.getModifiers()) && isAccessible(field)) { 25.144 + cfields.add(field); 25.145 + } 25.146 + } 25.147 + return cfields; 25.148 + } 25.149 + 25.150 + boolean isAccessible(Member m) { 25.151 + final Class<?> declaring = m.getDeclaringClass(); 25.152 + // (declaring == clazz) is just an optimization - we're calling this only from code that operates on a 25.153 + // non-restriced class, so if the declaring class is identical to the class being inspected, then forego 25.154 + // a potentially expensive restricted-package check. 25.155 + return declaring == clazz || !CheckRestrictedPackage.isRestrictedClass(declaring); 25.156 + } 25.157 + 25.158 + /** 25.159 + * Returns all the methods in the facet. 25.160 + * @return all the methods in the facet. 25.161 + */ 25.162 + Collection<Method> getMethods() { 25.163 + return membersLookup.getMethods(); 25.164 + } 25.165 + 25.166 + 25.167 + MethodHandle unreflectGetter(Field field) { 25.168 + return editMethodHandle(Lookup.PUBLIC.unreflectGetter(field)); 25.169 + } 25.170 + 25.171 + MethodHandle unreflectSetter(Field field) { 25.172 + return editMethodHandle(Lookup.PUBLIC.unreflectSetter(field)); 25.173 + } 25.174 + 25.175 + MethodHandle unreflect(Method method) { 25.176 + return editMethodHandle(Lookup.PUBLIC.unreflect(method)); 25.177 + } 25.178 + 25.179 + /** 25.180 + * Returns an edited method handle. A facet might need to edit an unreflected method handle before it is usable with 25.181 + * the facet. By default, returns the passed method handle unchanged. The class' static facet will introduce a 25.182 + * dropArguments. 25.183 + * @param mh the method handle to edit. 25.184 + * @return the edited method handle. 25.185 + */ 25.186 + abstract MethodHandle editMethodHandle(MethodHandle mh); 25.187 + 25.188 + @Override 25.189 + public void close() { 25.190 + } 25.191 +} 25.192 \ No newline at end of file
26.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 26.2 +++ b/src/jdk/internal/dynalink/beans/GuardedInvocationComponent.java Thu Feb 14 13:22:26 2013 +0100 26.3 @@ -0,0 +1,251 @@ 26.4 +/* 26.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 26.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 26.7 + * 26.8 + * This code is free software; you can redistribute it and/or modify it 26.9 + * under the terms of the GNU General Public License version 2 only, as 26.10 + * published by the Free Software Foundation. Oracle designates this 26.11 + * particular file as subject to the "Classpath" exception as provided 26.12 + * by Oracle in the LICENSE file that accompanied this code. 26.13 + * 26.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 26.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 26.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 26.17 + * version 2 for more details (a copy is included in the LICENSE file that 26.18 + * accompanied this code). 26.19 + * 26.20 + * You should have received a copy of the GNU General Public License version 26.21 + * 2 along with this work; if not, write to the Free Software Foundation, 26.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 26.23 + * 26.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 26.25 + * or visit www.oracle.com if you need additional information or have any 26.26 + * questions. 26.27 + */ 26.28 + 26.29 +/* 26.30 + * This file is available under and governed by the GNU General Public 26.31 + * License version 2 only, as published by the Free Software Foundation. 26.32 + * However, the following notice accompanied the original version of this 26.33 + * file, and Oracle licenses the original version of this file under the BSD 26.34 + * license: 26.35 + */ 26.36 +/* 26.37 + Copyright 2009-2013 Attila Szegedi 26.38 + 26.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 26.40 + and the BSD License (the "BSD License"), with licensee being free to 26.41 + choose either of the two at their discretion. 26.42 + 26.43 + You may not use this file except in compliance with either the Apache 26.44 + License or the BSD License. 26.45 + 26.46 + If you choose to use this file in compliance with the Apache License, the 26.47 + following notice applies to you: 26.48 + 26.49 + You may obtain a copy of the Apache License at 26.50 + 26.51 + http://www.apache.org/licenses/LICENSE-2.0 26.52 + 26.53 + Unless required by applicable law or agreed to in writing, software 26.54 + distributed under the License is distributed on an "AS IS" BASIS, 26.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 26.56 + implied. See the License for the specific language governing 26.57 + permissions and limitations under the License. 26.58 + 26.59 + If you choose to use this file in compliance with the BSD License, the 26.60 + following notice applies to you: 26.61 + 26.62 + Redistribution and use in source and binary forms, with or without 26.63 + modification, are permitted provided that the following conditions are 26.64 + met: 26.65 + * Redistributions of source code must retain the above copyright 26.66 + notice, this list of conditions and the following disclaimer. 26.67 + * Redistributions in binary form must reproduce the above copyright 26.68 + notice, this list of conditions and the following disclaimer in the 26.69 + documentation and/or other materials provided with the distribution. 26.70 + * Neither the name of the copyright holder nor the names of 26.71 + contributors may be used to endorse or promote products derived from 26.72 + this software without specific prior written permission. 26.73 + 26.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 26.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 26.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 26.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 26.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26.85 +*/ 26.86 + 26.87 +package jdk.internal.dynalink.beans; 26.88 + 26.89 +import java.lang.invoke.MethodHandle; 26.90 +import jdk.internal.dynalink.linker.GuardedInvocation; 26.91 + 26.92 + 26.93 +/** 26.94 + * Represents one component for a GuardedInvocation of a potentially composite operation of an 26.95 + * {@link AbstractJavaLinker}. In addition to holding a guarded invocation, it holds semantic information about its 26.96 + * guard. All guards produced in the AbstractJavaLinker are either "Class.isInstance()" or "getClass() == clazz" 26.97 + * expressions. This allows choosing the most restrictive guard as the guard for the composition of two components. 26.98 + * @author Attila Szegedi 26.99 + * @version $Id: $ 26.100 + */ 26.101 +class GuardedInvocationComponent { 26.102 + enum ValidationType { 26.103 + NONE, // No guard; the operation can be linked unconditionally (quite rare); least strict. 26.104 + INSTANCE_OF, // "validatorClass.isInstance(obj)" guard 26.105 + EXACT_CLASS, // "obj.getClass() == validatorClass" guard; most strict. 26.106 + IS_ARRAY, // "obj.getClass().isArray()" 26.107 + } 26.108 + 26.109 + private final GuardedInvocation guardedInvocation; 26.110 + private final Validator validator; 26.111 + 26.112 + GuardedInvocationComponent(MethodHandle invocation) { 26.113 + this(invocation, null, ValidationType.NONE); 26.114 + } 26.115 + 26.116 + GuardedInvocationComponent(MethodHandle invocation, MethodHandle guard, ValidationType validationType) { 26.117 + this(invocation, guard, null, validationType); 26.118 + } 26.119 + 26.120 + GuardedInvocationComponent(MethodHandle invocation, MethodHandle guard, Class<?> validatorClass, 26.121 + ValidationType validationType) { 26.122 + this(invocation, guard, new Validator(validatorClass, validationType)); 26.123 + } 26.124 + 26.125 + GuardedInvocationComponent(GuardedInvocation guardedInvocation, Class<?> validatorClass, 26.126 + ValidationType validationType) { 26.127 + this(guardedInvocation, new Validator(validatorClass, validationType)); 26.128 + } 26.129 + 26.130 + GuardedInvocationComponent replaceInvocation(MethodHandle newInvocation) { 26.131 + return replaceInvocation(newInvocation, guardedInvocation.getGuard()); 26.132 + } 26.133 + 26.134 + GuardedInvocationComponent replaceInvocation(MethodHandle newInvocation, MethodHandle newGuard) { 26.135 + return new GuardedInvocationComponent(guardedInvocation.replaceMethods(newInvocation, 26.136 + newGuard), validator); 26.137 + } 26.138 + 26.139 + private GuardedInvocationComponent(MethodHandle invocation, MethodHandle guard, Validator validator) { 26.140 + this(new GuardedInvocation(invocation, guard), validator); 26.141 + } 26.142 + 26.143 + private GuardedInvocationComponent(GuardedInvocation guardedInvocation, Validator validator) { 26.144 + this.guardedInvocation = guardedInvocation; 26.145 + this.validator = validator; 26.146 + } 26.147 + 26.148 + GuardedInvocation getGuardedInvocation() { 26.149 + return guardedInvocation; 26.150 + } 26.151 + 26.152 + Class<?> getValidatorClass() { 26.153 + return validator.validatorClass; 26.154 + } 26.155 + 26.156 + ValidationType getValidationType() { 26.157 + return validator.validationType; 26.158 + } 26.159 + 26.160 + GuardedInvocationComponent compose(MethodHandle compositeInvocation, MethodHandle otherGuard, 26.161 + Class<?> otherValidatorClass, ValidationType otherValidationType) { 26.162 + final Validator compositeValidator = validator.compose(new Validator(otherValidatorClass, otherValidationType)); 26.163 + final MethodHandle compositeGuard = compositeValidator == validator ? guardedInvocation.getGuard() : otherGuard; 26.164 + return new GuardedInvocationComponent(compositeInvocation, compositeGuard, compositeValidator); 26.165 + } 26.166 + 26.167 + private static class Validator { 26.168 + /*private*/ final Class<?> validatorClass; 26.169 + /*private*/ final ValidationType validationType; 26.170 + 26.171 + Validator(Class<?> validatorClass, ValidationType validationType) { 26.172 + this.validatorClass = validatorClass; 26.173 + this.validationType = validationType; 26.174 + } 26.175 + 26.176 + Validator compose(Validator other) { 26.177 + if(other.validationType == ValidationType.NONE) { 26.178 + return this; 26.179 + } 26.180 + switch(validationType) { 26.181 + case NONE: 26.182 + return other; 26.183 + case INSTANCE_OF: 26.184 + switch(other.validationType) { 26.185 + case INSTANCE_OF: 26.186 + if(isAssignableFrom(other)) { 26.187 + return other; 26.188 + } else if(other.isAssignableFrom(this)) { 26.189 + return this; 26.190 + } 26.191 + break; 26.192 + case EXACT_CLASS: 26.193 + if(isAssignableFrom(other)) { 26.194 + return other; 26.195 + } 26.196 + break; 26.197 + case IS_ARRAY: 26.198 + if(validatorClass.isArray()) { 26.199 + return this; 26.200 + } 26.201 + break; 26.202 + case NONE: 26.203 + throw new AssertionError(); // Not possible 26.204 + } 26.205 + break; 26.206 + case EXACT_CLASS: 26.207 + switch(other.validationType) { 26.208 + case INSTANCE_OF: 26.209 + if(other.isAssignableFrom(this)) { 26.210 + return this; 26.211 + } 26.212 + break; 26.213 + case EXACT_CLASS: 26.214 + if(validatorClass == other.validatorClass) { 26.215 + return this; 26.216 + } 26.217 + break; 26.218 + case IS_ARRAY: 26.219 + if(validatorClass.isArray()) { 26.220 + return this; 26.221 + } 26.222 + break; 26.223 + case NONE: 26.224 + throw new AssertionError(); // Not possible 26.225 + } 26.226 + break; 26.227 + case IS_ARRAY: 26.228 + switch(other.validationType) { 26.229 + case INSTANCE_OF: 26.230 + case EXACT_CLASS: 26.231 + if(other.validatorClass.isArray()) { 26.232 + return other; 26.233 + } 26.234 + break; 26.235 + case IS_ARRAY: 26.236 + return this; 26.237 + case NONE: 26.238 + throw new AssertionError(); // Not possible 26.239 + } 26.240 + break; 26.241 + } 26.242 + throw new AssertionError("Incompatible composition " + this + " vs " + other); 26.243 + } 26.244 + 26.245 + private boolean isAssignableFrom(Validator other) { 26.246 + return validatorClass.isAssignableFrom(other.validatorClass); 26.247 + } 26.248 + 26.249 + @Override 26.250 + public String toString() { 26.251 + return "Validator[" + validationType + (validatorClass == null ? "" : (" " + validatorClass.getName())) + "]"; 26.252 + } 26.253 + } 26.254 +}
27.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 27.2 +++ b/src/jdk/internal/dynalink/beans/MaximallySpecific.java Thu Feb 14 13:22:26 2013 +0100 27.3 @@ -0,0 +1,211 @@ 27.4 +/* 27.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 27.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 27.7 + * 27.8 + * This code is free software; you can redistribute it and/or modify it 27.9 + * under the terms of the GNU General Public License version 2 only, as 27.10 + * published by the Free Software Foundation. Oracle designates this 27.11 + * particular file as subject to the "Classpath" exception as provided 27.12 + * by Oracle in the LICENSE file that accompanied this code. 27.13 + * 27.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 27.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 27.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 27.17 + * version 2 for more details (a copy is included in the LICENSE file that 27.18 + * accompanied this code). 27.19 + * 27.20 + * You should have received a copy of the GNU General Public License version 27.21 + * 2 along with this work; if not, write to the Free Software Foundation, 27.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 27.23 + * 27.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 27.25 + * or visit www.oracle.com if you need additional information or have any 27.26 + * questions. 27.27 + */ 27.28 + 27.29 +/* 27.30 + * This file is available under and governed by the GNU General Public 27.31 + * License version 2 only, as published by the Free Software Foundation. 27.32 + * However, the following notice accompanied the original version of this 27.33 + * file, and Oracle licenses the original version of this file under the BSD 27.34 + * license: 27.35 + */ 27.36 +/* 27.37 + Copyright 2009-2013 Attila Szegedi 27.38 + 27.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 27.40 + and the BSD License (the "BSD License"), with licensee being free to 27.41 + choose either of the two at their discretion. 27.42 + 27.43 + You may not use this file except in compliance with either the Apache 27.44 + License or the BSD License. 27.45 + 27.46 + If you choose to use this file in compliance with the Apache License, the 27.47 + following notice applies to you: 27.48 + 27.49 + You may obtain a copy of the Apache License at 27.50 + 27.51 + http://www.apache.org/licenses/LICENSE-2.0 27.52 + 27.53 + Unless required by applicable law or agreed to in writing, software 27.54 + distributed under the License is distributed on an "AS IS" BASIS, 27.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 27.56 + implied. See the License for the specific language governing 27.57 + permissions and limitations under the License. 27.58 + 27.59 + If you choose to use this file in compliance with the BSD License, the 27.60 + following notice applies to you: 27.61 + 27.62 + Redistribution and use in source and binary forms, with or without 27.63 + modification, are permitted provided that the following conditions are 27.64 + met: 27.65 + * Redistributions of source code must retain the above copyright 27.66 + notice, this list of conditions and the following disclaimer. 27.67 + * Redistributions in binary form must reproduce the above copyright 27.68 + notice, this list of conditions and the following disclaimer in the 27.69 + documentation and/or other materials provided with the distribution. 27.70 + * Neither the name of the copyright holder nor the names of 27.71 + contributors may be used to endorse or promote products derived from 27.72 + this software without specific prior written permission. 27.73 + 27.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 27.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 27.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 27.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.85 +*/ 27.86 + 27.87 +package jdk.internal.dynalink.beans; 27.88 + 27.89 +import java.lang.invoke.MethodHandle; 27.90 +import java.lang.invoke.MethodType; 27.91 +import java.util.Iterator; 27.92 +import java.util.LinkedList; 27.93 +import java.util.List; 27.94 +import jdk.internal.dynalink.linker.ConversionComparator.Comparison; 27.95 +import jdk.internal.dynalink.linker.LinkerServices; 27.96 +import jdk.internal.dynalink.support.TypeUtilities; 27.97 + 27.98 + 27.99 +/** 27.100 + * Utility class that encapsulates the algorithm for choosing the maximally specific methods. 27.101 + * 27.102 + * @author Attila Szegedi 27.103 + */ 27.104 +class MaximallySpecific { 27.105 + /** 27.106 + * Given a list of methods, returns a list of maximally specific methods. 27.107 + * 27.108 + * @param methods the list of methods 27.109 + * @param varArgs whether to assume the methods are varargs 27.110 + * @return the list of maximally specific methods. 27.111 + */ 27.112 + static List<MethodHandle> getMaximallySpecificMethods(List<MethodHandle> methods, boolean varArgs) { 27.113 + return getMaximallySpecificMethods(methods, varArgs, null, null); 27.114 + } 27.115 + 27.116 + /** 27.117 + * Given a list of methods, returns a list of maximally specific methods, applying language-runtime specific 27.118 + * conversion preferences. 27.119 + * 27.120 + * @param methods the list of methods 27.121 + * @param varArgs whether to assume the methods are varargs 27.122 + * @param argTypes concrete argument types for the invocation 27.123 + * @return the list of maximally specific methods. 27.124 + */ 27.125 + static List<MethodHandle> getMaximallySpecificMethods(List<MethodHandle> methods, boolean varArgs, 27.126 + Class<?>[] argTypes, LinkerServices ls) { 27.127 + if(methods.size() < 2) { 27.128 + return methods; 27.129 + } 27.130 + final LinkedList<MethodHandle> maximals = new LinkedList<>(); 27.131 + for(MethodHandle m: methods) { 27.132 + final MethodType methodType = m.type(); 27.133 + boolean lessSpecific = false; 27.134 + for(Iterator<MethodHandle> maximal = maximals.iterator(); maximal.hasNext();) { 27.135 + final MethodHandle max = maximal.next(); 27.136 + switch(isMoreSpecific(methodType, max.type(), varArgs, argTypes, ls)) { 27.137 + case TYPE_1_BETTER: { 27.138 + maximal.remove(); 27.139 + break; 27.140 + } 27.141 + case TYPE_2_BETTER: { 27.142 + lessSpecific = true; 27.143 + break; 27.144 + } 27.145 + case INDETERMINATE: { 27.146 + // do nothing 27.147 + } 27.148 + } 27.149 + } 27.150 + if(!lessSpecific) { 27.151 + maximals.addLast(m); 27.152 + } 27.153 + } 27.154 + return maximals; 27.155 + } 27.156 + 27.157 + private static Comparison isMoreSpecific(MethodType t1, MethodType t2, boolean varArgs, Class<?>[] argTypes, 27.158 + LinkerServices ls) { 27.159 + final int pc1 = t1.parameterCount(); 27.160 + final int pc2 = t2.parameterCount(); 27.161 + assert varArgs || (pc1 == pc2) && (argTypes == null || argTypes.length == pc1); 27.162 + assert (argTypes == null) == (ls == null); 27.163 + final int maxPc = Math.max(Math.max(pc1, pc2), argTypes == null ? 0 : argTypes.length); 27.164 + boolean t1MoreSpecific = false; 27.165 + boolean t2MoreSpecific = false; 27.166 + // NOTE: Starting from 1 as overloaded method resolution doesn't depend on 0th element, which is the type of 27.167 + // 'this'. We're only dealing with instance methods here, not static methods. Actually, static methods will have 27.168 + // a fake 'this' of type StaticClass. 27.169 + for(int i = 1; i < maxPc; ++i) { 27.170 + final Class<?> c1 = getParameterClass(t1, pc1, i, varArgs); 27.171 + final Class<?> c2 = getParameterClass(t2, pc2, i, varArgs); 27.172 + if(c1 != c2) { 27.173 + final Comparison cmp = compare(c1, c2, argTypes, i, ls); 27.174 + if(cmp == Comparison.TYPE_1_BETTER && !t1MoreSpecific) { 27.175 + t1MoreSpecific = true; 27.176 + if(t2MoreSpecific) { 27.177 + return Comparison.INDETERMINATE; 27.178 + } 27.179 + } 27.180 + if(cmp == Comparison.TYPE_2_BETTER && !t2MoreSpecific) { 27.181 + t2MoreSpecific = true; 27.182 + if(t1MoreSpecific) { 27.183 + return Comparison.INDETERMINATE; 27.184 + } 27.185 + } 27.186 + } 27.187 + } 27.188 + if(t1MoreSpecific) { 27.189 + return Comparison.TYPE_1_BETTER; 27.190 + } else if(t2MoreSpecific) { 27.191 + return Comparison.TYPE_2_BETTER; 27.192 + } 27.193 + return Comparison.INDETERMINATE; 27.194 + } 27.195 + 27.196 + private static Comparison compare(Class<?> c1, Class<?> c2, Class<?>[] argTypes, int i, LinkerServices cmp) { 27.197 + if(cmp != null) { 27.198 + final Comparison c = cmp.compareConversion(argTypes[i], c1, c2); 27.199 + if(c != Comparison.INDETERMINATE) { 27.200 + return c; 27.201 + } 27.202 + } 27.203 + if(TypeUtilities.isSubtype(c1, c2)) { 27.204 + return Comparison.TYPE_1_BETTER; 27.205 + } if(TypeUtilities.isSubtype(c2, c1)) { 27.206 + return Comparison.TYPE_2_BETTER; 27.207 + } 27.208 + return Comparison.INDETERMINATE; 27.209 + } 27.210 + 27.211 + private static Class<?> getParameterClass(MethodType t, int l, int i, boolean varArgs) { 27.212 + return varArgs && i >= l - 1 ? t.parameterType(l - 1).getComponentType() : t.parameterType(i); 27.213 + } 27.214 +}
28.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 28.2 +++ b/src/jdk/internal/dynalink/beans/OverloadedDynamicMethod.java Thu Feb 14 13:22:26 2013 +0100 28.3 @@ -0,0 +1,313 @@ 28.4 +/* 28.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 28.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 28.7 + * 28.8 + * This code is free software; you can redistribute it and/or modify it 28.9 + * under the terms of the GNU General Public License version 2 only, as 28.10 + * published by the Free Software Foundation. Oracle designates this 28.11 + * particular file as subject to the "Classpath" exception as provided 28.12 + * by Oracle in the LICENSE file that accompanied this code. 28.13 + * 28.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 28.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 28.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 28.17 + * version 2 for more details (a copy is included in the LICENSE file that 28.18 + * accompanied this code). 28.19 + * 28.20 + * You should have received a copy of the GNU General Public License version 28.21 + * 2 along with this work; if not, write to the Free Software Foundation, 28.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 28.23 + * 28.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 28.25 + * or visit www.oracle.com if you need additional information or have any 28.26 + * questions. 28.27 + */ 28.28 + 28.29 +/* 28.30 + * This file is available under and governed by the GNU General Public 28.31 + * License version 2 only, as published by the Free Software Foundation. 28.32 + * However, the following notice accompanied the original version of this 28.33 + * file, and Oracle licenses the original version of this file under the BSD 28.34 + * license: 28.35 + */ 28.36 +/* 28.37 + Copyright 2009-2013 Attila Szegedi 28.38 + 28.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 28.40 + and the BSD License (the "BSD License"), with licensee being free to 28.41 + choose either of the two at their discretion. 28.42 + 28.43 + You may not use this file except in compliance with either the Apache 28.44 + License or the BSD License. 28.45 + 28.46 + If you choose to use this file in compliance with the Apache License, the 28.47 + following notice applies to you: 28.48 + 28.49 + You may obtain a copy of the Apache License at 28.50 + 28.51 + http://www.apache.org/licenses/LICENSE-2.0 28.52 + 28.53 + Unless required by applicable law or agreed to in writing, software 28.54 + distributed under the License is distributed on an "AS IS" BASIS, 28.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 28.56 + implied. See the License for the specific language governing 28.57 + permissions and limitations under the License. 28.58 + 28.59 + If you choose to use this file in compliance with the BSD License, the 28.60 + following notice applies to you: 28.61 + 28.62 + Redistribution and use in source and binary forms, with or without 28.63 + modification, are permitted provided that the following conditions are 28.64 + met: 28.65 + * Redistributions of source code must retain the above copyright 28.66 + notice, this list of conditions and the following disclaimer. 28.67 + * Redistributions in binary form must reproduce the above copyright 28.68 + notice, this list of conditions and the following disclaimer in the 28.69 + documentation and/or other materials provided with the distribution. 28.70 + * Neither the name of the copyright holder nor the names of 28.71 + contributors may be used to endorse or promote products derived from 28.72 + this software without specific prior written permission. 28.73 + 28.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 28.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 28.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 28.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28.85 +*/ 28.86 + 28.87 +package jdk.internal.dynalink.beans; 28.88 + 28.89 +import java.lang.invoke.MethodHandle; 28.90 +import java.lang.invoke.MethodType; 28.91 +import java.util.Iterator; 28.92 +import java.util.LinkedList; 28.93 +import java.util.List; 28.94 +import jdk.internal.dynalink.beans.ApplicableOverloadedMethods.ApplicabilityTest; 28.95 +import jdk.internal.dynalink.linker.LinkerServices; 28.96 +import jdk.internal.dynalink.support.TypeUtilities; 28.97 + 28.98 + 28.99 +/** 28.100 + * Represents an overloaded method. 28.101 + * 28.102 + * @author Attila Szegedi 28.103 + */ 28.104 +class OverloadedDynamicMethod extends DynamicMethod { 28.105 + /** 28.106 + * Holds a list of all methods. 28.107 + */ 28.108 + private final LinkedList<MethodHandle> methods; 28.109 + private final ClassLoader classLoader; 28.110 + 28.111 + /** 28.112 + * Creates a new overloaded dynamic method. 28.113 + * 28.114 + * @param clazz the class this method belongs to 28.115 + * @param name the name of the method 28.116 + */ 28.117 + OverloadedDynamicMethod(Class<?> clazz, String name) { 28.118 + this(new LinkedList<MethodHandle>(), clazz.getClassLoader(), getClassAndMethodName(clazz, name)); 28.119 + } 28.120 + 28.121 + private OverloadedDynamicMethod(LinkedList<MethodHandle> methods, ClassLoader classLoader, String name) { 28.122 + super(name); 28.123 + this.methods = methods; 28.124 + this.classLoader = classLoader; 28.125 + } 28.126 + 28.127 + @Override 28.128 + SimpleDynamicMethod getMethodForExactParamTypes(String paramTypes) { 28.129 + final LinkedList<MethodHandle> matchingMethods = new LinkedList<>(); 28.130 + for(MethodHandle method: methods) { 28.131 + if(typeMatchesDescription(paramTypes, method.type())) { 28.132 + matchingMethods.add(method); 28.133 + } 28.134 + } 28.135 + switch(matchingMethods.size()) { 28.136 + case 0: { 28.137 + return null; 28.138 + } 28.139 + case 1: { 28.140 + final MethodHandle target = matchingMethods.get(0); 28.141 + return new SimpleDynamicMethod(target, SimpleDynamicMethod.getMethodNameWithSignature(target, getName())); 28.142 + } 28.143 + default: { 28.144 + throw new BootstrapMethodError("Can't choose among " + matchingMethods + " for argument types " 28.145 + + paramTypes + " for method " + getName()); 28.146 + } 28.147 + } 28.148 + } 28.149 + 28.150 + @Override 28.151 + public MethodHandle getInvocation(final MethodType callSiteType, final LinkerServices linkerServices) { 28.152 + // First, find all methods applicable to the call site by subtyping (JLS 15.12.2.2) 28.153 + final ApplicableOverloadedMethods subtypingApplicables = getApplicables(callSiteType, 28.154 + ApplicableOverloadedMethods.APPLICABLE_BY_SUBTYPING); 28.155 + // Next, find all methods applicable by method invocation conversion to the call site (JLS 15.12.2.3). 28.156 + final ApplicableOverloadedMethods methodInvocationApplicables = getApplicables(callSiteType, 28.157 + ApplicableOverloadedMethods.APPLICABLE_BY_METHOD_INVOCATION_CONVERSION); 28.158 + // Finally, find all methods applicable by variable arity invocation. (JLS 15.12.2.4). 28.159 + final ApplicableOverloadedMethods variableArityApplicables = getApplicables(callSiteType, 28.160 + ApplicableOverloadedMethods.APPLICABLE_BY_VARIABLE_ARITY); 28.161 + 28.162 + // Find the methods that are maximally specific based on the call site signature 28.163 + List<MethodHandle> maximallySpecifics = subtypingApplicables.findMaximallySpecificMethods(); 28.164 + if(maximallySpecifics.isEmpty()) { 28.165 + maximallySpecifics = methodInvocationApplicables.findMaximallySpecificMethods(); 28.166 + if(maximallySpecifics.isEmpty()) { 28.167 + maximallySpecifics = variableArityApplicables.findMaximallySpecificMethods(); 28.168 + } 28.169 + } 28.170 + 28.171 + // Now, get a list of the rest of the methods; those that are *not* applicable to the call site signature based 28.172 + // on JLS rules. As paradoxical as that might sound, we have to consider these for dynamic invocation, as they 28.173 + // might match more concrete types passed in invocations. That's why we provisionally call them "invokables". 28.174 + // This is typical for very generic signatures at call sites. Typical example: call site specifies 28.175 + // (Object, Object), and we have a method whose parameter types are (String, int). None of the JLS applicability 28.176 + // rules will trigger, but we must consider the method, as it can be the right match for a concrete invocation. 28.177 + @SuppressWarnings({ "unchecked", "rawtypes" }) 28.178 + final List<MethodHandle> invokables = (List)methods.clone(); 28.179 + invokables.removeAll(subtypingApplicables.getMethods()); 28.180 + invokables.removeAll(methodInvocationApplicables.getMethods()); 28.181 + invokables.removeAll(variableArityApplicables.getMethods()); 28.182 + for(final Iterator<MethodHandle> it = invokables.iterator(); it.hasNext();) { 28.183 + final MethodHandle m = it.next(); 28.184 + if(!isApplicableDynamically(linkerServices, callSiteType, m)) { 28.185 + it.remove(); 28.186 + } 28.187 + } 28.188 + 28.189 + // If no additional methods can apply at invocation time, and there's more than one maximally specific method 28.190 + // based on call site signature, that is a link-time ambiguity. In a static scenario, javac would report an 28.191 + // ambiguity error. 28.192 + if(invokables.isEmpty() && maximallySpecifics.size() > 1) { 28.193 + throw new BootstrapMethodError("Can't choose among " + maximallySpecifics + " for argument types " 28.194 + + callSiteType); 28.195 + } 28.196 + 28.197 + // Merge them all. 28.198 + invokables.addAll(maximallySpecifics); 28.199 + switch(invokables.size()) { 28.200 + case 0: { 28.201 + // No overloads can ever match the call site type 28.202 + return null; 28.203 + } 28.204 + case 1: { 28.205 + // Very lucky, we ended up with a single candidate method handle based on the call site signature; we 28.206 + // can link it very simply by delegating to a SimpleDynamicMethod. 28.207 + final MethodHandle mh = invokables.iterator().next(); 28.208 + return new SimpleDynamicMethod(mh).getInvocation(callSiteType, linkerServices); 28.209 + } 28.210 + } 28.211 + 28.212 + // We have more than one candidate. We have no choice but to link to a method that resolves overloads on every 28.213 + // invocation (alternatively, we could opportunistically link the one method that resolves for the current 28.214 + // arguments, but we'd need to install a fairly complex guard for that and when it'd fail, we'd go back all the 28.215 + // way to candidate selection. 28.216 + // TODO: cache per call site type 28.217 + return new OverloadedMethod(invokables, this, callSiteType, linkerServices).getInvoker(); 28.218 + } 28.219 + 28.220 + @Override 28.221 + public boolean contains(MethodHandle mh) { 28.222 + final MethodType type = mh.type(); 28.223 + for(MethodHandle method: methods) { 28.224 + if(typesEqualNoReceiver(type, method.type())) { 28.225 + return true; 28.226 + } 28.227 + } 28.228 + return false; 28.229 + } 28.230 + 28.231 + private static boolean typesEqualNoReceiver(MethodType type1, MethodType type2) { 28.232 + final int pc = type1.parameterCount(); 28.233 + if(pc != type2.parameterCount()) { 28.234 + return false; 28.235 + } 28.236 + for(int i = 1; i < pc; ++i) { // i = 1: ignore receiver 28.237 + if(type1.parameterType(i) != type2.parameterType(i)) { 28.238 + return false; 28.239 + } 28.240 + } 28.241 + return true; 28.242 + } 28.243 + 28.244 + ClassLoader getClassLoader() { 28.245 + return classLoader; 28.246 + } 28.247 + 28.248 + private static boolean isApplicableDynamically(LinkerServices linkerServices, MethodType callSiteType, 28.249 + MethodHandle m) { 28.250 + final MethodType methodType = m.type(); 28.251 + final boolean varArgs = m.isVarargsCollector(); 28.252 + final int fixedArgLen = methodType.parameterCount() - (varArgs ? 1 : 0); 28.253 + final int callSiteArgLen = callSiteType.parameterCount(); 28.254 + if(varArgs) { 28.255 + if(callSiteArgLen < fixedArgLen) { 28.256 + return false; 28.257 + } 28.258 + } else if(callSiteArgLen != fixedArgLen) { 28.259 + return false; 28.260 + } 28.261 + // Starting from 1, as receiver type doesn't participate 28.262 + for(int i = 1; i < fixedArgLen; ++i) { 28.263 + if(!isApplicableDynamically(linkerServices, callSiteType.parameterType(i), methodType.parameterType(i))) { 28.264 + return false; 28.265 + } 28.266 + } 28.267 + if(varArgs) { 28.268 + final Class<?> varArgArrayType = methodType.parameterType(fixedArgLen); 28.269 + final Class<?> varArgType = varArgArrayType.getComponentType(); 28.270 + if(fixedArgLen == callSiteArgLen - 1) { 28.271 + final Class<?> callSiteArgType = callSiteType.parameterType(fixedArgLen); 28.272 + // Exactly one vararg; check both exact matching and component 28.273 + // matching. 28.274 + return isApplicableDynamically(linkerServices, callSiteArgType, varArgArrayType) 28.275 + || isApplicableDynamically(linkerServices, callSiteArgType, varArgType); 28.276 + } else { 28.277 + for(int i = fixedArgLen; i < callSiteArgLen; ++i) { 28.278 + if(!isApplicableDynamically(linkerServices, callSiteType.parameterType(i), varArgType)) { 28.279 + return false; 28.280 + } 28.281 + } 28.282 + return true; 28.283 + } 28.284 + } else { 28.285 + return true; 28.286 + } 28.287 + } 28.288 + 28.289 + private static boolean isApplicableDynamically(LinkerServices linkerServices, Class<?> callSiteType, 28.290 + Class<?> methodType) { 28.291 + return TypeUtilities.isPotentiallyConvertible(callSiteType, methodType) 28.292 + || linkerServices.canConvert(callSiteType, methodType); 28.293 + } 28.294 + 28.295 + private ApplicableOverloadedMethods getApplicables(MethodType callSiteType, ApplicabilityTest test) { 28.296 + return new ApplicableOverloadedMethods(methods, callSiteType, test); 28.297 + } 28.298 + 28.299 + /** 28.300 + * Add a method identified by a {@link SimpleDynamicMethod} to this overloaded method's set. 28.301 + * 28.302 + * @param method the method to add. 28.303 + */ 28.304 + public void addMethod(SimpleDynamicMethod method) { 28.305 + addMethod(method.getTarget()); 28.306 + } 28.307 + 28.308 + /** 28.309 + * Add a method to this overloaded method's set. 28.310 + * 28.311 + * @param method a method to add 28.312 + */ 28.313 + public void addMethod(MethodHandle method) { 28.314 + methods.add(method); 28.315 + } 28.316 +} 28.317 \ No newline at end of file
29.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 29.2 +++ b/src/jdk/internal/dynalink/beans/OverloadedMethod.java Thu Feb 14 13:22:26 2013 +0100 29.3 @@ -0,0 +1,266 @@ 29.4 +/* 29.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 29.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 29.7 + * 29.8 + * This code is free software; you can redistribute it and/or modify it 29.9 + * under the terms of the GNU General Public License version 2 only, as 29.10 + * published by the Free Software Foundation. Oracle designates this 29.11 + * particular file as subject to the "Classpath" exception as provided 29.12 + * by Oracle in the LICENSE file that accompanied this code. 29.13 + * 29.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 29.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 29.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 29.17 + * version 2 for more details (a copy is included in the LICENSE file that 29.18 + * accompanied this code). 29.19 + * 29.20 + * You should have received a copy of the GNU General Public License version 29.21 + * 2 along with this work; if not, write to the Free Software Foundation, 29.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 29.23 + * 29.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 29.25 + * or visit www.oracle.com if you need additional information or have any 29.26 + * questions. 29.27 + */ 29.28 + 29.29 +/* 29.30 + * This file is available under and governed by the GNU General Public 29.31 + * License version 2 only, as published by the Free Software Foundation. 29.32 + * However, the following notice accompanied the original version of this 29.33 + * file, and Oracle licenses the original version of this file under the BSD 29.34 + * license: 29.35 + */ 29.36 +/* 29.37 + Copyright 2009-2013 Attila Szegedi 29.38 + 29.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 29.40 + and the BSD License (the "BSD License"), with licensee being free to 29.41 + choose either of the two at their discretion. 29.42 + 29.43 + You may not use this file except in compliance with either the Apache 29.44 + License or the BSD License. 29.45 + 29.46 + If you choose to use this file in compliance with the Apache License, the 29.47 + following notice applies to you: 29.48 + 29.49 + You may obtain a copy of the Apache License at 29.50 + 29.51 + http://www.apache.org/licenses/LICENSE-2.0 29.52 + 29.53 + Unless required by applicable law or agreed to in writing, software 29.54 + distributed under the License is distributed on an "AS IS" BASIS, 29.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 29.56 + implied. See the License for the specific language governing 29.57 + permissions and limitations under the License. 29.58 + 29.59 + If you choose to use this file in compliance with the BSD License, the 29.60 + following notice applies to you: 29.61 + 29.62 + Redistribution and use in source and binary forms, with or without 29.63 + modification, are permitted provided that the following conditions are 29.64 + met: 29.65 + * Redistributions of source code must retain the above copyright 29.66 + notice, this list of conditions and the following disclaimer. 29.67 + * Redistributions in binary form must reproduce the above copyright 29.68 + notice, this list of conditions and the following disclaimer in the 29.69 + documentation and/or other materials provided with the distribution. 29.70 + * Neither the name of the copyright holder nor the names of 29.71 + contributors may be used to endorse or promote products derived from 29.72 + this software without specific prior written permission. 29.73 + 29.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 29.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 29.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 29.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 29.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29.85 +*/ 29.86 + 29.87 +package jdk.internal.dynalink.beans; 29.88 + 29.89 +import java.lang.invoke.MethodHandle; 29.90 +import java.lang.invoke.MethodHandles; 29.91 +import java.lang.invoke.MethodType; 29.92 +import java.util.ArrayList; 29.93 +import java.util.Iterator; 29.94 +import java.util.List; 29.95 +import java.util.Map; 29.96 +import java.util.concurrent.ConcurrentHashMap; 29.97 +import jdk.internal.dynalink.linker.LinkerServices; 29.98 +import jdk.internal.dynalink.support.Lookup; 29.99 + 29.100 + 29.101 +/** 29.102 + * Represents a subset of overloaded methods for a certain method name on a certain class. It can be either a fixarg or 29.103 + * a vararg subset depending on the subclass. The method is for a fixed number of arguments though (as it is generated 29.104 + * for a concrete call site). As such, all methods in the subset can be invoked with the specified number of arguments 29.105 + * (exactly matching for fixargs, or having less than or equal fixed arguments, for varargs). 29.106 + * 29.107 + * @author Attila Szegedi 29.108 + */ 29.109 +class OverloadedMethod { 29.110 + private final Map<ClassString, MethodHandle> argTypesToMethods = new ConcurrentHashMap<>(); 29.111 + private final OverloadedDynamicMethod parent; 29.112 + private final MethodType callSiteType; 29.113 + private final MethodHandle invoker; 29.114 + private final LinkerServices linkerServices; 29.115 + private final ArrayList<MethodHandle> fixArgMethods; 29.116 + private final ArrayList<MethodHandle> varArgMethods; 29.117 + 29.118 + OverloadedMethod(List<MethodHandle> methodHandles, OverloadedDynamicMethod parent, MethodType callSiteType, 29.119 + LinkerServices linkerServices) { 29.120 + this.parent = parent; 29.121 + this.callSiteType = callSiteType; 29.122 + this.linkerServices = linkerServices; 29.123 + 29.124 + fixArgMethods = new ArrayList<>(methodHandles.size()); 29.125 + varArgMethods = new ArrayList<>(methodHandles.size()); 29.126 + final int argNum = callSiteType.parameterCount(); 29.127 + for(MethodHandle mh: methodHandles) { 29.128 + if(mh.isVarargsCollector()) { 29.129 + final MethodHandle asFixed = mh.asFixedArity(); 29.130 + if(argNum == asFixed.type().parameterCount()) { 29.131 + fixArgMethods.add(asFixed); 29.132 + } 29.133 + varArgMethods.add(mh); 29.134 + } else { 29.135 + fixArgMethods.add(mh); 29.136 + } 29.137 + } 29.138 + fixArgMethods.trimToSize(); 29.139 + varArgMethods.trimToSize(); 29.140 + 29.141 + final MethodHandle bound = SELECT_METHOD.bindTo(this); 29.142 + final MethodHandle collecting = SimpleDynamicMethod.collectArguments(bound, argNum).asType( 29.143 + callSiteType.changeReturnType(MethodHandle.class)); 29.144 + invoker = MethodHandles.foldArguments(MethodHandles.exactInvoker(callSiteType), collecting); 29.145 + } 29.146 + 29.147 + MethodHandle getInvoker() { 29.148 + return invoker; 29.149 + } 29.150 + 29.151 + private static final MethodHandle SELECT_METHOD = Lookup.findOwnSpecial(MethodHandles.lookup(), "selectMethod", 29.152 + MethodHandle.class, Object[].class); 29.153 + 29.154 + @SuppressWarnings("unused") 29.155 + private MethodHandle selectMethod(Object[] args) throws NoSuchMethodException { 29.156 + final Class<?>[] argTypes = new Class[args.length]; 29.157 + for(int i = 0; i < argTypes.length; ++i) { 29.158 + final Object arg = args[i]; 29.159 + argTypes[i] = arg == null ? callSiteType.parameterType(i) : arg.getClass(); 29.160 + } 29.161 + final ClassString classString = new ClassString(argTypes); 29.162 + MethodHandle method = argTypesToMethods.get(classString); 29.163 + if(method == null) { 29.164 + List<MethodHandle> methods = classString.getMaximallySpecifics(fixArgMethods, linkerServices, false); 29.165 + if(methods.isEmpty()) { 29.166 + methods = classString.getMaximallySpecifics(varArgMethods, linkerServices, true); 29.167 + } 29.168 + switch(methods.size()) { 29.169 + case 0: { 29.170 + method = getNoSuchMethodThrower(argTypes); 29.171 + break; 29.172 + } 29.173 + case 1: { 29.174 + method = new SimpleDynamicMethod(methods.get(0)).getInvocation(callSiteType, linkerServices); 29.175 + break; 29.176 + } 29.177 + default: { 29.178 + // This is unfortunate - invocation time ambiguity. We can still save the day if 29.179 + method = getAmbiguousMethodThrower(argTypes, methods); 29.180 + break; 29.181 + } 29.182 + } 29.183 + // Avoid keeping references to unrelated classes; this ruins the performance a bit, but avoids class loader 29.184 + // memory leaks. 29.185 + if(classString.isVisibleFrom(parent.getClassLoader())) { 29.186 + argTypesToMethods.put(classString, method); 29.187 + } 29.188 + } 29.189 + return method; 29.190 + } 29.191 + 29.192 + private MethodHandle getNoSuchMethodThrower(Class<?>[] argTypes) { 29.193 + return adaptThrower(MethodHandles.insertArguments(THROW_NO_SUCH_METHOD, 0, this, argTypes)); 29.194 + } 29.195 + 29.196 + private static final MethodHandle THROW_NO_SUCH_METHOD = Lookup.findOwnSpecial(MethodHandles.lookup(), 29.197 + "throwNoSuchMethod", void.class, Class[].class); 29.198 + 29.199 + @SuppressWarnings("unused") 29.200 + private void throwNoSuchMethod(Class<?>[] argTypes) throws NoSuchMethodException { 29.201 + if(varArgMethods.isEmpty()) { 29.202 + throw new NoSuchMethodException("None of the fixed arity signatures " + getSignatureList(fixArgMethods) + 29.203 + " of method " + parent.getName() + " match the argument types " + argTypesString(argTypes)); 29.204 + } 29.205 + throw new NoSuchMethodException("None of the fixed arity signatures " + getSignatureList(fixArgMethods) + 29.206 + " or the variable arity signatures " + getSignatureList(varArgMethods) + " of the method " + 29.207 + parent.getName() + " match the argument types " + argTypesString(argTypes)); 29.208 + } 29.209 + 29.210 + private MethodHandle getAmbiguousMethodThrower(Class<?>[] argTypes, List<MethodHandle> methods) { 29.211 + return adaptThrower(MethodHandles.insertArguments(THROW_AMBIGUOUS_METHOD, 0, this, argTypes, methods)); 29.212 + } 29.213 + 29.214 + private MethodHandle adaptThrower(MethodHandle rawThrower) { 29.215 + return MethodHandles.dropArguments(rawThrower, 0, callSiteType.parameterList()).asType(callSiteType); 29.216 + } 29.217 + 29.218 + private static final MethodHandle THROW_AMBIGUOUS_METHOD = Lookup.findOwnSpecial(MethodHandles.lookup(), 29.219 + "throwAmbiguousMethod", void.class, Class[].class, List.class); 29.220 + 29.221 + @SuppressWarnings("unused") 29.222 + private void throwAmbiguousMethod(Class<?>[] argTypes, List<MethodHandle> methods) throws NoSuchMethodException { 29.223 + final String arity = methods.get(0).isVarargsCollector() ? "variable" : "fixed"; 29.224 + throw new NoSuchMethodException("Can't unambiguously select between " + arity + " arity signatures " + 29.225 + getSignatureList(methods) + " of the method " + parent.getName() + " for argument types " + 29.226 + argTypesString(argTypes)); 29.227 + } 29.228 + 29.229 + private static String argTypesString(Class<?>[] classes) { 29.230 + final StringBuilder b = new StringBuilder().append('['); 29.231 + appendTypes(b, classes, false); 29.232 + return b.append(']').toString(); 29.233 + } 29.234 + 29.235 + private static String getSignatureList(List<MethodHandle> methods) { 29.236 + final StringBuilder b = new StringBuilder().append('['); 29.237 + final Iterator<MethodHandle> it = methods.iterator(); 29.238 + if(it.hasNext()) { 29.239 + appendSig(b, it.next()); 29.240 + while(it.hasNext()) { 29.241 + appendSig(b.append(", "), it.next()); 29.242 + } 29.243 + } 29.244 + return b.append(']').toString(); 29.245 + } 29.246 + 29.247 + private static void appendSig(StringBuilder b, MethodHandle m) { 29.248 + b.append('('); 29.249 + appendTypes(b, m.type().parameterArray(), m.isVarargsCollector()); 29.250 + b.append(')'); 29.251 + } 29.252 + 29.253 + private static void appendTypes(StringBuilder b, Class<?>[] classes, boolean varArg) { 29.254 + final int l = classes.length; 29.255 + if(!varArg) { 29.256 + if(l > 1) { 29.257 + b.append(classes[1].getCanonicalName()); 29.258 + for(int i = 2; i < l; ++i) { 29.259 + b.append(", ").append(classes[i].getCanonicalName()); 29.260 + } 29.261 + } 29.262 + } else { 29.263 + for(int i = 1; i < l - 1; ++i) { 29.264 + b.append(classes[i].getCanonicalName()).append(", "); 29.265 + } 29.266 + b.append(classes[l - 1].getComponentType().getCanonicalName()).append("..."); 29.267 + } 29.268 + } 29.269 +} 29.270 \ No newline at end of file
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 30.2 +++ b/src/jdk/internal/dynalink/beans/RestrictedPackageTester.java Thu Feb 14 13:22:26 2013 +0100 30.3 @@ -0,0 +1,113 @@ 30.4 +/* 30.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 30.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 30.7 + * 30.8 + * This code is free software; you can redistribute it and/or modify it 30.9 + * under the terms of the GNU General Public License version 2 only, as 30.10 + * published by the Free Software Foundation. Oracle designates this 30.11 + * particular file as subject to the "Classpath" exception as provided 30.12 + * by Oracle in the LICENSE file that accompanied this code. 30.13 + * 30.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 30.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 30.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 30.17 + * version 2 for more details (a copy is included in the LICENSE file that 30.18 + * accompanied this code). 30.19 + * 30.20 + * You should have received a copy of the GNU General Public License version 30.21 + * 2 along with this work; if not, write to the Free Software Foundation, 30.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 30.23 + * 30.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 30.25 + * or visit www.oracle.com if you need additional information or have any 30.26 + * questions. 30.27 + */ 30.28 + 30.29 +/* 30.30 + * This file is available under and governed by the GNU General Public 30.31 + * License version 2 only, as published by the Free Software Foundation. 30.32 + * However, the following notice accompanied the original version of this 30.33 + * file, and Oracle licenses the original version of this file under the BSD 30.34 + * license: 30.35 + */ 30.36 +/* 30.37 + Copyright 2009-2013 Attila Szegedi 30.38 + 30.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 30.40 + and the BSD License (the "BSD License"), with licensee being free to 30.41 + choose either of the two at their discretion. 30.42 + 30.43 + You may not use this file except in compliance with either the Apache 30.44 + License or the BSD License. 30.45 + 30.46 + If you choose to use this file in compliance with the Apache License, the 30.47 + following notice applies to you: 30.48 + 30.49 + You may obtain a copy of the Apache License at 30.50 + 30.51 + http://www.apache.org/licenses/LICENSE-2.0 30.52 + 30.53 + Unless required by applicable law or agreed to in writing, software 30.54 + distributed under the License is distributed on an "AS IS" BASIS, 30.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 30.56 + implied. See the License for the specific language governing 30.57 + permissions and limitations under the License. 30.58 + 30.59 + If you choose to use this file in compliance with the BSD License, the 30.60 + following notice applies to you: 30.61 + 30.62 + Redistribution and use in source and binary forms, with or without 30.63 + modification, are permitted provided that the following conditions are 30.64 + met: 30.65 + * Redistributions of source code must retain the above copyright 30.66 + notice, this list of conditions and the following disclaimer. 30.67 + * Redistributions in binary form must reproduce the above copyright 30.68 + notice, this list of conditions and the following disclaimer in the 30.69 + documentation and/or other materials provided with the distribution. 30.70 + * Neither the name of the copyright holder nor the names of 30.71 + contributors may be used to endorse or promote products derived from 30.72 + this software without specific prior written permission. 30.73 + 30.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 30.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 30.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 30.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 30.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 30.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30.85 +*/ 30.86 + 30.87 +package jdk.internal.dynalink.beans; 30.88 + 30.89 +import java.security.AccessController; 30.90 +import java.security.PrivilegedAction; 30.91 + 30.92 +/** 30.93 + * This class is never referenced directly from code of any other class, but is loaded into a secure class loader that 30.94 + * gives it no permissions whatsoever, so it can be used to reliably test whether a given package has restricted access 30.95 + * or not. See {@link CheckRestrictedPackageInternal} for details. 30.96 + * @author Attila Szegedi 30.97 + * @version $Id: $ 30.98 + */ 30.99 +class RestrictedPackageTester implements PrivilegedAction<Void> { 30.100 + 30.101 + private final String pkgName; 30.102 + 30.103 + private RestrictedPackageTester(String pkgName) { 30.104 + this.pkgName = pkgName; 30.105 + } 30.106 + 30.107 + static void checkPackageAccess(String pkgName) { 30.108 + AccessController.doPrivileged(new RestrictedPackageTester(pkgName)); 30.109 + } 30.110 + 30.111 + @Override 30.112 + public Void run() { 30.113 + System.getSecurityManager().checkPackageAccess(pkgName); 30.114 + return null; 30.115 + } 30.116 +}
31.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 31.2 +++ b/src/jdk/internal/dynalink/beans/SimpleDynamicMethod.java Thu Feb 14 13:22:26 2013 +0100 31.3 @@ -0,0 +1,241 @@ 31.4 +/* 31.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 31.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 31.7 + * 31.8 + * This code is free software; you can redistribute it and/or modify it 31.9 + * under the terms of the GNU General Public License version 2 only, as 31.10 + * published by the Free Software Foundation. Oracle designates this 31.11 + * particular file as subject to the "Classpath" exception as provided 31.12 + * by Oracle in the LICENSE file that accompanied this code. 31.13 + * 31.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 31.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 31.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 31.17 + * version 2 for more details (a copy is included in the LICENSE file that 31.18 + * accompanied this code). 31.19 + * 31.20 + * You should have received a copy of the GNU General Public License version 31.21 + * 2 along with this work; if not, write to the Free Software Foundation, 31.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 31.23 + * 31.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 31.25 + * or visit www.oracle.com if you need additional information or have any 31.26 + * questions. 31.27 + */ 31.28 + 31.29 +/* 31.30 + * This file is available under and governed by the GNU General Public 31.31 + * License version 2 only, as published by the Free Software Foundation. 31.32 + * However, the following notice accompanied the original version of this 31.33 + * file, and Oracle licenses the original version of this file under the BSD 31.34 + * license: 31.35 + */ 31.36 +/* 31.37 + Copyright 2009-2013 Attila Szegedi 31.38 + 31.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 31.40 + and the BSD License (the "BSD License"), with licensee being free to 31.41 + choose either of the two at their discretion. 31.42 + 31.43 + You may not use this file except in compliance with either the Apache 31.44 + License or the BSD License. 31.45 + 31.46 + If you choose to use this file in compliance with the Apache License, the 31.47 + following notice applies to you: 31.48 + 31.49 + You may obtain a copy of the Apache License at 31.50 + 31.51 + http://www.apache.org/licenses/LICENSE-2.0 31.52 + 31.53 + Unless required by applicable law or agreed to in writing, software 31.54 + distributed under the License is distributed on an "AS IS" BASIS, 31.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 31.56 + implied. See the License for the specific language governing 31.57 + permissions and limitations under the License. 31.58 + 31.59 + If you choose to use this file in compliance with the BSD License, the 31.60 + following notice applies to you: 31.61 + 31.62 + Redistribution and use in source and binary forms, with or without 31.63 + modification, are permitted provided that the following conditions are 31.64 + met: 31.65 + * Redistributions of source code must retain the above copyright 31.66 + notice, this list of conditions and the following disclaimer. 31.67 + * Redistributions in binary form must reproduce the above copyright 31.68 + notice, this list of conditions and the following disclaimer in the 31.69 + documentation and/or other materials provided with the distribution. 31.70 + * Neither the name of the copyright holder nor the names of 31.71 + contributors may be used to endorse or promote products derived from 31.72 + this software without specific prior written permission. 31.73 + 31.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 31.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 31.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 31.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 31.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31.85 +*/ 31.86 + 31.87 +package jdk.internal.dynalink.beans; 31.88 + 31.89 +import java.lang.invoke.MethodHandle; 31.90 +import java.lang.invoke.MethodHandles; 31.91 +import java.lang.invoke.MethodType; 31.92 +import java.lang.reflect.Array; 31.93 +import jdk.internal.dynalink.linker.LinkerServices; 31.94 +import jdk.internal.dynalink.support.Guards; 31.95 + 31.96 + 31.97 +/** 31.98 + * A dynamic method bound to exactly one, non-overloaded Java method. Handles varargs. 31.99 + * 31.100 + * @author Attila Szegedi 31.101 + */ 31.102 +class SimpleDynamicMethod extends DynamicMethod { 31.103 + private final MethodHandle target; 31.104 + 31.105 + /** 31.106 + * Creates a simple dynamic method with no name. 31.107 + * @param target the target method handle 31.108 + */ 31.109 + SimpleDynamicMethod(MethodHandle target) { 31.110 + this(target, null); 31.111 + } 31.112 + 31.113 + /** 31.114 + * Creates a new simple dynamic method, with a name constructed from the class name, method name, and handle 31.115 + * signature. 31.116 + * 31.117 + * @param target the target method handle 31.118 + * @param clazz the class declaring the method 31.119 + * @param name the simple name of the method 31.120 + */ 31.121 + SimpleDynamicMethod(MethodHandle target, Class<?> clazz, String name) { 31.122 + this(target, getName(target, clazz, name)); 31.123 + } 31.124 + 31.125 + SimpleDynamicMethod(MethodHandle target, String name) { 31.126 + super(name); 31.127 + this.target = target; 31.128 + } 31.129 + 31.130 + private static String getName(MethodHandle target, Class<?> clazz, String name) { 31.131 + return getMethodNameWithSignature(target, getClassAndMethodName(clazz, name)); 31.132 + } 31.133 + 31.134 + static String getMethodNameWithSignature(MethodHandle target, String methodName) { 31.135 + final String typeStr = target.type().toString(); 31.136 + final int retTypeIndex = typeStr.lastIndexOf(')') + 1; 31.137 + int secondParamIndex = typeStr.indexOf(',') + 1; 31.138 + if(secondParamIndex == 0) { 31.139 + secondParamIndex = retTypeIndex - 1; 31.140 + } 31.141 + return typeStr.substring(retTypeIndex) + " " + methodName + "(" + typeStr.substring(secondParamIndex, retTypeIndex); 31.142 + } 31.143 + 31.144 + /** 31.145 + * Returns the target of this dynamic method 31.146 + * 31.147 + * @return the target of this dynamic method 31.148 + */ 31.149 + MethodHandle getTarget() { 31.150 + return target; 31.151 + } 31.152 + 31.153 + @Override 31.154 + SimpleDynamicMethod getMethodForExactParamTypes(String paramTypes) { 31.155 + return typeMatchesDescription(paramTypes, target.type()) ? this : null; 31.156 + } 31.157 + 31.158 + @Override 31.159 + MethodHandle getInvocation(MethodType callSiteType, LinkerServices linkerServices) { 31.160 + final MethodType methodType = target.type(); 31.161 + final int paramsLen = methodType.parameterCount(); 31.162 + final boolean varArgs = target.isVarargsCollector(); 31.163 + final MethodHandle fixTarget = varArgs ? target.asFixedArity() : target; 31.164 + final int fixParamsLen = varArgs ? paramsLen - 1 : paramsLen; 31.165 + final int argsLen = callSiteType.parameterCount(); 31.166 + if(argsLen < fixParamsLen) { 31.167 + // Less actual arguments than number of fixed declared arguments; can't invoke. 31.168 + return null; 31.169 + } 31.170 + // Method handle of the same number of arguments as the call site type 31.171 + if(argsLen == fixParamsLen) { 31.172 + // Method handle that matches the number of actual arguments as the number of fixed arguments 31.173 + final MethodHandle matchedMethod; 31.174 + if(varArgs) { 31.175 + // If vararg, add a zero-length array of the expected type as the last argument to signify no variable 31.176 + // arguments. 31.177 + // TODO: check whether collectArguments() would handle this too. 31.178 + matchedMethod = MethodHandles.insertArguments(fixTarget, fixParamsLen, Array.newInstance( 31.179 + methodType.parameterType(fixParamsLen).getComponentType(), 0)); 31.180 + } else { 31.181 + // Otherwise, just use the method 31.182 + matchedMethod = fixTarget; 31.183 + } 31.184 + return createConvertingInvocation(matchedMethod, linkerServices, callSiteType); 31.185 + } 31.186 + 31.187 + // What's below only works for varargs 31.188 + if(!varArgs) { 31.189 + return null; 31.190 + } 31.191 + 31.192 + final Class<?> varArgType = methodType.parameterType(fixParamsLen); 31.193 + // Handle a somewhat sinister corner case: caller passes exactly one argument in the vararg position, and we 31.194 + // must handle both a prepacked vararg array as well as a genuine 1-long vararg sequence. 31.195 + if(argsLen == paramsLen) { 31.196 + final Class<?> callSiteLastArgType = callSiteType.parameterType(fixParamsLen); 31.197 + if(varArgType.isAssignableFrom(callSiteLastArgType)) { 31.198 + // Call site signature guarantees we'll always be passed a single compatible array; just link directly 31.199 + // to the method. 31.200 + return createConvertingInvocation(fixTarget, linkerServices, callSiteType); 31.201 + } else if(!linkerServices.canConvert(callSiteLastArgType, varArgType)) { 31.202 + // Call site signature guarantees the argument can definitely not be an array (i.e. it is primitive); 31.203 + // link immediately to a vararg-packing method handle. 31.204 + return createConvertingInvocation(collectArguments(fixTarget, argsLen), linkerServices, callSiteType); 31.205 + } else { 31.206 + // Call site signature makes no guarantees that the single argument in the vararg position will be 31.207 + // compatible across all invocations. Need to insert an appropriate guard and fall back to generic 31.208 + // vararg method when it is not. 31.209 + return MethodHandles.guardWithTest(Guards.isInstance(varArgType, fixParamsLen, callSiteType), 31.210 + createConvertingInvocation(fixTarget, linkerServices, callSiteType), 31.211 + createConvertingInvocation(collectArguments(fixTarget, argsLen), linkerServices, callSiteType)); 31.212 + } 31.213 + } else { 31.214 + // Remaining case: more than one vararg. 31.215 + return createConvertingInvocation(collectArguments(fixTarget, argsLen), linkerServices, callSiteType); 31.216 + } 31.217 + } 31.218 + 31.219 + @Override 31.220 + public boolean contains(MethodHandle mh) { 31.221 + return target.type().parameterList().equals(mh.type().parameterList()); 31.222 + } 31.223 + 31.224 + /** 31.225 + * Creates a method handle out of the original target that will collect the varargs for the exact component type of 31.226 + * the varArg array. Note that this will nicely trigger language-specific type converters for exactly those varargs 31.227 + * for which it is necessary when later passed to linkerServices.convertArguments(). 31.228 + * 31.229 + * @param target the original method handle 31.230 + * @param parameterCount the total number of arguments in the new method handle 31.231 + * @return a collecting method handle 31.232 + */ 31.233 + static MethodHandle collectArguments(MethodHandle target, final int parameterCount) { 31.234 + final MethodType methodType = target.type(); 31.235 + final int fixParamsLen = methodType.parameterCount() - 1; 31.236 + final Class<?> arrayType = methodType.parameterType(fixParamsLen); 31.237 + return target.asCollector(arrayType, parameterCount - fixParamsLen); 31.238 + } 31.239 + 31.240 + private static MethodHandle createConvertingInvocation(final MethodHandle sizedMethod, 31.241 + final LinkerServices linkerServices, final MethodType callSiteType) { 31.242 + return linkerServices.asType(sizedMethod, callSiteType); 31.243 + } 31.244 +} 31.245 \ No newline at end of file
32.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 32.2 +++ b/src/jdk/internal/dynalink/beans/StaticClass.java Thu Feb 14 13:22:26 2013 +0100 32.3 @@ -0,0 +1,138 @@ 32.4 +/* 32.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 32.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 32.7 + * 32.8 + * This code is free software; you can redistribute it and/or modify it 32.9 + * under the terms of the GNU General Public License version 2 only, as 32.10 + * published by the Free Software Foundation. Oracle designates this 32.11 + * particular file as subject to the "Classpath" exception as provided 32.12 + * by Oracle in the LICENSE file that accompanied this code. 32.13 + * 32.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 32.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 32.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 32.17 + * version 2 for more details (a copy is included in the LICENSE file that 32.18 + * accompanied this code). 32.19 + * 32.20 + * You should have received a copy of the GNU General Public License version 32.21 + * 2 along with this work; if not, write to the Free Software Foundation, 32.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 32.23 + * 32.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 32.25 + * or visit www.oracle.com if you need additional information or have any 32.26 + * questions. 32.27 + */ 32.28 + 32.29 +/* 32.30 + * This file is available under and governed by the GNU General Public 32.31 + * License version 2 only, as published by the Free Software Foundation. 32.32 + * However, the following notice accompanied the original version of this 32.33 + * file, and Oracle licenses the original version of this file under the BSD 32.34 + * license: 32.35 + */ 32.36 +/* 32.37 + Copyright 2009-2013 Attila Szegedi 32.38 + 32.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 32.40 + and the BSD License (the "BSD License"), with licensee being free to 32.41 + choose either of the two at their discretion. 32.42 + 32.43 + You may not use this file except in compliance with either the Apache 32.44 + License or the BSD License. 32.45 + 32.46 + If you choose to use this file in compliance with the Apache License, the 32.47 + following notice applies to you: 32.48 + 32.49 + You may obtain a copy of the Apache License at 32.50 + 32.51 + http://www.apache.org/licenses/LICENSE-2.0 32.52 + 32.53 + Unless required by applicable law or agreed to in writing, software 32.54 + distributed under the License is distributed on an "AS IS" BASIS, 32.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 32.56 + implied. See the License for the specific language governing 32.57 + permissions and limitations under the License. 32.58 + 32.59 + If you choose to use this file in compliance with the BSD License, the 32.60 + following notice applies to you: 32.61 + 32.62 + Redistribution and use in source and binary forms, with or without 32.63 + modification, are permitted provided that the following conditions are 32.64 + met: 32.65 + * Redistributions of source code must retain the above copyright 32.66 + notice, this list of conditions and the following disclaimer. 32.67 + * Redistributions in binary form must reproduce the above copyright 32.68 + notice, this list of conditions and the following disclaimer in the 32.69 + documentation and/or other materials provided with the distribution. 32.70 + * Neither the name of the copyright holder nor the names of 32.71 + contributors may be used to endorse or promote products derived from 32.72 + this software without specific prior written permission. 32.73 + 32.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 32.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 32.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 32.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 32.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 32.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32.85 +*/ 32.86 + 32.87 +package jdk.internal.dynalink.beans; 32.88 + 32.89 +import java.io.Serializable; 32.90 + 32.91 +/** 32.92 + * Object that represents the static facet of a class (its static methods, properties, and fields, as well as 32.93 + * construction of instances using "dyn:new"). Objects of this class are recognized by the {@link BeansLinker} as being 32.94 + * special, and operations on them will be linked against the represented class' static facet. The "class" synthetic 32.95 + * property is additionally recognized and returns the Java {@link Class} object, as per {@link #getRepresentedClass()} 32.96 + * method. Conversely, {@link Class} objects exposed through {@link BeansLinker} expose the "static" synthetic property 32.97 + * which returns an instance of this class. 32.98 + */ 32.99 +public class StaticClass implements Serializable { 32.100 + private static final ClassValue<StaticClass> staticClasses = new ClassValue<StaticClass>() { 32.101 + @Override 32.102 + protected StaticClass computeValue(Class<?> type) { 32.103 + return new StaticClass(type); 32.104 + } 32.105 + }; 32.106 + 32.107 + private static final long serialVersionUID = 1L; 32.108 + 32.109 + private final Class<?> clazz; 32.110 + 32.111 + /*private*/ StaticClass(Class<?> clazz) { 32.112 + clazz.getClass(); // NPE check 32.113 + this.clazz = clazz; 32.114 + } 32.115 + 32.116 + /** 32.117 + * Retrieves the {@link StaticClass} instance for the specified class. 32.118 + * @param clazz the class for which the static facet is requested. 32.119 + * @return the {@link StaticClass} instance representing the specified class. 32.120 + */ 32.121 + public static StaticClass forClass(Class<?> clazz) { 32.122 + return staticClasses.get(clazz); 32.123 + } 32.124 + 32.125 + /** 32.126 + * Returns the represented Java class. 32.127 + * @return the represented Java class. 32.128 + */ 32.129 + public Class<?> getRepresentedClass() { 32.130 + return clazz; 32.131 + } 32.132 + 32.133 + @Override 32.134 + public String toString() { 32.135 + return "JavaClassStatics[" + clazz.getName() + "]"; 32.136 + } 32.137 + 32.138 + private Object readResolve() { 32.139 + return forClass(clazz); 32.140 + } 32.141 +} 32.142 \ No newline at end of file
33.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 33.2 +++ b/src/jdk/internal/dynalink/beans/StaticClassIntrospector.java Thu Feb 14 13:22:26 2013 +0100 33.3 @@ -0,0 +1,117 @@ 33.4 +/* 33.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 33.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 33.7 + * 33.8 + * This code is free software; you can redistribute it and/or modify it 33.9 + * under the terms of the GNU General Public License version 2 only, as 33.10 + * published by the Free Software Foundation. Oracle designates this 33.11 + * particular file as subject to the "Classpath" exception as provided 33.12 + * by Oracle in the LICENSE file that accompanied this code. 33.13 + * 33.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 33.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 33.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 33.17 + * version 2 for more details (a copy is included in the LICENSE file that 33.18 + * accompanied this code). 33.19 + * 33.20 + * You should have received a copy of the GNU General Public License version 33.21 + * 2 along with this work; if not, write to the Free Software Foundation, 33.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 33.23 + * 33.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 33.25 + * or visit www.oracle.com if you need additional information or have any 33.26 + * questions. 33.27 + */ 33.28 + 33.29 +/* 33.30 + * This file is available under and governed by the GNU General Public 33.31 + * License version 2 only, as published by the Free Software Foundation. 33.32 + * However, the following notice accompanied the original version of this 33.33 + * file, and Oracle licenses the original version of this file under the BSD 33.34 + * license: 33.35 + */ 33.36 +/* 33.37 + Copyright 2009-2013 Attila Szegedi 33.38 + 33.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 33.40 + and the BSD License (the "BSD License"), with licensee being free to 33.41 + choose either of the two at their discretion. 33.42 + 33.43 + You may not use this file except in compliance with either the Apache 33.44 + License or the BSD License. 33.45 + 33.46 + If you choose to use this file in compliance with the Apache License, the 33.47 + following notice applies to you: 33.48 + 33.49 + You may obtain a copy of the Apache License at 33.50 + 33.51 + http://www.apache.org/licenses/LICENSE-2.0 33.52 + 33.53 + Unless required by applicable law or agreed to in writing, software 33.54 + distributed under the License is distributed on an "AS IS" BASIS, 33.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 33.56 + implied. See the License for the specific language governing 33.57 + permissions and limitations under the License. 33.58 + 33.59 + If you choose to use this file in compliance with the BSD License, the 33.60 + following notice applies to you: 33.61 + 33.62 + Redistribution and use in source and binary forms, with or without 33.63 + modification, are permitted provided that the following conditions are 33.64 + met: 33.65 + * Redistributions of source code must retain the above copyright 33.66 + notice, this list of conditions and the following disclaimer. 33.67 + * Redistributions in binary form must reproduce the above copyright 33.68 + notice, this list of conditions and the following disclaimer in the 33.69 + documentation and/or other materials provided with the distribution. 33.70 + * Neither the name of the copyright holder nor the names of 33.71 + contributors may be used to endorse or promote products derived from 33.72 + this software without specific prior written permission. 33.73 + 33.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 33.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 33.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 33.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 33.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 33.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33.85 +*/ 33.86 + 33.87 +package jdk.internal.dynalink.beans; 33.88 + 33.89 +import java.lang.invoke.MethodHandle; 33.90 +import java.lang.invoke.MethodHandles; 33.91 +import java.lang.invoke.MethodType; 33.92 +import java.util.HashMap; 33.93 +import java.util.Map; 33.94 + 33.95 +class StaticClassIntrospector extends FacetIntrospector { 33.96 + StaticClassIntrospector(Class<?> clazz) { 33.97 + super(clazz, false); 33.98 + } 33.99 + 33.100 + @Override 33.101 + Map<String, MethodHandle> getInnerClassGetters() { 33.102 + final Map<String, MethodHandle> map = new HashMap<>(); 33.103 + for(Class<?> innerClass: membersLookup.getInnerClasses()) { 33.104 + map.put(innerClass.getSimpleName(), editMethodHandle(MethodHandles.constant(StaticClass.class, 33.105 + StaticClass.forClass(innerClass)))); 33.106 + } 33.107 + return map; 33.108 + } 33.109 + 33.110 + @Override 33.111 + MethodHandle editMethodHandle(MethodHandle mh) { 33.112 + MethodHandle newHandle = MethodHandles.dropArguments(mh, 0, Object.class); 33.113 + // NOTE: this is a workaround for the fact that dropArguments doesn't preserve vararg collector state. 33.114 + if(mh.isVarargsCollector() && !newHandle.isVarargsCollector()) { 33.115 + final MethodType type = mh.type(); 33.116 + newHandle = newHandle.asVarargsCollector(type.parameterType(type.parameterCount() - 1)); 33.117 + } 33.118 + return newHandle; 33.119 + } 33.120 +}
34.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 34.2 +++ b/src/jdk/internal/dynalink/beans/StaticClassLinker.java Thu Feb 14 13:22:26 2013 +0100 34.3 @@ -0,0 +1,204 @@ 34.4 +/* 34.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 34.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 34.7 + * 34.8 + * This code is free software; you can redistribute it and/or modify it 34.9 + * under the terms of the GNU General Public License version 2 only, as 34.10 + * published by the Free Software Foundation. Oracle designates this 34.11 + * particular file as subject to the "Classpath" exception as provided 34.12 + * by Oracle in the LICENSE file that accompanied this code. 34.13 + * 34.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 34.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 34.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 34.17 + * version 2 for more details (a copy is included in the LICENSE file that 34.18 + * accompanied this code). 34.19 + * 34.20 + * You should have received a copy of the GNU General Public License version 34.21 + * 2 along with this work; if not, write to the Free Software Foundation, 34.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 34.23 + * 34.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 34.25 + * or visit www.oracle.com if you need additional information or have any 34.26 + * questions. 34.27 + */ 34.28 + 34.29 +/* 34.30 + * This file is available under and governed by the GNU General Public 34.31 + * License version 2 only, as published by the Free Software Foundation. 34.32 + * However, the following notice accompanied the original version of this 34.33 + * file, and Oracle licenses the original version of this file under the BSD 34.34 + * license: 34.35 + */ 34.36 +/* 34.37 + Copyright 2009-2013 Attila Szegedi 34.38 + 34.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 34.40 + and the BSD License (the "BSD License"), with licensee being free to 34.41 + choose either of the two at their discretion. 34.42 + 34.43 + You may not use this file except in compliance with either the Apache 34.44 + License or the BSD License. 34.45 + 34.46 + If you choose to use this file in compliance with the Apache License, the 34.47 + following notice applies to you: 34.48 + 34.49 + You may obtain a copy of the Apache License at 34.50 + 34.51 + http://www.apache.org/licenses/LICENSE-2.0 34.52 + 34.53 + Unless required by applicable law or agreed to in writing, software 34.54 + distributed under the License is distributed on an "AS IS" BASIS, 34.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 34.56 + implied. See the License for the specific language governing 34.57 + permissions and limitations under the License. 34.58 + 34.59 + If you choose to use this file in compliance with the BSD License, the 34.60 + following notice applies to you: 34.61 + 34.62 + Redistribution and use in source and binary forms, with or without 34.63 + modification, are permitted provided that the following conditions are 34.64 + met: 34.65 + * Redistributions of source code must retain the above copyright 34.66 + notice, this list of conditions and the following disclaimer. 34.67 + * Redistributions in binary form must reproduce the above copyright 34.68 + notice, this list of conditions and the following disclaimer in the 34.69 + documentation and/or other materials provided with the distribution. 34.70 + * Neither the name of the copyright holder nor the names of 34.71 + contributors may be used to endorse or promote products derived from 34.72 + this software without specific prior written permission. 34.73 + 34.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 34.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 34.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 34.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 34.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 34.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 34.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 34.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34.85 +*/ 34.86 + 34.87 +package jdk.internal.dynalink.beans; 34.88 + 34.89 +import java.lang.invoke.MethodHandle; 34.90 +import java.lang.invoke.MethodHandles; 34.91 +import java.lang.invoke.MethodType; 34.92 +import java.lang.reflect.Array; 34.93 +import java.lang.reflect.Constructor; 34.94 +import java.util.ArrayList; 34.95 +import java.util.List; 34.96 +import jdk.internal.dynalink.CallSiteDescriptor; 34.97 +import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType; 34.98 +import jdk.internal.dynalink.linker.GuardedInvocation; 34.99 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 34.100 +import jdk.internal.dynalink.linker.LinkRequest; 34.101 +import jdk.internal.dynalink.linker.LinkerServices; 34.102 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 34.103 +import jdk.internal.dynalink.support.Lookup; 34.104 + 34.105 + 34.106 +/** 34.107 + * Provides a linker for the {@link StaticClass} objects. 34.108 + * @author Attila Szegedi 34.109 + */ 34.110 +class StaticClassLinker implements TypeBasedGuardingDynamicLinker { 34.111 + private final ClassValue<GuardingDynamicLinker> linkers = new ClassValue<GuardingDynamicLinker>() { 34.112 + @Override 34.113 + protected GuardingDynamicLinker computeValue(Class<?> clazz) { 34.114 + return new SingleClassStaticsLinker(clazz); 34.115 + } 34.116 + }; 34.117 + 34.118 + private static class SingleClassStaticsLinker extends AbstractJavaLinker { 34.119 + private final DynamicMethod constructor; 34.120 + 34.121 + SingleClassStaticsLinker(Class<?> clazz) { 34.122 + super(clazz, IS_CLASS.bindTo(clazz)); 34.123 + // Map "staticClassObject.class" to StaticClass.getRepresentedClass(). Some adventurous soul could subclass 34.124 + // StaticClass, so we use INSTANCE_OF validation instead of EXACT_CLASS. 34.125 + setPropertyGetter("class", GET_CLASS, ValidationType.INSTANCE_OF); 34.126 + constructor = createConstructorMethod(clazz); 34.127 + } 34.128 + 34.129 + /** 34.130 + * Creates a dynamic method containing all overloads of a class' public constructor 34.131 + * @param clazz the target class 34.132 + * @return a dynamic method containing all overloads of a class' public constructor. If the class has no public 34.133 + * constructors, returns null. 34.134 + */ 34.135 + private static DynamicMethod createConstructorMethod(Class<?> clazz) { 34.136 + if(clazz.isArray()) { 34.137 + final MethodHandle boundArrayCtor = ARRAY_CTOR.bindTo(clazz.getComponentType()); 34.138 + return new SimpleDynamicMethod(drop(boundArrayCtor.asType(boundArrayCtor.type().changeReturnType( 34.139 + clazz))), clazz, "<init>"); 34.140 + } 34.141 + 34.142 + final Constructor<?>[] ctrs = clazz.getConstructors(); 34.143 + final List<MethodHandle> mhs = new ArrayList<>(ctrs.length); 34.144 + for(int i = 0; i < ctrs.length; ++i) { 34.145 + mhs.add(drop(Lookup.PUBLIC.unreflectConstructor(ctrs[i]))); 34.146 + } 34.147 + return createDynamicMethod(mhs, clazz, "<init>"); 34.148 + } 34.149 + 34.150 + private static MethodHandle drop(MethodHandle mh) { 34.151 + return MethodHandles.dropArguments(mh, 0, StaticClass.class); 34.152 + } 34.153 + 34.154 + @Override 34.155 + FacetIntrospector createFacetIntrospector() { 34.156 + return new StaticClassIntrospector(clazz); 34.157 + } 34.158 + 34.159 + @Override 34.160 + public GuardedInvocation getGuardedInvocation(LinkRequest request, LinkerServices linkerServices) 34.161 + throws Exception { 34.162 + final GuardedInvocation gi = super.getGuardedInvocation(request, linkerServices); 34.163 + if(gi != null) { 34.164 + return gi; 34.165 + } 34.166 + final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 34.167 + final String op = desc.getNameToken(CallSiteDescriptor.OPERATOR); 34.168 + final MethodType methodType = desc.getMethodType(); 34.169 + if("new" == op && constructor != null) { 34.170 + final MethodHandle ctorInvocation = constructor.getInvocation(methodType, linkerServices); 34.171 + if(ctorInvocation != null) { 34.172 + return new GuardedInvocation(ctorInvocation, getClassGuard(methodType)); 34.173 + } 34.174 + } 34.175 + return null; 34.176 + } 34.177 + } 34.178 + 34.179 + @Override 34.180 + public GuardedInvocation getGuardedInvocation(LinkRequest request, LinkerServices linkerServices) throws Exception { 34.181 + final Object receiver = request.getReceiver(); 34.182 + if(receiver instanceof StaticClass) { 34.183 + return linkers.get(((StaticClass)receiver).getRepresentedClass()).getGuardedInvocation(request, 34.184 + linkerServices); 34.185 + } 34.186 + return null; 34.187 + } 34.188 + 34.189 + @Override 34.190 + public boolean canLinkType(Class<?> type) { 34.191 + return type == StaticClass.class; 34.192 + } 34.193 + 34.194 + /*private*/ static final MethodHandle GET_CLASS = new Lookup(MethodHandles.lookup()).findVirtual(StaticClass.class, 34.195 + "getRepresentedClass", MethodType.methodType(Class.class)); 34.196 + 34.197 + /*private*/ static final MethodHandle IS_CLASS = new Lookup(MethodHandles.lookup()).findStatic(StaticClassLinker.class, 34.198 + "isClass", MethodType.methodType(Boolean.TYPE, Class.class, Object.class)); 34.199 + 34.200 + /*private*/ static final MethodHandle ARRAY_CTOR = Lookup.PUBLIC.findStatic(Array.class, "newInstance", 34.201 + MethodType.methodType(Object.class, Class.class, int.class)); 34.202 + 34.203 + @SuppressWarnings("unused") 34.204 + private static boolean isClass(Class<?> clazz, Object obj) { 34.205 + return obj instanceof StaticClass && ((StaticClass)obj).getRepresentedClass() == clazz; 34.206 + } 34.207 +} 34.208 \ No newline at end of file
35.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 35.2 +++ b/src/jdk/internal/dynalink/beans/messages.properties Thu Feb 14 13:22:26 2013 +0100 35.3 @@ -0,0 +1,25 @@ 35.4 +# Copyright 2009-2013 Attila Szegedi 35.5 +# 35.6 +# Licensed under either the Apache License, Version 2.0 (the "Apache 35.7 +# License") or the BSD License (the "BSD License"), with licensee 35.8 +# being free to choose either of the two at their discretion. 35.9 +# 35.10 +# You may not use this file except in compliance with either the Apache 35.11 +# License or the BSD License. 35.12 +# 35.13 +# A copy of the BSD License is available in the root directory of the 35.14 +# source distribution of the project under the file name 35.15 +# "Dynalink-License-BSD.txt". 35.16 +# 35.17 +# A copy of the Apache License is available in the root directory of the 35.18 +# source distribution of the project under the file name 35.19 +# "Dynalink-License-Apache-2.0.txt". Alternatively, you may obtain a 35.20 +# copy of the Apache License at <http://www.apache.org/licenses/LICENSE-2.0> 35.21 +# 35.22 +# Unless required by applicable law or agreed to in writing, software 35.23 +# distributed under the License is distributed on an "AS IS" BASIS, 35.24 +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35.25 +# See your chosen License for the specific language governing permissions 35.26 +# and limitations under that License. 35.27 + 35.28 +couldNotDiscoverAccessibleMethods=Could not discover accessible methods of class {0}, trying its superclasses and interfaces. 35.29 \ No newline at end of file
36.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 36.2 +++ b/src/jdk/internal/dynalink/beans/package.html Thu Feb 14 13:22:26 2013 +0100 36.3 @@ -0,0 +1,86 @@ 36.4 +<!-- 36.5 + Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 36.6 + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 36.7 + 36.8 + This code is free software; you can redistribute it and/or modify it 36.9 + under the terms of the GNU General Public License version 2 only, as 36.10 + published by the Free Software Foundation. Oracle designates this 36.11 + particular file as subject to the "Classpath" exception as provided 36.12 + by Oracle in the LICENSE file that accompanied this code. 36.13 + 36.14 + This code is distributed in the hope that it will be useful, but WITHOUT 36.15 + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 36.16 + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 36.17 + version 2 for more details (a copy is included in the LICENSE file that 36.18 + accompanied this code). 36.19 + 36.20 + You should have received a copy of the GNU General Public License version 36.21 + 2 along with this work; if not, write to the Free Software Foundation, 36.22 + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 36.23 + 36.24 + Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 36.25 + or visit www.oracle.com if you need additional information or have any 36.26 + questions. 36.27 +--> 36.28 + 36.29 +<!-- 36.30 + This file is available under and governed by the GNU General Public 36.31 + License version 2 only, as published by the Free Software Foundation. 36.32 + However, the following notice accompanied the original version of this 36.33 + file, and Oracle licenses the original version of this file under the BSD 36.34 + license: 36.35 + 36.36 + Copyright 2009-2013 Attila Szegedi 36.37 + 36.38 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 36.39 + and the BSD License (the "BSD License"), with licensee being free to 36.40 + choose either of the two at their discretion. 36.41 + 36.42 + You may not use this file except in compliance with either the Apache 36.43 + License or the BSD License. 36.44 + 36.45 + If you choose to use this file in compliance with the Apache License, the 36.46 + following notice applies to you: 36.47 + 36.48 + You may obtain a copy of the Apache License at 36.49 + 36.50 + http://www.apache.org/licenses/LICENSE-2.0 36.51 + 36.52 + Unless required by applicable law or agreed to in writing, software 36.53 + distributed under the License is distributed on an "AS IS" BASIS, 36.54 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 36.55 + implied. See the License for the specific language governing 36.56 + permissions and limitations under the License. 36.57 + 36.58 + If you choose to use this file in compliance with the BSD License, the 36.59 + following notice applies to you: 36.60 + 36.61 + Redistribution and use in source and binary forms, with or without 36.62 + modification, are permitted provided that the following conditions are 36.63 + met: 36.64 + * Redistributions of source code must retain the above copyright 36.65 + notice, this list of conditions and the following disclaimer. 36.66 + * Redistributions in binary form must reproduce the above copyright 36.67 + notice, this list of conditions and the following disclaimer in the 36.68 + documentation and/or other materials provided with the distribution. 36.69 + * Neither the name of the copyright holder nor the names of 36.70 + contributors may be used to endorse or promote products derived from 36.71 + this software without specific prior written permission. 36.72 + 36.73 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 36.74 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 36.75 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 36.76 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 36.77 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 36.78 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36.79 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 36.80 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 36.81 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 36.82 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 36.83 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36.84 +--> 36.85 + <body> 36.86 + <p> 36.87 + Contains the linker for POJOs. 36.88 + </p> 36.89 + </body>
37.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 37.2 +++ b/src/jdk/internal/dynalink/linker/ConversionComparator.java Thu Feb 14 13:22:26 2013 +0100 37.3 @@ -0,0 +1,114 @@ 37.4 +/* 37.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 37.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 37.7 + * 37.8 + * This code is free software; you can redistribute it and/or modify it 37.9 + * under the terms of the GNU General Public License version 2 only, as 37.10 + * published by the Free Software Foundation. Oracle designates this 37.11 + * particular file as subject to the "Classpath" exception as provided 37.12 + * by Oracle in the LICENSE file that accompanied this code. 37.13 + * 37.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 37.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 37.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 37.17 + * version 2 for more details (a copy is included in the LICENSE file that 37.18 + * accompanied this code). 37.19 + * 37.20 + * You should have received a copy of the GNU General Public License version 37.21 + * 2 along with this work; if not, write to the Free Software Foundation, 37.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 37.23 + * 37.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 37.25 + * or visit www.oracle.com if you need additional information or have any 37.26 + * questions. 37.27 + */ 37.28 + 37.29 +/* 37.30 + * This file is available under and governed by the GNU General Public 37.31 + * License version 2 only, as published by the Free Software Foundation. 37.32 + * However, the following notice accompanied the original version of this 37.33 + * file, and Oracle licenses the original version of this file under the BSD 37.34 + * license: 37.35 + */ 37.36 +/* 37.37 + Copyright 2009-2013 Attila Szegedi 37.38 + 37.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 37.40 + and the BSD License (the "BSD License"), with licensee being free to 37.41 + choose either of the two at their discretion. 37.42 + 37.43 + You may not use this file except in compliance with either the Apache 37.44 + License or the BSD License. 37.45 + 37.46 + If you choose to use this file in compliance with the Apache License, the 37.47 + following notice applies to you: 37.48 + 37.49 + You may obtain a copy of the Apache License at 37.50 + 37.51 + http://www.apache.org/licenses/LICENSE-2.0 37.52 + 37.53 + Unless required by applicable law or agreed to in writing, software 37.54 + distributed under the License is distributed on an "AS IS" BASIS, 37.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 37.56 + implied. See the License for the specific language governing 37.57 + permissions and limitations under the License. 37.58 + 37.59 + If you choose to use this file in compliance with the BSD License, the 37.60 + following notice applies to you: 37.61 + 37.62 + Redistribution and use in source and binary forms, with or without 37.63 + modification, are permitted provided that the following conditions are 37.64 + met: 37.65 + * Redistributions of source code must retain the above copyright 37.66 + notice, this list of conditions and the following disclaimer. 37.67 + * Redistributions in binary form must reproduce the above copyright 37.68 + notice, this list of conditions and the following disclaimer in the 37.69 + documentation and/or other materials provided with the distribution. 37.70 + * Neither the name of the copyright holder nor the names of 37.71 + contributors may be used to endorse or promote products derived from 37.72 + this software without specific prior written permission. 37.73 + 37.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 37.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 37.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 37.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 37.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 37.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 37.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 37.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 37.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 37.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37.85 +*/ 37.86 + 37.87 +package jdk.internal.dynalink.linker; 37.88 + 37.89 + 37.90 +/** 37.91 + * Optional interface to be implemented by {@link GuardingTypeConverterFactory} implementers. Language-specific 37.92 + * conversions can cause increased overloaded method resolution ambiguity, as many methods can become applicable because 37.93 + * of additional conversions. The static way of selecting the "most specific" method will fail more often, because there 37.94 + * will be multiple maximally specific method with unrelated signatures. In these cases, language runtimes can be asked 37.95 + * to resolve the ambiguity by expressing preferences for one conversion over the other. 37.96 + * @author Attila Szegedi 37.97 + */ 37.98 +public interface ConversionComparator { 37.99 + /** 37.100 + * Enumeration of possible outcomes of comparing one conversion to another. 37.101 + */ 37.102 + enum Comparison { 37.103 + INDETERMINATE, 37.104 + TYPE_1_BETTER, 37.105 + TYPE_2_BETTER, 37.106 + } 37.107 + 37.108 + /** 37.109 + * Determines which of the two target types is the preferred conversion target from a source type. 37.110 + * @param sourceType the source type. 37.111 + * @param targetType1 one potential target type 37.112 + * @param targetType2 another potential target type. 37.113 + * @return one of Comparison constants that establish which - if any - of the target types is preferred for the 37.114 + * conversion. 37.115 + */ 37.116 + public Comparison compareConversion(Class<?> sourceType, Class<?> targetType1, Class<?> targetType2); 37.117 +}
38.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 38.2 +++ b/src/jdk/internal/dynalink/linker/GuardedInvocation.java Thu Feb 14 13:22:26 2013 +0100 38.3 @@ -0,0 +1,315 @@ 38.4 +/* 38.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 38.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 38.7 + * 38.8 + * This code is free software; you can redistribute it and/or modify it 38.9 + * under the terms of the GNU General Public License version 2 only, as 38.10 + * published by the Free Software Foundation. Oracle designates this 38.11 + * particular file as subject to the "Classpath" exception as provided 38.12 + * by Oracle in the LICENSE file that accompanied this code. 38.13 + * 38.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 38.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 38.17 + * version 2 for more details (a copy is included in the LICENSE file that 38.18 + * accompanied this code). 38.19 + * 38.20 + * You should have received a copy of the GNU General Public License version 38.21 + * 2 along with this work; if not, write to the Free Software Foundation, 38.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 38.23 + * 38.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 38.25 + * or visit www.oracle.com if you need additional information or have any 38.26 + * questions. 38.27 + */ 38.28 + 38.29 +/* 38.30 + * This file is available under and governed by the GNU General Public 38.31 + * License version 2 only, as published by the Free Software Foundation. 38.32 + * However, the following notice accompanied the original version of this 38.33 + * file, and Oracle licenses the original version of this file under the BSD 38.34 + * license: 38.35 + */ 38.36 +/* 38.37 + Copyright 2009-2013 Attila Szegedi 38.38 + 38.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 38.40 + and the BSD License (the "BSD License"), with licensee being free to 38.41 + choose either of the two at their discretion. 38.42 + 38.43 + You may not use this file except in compliance with either the Apache 38.44 + License or the BSD License. 38.45 + 38.46 + If you choose to use this file in compliance with the Apache License, the 38.47 + following notice applies to you: 38.48 + 38.49 + You may obtain a copy of the Apache License at 38.50 + 38.51 + http://www.apache.org/licenses/LICENSE-2.0 38.52 + 38.53 + Unless required by applicable law or agreed to in writing, software 38.54 + distributed under the License is distributed on an "AS IS" BASIS, 38.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 38.56 + implied. See the License for the specific language governing 38.57 + permissions and limitations under the License. 38.58 + 38.59 + If you choose to use this file in compliance with the BSD License, the 38.60 + following notice applies to you: 38.61 + 38.62 + Redistribution and use in source and binary forms, with or without 38.63 + modification, are permitted provided that the following conditions are 38.64 + met: 38.65 + * Redistributions of source code must retain the above copyright 38.66 + notice, this list of conditions and the following disclaimer. 38.67 + * Redistributions in binary form must reproduce the above copyright 38.68 + notice, this list of conditions and the following disclaimer in the 38.69 + documentation and/or other materials provided with the distribution. 38.70 + * Neither the name of the copyright holder nor the names of 38.71 + contributors may be used to endorse or promote products derived from 38.72 + this software without specific prior written permission. 38.73 + 38.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 38.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 38.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 38.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 38.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 38.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 38.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 38.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 38.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 38.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38.85 +*/ 38.86 + 38.87 +package jdk.internal.dynalink.linker; 38.88 + 38.89 +import java.lang.invoke.MethodHandle; 38.90 +import java.lang.invoke.MethodHandles; 38.91 +import java.lang.invoke.MethodType; 38.92 +import java.lang.invoke.SwitchPoint; 38.93 +import java.lang.invoke.WrongMethodTypeException; 38.94 +import java.util.List; 38.95 +import jdk.internal.dynalink.CallSiteDescriptor; 38.96 +import jdk.internal.dynalink.support.Guards; 38.97 + 38.98 + 38.99 +/** 38.100 + * Represents a conditionally valid method handle. It is an immutable triple of an invocation method handle, a guard 38.101 + * method handle that defines the applicability of the invocation handle, and a switch point that can be used for 38.102 + * external invalidation of the invocation handle. The invocation handle is suitable for invocation if the guard 38.103 + * handle returns true for its arguments, and as long as the switch point is not invalidated. Both the guard and the 38.104 + * switch point are optional; neither, one, or both can be present. 38.105 + * 38.106 + * @author Attila Szegedi 38.107 + */ 38.108 +public class GuardedInvocation { 38.109 + private final MethodHandle invocation; 38.110 + private final MethodHandle guard; 38.111 + private final SwitchPoint switchPoint; 38.112 + 38.113 + /** 38.114 + * Creates a new guarded invocation. 38.115 + * 38.116 + * @param invocation the method handle representing the invocation. Must not be null. 38.117 + * @param guard the method handle representing the guard. Must have the same method type as the invocation, except 38.118 + * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null to represent 38.119 + * an unconditional invocation, although that is unusual. 38.120 + * @throws NullPointerException if invocation is null. 38.121 + */ 38.122 + public GuardedInvocation(MethodHandle invocation, MethodHandle guard) { 38.123 + this(invocation, guard, null); 38.124 + } 38.125 + 38.126 + /** 38.127 + * Creates a new guarded invocation. 38.128 + * 38.129 + * @param invocation the method handle representing the invocation. Must not be null. 38.130 + * @param guard the method handle representing the guard. Must have the same method type as the invocation, except 38.131 + * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it 38.132 + * and the switch point are null, this represents an unconditional invocation, which is legal but unusual. 38.133 + * @param switchPoint the optional switch point that can be used to invalidate this linkage. 38.134 + * @throws NullPointerException if invocation is null. 38.135 + */ 38.136 + public GuardedInvocation(MethodHandle invocation, MethodHandle guard, SwitchPoint switchPoint) { 38.137 + invocation.getClass(); // NPE check 38.138 + this.invocation = invocation; 38.139 + this.guard = guard; 38.140 + this.switchPoint = switchPoint; 38.141 + } 38.142 + 38.143 + /** 38.144 + * Creates a new guarded invocation. 38.145 + * 38.146 + * @param invocation the method handle representing the invocation. Must not be null. 38.147 + * @param switchPoint the optional switch point that can be used to invalidate this linkage. 38.148 + * @param guard the method handle representing the guard. Must have the same method type as the invocation, except 38.149 + * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it 38.150 + * and the switch point are null, this represents an unconditional invocation, which is legal but unusual. 38.151 + * @throws NullPointerException if invocation is null. 38.152 + */ 38.153 + public GuardedInvocation(MethodHandle invocation, SwitchPoint switchPoint, MethodHandle guard) { 38.154 + this(invocation, guard, switchPoint); 38.155 + } 38.156 + /** 38.157 + * Returns the invocation method handle. 38.158 + * 38.159 + * @return the invocation method handle. It will never be null. 38.160 + */ 38.161 + public MethodHandle getInvocation() { 38.162 + return invocation; 38.163 + } 38.164 + 38.165 + /** 38.166 + * Returns the guard method handle. 38.167 + * 38.168 + * @return the guard method handle. Can be null. 38.169 + */ 38.170 + public MethodHandle getGuard() { 38.171 + return guard; 38.172 + } 38.173 + 38.174 + /** 38.175 + * Returns the switch point that can be used to invalidate the invocation handle. 38.176 + * 38.177 + * @return the switch point that can be used to invalidate the invocation handle. Can be null. 38.178 + */ 38.179 + public SwitchPoint getSwitchPoint() { 38.180 + return switchPoint; 38.181 + } 38.182 + 38.183 + /** 38.184 + * Returns true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated. 38.185 + * @return true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated. 38.186 + */ 38.187 + public boolean hasBeenInvalidated() { 38.188 + return switchPoint != null && switchPoint.hasBeenInvalidated(); 38.189 + } 38.190 + 38.191 + /** 38.192 + * Asserts that the invocation is of the specified type, and the guard (if present) is of the specified type with a 38.193 + * boolean return type. 38.194 + * 38.195 + * @param type the asserted type 38.196 + * @throws WrongMethodTypeException if the invocation and the guard are not of the expected method type. 38.197 + */ 38.198 + public void assertType(MethodType type) { 38.199 + assertType(invocation, type); 38.200 + if(guard != null) { 38.201 + assertType(guard, type.changeReturnType(Boolean.TYPE)); 38.202 + } 38.203 + } 38.204 + 38.205 + /** 38.206 + * Creates a new guarded invocation with different methods, preserving the switch point. 38.207 + * 38.208 + * @param newInvocation the new invocation 38.209 + * @param newGuard the new guard 38.210 + * @return a new guarded invocation with the replaced methods and the same switch point as this invocation. 38.211 + */ 38.212 + public GuardedInvocation replaceMethods(MethodHandle newInvocation, MethodHandle newGuard) { 38.213 + return new GuardedInvocation(newInvocation, newGuard, switchPoint); 38.214 + } 38.215 + 38.216 + private GuardedInvocation replaceMethodsOrThis(MethodHandle newInvocation, MethodHandle newGuard) { 38.217 + if(newInvocation == invocation && newGuard == guard) { 38.218 + return this; 38.219 + } 38.220 + return replaceMethods(newInvocation, newGuard); 38.221 + } 38.222 + 38.223 + /** 38.224 + * Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation 38.225 + * and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for 38.226 + * the guard). If the invocation already is of the required type, returns this object. 38.227 + * @param newType the new type of the invocation. 38.228 + * @return a guarded invocation with the new type applied to it. 38.229 + */ 38.230 + public GuardedInvocation asType(MethodType newType) { 38.231 + return replaceMethodsOrThis(invocation.asType(newType), guard == null ? null : Guards.asType(guard, newType)); 38.232 + } 38.233 + 38.234 + /** 38.235 + * Changes the type of the invocation, as if {@link LinkerServices#asType(MethodHandle, MethodType)} was applied to 38.236 + * its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially 38.237 + * truncated for the guard). If the invocation already is of the required type, returns this object. 38.238 + * @param linkerServices the linker services to use for the conversion 38.239 + * @param newType the new type of the invocation. 38.240 + * @return a guarded invocation with the new type applied to it. 38.241 + */ 38.242 + public GuardedInvocation asType(LinkerServices linkerServices, MethodType newType) { 38.243 + return replaceMethodsOrThis(linkerServices.asType(invocation, newType), guard == null ? null : 38.244 + Guards.asType(linkerServices, guard, newType)); 38.245 + } 38.246 + 38.247 + /** 38.248 + * Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation 38.249 + * and its guard, if it has one (with return type changed to boolean for guard). If the invocation already is of the 38.250 + * required type, returns this object. 38.251 + * @param desc a call descriptor whose method type is adapted. 38.252 + * @return a guarded invocation with the new type applied to it. 38.253 + */ 38.254 + public GuardedInvocation asType(CallSiteDescriptor desc) { 38.255 + return asType(desc.getMethodType()); 38.256 + } 38.257 + 38.258 + /** 38.259 + * Applies argument filters to both the invocation and the guard (if there is one). 38.260 + * @param pos the position of the first argumen being filtered 38.261 + * @param filters the argument filters 38.262 + * @return a filtered invocation 38.263 + */ 38.264 + public GuardedInvocation filterArguments(int pos, MethodHandle... filters) { 38.265 + return replaceMethods(MethodHandles.filterArguments(invocation, pos, filters), guard == null ? null : 38.266 + MethodHandles.filterArguments(guard, pos, filters)); 38.267 + } 38.268 + 38.269 + /** 38.270 + * Makes an invocation that drops arguments in both the invocation and the guard (if there is one). 38.271 + * @param pos the position of the first argument being dropped 38.272 + * @param valueTypes the types of the values being dropped 38.273 + * @return an invocation that drops arguments 38.274 + */ 38.275 + public GuardedInvocation dropArguments(int pos, List<Class<?>> valueTypes) { 38.276 + return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null : 38.277 + MethodHandles.dropArguments(guard, pos, valueTypes)); 38.278 + } 38.279 + 38.280 + /** 38.281 + * Makes an invocation that drops arguments in both the invocation and the guard (if there is one). 38.282 + * @param pos the position of the first argument being dropped 38.283 + * @param valueTypes the types of the values being dropped 38.284 + * @return an invocation that drops arguments 38.285 + */ 38.286 + public GuardedInvocation dropArguments(int pos, Class<?>... valueTypes) { 38.287 + return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null : 38.288 + MethodHandles.dropArguments(guard, pos, valueTypes)); 38.289 + } 38.290 + 38.291 + 38.292 + /** 38.293 + * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back. 38.294 + * @param fallback the fallback method handle in case switchpoint is invalidated or guard returns false. 38.295 + * @return a composite method handle. 38.296 + */ 38.297 + public MethodHandle compose(MethodHandle fallback) { 38.298 + return compose(fallback, fallback); 38.299 + } 38.300 + 38.301 + /** 38.302 + * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back. 38.303 + * @param switchpointFallback the fallback method handle in case switchpoint is invalidated. 38.304 + * @param guardFallback the fallback method handle in case guard returns false. 38.305 + * @return a composite method handle. 38.306 + */ 38.307 + public MethodHandle compose(MethodHandle switchpointFallback, MethodHandle guardFallback) { 38.308 + final MethodHandle guarded = 38.309 + guard == null ? invocation : MethodHandles.guardWithTest(guard, invocation, guardFallback); 38.310 + return switchPoint == null ? guarded : switchPoint.guardWithTest(guarded, switchpointFallback); 38.311 + } 38.312 + 38.313 + private static void assertType(MethodHandle mh, MethodType type) { 38.314 + if(!mh.type().equals(type)) { 38.315 + throw new WrongMethodTypeException("Expected type: " + type + " actual type: " + mh.type()); 38.316 + } 38.317 + } 38.318 +} 38.319 \ No newline at end of file
39.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 39.2 +++ b/src/jdk/internal/dynalink/linker/GuardingDynamicLinker.java Thu Feb 14 13:22:26 2013 +0100 39.3 @@ -0,0 +1,112 @@ 39.4 +/* 39.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 39.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 39.7 + * 39.8 + * This code is free software; you can redistribute it and/or modify it 39.9 + * under the terms of the GNU General Public License version 2 only, as 39.10 + * published by the Free Software Foundation. Oracle designates this 39.11 + * particular file as subject to the "Classpath" exception as provided 39.12 + * by Oracle in the LICENSE file that accompanied this code. 39.13 + * 39.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 39.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 39.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 39.17 + * version 2 for more details (a copy is included in the LICENSE file that 39.18 + * accompanied this code). 39.19 + * 39.20 + * You should have received a copy of the GNU General Public License version 39.21 + * 2 along with this work; if not, write to the Free Software Foundation, 39.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 39.23 + * 39.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 39.25 + * or visit www.oracle.com if you need additional information or have any 39.26 + * questions. 39.27 + */ 39.28 + 39.29 +/* 39.30 + * This file is available under and governed by the GNU General Public 39.31 + * License version 2 only, as published by the Free Software Foundation. 39.32 + * However, the following notice accompanied the original version of this 39.33 + * file, and Oracle licenses the original version of this file under the BSD 39.34 + * license: 39.35 + */ 39.36 +/* 39.37 + Copyright 2009-2013 Attila Szegedi 39.38 + 39.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 39.40 + and the BSD License (the "BSD License"), with licensee being free to 39.41 + choose either of the two at their discretion. 39.42 + 39.43 + You may not use this file except in compliance with either the Apache 39.44 + License or the BSD License. 39.45 + 39.46 + If you choose to use this file in compliance with the Apache License, the 39.47 + following notice applies to you: 39.48 + 39.49 + You may obtain a copy of the Apache License at 39.50 + 39.51 + http://www.apache.org/licenses/LICENSE-2.0 39.52 + 39.53 + Unless required by applicable law or agreed to in writing, software 39.54 + distributed under the License is distributed on an "AS IS" BASIS, 39.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 39.56 + implied. See the License for the specific language governing 39.57 + permissions and limitations under the License. 39.58 + 39.59 + If you choose to use this file in compliance with the BSD License, the 39.60 + following notice applies to you: 39.61 + 39.62 + Redistribution and use in source and binary forms, with or without 39.63 + modification, are permitted provided that the following conditions are 39.64 + met: 39.65 + * Redistributions of source code must retain the above copyright 39.66 + notice, this list of conditions and the following disclaimer. 39.67 + * Redistributions in binary form must reproduce the above copyright 39.68 + notice, this list of conditions and the following disclaimer in the 39.69 + documentation and/or other materials provided with the distribution. 39.70 + * Neither the name of the copyright holder nor the names of 39.71 + contributors may be used to endorse or promote products derived from 39.72 + this software without specific prior written permission. 39.73 + 39.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 39.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 39.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 39.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 39.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 39.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 39.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 39.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 39.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 39.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 39.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39.85 +*/ 39.86 + 39.87 +package jdk.internal.dynalink.linker; 39.88 + 39.89 +/** 39.90 + * The base interface for language-specific dynamic linkers. Such linkers always have to produce method handles with 39.91 + * guards, as the validity of the method handle for calls at a call site inevitably depends on some condition (at the 39.92 + * very least, it depends on the receiver belonging to the language runtime of the linker). Language runtime 39.93 + * implementors will normally implement one for their own language, and declare it in the 39.94 + * <tt>META-INF/services/jdk.internal.dynalink.linker.GuardingDynamicLinker</tt> file within their JAR file. 39.95 + * 39.96 + * @author Attila Szegedi 39.97 + */ 39.98 +public interface GuardingDynamicLinker { 39.99 + /** 39.100 + * Creates a guarded invocation appropriate for a particular invocation with the specified arguments at a call site. 39.101 + * 39.102 + * @param linkRequest the object describing the request for linking a particular invocation 39.103 + * @param linkerServices linker services 39.104 + * @return a guarded invocation with a method handle suitable for the arguments, as well as a guard condition that 39.105 + * if fails should trigger relinking. Must return null if it can't resolve the invocation. If the returned 39.106 + * invocation is unconditional (which is actually quite rare), the guard in the return value can be null. The 39.107 + * invocation can also have a switch point for asynchronous invalidation of the linkage. If the linker does not 39.108 + * recognize any native language runtime contexts in arguments, or does recognize its own, but receives a call site 39.109 + * descriptor without its recognized context in the arguments, it should invoke 39.110 + * {@link LinkRequest#withoutRuntimeContext()} and link for that. 39.111 + * @throws Exception if the operation fails for whatever reason 39.112 + */ 39.113 + public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) 39.114 + throws Exception; 39.115 +} 39.116 \ No newline at end of file
40.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 40.2 +++ b/src/jdk/internal/dynalink/linker/GuardingTypeConverterFactory.java Thu Feb 14 13:22:26 2013 +0100 40.3 @@ -0,0 +1,114 @@ 40.4 +/* 40.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 40.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 40.7 + * 40.8 + * This code is free software; you can redistribute it and/or modify it 40.9 + * under the terms of the GNU General Public License version 2 only, as 40.10 + * published by the Free Software Foundation. Oracle designates this 40.11 + * particular file as subject to the "Classpath" exception as provided 40.12 + * by Oracle in the LICENSE file that accompanied this code. 40.13 + * 40.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 40.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 40.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 40.17 + * version 2 for more details (a copy is included in the LICENSE file that 40.18 + * accompanied this code). 40.19 + * 40.20 + * You should have received a copy of the GNU General Public License version 40.21 + * 2 along with this work; if not, write to the Free Software Foundation, 40.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 40.23 + * 40.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 40.25 + * or visit www.oracle.com if you need additional information or have any 40.26 + * questions. 40.27 + */ 40.28 + 40.29 +/* 40.30 + * This file is available under and governed by the GNU General Public 40.31 + * License version 2 only, as published by the Free Software Foundation. 40.32 + * However, the following notice accompanied the original version of this 40.33 + * file, and Oracle licenses the original version of this file under the BSD 40.34 + * license: 40.35 + */ 40.36 +/* 40.37 + Copyright 2009-2013 Attila Szegedi 40.38 + 40.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 40.40 + and the BSD License (the "BSD License"), with licensee being free to 40.41 + choose either of the two at their discretion. 40.42 + 40.43 + You may not use this file except in compliance with either the Apache 40.44 + License or the BSD License. 40.45 + 40.46 + If you choose to use this file in compliance with the Apache License, the 40.47 + following notice applies to you: 40.48 + 40.49 + You may obtain a copy of the Apache License at 40.50 + 40.51 + http://www.apache.org/licenses/LICENSE-2.0 40.52 + 40.53 + Unless required by applicable law or agreed to in writing, software 40.54 + distributed under the License is distributed on an "AS IS" BASIS, 40.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 40.56 + implied. See the License for the specific language governing 40.57 + permissions and limitations under the License. 40.58 + 40.59 + If you choose to use this file in compliance with the BSD License, the 40.60 + following notice applies to you: 40.61 + 40.62 + Redistribution and use in source and binary forms, with or without 40.63 + modification, are permitted provided that the following conditions are 40.64 + met: 40.65 + * Redistributions of source code must retain the above copyright 40.66 + notice, this list of conditions and the following disclaimer. 40.67 + * Redistributions in binary form must reproduce the above copyright 40.68 + notice, this list of conditions and the following disclaimer in the 40.69 + documentation and/or other materials provided with the distribution. 40.70 + * Neither the name of the copyright holder nor the names of 40.71 + contributors may be used to endorse or promote products derived from 40.72 + this software without specific prior written permission. 40.73 + 40.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 40.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 40.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 40.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 40.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 40.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 40.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 40.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 40.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 40.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 40.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40.85 +*/ 40.86 + 40.87 +package jdk.internal.dynalink.linker; 40.88 + 40.89 +import jdk.internal.dynalink.support.TypeUtilities; 40.90 + 40.91 +/** 40.92 + * Optional interface that can be implemented by {@link GuardingDynamicLinker} implementations to provide 40.93 + * language-runtime specific implicit type conversion capabilities. Note that if you implement this interface, you will 40.94 + * very likely want to implement {@link ConversionComparator} interface too, as your additional language-specific 40.95 + * conversions, in absence of a strategy for prioritizing these conversions, will cause more ambiguity in selecting the 40.96 + * correct overload when trying to link to an overloaded POJO method. 40.97 + * 40.98 + * @author Attila Szegedi 40.99 + */ 40.100 +public interface GuardingTypeConverterFactory { 40.101 + /** 40.102 + * Returns a guarded invocation that receives an Object of the specified source type and returns an Object converted 40.103 + * to the specified target type. The type of the invocation is targetType(sourceType), while the type of the guard 40.104 + * is boolean(sourceType). Note that this will never be invoked for type conversions allowed by the JLS 5.3 "Method 40.105 + * Invocation Conversion", see {@link TypeUtilities#isMethodInvocationConvertible(Class, Class)} for details. An 40.106 + * implementation can assume it is never requested to produce a converter for these conversions. 40.107 + * 40.108 + * @param sourceType source type 40.109 + * @param targetType the target type. 40.110 + * @return a guarded invocation that can take an object (if it passes guard) and returns another object that is its 40.111 + * representation coerced into the target type. In case the factory is certain it is unable to handle a conversion, 40.112 + * it can return null. In case the factory is certain that it can always handle the conversion, it can return an 40.113 + * unconditional invocation (one whose guard is null). 40.114 + * @throws Exception if there was an error during creation of the converter 40.115 + */ 40.116 + public GuardedInvocation convertToType(Class<?> sourceType, Class<?> targetType) throws Exception; 40.117 +} 40.118 \ No newline at end of file
41.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 41.2 +++ b/src/jdk/internal/dynalink/linker/LinkRequest.java Thu Feb 14 13:22:26 2013 +0100 41.3 @@ -0,0 +1,149 @@ 41.4 +/* 41.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 41.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41.7 + * 41.8 + * This code is free software; you can redistribute it and/or modify it 41.9 + * under the terms of the GNU General Public License version 2 only, as 41.10 + * published by the Free Software Foundation. Oracle designates this 41.11 + * particular file as subject to the "Classpath" exception as provided 41.12 + * by Oracle in the LICENSE file that accompanied this code. 41.13 + * 41.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 41.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 41.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 41.17 + * version 2 for more details (a copy is included in the LICENSE file that 41.18 + * accompanied this code). 41.19 + * 41.20 + * You should have received a copy of the GNU General Public License version 41.21 + * 2 along with this work; if not, write to the Free Software Foundation, 41.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 41.23 + * 41.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 41.25 + * or visit www.oracle.com if you need additional information or have any 41.26 + * questions. 41.27 + */ 41.28 + 41.29 +/* 41.30 + * This file is available under and governed by the GNU General Public 41.31 + * License version 2 only, as published by the Free Software Foundation. 41.32 + * However, the following notice accompanied the original version of this 41.33 + * file, and Oracle licenses the original version of this file under the BSD 41.34 + * license: 41.35 + */ 41.36 +/* 41.37 + Copyright 2009-2013 Attila Szegedi 41.38 + 41.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 41.40 + and the BSD License (the "BSD License"), with licensee being free to 41.41 + choose either of the two at their discretion. 41.42 + 41.43 + You may not use this file except in compliance with either the Apache 41.44 + License or the BSD License. 41.45 + 41.46 + If you choose to use this file in compliance with the Apache License, the 41.47 + following notice applies to you: 41.48 + 41.49 + You may obtain a copy of the Apache License at 41.50 + 41.51 + http://www.apache.org/licenses/LICENSE-2.0 41.52 + 41.53 + Unless required by applicable law or agreed to in writing, software 41.54 + distributed under the License is distributed on an "AS IS" BASIS, 41.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 41.56 + implied. See the License for the specific language governing 41.57 + permissions and limitations under the License. 41.58 + 41.59 + If you choose to use this file in compliance with the BSD License, the 41.60 + following notice applies to you: 41.61 + 41.62 + Redistribution and use in source and binary forms, with or without 41.63 + modification, are permitted provided that the following conditions are 41.64 + met: 41.65 + * Redistributions of source code must retain the above copyright 41.66 + notice, this list of conditions and the following disclaimer. 41.67 + * Redistributions in binary form must reproduce the above copyright 41.68 + notice, this list of conditions and the following disclaimer in the 41.69 + documentation and/or other materials provided with the distribution. 41.70 + * Neither the name of the copyright holder nor the names of 41.71 + contributors may be used to endorse or promote products derived from 41.72 + this software without specific prior written permission. 41.73 + 41.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 41.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 41.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 41.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 41.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 41.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 41.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 41.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 41.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 41.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41.85 +*/ 41.86 + 41.87 +package jdk.internal.dynalink.linker; 41.88 + 41.89 +import jdk.internal.dynalink.CallSiteDescriptor; 41.90 +import jdk.internal.dynalink.DynamicLinkerFactory; 41.91 + 41.92 +/** 41.93 + * Represents a request to link a particular invocation at a particular call site. Instances of these requests are being 41.94 + * passed to {@link GuardingDynamicLinker}. 41.95 + * 41.96 + * @author Attila Szegedi 41.97 + */ 41.98 +public interface LinkRequest { 41.99 + /** 41.100 + * Returns the call site descriptor for the call site being linked. 41.101 + * 41.102 + * @return the call site descriptor for the call site being linked. 41.103 + */ 41.104 + public CallSiteDescriptor getCallSiteDescriptor(); 41.105 + 41.106 + /** 41.107 + * Returns the arguments for the invocation being linked. The returned array is a clone; modifications to it won't 41.108 + * affect the arguments in this request. 41.109 + * 41.110 + * @return the arguments for the invocation being linked. 41.111 + */ 41.112 + public Object[] getArguments(); 41.113 + 41.114 + /** 41.115 + * Returns the 0th argument for the invocation being linked; this is typically the receiver object. 41.116 + * 41.117 + * @return the receiver object. 41.118 + */ 41.119 + public Object getReceiver(); 41.120 + 41.121 + /** 41.122 + * Returns true if the call site is considered unstable, that is, it has been relinked more times than was 41.123 + * specified in {@link DynamicLinkerFactory#setUnstableRelinkThreshold(int)}. Linkers should use this as a 41.124 + * hint to prefer producing linkage that is more stable (its guard fails less frequently), even if that assumption 41.125 + * causes a less effective version of an operation to be linked. This is just a hint, of course, and linkers are 41.126 + * free to ignore this property. 41.127 + * @return true if the call site is considered unstable. 41.128 + */ 41.129 + public boolean isCallSiteUnstable(); 41.130 + 41.131 + /** 41.132 + * Returns a request stripped from runtime context arguments. Some language runtimes will include runtime-specific 41.133 + * context parameters in their call sites as few arguments between 0th argument "this" and the normal arguments. If 41.134 + * a linker does not recognize such contexts at all, or does not recognize the call site as one with its own 41.135 + * context, it can ask for the alternative link request with context parameters and arguments removed, and link 41.136 + * against it instead. 41.137 + * 41.138 + * @return the context-stripped request. If the link request does not have any language runtime specific context 41.139 + * parameters, the same link request is returned. 41.140 + */ 41.141 + public LinkRequest withoutRuntimeContext(); 41.142 + 41.143 + /** 41.144 + * Returns a request identical to this one with call site descriptor and arguments replaced with the ones specified. 41.145 + * 41.146 + * @param callSiteDescriptor the new call site descriptor 41.147 + * @param arguments the new arguments 41.148 + * @return a new request identical to this one, except with the call site descriptor and arguments replaced with the 41.149 + * specified ones. 41.150 + */ 41.151 + public LinkRequest replaceArguments(CallSiteDescriptor callSiteDescriptor, Object[] arguments); 41.152 +} 41.153 \ No newline at end of file
42.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 42.2 +++ b/src/jdk/internal/dynalink/linker/LinkerServices.java Thu Feb 14 13:22:26 2013 +0100 42.3 @@ -0,0 +1,165 @@ 42.4 +/* 42.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 42.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 42.7 + * 42.8 + * This code is free software; you can redistribute it and/or modify it 42.9 + * under the terms of the GNU General Public License version 2 only, as 42.10 + * published by the Free Software Foundation. Oracle designates this 42.11 + * particular file as subject to the "Classpath" exception as provided 42.12 + * by Oracle in the LICENSE file that accompanied this code. 42.13 + * 42.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 42.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 42.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 42.17 + * version 2 for more details (a copy is included in the LICENSE file that 42.18 + * accompanied this code). 42.19 + * 42.20 + * You should have received a copy of the GNU General Public License version 42.21 + * 2 along with this work; if not, write to the Free Software Foundation, 42.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 42.23 + * 42.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 42.25 + * or visit www.oracle.com if you need additional information or have any 42.26 + * questions. 42.27 + */ 42.28 + 42.29 +/* 42.30 + * This file is available under and governed by the GNU General Public 42.31 + * License version 2 only, as published by the Free Software Foundation. 42.32 + * However, the following notice accompanied the original version of this 42.33 + * file, and Oracle licenses the original version of this file under the BSD 42.34 + * license: 42.35 + */ 42.36 +/* 42.37 + Copyright 2009-2013 Attila Szegedi 42.38 + 42.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 42.40 + and the BSD License (the "BSD License"), with licensee being free to 42.41 + choose either of the two at their discretion. 42.42 + 42.43 + You may not use this file except in compliance with either the Apache 42.44 + License or the BSD License. 42.45 + 42.46 + If you choose to use this file in compliance with the Apache License, the 42.47 + following notice applies to you: 42.48 + 42.49 + You may obtain a copy of the Apache License at 42.50 + 42.51 + http://www.apache.org/licenses/LICENSE-2.0 42.52 + 42.53 + Unless required by applicable law or agreed to in writing, software 42.54 + distributed under the License is distributed on an "AS IS" BASIS, 42.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 42.56 + implied. See the License for the specific language governing 42.57 + permissions and limitations under the License. 42.58 + 42.59 + If you choose to use this file in compliance with the BSD License, the 42.60 + following notice applies to you: 42.61 + 42.62 + Redistribution and use in source and binary forms, with or without 42.63 + modification, are permitted provided that the following conditions are 42.64 + met: 42.65 + * Redistributions of source code must retain the above copyright 42.66 + notice, this list of conditions and the following disclaimer. 42.67 + * Redistributions in binary form must reproduce the above copyright 42.68 + notice, this list of conditions and the following disclaimer in the 42.69 + documentation and/or other materials provided with the distribution. 42.70 + * Neither the name of the copyright holder nor the names of 42.71 + contributors may be used to endorse or promote products derived from 42.72 + this software without specific prior written permission. 42.73 + 42.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 42.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 42.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 42.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 42.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 42.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 42.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 42.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 42.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 42.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 42.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42.85 +*/ 42.86 + 42.87 +package jdk.internal.dynalink.linker; 42.88 + 42.89 +import java.lang.invoke.MethodHandle; 42.90 +import java.lang.invoke.MethodHandles; 42.91 +import java.lang.invoke.MethodType; 42.92 +import jdk.internal.dynalink.DynamicLinker; 42.93 +import jdk.internal.dynalink.linker.ConversionComparator.Comparison; 42.94 + 42.95 + 42.96 +/** 42.97 + * Interface for services provided to {@link GuardingDynamicLinker} instances by the {@link DynamicLinker} that owns 42.98 + * them. You can think of it as the interface of the {@link DynamicLinker} that faces the {@link GuardingDynamicLinker} 42.99 + * s. 42.100 + * 42.101 + * @author Attila Szegedi 42.102 + */ 42.103 +public interface LinkerServices { 42.104 + /** 42.105 + * Similar to {@link MethodHandle#asType(MethodType)} except it also hooks in method handles produced by 42.106 + * {@link GuardingTypeConverterFactory} implementations, providing for language-specific type coercing of 42.107 + * parameters. It will apply {@link MethodHandle#asType(MethodType)} for all primitive-to-primitive, 42.108 + * wrapper-to-primitive, primitive-to-wrapper conversions as well as for all upcasts. For all other conversions, 42.109 + * it'll insert {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)} with composite filters 42.110 + * provided by {@link GuardingTypeConverterFactory} implementations. It doesn't use language-specific conversions on 42.111 + * the return type. 42.112 + * 42.113 + * @param handle target method handle 42.114 + * @param fromType the types of source arguments 42.115 + * @return a method handle that is a suitable combination of {@link MethodHandle#asType(MethodType)} and 42.116 + * {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)} with 42.117 + * {@link GuardingTypeConverterFactory} produced type converters as filters. 42.118 + */ 42.119 + public MethodHandle asType(MethodHandle handle, MethodType fromType); 42.120 + 42.121 + /** 42.122 + * Given a source and target type, returns a method handle that converts between them. Never returns null; in worst 42.123 + * case it will return an identity conversion (that might fail for some values at runtime). You rarely need to use 42.124 + * this method directly; you should mostly rely on {@link #asType(MethodHandle, MethodType)} instead. You really 42.125 + * only need this method if you have a piece of your program that is written in Java, and you need to reuse existing 42.126 + * type conversion machinery in a non-invokedynamic context. 42.127 + * @param sourceType the type to convert from 42.128 + * @param targetType the type to convert to 42.129 + * @return a method handle performing the conversion. 42.130 + */ 42.131 + public MethodHandle getTypeConverter(Class<?> sourceType, Class<?> targetType); 42.132 + 42.133 + /** 42.134 + * Returns true if there might exist a conversion between the requested types (either an automatic JVM conversion, 42.135 + * or one provided by any available {@link GuardingTypeConverterFactory}), or false if there definitely does not 42.136 + * exist a conversion between the requested types. Note that returning true does not guarantee that the conversion 42.137 + * will succeed at runtime (notably, if the "from" or "to" types are sufficiently generic), but returning false 42.138 + * guarantees that it would fail. 42.139 + * 42.140 + * @param from the source type for the conversion 42.141 + * @param to the target type for the conversion 42.142 + * @return true if there can be a conversion, false if there can not. 42.143 + */ 42.144 + public boolean canConvert(Class<?> from, Class<?> to); 42.145 + 42.146 + /** 42.147 + * Creates a guarded invocation using the {@link DynamicLinker} that exposes this linker services interface. Linkers 42.148 + * can typically use them to delegate linking of wrapped objects. 42.149 + * 42.150 + * @param linkRequest a request for linking the invocation 42.151 + * @return a guarded invocation linked by the top-level linker (or any of its delegates). Can be null if no 42.152 + * available linker is able to link the invocation. 42.153 + * @throws Exception in case the top-level linker throws an exception 42.154 + */ 42.155 + public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest) throws Exception; 42.156 + 42.157 + /** 42.158 + * Determines which of the two type conversions from a source type to the two target types is preferred. This is 42.159 + * used for dynamic overloaded method resolution. If the source type is convertible to exactly one target type with 42.160 + * a method invocation conversion, it is chosen, otherwise available {@link ConversionComparator}s are consulted. 42.161 + * @param sourceType the source type. 42.162 + * @param targetType1 one potential target type 42.163 + * @param targetType2 another potential target type. 42.164 + * @return one of Comparison constants that establish which - if any - of the target types is preferable for the 42.165 + * conversion. 42.166 + */ 42.167 + public Comparison compareConversion(Class<?> sourceType, Class<?> targetType1, Class<?> targetType2); 42.168 +} 42.169 \ No newline at end of file
43.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 43.2 +++ b/src/jdk/internal/dynalink/linker/TypeBasedGuardingDynamicLinker.java Thu Feb 14 13:22:26 2013 +0100 43.3 @@ -0,0 +1,102 @@ 43.4 +/* 43.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 43.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 43.7 + * 43.8 + * This code is free software; you can redistribute it and/or modify it 43.9 + * under the terms of the GNU General Public License version 2 only, as 43.10 + * published by the Free Software Foundation. Oracle designates this 43.11 + * particular file as subject to the "Classpath" exception as provided 43.12 + * by Oracle in the LICENSE file that accompanied this code. 43.13 + * 43.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 43.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 43.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 43.17 + * version 2 for more details (a copy is included in the LICENSE file that 43.18 + * accompanied this code). 43.19 + * 43.20 + * You should have received a copy of the GNU General Public License version 43.21 + * 2 along with this work; if not, write to the Free Software Foundation, 43.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 43.23 + * 43.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 43.25 + * or visit www.oracle.com if you need additional information or have any 43.26 + * questions. 43.27 + */ 43.28 + 43.29 +/* 43.30 + * This file is available under and governed by the GNU General Public 43.31 + * License version 2 only, as published by the Free Software Foundation. 43.32 + * However, the following notice accompanied the original version of this 43.33 + * file, and Oracle licenses the original version of this file under the BSD 43.34 + * license: 43.35 + */ 43.36 +/* 43.37 + Copyright 2009-2013 Attila Szegedi 43.38 + 43.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 43.40 + and the BSD License (the "BSD License"), with licensee being free to 43.41 + choose either of the two at their discretion. 43.42 + 43.43 + You may not use this file except in compliance with either the Apache 43.44 + License or the BSD License. 43.45 + 43.46 + If you choose to use this file in compliance with the Apache License, the 43.47 + following notice applies to you: 43.48 + 43.49 + You may obtain a copy of the Apache License at 43.50 + 43.51 + http://www.apache.org/licenses/LICENSE-2.0 43.52 + 43.53 + Unless required by applicable law or agreed to in writing, software 43.54 + distributed under the License is distributed on an "AS IS" BASIS, 43.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 43.56 + implied. See the License for the specific language governing 43.57 + permissions and limitations under the License. 43.58 + 43.59 + If you choose to use this file in compliance with the BSD License, the 43.60 + following notice applies to you: 43.61 + 43.62 + Redistribution and use in source and binary forms, with or without 43.63 + modification, are permitted provided that the following conditions are 43.64 + met: 43.65 + * Redistributions of source code must retain the above copyright 43.66 + notice, this list of conditions and the following disclaimer. 43.67 + * Redistributions in binary form must reproduce the above copyright 43.68 + notice, this list of conditions and the following disclaimer in the 43.69 + documentation and/or other materials provided with the distribution. 43.70 + * Neither the name of the copyright holder nor the names of 43.71 + contributors may be used to endorse or promote products derived from 43.72 + this software without specific prior written permission. 43.73 + 43.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 43.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 43.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 43.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 43.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 43.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 43.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 43.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 43.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 43.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 43.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 43.85 +*/ 43.86 + 43.87 +package jdk.internal.dynalink.linker; 43.88 + 43.89 +/** 43.90 + * A guarding dynamic linker that can determine whether it can link the call site solely based on the type of the first 43.91 + * argument at linking invocation time. (The first argument is usually the receiver class). Most language-specific 43.92 + * linkers will fall into this category, as they recognize their native objects as Java objects of classes implementing 43.93 + * a specific language-native interface or superclass. The linker mechanism can optimize the dispatch for these linkers. 43.94 + * 43.95 + * @author Attila Szegedi 43.96 + */ 43.97 +public interface TypeBasedGuardingDynamicLinker extends GuardingDynamicLinker { 43.98 + /** 43.99 + * Returns true if the linker can link an invocation where the first argument (receiver) is of the specified type. 43.100 + * 43.101 + * @param type the type to link 43.102 + * @return true if the linker can link calls for the receiver type, or false otherwise. 43.103 + */ 43.104 + public boolean canLinkType(Class<?> type); 43.105 +}
44.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 44.2 +++ b/src/jdk/internal/dynalink/linker/package.html Thu Feb 14 13:22:26 2013 +0100 44.3 @@ -0,0 +1,87 @@ 44.4 +<!-- 44.5 + Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 44.6 + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 44.7 + 44.8 + This code is free software; you can redistribute it and/or modify it 44.9 + under the terms of the GNU General Public License version 2 only, as 44.10 + published by the Free Software Foundation. Oracle designates this 44.11 + particular file as subject to the "Classpath" exception as provided 44.12 + by Oracle in the LICENSE file that accompanied this code. 44.13 + 44.14 + This code is distributed in the hope that it will be useful, but WITHOUT 44.15 + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 44.16 + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 44.17 + version 2 for more details (a copy is included in the LICENSE file that 44.18 + accompanied this code). 44.19 + 44.20 + You should have received a copy of the GNU General Public License version 44.21 + 2 along with this work; if not, write to the Free Software Foundation, 44.22 + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 44.23 + 44.24 + Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 44.25 + or visit www.oracle.com if you need additional information or have any 44.26 + questions. 44.27 +--> 44.28 + 44.29 +<!-- 44.30 + This file is available under and governed by the GNU General Public 44.31 + License version 2 only, as published by the Free Software Foundation. 44.32 + However, the following notice accompanied the original version of this 44.33 + file, and Oracle licenses the original version of this file under the BSD 44.34 + license: 44.35 + 44.36 + Copyright 2009-2013 Attila Szegedi 44.37 + 44.38 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 44.39 + and the BSD License (the "BSD License"), with licensee being free to 44.40 + choose either of the two at their discretion. 44.41 + 44.42 + You may not use this file except in compliance with either the Apache 44.43 + License or the BSD License. 44.44 + 44.45 + If you choose to use this file in compliance with the Apache License, the 44.46 + following notice applies to you: 44.47 + 44.48 + You may obtain a copy of the Apache License at 44.49 + 44.50 + http://www.apache.org/licenses/LICENSE-2.0 44.51 + 44.52 + Unless required by applicable law or agreed to in writing, software 44.53 + distributed under the License is distributed on an "AS IS" BASIS, 44.54 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 44.55 + implied. See the License for the specific language governing 44.56 + permissions and limitations under the License. 44.57 + 44.58 + If you choose to use this file in compliance with the BSD License, the 44.59 + following notice applies to you: 44.60 + 44.61 + Redistribution and use in source and binary forms, with or without 44.62 + modification, are permitted provided that the following conditions are 44.63 + met: 44.64 + * Redistributions of source code must retain the above copyright 44.65 + notice, this list of conditions and the following disclaimer. 44.66 + * Redistributions in binary form must reproduce the above copyright 44.67 + notice, this list of conditions and the following disclaimer in the 44.68 + documentation and/or other materials provided with the distribution. 44.69 + * Neither the name of the copyright holder nor the names of 44.70 + contributors may be used to endorse or promote products derived from 44.71 + this software without specific prior written permission. 44.72 + 44.73 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 44.74 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 44.75 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 44.76 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 44.77 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 44.78 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 44.79 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 44.80 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 44.81 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 44.82 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 44.83 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 44.84 +--> 44.85 + <body> 44.86 + <p> 44.87 + Contains interfaces and classes needed by language runtimes to implement 44.88 + their own language-specific linkers. 44.89 + </p> 44.90 + </body>
45.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 45.2 +++ b/src/jdk/internal/dynalink/package.html Thu Feb 14 13:22:26 2013 +0100 45.3 @@ -0,0 +1,93 @@ 45.4 +<!-- 45.5 + Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 45.6 + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 45.7 + 45.8 + This code is free software; you can redistribute it and/or modify it 45.9 + under the terms of the GNU General Public License version 2 only, as 45.10 + published by the Free Software Foundation. Oracle designates this 45.11 + particular file as subject to the "Classpath" exception as provided 45.12 + by Oracle in the LICENSE file that accompanied this code. 45.13 + 45.14 + This code is distributed in the hope that it will be useful, but WITHOUT 45.15 + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 45.16 + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 45.17 + version 2 for more details (a copy is included in the LICENSE file that 45.18 + accompanied this code). 45.19 + 45.20 + You should have received a copy of the GNU General Public License version 45.21 + 2 along with this work; if not, write to the Free Software Foundation, 45.22 + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 45.23 + 45.24 + Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 45.25 + or visit www.oracle.com if you need additional information or have any 45.26 + questions. 45.27 +--> 45.28 + 45.29 +<!-- 45.30 + This file is available under and governed by the GNU General Public 45.31 + License version 2 only, as published by the Free Software Foundation. 45.32 + However, the following notice accompanied the original version of this 45.33 + file, and Oracle licenses the original version of this file under the BSD 45.34 + license: 45.35 + 45.36 + Copyright 2009-2013 Attila Szegedi 45.37 + 45.38 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 45.39 + and the BSD License (the "BSD License"), with licensee being free to 45.40 + choose either of the two at their discretion. 45.41 + 45.42 + You may not use this file except in compliance with either the Apache 45.43 + License or the BSD License. 45.44 + 45.45 + If you choose to use this file in compliance with the Apache License, the 45.46 + following notice applies to you: 45.47 + 45.48 + You may obtain a copy of the Apache License at 45.49 + 45.50 + http://www.apache.org/licenses/LICENSE-2.0 45.51 + 45.52 + Unless required by applicable law or agreed to in writing, software 45.53 + distributed under the License is distributed on an "AS IS" BASIS, 45.54 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 45.55 + implied. See the License for the specific language governing 45.56 + permissions and limitations under the License. 45.57 + 45.58 + If you choose to use this file in compliance with the BSD License, the 45.59 + following notice applies to you: 45.60 + 45.61 + Redistribution and use in source and binary forms, with or without 45.62 + modification, are permitted provided that the following conditions are 45.63 + met: 45.64 + * Redistributions of source code must retain the above copyright 45.65 + notice, this list of conditions and the following disclaimer. 45.66 + * Redistributions in binary form must reproduce the above copyright 45.67 + notice, this list of conditions and the following disclaimer in the 45.68 + documentation and/or other materials provided with the distribution. 45.69 + * Neither the name of the copyright holder nor the names of 45.70 + contributors may be used to endorse or promote products derived from 45.71 + this software without specific prior written permission. 45.72 + 45.73 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 45.74 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 45.75 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 45.76 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 45.77 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 45.78 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 45.79 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 45.80 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 45.81 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 45.82 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 45.83 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 45.84 +--> 45.85 + <body> 45.86 + <p> 45.87 + Contains the main API for using the dynamic linking facilities. A 45.88 + scripting framework or a language runtime that does not define its own 45.89 + linker but only uses linkers available in the system will only need to 45.90 + use classes and interfaces from this package. 45.91 + </p> 45.92 + <p> 45.93 + Languages that wish to define and use their own linkers will also need to 45.94 + use the {@link jdk.internal.dynalink.linker} package. 45.95 + </p> 45.96 + </body>
46.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 46.2 +++ b/src/jdk/internal/dynalink/support/AbstractCallSiteDescriptor.java Thu Feb 14 13:22:26 2013 +0100 46.3 @@ -0,0 +1,186 @@ 46.4 +/* 46.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 46.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 46.7 + * 46.8 + * This code is free software; you can redistribute it and/or modify it 46.9 + * under the terms of the GNU General Public License version 2 only, as 46.10 + * published by the Free Software Foundation. Oracle designates this 46.11 + * particular file as subject to the "Classpath" exception as provided 46.12 + * by Oracle in the LICENSE file that accompanied this code. 46.13 + * 46.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 46.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 46.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 46.17 + * version 2 for more details (a copy is included in the LICENSE file that 46.18 + * accompanied this code). 46.19 + * 46.20 + * You should have received a copy of the GNU General Public License version 46.21 + * 2 along with this work; if not, write to the Free Software Foundation, 46.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 46.23 + * 46.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 46.25 + * or visit www.oracle.com if you need additional information or have any 46.26 + * questions. 46.27 + */ 46.28 + 46.29 +/* 46.30 + * This file is available under and governed by the GNU General Public 46.31 + * License version 2 only, as published by the Free Software Foundation. 46.32 + * However, the following notice accompanied the original version of this 46.33 + * file, and Oracle licenses the original version of this file under the BSD 46.34 + * license: 46.35 + */ 46.36 +/* 46.37 + Copyright 2009-2013 Attila Szegedi 46.38 + 46.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 46.40 + and the BSD License (the "BSD License"), with licensee being free to 46.41 + choose either of the two at their discretion. 46.42 + 46.43 + You may not use this file except in compliance with either the Apache 46.44 + License or the BSD License. 46.45 + 46.46 + If you choose to use this file in compliance with the Apache License, the 46.47 + following notice applies to you: 46.48 + 46.49 + You may obtain a copy of the Apache License at 46.50 + 46.51 + http://www.apache.org/licenses/LICENSE-2.0 46.52 + 46.53 + Unless required by applicable law or agreed to in writing, software 46.54 + distributed under the License is distributed on an "AS IS" BASIS, 46.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 46.56 + implied. See the License for the specific language governing 46.57 + permissions and limitations under the License. 46.58 + 46.59 + If you choose to use this file in compliance with the BSD License, the 46.60 + following notice applies to you: 46.61 + 46.62 + Redistribution and use in source and binary forms, with or without 46.63 + modification, are permitted provided that the following conditions are 46.64 + met: 46.65 + * Redistributions of source code must retain the above copyright 46.66 + notice, this list of conditions and the following disclaimer. 46.67 + * Redistributions in binary form must reproduce the above copyright 46.68 + notice, this list of conditions and the following disclaimer in the 46.69 + documentation and/or other materials provided with the distribution. 46.70 + * Neither the name of the copyright holder nor the names of 46.71 + contributors may be used to endorse or promote products derived from 46.72 + this software without specific prior written permission. 46.73 + 46.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 46.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 46.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 46.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 46.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 46.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 46.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 46.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 46.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 46.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 46.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 46.85 +*/ 46.86 + 46.87 +package jdk.internal.dynalink.support; 46.88 + 46.89 +import java.lang.invoke.MethodHandles; 46.90 +import java.lang.invoke.MethodHandles.Lookup; 46.91 +import java.util.Objects; 46.92 +import jdk.internal.dynalink.CallSiteDescriptor; 46.93 + 46.94 + 46.95 +/** 46.96 + * A base class for call site descriptor implementations. Provides reconstruction of the name from the tokens, as well 46.97 + * as a generally useful {@code equals} and {@code hashCode} methods. 46.98 + * @author Attila Szegedi 46.99 + */ 46.100 +public abstract class AbstractCallSiteDescriptor implements CallSiteDescriptor { 46.101 + 46.102 + @Override 46.103 + public String getName() { 46.104 + return appendName(new StringBuilder(getNameLength())).toString(); 46.105 + } 46.106 + 46.107 + @Override 46.108 + public Lookup getLookup() { 46.109 + return MethodHandles.publicLookup(); 46.110 + } 46.111 + 46.112 + @Override 46.113 + public boolean equals(Object obj) { 46.114 + return obj instanceof CallSiteDescriptor && equals((CallSiteDescriptor)obj); 46.115 + } 46.116 + 46.117 + /** 46.118 + * Returns true if this call site descriptor is equal to the passed call site descriptor. 46.119 + * @param csd the other call site descriptor. 46.120 + * @return true if they are equal. 46.121 + */ 46.122 + public boolean equals(CallSiteDescriptor csd) { 46.123 + if(csd == null) { 46.124 + return false; 46.125 + } 46.126 + if(csd == this) { 46.127 + return true; 46.128 + } 46.129 + final int ntc = getNameTokenCount(); 46.130 + if(ntc != csd.getNameTokenCount()) { 46.131 + return false; 46.132 + } 46.133 + for(int i = ntc; i-- > 0;) { // Reverse order as variability is higher at the end 46.134 + if(!Objects.equals(getNameToken(i), csd.getNameToken(i))) { 46.135 + return false; 46.136 + } 46.137 + } 46.138 + if(!getMethodType().equals(csd.getMethodType())) { 46.139 + return false; 46.140 + } 46.141 + return lookupsEqual(getLookup(), csd.getLookup()); 46.142 + } 46.143 + 46.144 + @Override 46.145 + public int hashCode() { 46.146 + final int c = getNameTokenCount(); 46.147 + int h = 0; 46.148 + for(int i = 0; i < c; ++i) { 46.149 + h = h * 31 + getNameToken(i).hashCode(); 46.150 + } 46.151 + return h * 31 + getMethodType().hashCode(); 46.152 + } 46.153 + 46.154 + @Override 46.155 + public String toString() { 46.156 + final String mt = getMethodType().toString(); 46.157 + final String l = getLookup().toString(); 46.158 + final StringBuilder b = new StringBuilder(l.length() + 1 + mt.length() + getNameLength()); 46.159 + return appendName(b).append(mt).append("@").append(l).toString(); 46.160 + } 46.161 + 46.162 + private int getNameLength() { 46.163 + final int c = getNameTokenCount(); 46.164 + int l = 0; 46.165 + for(int i = 0; i < c; ++i) { 46.166 + l += getNameToken(i).length(); 46.167 + } 46.168 + return l + c - 1; 46.169 + } 46.170 + 46.171 + private StringBuilder appendName(StringBuilder b) { 46.172 + b.append(getNameToken(0)); 46.173 + final int c = getNameTokenCount(); 46.174 + for(int i = 1; i < c; ++i) { 46.175 + b.append(':').append(getNameToken(i)); 46.176 + } 46.177 + return b; 46.178 + } 46.179 + 46.180 + private static boolean lookupsEqual(Lookup l1, Lookup l2) { 46.181 + if(l1 == l2) { 46.182 + return true; 46.183 + } 46.184 + if(l1.lookupClass() != l2.lookupClass()) { 46.185 + return false; 46.186 + } 46.187 + return l1.lookupModes() == l2.lookupModes(); 46.188 + } 46.189 +}
47.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 47.2 +++ b/src/jdk/internal/dynalink/support/AbstractRelinkableCallSite.java Thu Feb 14 13:22:26 2013 +0100 47.3 @@ -0,0 +1,118 @@ 47.4 +/* 47.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 47.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 47.7 + * 47.8 + * This code is free software; you can redistribute it and/or modify it 47.9 + * under the terms of the GNU General Public License version 2 only, as 47.10 + * published by the Free Software Foundation. Oracle designates this 47.11 + * particular file as subject to the "Classpath" exception as provided 47.12 + * by Oracle in the LICENSE file that accompanied this code. 47.13 + * 47.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 47.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 47.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 47.17 + * version 2 for more details (a copy is included in the LICENSE file that 47.18 + * accompanied this code). 47.19 + * 47.20 + * You should have received a copy of the GNU General Public License version 47.21 + * 2 along with this work; if not, write to the Free Software Foundation, 47.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 47.23 + * 47.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 47.25 + * or visit www.oracle.com if you need additional information or have any 47.26 + * questions. 47.27 + */ 47.28 + 47.29 +/* 47.30 + * This file is available under and governed by the GNU General Public 47.31 + * License version 2 only, as published by the Free Software Foundation. 47.32 + * However, the following notice accompanied the original version of this 47.33 + * file, and Oracle licenses the original version of this file under the BSD 47.34 + * license: 47.35 + */ 47.36 +/* 47.37 + Copyright 2009-2013 Attila Szegedi 47.38 + 47.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 47.40 + and the BSD License (the "BSD License"), with licensee being free to 47.41 + choose either of the two at their discretion. 47.42 + 47.43 + You may not use this file except in compliance with either the Apache 47.44 + License or the BSD License. 47.45 + 47.46 + If you choose to use this file in compliance with the Apache License, the 47.47 + following notice applies to you: 47.48 + 47.49 + You may obtain a copy of the Apache License at 47.50 + 47.51 + http://www.apache.org/licenses/LICENSE-2.0 47.52 + 47.53 + Unless required by applicable law or agreed to in writing, software 47.54 + distributed under the License is distributed on an "AS IS" BASIS, 47.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 47.56 + implied. See the License for the specific language governing 47.57 + permissions and limitations under the License. 47.58 + 47.59 + If you choose to use this file in compliance with the BSD License, the 47.60 + following notice applies to you: 47.61 + 47.62 + Redistribution and use in source and binary forms, with or without 47.63 + modification, are permitted provided that the following conditions are 47.64 + met: 47.65 + * Redistributions of source code must retain the above copyright 47.66 + notice, this list of conditions and the following disclaimer. 47.67 + * Redistributions in binary form must reproduce the above copyright 47.68 + notice, this list of conditions and the following disclaimer in the 47.69 + documentation and/or other materials provided with the distribution. 47.70 + * Neither the name of the copyright holder nor the names of 47.71 + contributors may be used to endorse or promote products derived from 47.72 + this software without specific prior written permission. 47.73 + 47.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 47.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 47.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 47.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 47.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 47.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 47.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 47.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 47.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 47.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 47.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 47.85 +*/ 47.86 + 47.87 +package jdk.internal.dynalink.support; 47.88 + 47.89 +import java.lang.invoke.MethodHandle; 47.90 +import java.lang.invoke.MutableCallSite; 47.91 +import jdk.internal.dynalink.CallSiteDescriptor; 47.92 +import jdk.internal.dynalink.RelinkableCallSite; 47.93 + 47.94 + 47.95 +/** 47.96 + * A basic implementation of the {@link RelinkableCallSite} as a {@link MutableCallSite} subclass. 47.97 + * 47.98 + * @author Attila Szegedi 47.99 + */ 47.100 +public abstract class AbstractRelinkableCallSite extends MutableCallSite implements RelinkableCallSite { 47.101 + private final CallSiteDescriptor descriptor; 47.102 + 47.103 + /** 47.104 + * Creates a new relinkable call site. 47.105 + * @param descriptor the descriptor for this call site 47.106 + */ 47.107 + protected AbstractRelinkableCallSite(CallSiteDescriptor descriptor) { 47.108 + super(descriptor.getMethodType()); 47.109 + this.descriptor = descriptor; 47.110 + } 47.111 + 47.112 + @Override 47.113 + public CallSiteDescriptor getDescriptor() { 47.114 + return descriptor; 47.115 + } 47.116 + 47.117 + @Override 47.118 + public void initialize(MethodHandle relinkAndInvoke) { 47.119 + setTarget(relinkAndInvoke); 47.120 + } 47.121 +} 47.122 \ No newline at end of file
48.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 48.2 +++ b/src/jdk/internal/dynalink/support/AutoDiscovery.java Thu Feb 14 13:22:26 2013 +0100 48.3 @@ -0,0 +1,134 @@ 48.4 +/* 48.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 48.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 48.7 + * 48.8 + * This code is free software; you can redistribute it and/or modify it 48.9 + * under the terms of the GNU General Public License version 2 only, as 48.10 + * published by the Free Software Foundation. Oracle designates this 48.11 + * particular file as subject to the "Classpath" exception as provided 48.12 + * by Oracle in the LICENSE file that accompanied this code. 48.13 + * 48.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 48.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 48.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 48.17 + * version 2 for more details (a copy is included in the LICENSE file that 48.18 + * accompanied this code). 48.19 + * 48.20 + * You should have received a copy of the GNU General Public License version 48.21 + * 2 along with this work; if not, write to the Free Software Foundation, 48.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 48.23 + * 48.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 48.25 + * or visit www.oracle.com if you need additional information or have any 48.26 + * questions. 48.27 + */ 48.28 + 48.29 +/* 48.30 + * This file is available under and governed by the GNU General Public 48.31 + * License version 2 only, as published by the Free Software Foundation. 48.32 + * However, the following notice accompanied the original version of this 48.33 + * file, and Oracle licenses the original version of this file under the BSD 48.34 + * license: 48.35 + */ 48.36 +/* 48.37 + Copyright 2009-2013 Attila Szegedi 48.38 + 48.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 48.40 + and the BSD License (the "BSD License"), with licensee being free to 48.41 + choose either of the two at their discretion. 48.42 + 48.43 + You may not use this file except in compliance with either the Apache 48.44 + License or the BSD License. 48.45 + 48.46 + If you choose to use this file in compliance with the Apache License, the 48.47 + following notice applies to you: 48.48 + 48.49 + You may obtain a copy of the Apache License at 48.50 + 48.51 + http://www.apache.org/licenses/LICENSE-2.0 48.52 + 48.53 + Unless required by applicable law or agreed to in writing, software 48.54 + distributed under the License is distributed on an "AS IS" BASIS, 48.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 48.56 + implied. See the License for the specific language governing 48.57 + permissions and limitations under the License. 48.58 + 48.59 + If you choose to use this file in compliance with the BSD License, the 48.60 + following notice applies to you: 48.61 + 48.62 + Redistribution and use in source and binary forms, with or without 48.63 + modification, are permitted provided that the following conditions are 48.64 + met: 48.65 + * Redistributions of source code must retain the above copyright 48.66 + notice, this list of conditions and the following disclaimer. 48.67 + * Redistributions in binary form must reproduce the above copyright 48.68 + notice, this list of conditions and the following disclaimer in the 48.69 + documentation and/or other materials provided with the distribution. 48.70 + * Neither the name of the copyright holder nor the names of 48.71 + contributors may be used to endorse or promote products derived from 48.72 + this software without specific prior written permission. 48.73 + 48.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 48.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 48.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 48.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 48.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 48.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 48.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 48.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 48.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 48.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 48.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48.85 +*/ 48.86 + 48.87 +package jdk.internal.dynalink.support; 48.88 + 48.89 +import java.util.LinkedList; 48.90 +import java.util.List; 48.91 +import java.util.ServiceLoader; 48.92 +import jdk.internal.dynalink.DynamicLinkerFactory; 48.93 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 48.94 + 48.95 + 48.96 +/** 48.97 + * Provides methods for automatic discovery of all guarding dynamic linkers listed in the 48.98 + * <tt>/META-INF/services/jdk.internal.dynalink.linker.GuardingDynamicLinker</tt> resources of all JAR files for a 48.99 + * particular class loader. Ordinarily, you will not use this class directly, but you will use a 48.100 + * {@link DynamicLinkerFactory} instead. 48.101 + */ 48.102 +public class AutoDiscovery { 48.103 + 48.104 + private AutoDiscovery() { 48.105 + } 48.106 + 48.107 + /** 48.108 + * Discovers all guarding dynamic linkers listed in JAR files of the context class loader of the current thread. 48.109 + * 48.110 + * @return a list of available linkers. Can be zero-length list but not null. 48.111 + */ 48.112 + public static List<GuardingDynamicLinker> loadLinkers() { 48.113 + return getLinkers(ServiceLoader.load(GuardingDynamicLinker.class)); 48.114 + } 48.115 + 48.116 + /** 48.117 + * Discovers all guarding dynamic linkers listed in JAR files of the specified class loader. 48.118 + * 48.119 + * @param cl the class loader to use 48.120 + * @return a list of guarding dynamic linkers available through the specified class loader. Can be zero-length list 48.121 + * but not null. 48.122 + */ 48.123 + public static List<GuardingDynamicLinker> loadLinkers(ClassLoader cl) { 48.124 + return getLinkers(ServiceLoader.load(GuardingDynamicLinker.class, cl)); 48.125 + } 48.126 + 48.127 + /** 48.128 + * I can't believe there's no Collections API for making a List given an Iterator... 48.129 + */ 48.130 + private static <T> List<T> getLinkers(ServiceLoader<T> loader) { 48.131 + final List<T> list = new LinkedList<>(); 48.132 + for(final T linker: loader) { 48.133 + list.add(linker); 48.134 + } 48.135 + return list; 48.136 + } 48.137 +} 48.138 \ No newline at end of file
49.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 49.2 +++ b/src/jdk/internal/dynalink/support/Backport.java Thu Feb 14 13:22:26 2013 +0100 49.3 @@ -0,0 +1,99 @@ 49.4 +/* 49.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 49.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 49.7 + * 49.8 + * This code is free software; you can redistribute it and/or modify it 49.9 + * under the terms of the GNU General Public License version 2 only, as 49.10 + * published by the Free Software Foundation. Oracle designates this 49.11 + * particular file as subject to the "Classpath" exception as provided 49.12 + * by Oracle in the LICENSE file that accompanied this code. 49.13 + * 49.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 49.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 49.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 49.17 + * version 2 for more details (a copy is included in the LICENSE file that 49.18 + * accompanied this code). 49.19 + * 49.20 + * You should have received a copy of the GNU General Public License version 49.21 + * 2 along with this work; if not, write to the Free Software Foundation, 49.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 49.23 + * 49.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 49.25 + * or visit www.oracle.com if you need additional information or have any 49.26 + * questions. 49.27 + */ 49.28 + 49.29 +/* 49.30 + * This file is available under and governed by the GNU General Public 49.31 + * License version 2 only, as published by the Free Software Foundation. 49.32 + * However, the following notice accompanied the original version of this 49.33 + * file, and Oracle licenses the original version of this file under the BSD 49.34 + * license: 49.35 + */ 49.36 +/* 49.37 + Copyright 2009-2013 Attila Szegedi 49.38 + 49.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 49.40 + and the BSD License (the "BSD License"), with licensee being free to 49.41 + choose either of the two at their discretion. 49.42 + 49.43 + You may not use this file except in compliance with either the Apache 49.44 + License or the BSD License. 49.45 + 49.46 + If you choose to use this file in compliance with the Apache License, the 49.47 + following notice applies to you: 49.48 + 49.49 + You may obtain a copy of the Apache License at 49.50 + 49.51 + http://www.apache.org/licenses/LICENSE-2.0 49.52 + 49.53 + Unless required by applicable law or agreed to in writing, software 49.54 + distributed under the License is distributed on an "AS IS" BASIS, 49.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 49.56 + implied. See the License for the specific language governing 49.57 + permissions and limitations under the License. 49.58 + 49.59 + If you choose to use this file in compliance with the BSD License, the 49.60 + following notice applies to you: 49.61 + 49.62 + Redistribution and use in source and binary forms, with or without 49.63 + modification, are permitted provided that the following conditions are 49.64 + met: 49.65 + * Redistributions of source code must retain the above copyright 49.66 + notice, this list of conditions and the following disclaimer. 49.67 + * Redistributions in binary form must reproduce the above copyright 49.68 + notice, this list of conditions and the following disclaimer in the 49.69 + documentation and/or other materials provided with the distribution. 49.70 + * Neither the name of the copyright holder nor the names of 49.71 + contributors may be used to endorse or promote products derived from 49.72 + this software without specific prior written permission. 49.73 + 49.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 49.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 49.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 49.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 49.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 49.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 49.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 49.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 49.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 49.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 49.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49.85 +*/ 49.86 + 49.87 +package jdk.internal.dynalink.support; 49.88 + 49.89 +import java.lang.invoke.MethodHandles; 49.90 + 49.91 +/** 49.92 + * @author Attila Szegedi 49.93 + */ 49.94 +public class Backport { 49.95 + /** 49.96 + * True if Remi's JSR-292 backport agent is active; false if we're using native OpenJDK JSR-292 support. 49.97 + */ 49.98 + public static final boolean inUse = MethodHandles.class.getName().startsWith("jsr292"); 49.99 + 49.100 + private Backport() { 49.101 + } 49.102 +}
50.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 50.2 +++ b/src/jdk/internal/dynalink/support/BottomGuardingDynamicLinker.java Thu Feb 14 13:22:26 2013 +0100 50.3 @@ -0,0 +1,116 @@ 50.4 +/* 50.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 50.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 50.7 + * 50.8 + * This code is free software; you can redistribute it and/or modify it 50.9 + * under the terms of the GNU General Public License version 2 only, as 50.10 + * published by the Free Software Foundation. Oracle designates this 50.11 + * particular file as subject to the "Classpath" exception as provided 50.12 + * by Oracle in the LICENSE file that accompanied this code. 50.13 + * 50.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 50.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 50.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 50.17 + * version 2 for more details (a copy is included in the LICENSE file that 50.18 + * accompanied this code). 50.19 + * 50.20 + * You should have received a copy of the GNU General Public License version 50.21 + * 2 along with this work; if not, write to the Free Software Foundation, 50.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 50.23 + * 50.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 50.25 + * or visit www.oracle.com if you need additional information or have any 50.26 + * questions. 50.27 + */ 50.28 + 50.29 +/* 50.30 + * This file is available under and governed by the GNU General Public 50.31 + * License version 2 only, as published by the Free Software Foundation. 50.32 + * However, the following notice accompanied the original version of this 50.33 + * file, and Oracle licenses the original version of this file under the BSD 50.34 + * license: 50.35 + */ 50.36 +/* 50.37 + Copyright 2009-2013 Attila Szegedi 50.38 + 50.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 50.40 + and the BSD License (the "BSD License"), with licensee being free to 50.41 + choose either of the two at their discretion. 50.42 + 50.43 + You may not use this file except in compliance with either the Apache 50.44 + License or the BSD License. 50.45 + 50.46 + If you choose to use this file in compliance with the Apache License, the 50.47 + following notice applies to you: 50.48 + 50.49 + You may obtain a copy of the Apache License at 50.50 + 50.51 + http://www.apache.org/licenses/LICENSE-2.0 50.52 + 50.53 + Unless required by applicable law or agreed to in writing, software 50.54 + distributed under the License is distributed on an "AS IS" BASIS, 50.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 50.56 + implied. See the License for the specific language governing 50.57 + permissions and limitations under the License. 50.58 + 50.59 + If you choose to use this file in compliance with the BSD License, the 50.60 + following notice applies to you: 50.61 + 50.62 + Redistribution and use in source and binary forms, with or without 50.63 + modification, are permitted provided that the following conditions are 50.64 + met: 50.65 + * Redistributions of source code must retain the above copyright 50.66 + notice, this list of conditions and the following disclaimer. 50.67 + * Redistributions in binary form must reproduce the above copyright 50.68 + notice, this list of conditions and the following disclaimer in the 50.69 + documentation and/or other materials provided with the distribution. 50.70 + * Neither the name of the copyright holder nor the names of 50.71 + contributors may be used to endorse or promote products derived from 50.72 + this software without specific prior written permission. 50.73 + 50.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 50.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 50.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 50.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 50.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 50.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 50.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 50.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 50.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 50.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 50.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50.85 +*/ 50.86 + 50.87 +package jdk.internal.dynalink.support; 50.88 + 50.89 +import jdk.internal.dynalink.linker.GuardedInvocation; 50.90 +import jdk.internal.dynalink.linker.LinkRequest; 50.91 +import jdk.internal.dynalink.linker.LinkerServices; 50.92 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 50.93 + 50.94 +/** 50.95 + * A linker that can't link any call site. Only used internally by {@link CompositeTypeBasedGuardingDynamicLinker}. Can 50.96 + * be used by other language runtimes if they need it though. 50.97 + * 50.98 + * @author Attila Szegedi 50.99 + */ 50.100 +public class BottomGuardingDynamicLinker implements TypeBasedGuardingDynamicLinker { 50.101 + 50.102 + /** 50.103 + * The sole instance of this stateless linker. 50.104 + */ 50.105 + public static final BottomGuardingDynamicLinker INSTANCE = new BottomGuardingDynamicLinker(); 50.106 + 50.107 + private BottomGuardingDynamicLinker() { 50.108 + } 50.109 + 50.110 + @Override 50.111 + public boolean canLinkType(Class<?> type) { 50.112 + return false; 50.113 + } 50.114 + 50.115 + @Override 50.116 + public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) { 50.117 + return null; 50.118 + } 50.119 +} 50.120 \ No newline at end of file
51.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 51.2 +++ b/src/jdk/internal/dynalink/support/CallSiteDescriptorFactory.java Thu Feb 14 13:22:26 2013 +0100 51.3 @@ -0,0 +1,265 @@ 51.4 +/* 51.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 51.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 51.7 + * 51.8 + * This code is free software; you can redistribute it and/or modify it 51.9 + * under the terms of the GNU General Public License version 2 only, as 51.10 + * published by the Free Software Foundation. Oracle designates this 51.11 + * particular file as subject to the "Classpath" exception as provided 51.12 + * by Oracle in the LICENSE file that accompanied this code. 51.13 + * 51.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 51.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 51.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 51.17 + * version 2 for more details (a copy is included in the LICENSE file that 51.18 + * accompanied this code). 51.19 + * 51.20 + * You should have received a copy of the GNU General Public License version 51.21 + * 2 along with this work; if not, write to the Free Software Foundation, 51.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 51.23 + * 51.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 51.25 + * or visit www.oracle.com if you need additional information or have any 51.26 + * questions. 51.27 + */ 51.28 + 51.29 +/* 51.30 + * This file is available under and governed by the GNU General Public 51.31 + * License version 2 only, as published by the Free Software Foundation. 51.32 + * However, the following notice accompanied the original version of this 51.33 + * file, and Oracle licenses the original version of this file under the BSD 51.34 + * license: 51.35 + */ 51.36 +/* 51.37 + Copyright 2009-2013 Attila Szegedi 51.38 + 51.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 51.40 + and the BSD License (the "BSD License"), with licensee being free to 51.41 + choose either of the two at their discretion. 51.42 + 51.43 + You may not use this file except in compliance with either the Apache 51.44 + License or the BSD License. 51.45 + 51.46 + If you choose to use this file in compliance with the Apache License, the 51.47 + following notice applies to you: 51.48 + 51.49 + You may obtain a copy of the Apache License at 51.50 + 51.51 + http://www.apache.org/licenses/LICENSE-2.0 51.52 + 51.53 + Unless required by applicable law or agreed to in writing, software 51.54 + distributed under the License is distributed on an "AS IS" BASIS, 51.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 51.56 + implied. See the License for the specific language governing 51.57 + permissions and limitations under the License. 51.58 + 51.59 + If you choose to use this file in compliance with the BSD License, the 51.60 + following notice applies to you: 51.61 + 51.62 + Redistribution and use in source and binary forms, with or without 51.63 + modification, are permitted provided that the following conditions are 51.64 + met: 51.65 + * Redistributions of source code must retain the above copyright 51.66 + notice, this list of conditions and the following disclaimer. 51.67 + * Redistributions in binary form must reproduce the above copyright 51.68 + notice, this list of conditions and the following disclaimer in the 51.69 + documentation and/or other materials provided with the distribution. 51.70 + * Neither the name of the copyright holder nor the names of 51.71 + contributors may be used to endorse or promote products derived from 51.72 + this software without specific prior written permission. 51.73 + 51.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 51.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 51.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 51.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 51.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 51.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 51.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 51.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 51.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 51.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 51.85 +*/ 51.86 + 51.87 +package jdk.internal.dynalink.support; 51.88 + 51.89 +import java.lang.invoke.MethodHandles; 51.90 +import java.lang.invoke.MethodHandles.Lookup; 51.91 +import java.lang.invoke.MethodType; 51.92 +import java.lang.ref.WeakReference; 51.93 +import java.util.Arrays; 51.94 +import java.util.Collections; 51.95 +import java.util.List; 51.96 +import java.util.StringTokenizer; 51.97 +import java.util.WeakHashMap; 51.98 +import jdk.internal.dynalink.CallSiteDescriptor; 51.99 + 51.100 + 51.101 +/** 51.102 + * Usable as a default factory for call site descriptor implementations. It is weakly canonicalizing, meaning it will 51.103 + * return the same immutable call site descriptor for identical inputs, i.e. repeated requests for a descriptor 51.104 + * signifying public lookup for "dyn:getProp:color" of type "Object(Object)" will return the same object as long as 51.105 + * a previously created, at least softly reachable one exists. It also uses several different implementations of the 51.106 + * {@link CallSiteDescriptor} internally, and chooses the most space-efficient one based on the input. 51.107 + * @author Attila Szegedi 51.108 + */ 51.109 +public class CallSiteDescriptorFactory { 51.110 + private static final WeakHashMap<CallSiteDescriptor, WeakReference<CallSiteDescriptor>> publicDescs = 51.111 + new WeakHashMap<>(); 51.112 + 51.113 + 51.114 + private CallSiteDescriptorFactory() { 51.115 + } 51.116 + 51.117 + /** 51.118 + * Creates a new call site descriptor instance. The actual underlying class of the instance is dependent on the 51.119 + * passed arguments to be space efficient; i.e. if you only use the public lookup, you'll get back an 51.120 + * implementation that doesn't waste space on storing the lookup object. 51.121 + * @param lookup the lookup that determines access rights at the call site. If your language runtime doesn't have 51.122 + * equivalents of Java access concepts, just use {@link MethodHandles#publicLookup()}. Must not be null. 51.123 + * @param name the name of the method at the call site. Must not be null. 51.124 + * @param methodType the type of the method at the call site. Must not be null. 51.125 + * @return a call site descriptor representing the input. Note that although the method name is "create", it will 51.126 + * in fact return a weakly-referenced canonical instance. 51.127 + */ 51.128 + public static CallSiteDescriptor create(Lookup lookup, String name, MethodType methodType) { 51.129 + name.getClass(); // NPE check 51.130 + methodType.getClass(); // NPE check 51.131 + lookup.getClass(); // NPE check 51.132 + final String[] tokenizedName = tokenizeName(name); 51.133 + if(isPublicLookup(lookup)) { 51.134 + return getCanonicalPublicDescriptor(createPublicCallSiteDescriptor(tokenizedName, methodType)); 51.135 + } else { 51.136 + return new LookupCallSiteDescriptor(tokenizedName, methodType, lookup); 51.137 + } 51.138 + } 51.139 + 51.140 + static CallSiteDescriptor getCanonicalPublicDescriptor(final CallSiteDescriptor desc) { 51.141 + synchronized(publicDescs) { 51.142 + final WeakReference<CallSiteDescriptor> ref = publicDescs.get(desc); 51.143 + if(ref != null) { 51.144 + final CallSiteDescriptor canonical = ref.get(); 51.145 + if(canonical != null) { 51.146 + return canonical; 51.147 + } 51.148 + } 51.149 + publicDescs.put(desc, new WeakReference<>(desc)); 51.150 + } 51.151 + return desc; 51.152 + } 51.153 + 51.154 + private static CallSiteDescriptor createPublicCallSiteDescriptor(String[] tokenizedName, MethodType methodType) { 51.155 + final int l = tokenizedName.length; 51.156 + if(l > 0 && tokenizedName[0] == "dyn") { 51.157 + if(l == 2) { 51.158 + return new UnnamedDynCallSiteDescriptor(tokenizedName[1], methodType); 51.159 + } if (l == 3) { 51.160 + return new NamedDynCallSiteDescriptor(tokenizedName[1], tokenizedName[2], methodType); 51.161 + } 51.162 + } 51.163 + return new DefaultCallSiteDescriptor(tokenizedName, methodType); 51.164 + } 51.165 + 51.166 + private static boolean isPublicLookup(Lookup lookup) { 51.167 + return lookup == MethodHandles.publicLookup(); 51.168 + } 51.169 + 51.170 + /** 51.171 + * Tokenizes the composite name along colons, as well as {@link NameCodec#decode(String) demangles} and interns 51.172 + * the tokens. The first two tokens are not demangled as they are supposed to be the naming scheme and the name of 51.173 + * the operation which can be expected to consist of just alphabetical characters. 51.174 + * @param name the composite name consisting of colon-separated, possibly mangled tokens. 51.175 + * @return an array of tokens 51.176 + */ 51.177 + public static String[] tokenizeName(String name) { 51.178 + final StringTokenizer tok = new StringTokenizer(name, CallSiteDescriptor.TOKEN_DELIMITER); 51.179 + final String[] tokens = new String[tok.countTokens()]; 51.180 + for(int i = 0; i < tokens.length; ++i) { 51.181 + String token = tok.nextToken(); 51.182 + if(i > 1) { 51.183 + token = NameCodec.decode(token); 51.184 + } 51.185 + tokens[i] = token.intern(); 51.186 + } 51.187 + return tokens; 51.188 + } 51.189 + 51.190 + /** 51.191 + * Tokenizes a composite operation name along pipe characters. I.e. if you have a "dyn:getElem|getProp|getMethod" 51.192 + * operation, returns a list of ["getElem", "getProp", "getMethod"]. The tokens are not interned. 51.193 + * @param desc the call site descriptor with the operation 51.194 + * @return a list of tokens 51.195 + */ 51.196 + public static List<String> tokenizeOperators(CallSiteDescriptor desc) { 51.197 + final String ops = desc.getNameToken(CallSiteDescriptor.OPERATOR); 51.198 + final StringTokenizer tok = new StringTokenizer(ops, CallSiteDescriptor.OPERATOR_DELIMITER); 51.199 + final int count = tok.countTokens(); 51.200 + if(count == 1) { 51.201 + return Collections.singletonList(ops); 51.202 + } 51.203 + final String[] tokens = new String[count]; 51.204 + for(int i = 0; i < count; ++i) { 51.205 + tokens[i] = tok.nextToken(); 51.206 + } 51.207 + return Arrays.asList(tokens); 51.208 + } 51.209 + 51.210 + /** 51.211 + * Returns a new call site descriptor that is identical to the passed one, except that it has some parameter types 51.212 + * removed from its method type. 51.213 + * @param desc the original call site descriptor 51.214 + * @param start index of the first parameter to remove 51.215 + * @param end index of the first parameter to not remove 51.216 + * @return a new call site descriptor with modified method type 51.217 + */ 51.218 + public static CallSiteDescriptor dropParameterTypes(CallSiteDescriptor desc, int start, int end) { 51.219 + return desc.changeMethodType(desc.getMethodType().dropParameterTypes(start, end)); 51.220 + } 51.221 + 51.222 + /** 51.223 + * Returns a new call site descriptor that is identical to the passed one, except that it has a single parameter 51.224 + * type changed in its method type. 51.225 + * @param desc the original call site descriptor 51.226 + * @param num index of the parameter to change 51.227 + * @param nptype the new parameter type 51.228 + * @return a new call site descriptor with modified method type 51.229 + */ 51.230 + public static CallSiteDescriptor changeParameterType(CallSiteDescriptor desc, int num, Class<?> nptype) { 51.231 + return desc.changeMethodType(desc.getMethodType().changeParameterType(num, nptype)); 51.232 + } 51.233 + 51.234 + /** 51.235 + * Returns a new call site descriptor that is identical to the passed one, except that it has the return type 51.236 + * changed in its method type. 51.237 + * @param desc the original call site descriptor 51.238 + * @param nrtype the new return type 51.239 + * @return a new call site descriptor with modified method type 51.240 + */ 51.241 + public static CallSiteDescriptor changeReturnType(CallSiteDescriptor desc, Class<?> nrtype) { 51.242 + return desc.changeMethodType(desc.getMethodType().changeReturnType(nrtype)); 51.243 + } 51.244 + 51.245 + /** 51.246 + * Returns a new call site descriptor that is identical to the passed one, except that it has additional parameter 51.247 + * types inserted into its method type. 51.248 + * @param desc the original call site descriptor 51.249 + * @param num index at which the new parameters are inserted 51.250 + * @param ptypesToInsert the new types to insert 51.251 + * @return a new call site descriptor with modified method type 51.252 + */ 51.253 + public static CallSiteDescriptor insertParameterTypes(CallSiteDescriptor desc, int num, Class<?>... ptypesToInsert) { 51.254 + return desc.changeMethodType(desc.getMethodType().insertParameterTypes(num, ptypesToInsert)); 51.255 + } 51.256 + 51.257 + /** 51.258 + * Returns a new call site descriptor that is identical to the passed one, except that it has additional parameter 51.259 + * types inserted into its method type. 51.260 + * @param desc the original call site descriptor 51.261 + * @param num index at which the new parameters are inserted 51.262 + * @param ptypesToInsert the new types to insert 51.263 + * @return a new call site descriptor with modified method type 51.264 + */ 51.265 + public static CallSiteDescriptor insertParameterTypes(CallSiteDescriptor desc, int num, List<Class<?>> ptypesToInsert) { 51.266 + return desc.changeMethodType(desc.getMethodType().insertParameterTypes(num, ptypesToInsert)); 51.267 + } 51.268 +} 51.269 \ No newline at end of file
52.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 52.2 +++ b/src/jdk/internal/dynalink/support/ClassMap.java Thu Feb 14 13:22:26 2013 +0100 52.3 @@ -0,0 +1,177 @@ 52.4 +/* 52.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 52.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 52.7 + * 52.8 + * This code is free software; you can redistribute it and/or modify it 52.9 + * under the terms of the GNU General Public License version 2 only, as 52.10 + * published by the Free Software Foundation. Oracle designates this 52.11 + * particular file as subject to the "Classpath" exception as provided 52.12 + * by Oracle in the LICENSE file that accompanied this code. 52.13 + * 52.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 52.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 52.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 52.17 + * version 2 for more details (a copy is included in the LICENSE file that 52.18 + * accompanied this code). 52.19 + * 52.20 + * You should have received a copy of the GNU General Public License version 52.21 + * 2 along with this work; if not, write to the Free Software Foundation, 52.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 52.23 + * 52.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 52.25 + * or visit www.oracle.com if you need additional information or have any 52.26 + * questions. 52.27 + */ 52.28 + 52.29 +/* 52.30 + * This file is available under and governed by the GNU General Public 52.31 + * License version 2 only, as published by the Free Software Foundation. 52.32 + * However, the following notice accompanied the original version of this 52.33 + * file, and Oracle licenses the original version of this file under the BSD 52.34 + * license: 52.35 + */ 52.36 +/* 52.37 + Copyright 2009-2013 Attila Szegedi 52.38 + 52.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 52.40 + and the BSD License (the "BSD License"), with licensee being free to 52.41 + choose either of the two at their discretion. 52.42 + 52.43 + You may not use this file except in compliance with either the Apache 52.44 + License or the BSD License. 52.45 + 52.46 + If you choose to use this file in compliance with the Apache License, the 52.47 + following notice applies to you: 52.48 + 52.49 + You may obtain a copy of the Apache License at 52.50 + 52.51 + http://www.apache.org/licenses/LICENSE-2.0 52.52 + 52.53 + Unless required by applicable law or agreed to in writing, software 52.54 + distributed under the License is distributed on an "AS IS" BASIS, 52.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 52.56 + implied. See the License for the specific language governing 52.57 + permissions and limitations under the License. 52.58 + 52.59 + If you choose to use this file in compliance with the BSD License, the 52.60 + following notice applies to you: 52.61 + 52.62 + Redistribution and use in source and binary forms, with or without 52.63 + modification, are permitted provided that the following conditions are 52.64 + met: 52.65 + * Redistributions of source code must retain the above copyright 52.66 + notice, this list of conditions and the following disclaimer. 52.67 + * Redistributions in binary form must reproduce the above copyright 52.68 + notice, this list of conditions and the following disclaimer in the 52.69 + documentation and/or other materials provided with the distribution. 52.70 + * Neither the name of the copyright holder nor the names of 52.71 + contributors may be used to endorse or promote products derived from 52.72 + this software without specific prior written permission. 52.73 + 52.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 52.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 52.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 52.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 52.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 52.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 52.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 52.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 52.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 52.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 52.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 52.85 +*/ 52.86 + 52.87 +package jdk.internal.dynalink.support; 52.88 + 52.89 +import java.lang.ref.Reference; 52.90 +import java.lang.ref.SoftReference; 52.91 +import java.util.Map; 52.92 +import java.util.WeakHashMap; 52.93 +import java.util.concurrent.ConcurrentHashMap; 52.94 +import java.util.concurrent.ConcurrentMap; 52.95 + 52.96 +/** 52.97 + * A dual map that can either strongly or weakly reference a given class depending on whether the class is visible from 52.98 + * a class loader or not. 52.99 + * 52.100 + * @author Attila Szegedi 52.101 + * @param <T> the type of the values in the map 52.102 + */ 52.103 +public abstract class ClassMap<T> { 52.104 + private final ConcurrentMap<Class<?>, T> map = new ConcurrentHashMap<>(); 52.105 + private final Map<Class<?>, Reference<T>> weakMap = new WeakHashMap<>(); 52.106 + private final ClassLoader classLoader; 52.107 + 52.108 + /** 52.109 + * Creates a new class map. It will use strong references for all keys and values where the key is a class visible 52.110 + * from the class loader, and will use weak keys and soft values for all other classes. 52.111 + * 52.112 + * @param classLoader the classloader that determines strong referenceability. 52.113 + */ 52.114 + protected ClassMap(ClassLoader classLoader) { 52.115 + this.classLoader = classLoader; 52.116 + } 52.117 + 52.118 + /** 52.119 + * Compute the value associated with the given class. It is possible that the method will be invoked several times 52.120 + * (or even concurrently) for the same class parameter. 52.121 + * 52.122 + * @param clazz the class to compute the value for 52.123 + * @return the return value. Must not be null. 52.124 + */ 52.125 + protected abstract T computeValue(Class<?> clazz); 52.126 + 52.127 + /** 52.128 + * Returns the class loader that governs the strong referenceability of this class map. 52.129 + * 52.130 + * @return the class loader that governs the strong referenceability of this class map. 52.131 + */ 52.132 + public ClassLoader getClassLoader() { 52.133 + return classLoader; 52.134 + } 52.135 + 52.136 + /** 52.137 + * Returns the value associated with the class 52.138 + * 52.139 + * @param clazz the class 52.140 + * @return the value associated with the class 52.141 + */ 52.142 + public T get(Class<?> clazz) { 52.143 + // Check in fastest first - objects we're allowed to strongly reference 52.144 + final T v = map.get(clazz); 52.145 + if(v != null) { 52.146 + return v; 52.147 + } 52.148 + // Check objects we're not allowed to strongly reference 52.149 + Reference<T> ref; 52.150 + synchronized(weakMap) { 52.151 + ref = weakMap.get(clazz); 52.152 + } 52.153 + if(ref != null) { 52.154 + final T refv = ref.get(); 52.155 + if(refv != null) { 52.156 + return refv; 52.157 + } 52.158 + } 52.159 + // Not found in either place; create a new value 52.160 + final T newV = computeValue(clazz); 52.161 + assert newV != null; 52.162 + // If allowed to strongly reference, put it in the fast map 52.163 + if(Guards.canReferenceDirectly(classLoader, clazz.getClassLoader())) { 52.164 + final T oldV = map.putIfAbsent(clazz, newV); 52.165 + return oldV != null ? oldV : newV; 52.166 + } 52.167 + // Otherwise, put it into the weak map 52.168 + synchronized(weakMap) { 52.169 + ref = weakMap.get(clazz); 52.170 + if(ref != null) { 52.171 + final T oldV = ref.get(); 52.172 + if(oldV != null) { 52.173 + return oldV; 52.174 + } 52.175 + } 52.176 + weakMap.put(clazz, new SoftReference<>(newV)); 52.177 + return newV; 52.178 + } 52.179 + } 52.180 +}
53.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 53.2 +++ b/src/jdk/internal/dynalink/support/CompositeGuardingDynamicLinker.java Thu Feb 14 13:22:26 2013 +0100 53.3 @@ -0,0 +1,132 @@ 53.4 +/* 53.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 53.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 53.7 + * 53.8 + * This code is free software; you can redistribute it and/or modify it 53.9 + * under the terms of the GNU General Public License version 2 only, as 53.10 + * published by the Free Software Foundation. Oracle designates this 53.11 + * particular file as subject to the "Classpath" exception as provided 53.12 + * by Oracle in the LICENSE file that accompanied this code. 53.13 + * 53.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 53.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 53.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 53.17 + * version 2 for more details (a copy is included in the LICENSE file that 53.18 + * accompanied this code). 53.19 + * 53.20 + * You should have received a copy of the GNU General Public License version 53.21 + * 2 along with this work; if not, write to the Free Software Foundation, 53.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 53.23 + * 53.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 53.25 + * or visit www.oracle.com if you need additional information or have any 53.26 + * questions. 53.27 + */ 53.28 + 53.29 +/* 53.30 + * This file is available under and governed by the GNU General Public 53.31 + * License version 2 only, as published by the Free Software Foundation. 53.32 + * However, the following notice accompanied the original version of this 53.33 + * file, and Oracle licenses the original version of this file under the BSD 53.34 + * license: 53.35 + */ 53.36 +/* 53.37 + Copyright 2009-2013 Attila Szegedi 53.38 + 53.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 53.40 + and the BSD License (the "BSD License"), with licensee being free to 53.41 + choose either of the two at their discretion. 53.42 + 53.43 + You may not use this file except in compliance with either the Apache 53.44 + License or the BSD License. 53.45 + 53.46 + If you choose to use this file in compliance with the Apache License, the 53.47 + following notice applies to you: 53.48 + 53.49 + You may obtain a copy of the Apache License at 53.50 + 53.51 + http://www.apache.org/licenses/LICENSE-2.0 53.52 + 53.53 + Unless required by applicable law or agreed to in writing, software 53.54 + distributed under the License is distributed on an "AS IS" BASIS, 53.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 53.56 + implied. See the License for the specific language governing 53.57 + permissions and limitations under the License. 53.58 + 53.59 + If you choose to use this file in compliance with the BSD License, the 53.60 + following notice applies to you: 53.61 + 53.62 + Redistribution and use in source and binary forms, with or without 53.63 + modification, are permitted provided that the following conditions are 53.64 + met: 53.65 + * Redistributions of source code must retain the above copyright 53.66 + notice, this list of conditions and the following disclaimer. 53.67 + * Redistributions in binary form must reproduce the above copyright 53.68 + notice, this list of conditions and the following disclaimer in the 53.69 + documentation and/or other materials provided with the distribution. 53.70 + * Neither the name of the copyright holder nor the names of 53.71 + contributors may be used to endorse or promote products derived from 53.72 + this software without specific prior written permission. 53.73 + 53.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 53.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 53.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 53.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 53.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 53.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 53.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 53.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 53.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 53.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 53.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 53.85 +*/ 53.86 + 53.87 +package jdk.internal.dynalink.support; 53.88 + 53.89 +import java.io.Serializable; 53.90 +import java.util.LinkedList; 53.91 +import java.util.List; 53.92 +import jdk.internal.dynalink.linker.GuardedInvocation; 53.93 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 53.94 +import jdk.internal.dynalink.linker.LinkRequest; 53.95 +import jdk.internal.dynalink.linker.LinkerServices; 53.96 + 53.97 + 53.98 +/** 53.99 + * A {@link GuardingDynamicLinker} that delegates sequentially to a list of other guarding dynamic linkers. The first 53.100 + * value returned from a component linker other than null is returned. If no component linker returns an invocation, 53.101 + * null is returned. 53.102 + * 53.103 + * @author Attila Szegedi 53.104 + */ 53.105 +public class CompositeGuardingDynamicLinker implements GuardingDynamicLinker, Serializable { 53.106 + 53.107 + private static final long serialVersionUID = 1L; 53.108 + 53.109 + private final GuardingDynamicLinker[] linkers; 53.110 + 53.111 + /** 53.112 + * Creates a new composite linker. 53.113 + * 53.114 + * @param linkers a list of component linkers. 53.115 + */ 53.116 + public CompositeGuardingDynamicLinker(Iterable<? extends GuardingDynamicLinker> linkers) { 53.117 + final List<GuardingDynamicLinker> l = new LinkedList<>(); 53.118 + for(GuardingDynamicLinker linker: linkers) { 53.119 + l.add(linker); 53.120 + } 53.121 + this.linkers = l.toArray(new GuardingDynamicLinker[l.size()]); 53.122 + } 53.123 + 53.124 + @Override 53.125 + public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, final LinkerServices linkerServices) 53.126 + throws Exception { 53.127 + for(final GuardingDynamicLinker linker: linkers) { 53.128 + final GuardedInvocation invocation = linker.getGuardedInvocation(linkRequest, linkerServices); 53.129 + if(invocation != null) { 53.130 + return invocation; 53.131 + } 53.132 + } 53.133 + return null; 53.134 + } 53.135 +} 53.136 \ No newline at end of file
54.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 54.2 +++ b/src/jdk/internal/dynalink/support/CompositeTypeBasedGuardingDynamicLinker.java Thu Feb 14 13:22:26 2013 +0100 54.3 @@ -0,0 +1,226 @@ 54.4 +/* 54.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 54.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 54.7 + * 54.8 + * This code is free software; you can redistribute it and/or modify it 54.9 + * under the terms of the GNU General Public License version 2 only, as 54.10 + * published by the Free Software Foundation. Oracle designates this 54.11 + * particular file as subject to the "Classpath" exception as provided 54.12 + * by Oracle in the LICENSE file that accompanied this code. 54.13 + * 54.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 54.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 54.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 54.17 + * version 2 for more details (a copy is included in the LICENSE file that 54.18 + * accompanied this code). 54.19 + * 54.20 + * You should have received a copy of the GNU General Public License version 54.21 + * 2 along with this work; if not, write to the Free Software Foundation, 54.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 54.23 + * 54.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 54.25 + * or visit www.oracle.com if you need additional information or have any 54.26 + * questions. 54.27 + */ 54.28 + 54.29 +/* 54.30 + * This file is available under and governed by the GNU General Public 54.31 + * License version 2 only, as published by the Free Software Foundation. 54.32 + * However, the following notice accompanied the original version of this 54.33 + * file, and Oracle licenses the original version of this file under the BSD 54.34 + * license: 54.35 + */ 54.36 +/* 54.37 + Copyright 2009-2013 Attila Szegedi 54.38 + 54.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 54.40 + and the BSD License (the "BSD License"), with licensee being free to 54.41 + choose either of the two at their discretion. 54.42 + 54.43 + You may not use this file except in compliance with either the Apache 54.44 + License or the BSD License. 54.45 + 54.46 + If you choose to use this file in compliance with the Apache License, the 54.47 + following notice applies to you: 54.48 + 54.49 + You may obtain a copy of the Apache License at 54.50 + 54.51 + http://www.apache.org/licenses/LICENSE-2.0 54.52 + 54.53 + Unless required by applicable law or agreed to in writing, software 54.54 + distributed under the License is distributed on an "AS IS" BASIS, 54.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 54.56 + implied. See the License for the specific language governing 54.57 + permissions and limitations under the License. 54.58 + 54.59 + If you choose to use this file in compliance with the BSD License, the 54.60 + following notice applies to you: 54.61 + 54.62 + Redistribution and use in source and binary forms, with or without 54.63 + modification, are permitted provided that the following conditions are 54.64 + met: 54.65 + * Redistributions of source code must retain the above copyright 54.66 + notice, this list of conditions and the following disclaimer. 54.67 + * Redistributions in binary form must reproduce the above copyright 54.68 + notice, this list of conditions and the following disclaimer in the 54.69 + documentation and/or other materials provided with the distribution. 54.70 + * Neither the name of the copyright holder nor the names of 54.71 + contributors may be used to endorse or promote products derived from 54.72 + this software without specific prior written permission. 54.73 + 54.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 54.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 54.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 54.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 54.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 54.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 54.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 54.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 54.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 54.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 54.85 +*/ 54.86 + 54.87 +package jdk.internal.dynalink.support; 54.88 + 54.89 +import java.io.Serializable; 54.90 +import java.util.Collections; 54.91 +import java.util.LinkedList; 54.92 +import java.util.List; 54.93 +import jdk.internal.dynalink.linker.GuardedInvocation; 54.94 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 54.95 +import jdk.internal.dynalink.linker.LinkRequest; 54.96 +import jdk.internal.dynalink.linker.LinkerServices; 54.97 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 54.98 + 54.99 + 54.100 +/** 54.101 + * A composite type-based guarding dynamic linker. When a receiver of a not yet seen class is encountered, all linkers 54.102 + * are queried sequentially on their {@link TypeBasedGuardingDynamicLinker#canLinkType(Class)} method. The linkers 54.103 + * returning true are then bound to the class, and next time a receiver of same type is encountered, the linking is 54.104 + * delegated to those linkers only, speeding up dispatch. 54.105 + * 54.106 + * @author Attila Szegedi 54.107 + */ 54.108 +public class CompositeTypeBasedGuardingDynamicLinker implements TypeBasedGuardingDynamicLinker, Serializable { 54.109 + private static final long serialVersionUID = 1L; 54.110 + 54.111 + // Using a separate static class instance so there's no strong reference from the class value back to the composite 54.112 + // linker. 54.113 + private static class ClassToLinker extends ClassValue<List<TypeBasedGuardingDynamicLinker>> { 54.114 + private static final List<TypeBasedGuardingDynamicLinker> NO_LINKER = Collections.emptyList(); 54.115 + private final TypeBasedGuardingDynamicLinker[] linkers; 54.116 + private final List<TypeBasedGuardingDynamicLinker>[] singletonLinkers; 54.117 + 54.118 + @SuppressWarnings("unchecked") 54.119 + ClassToLinker(TypeBasedGuardingDynamicLinker[] linkers) { 54.120 + this.linkers = linkers; 54.121 + singletonLinkers = new List[linkers.length]; 54.122 + for(int i = 0; i < linkers.length; ++i) { 54.123 + singletonLinkers[i] = Collections.singletonList(linkers[i]); 54.124 + } 54.125 + } 54.126 + 54.127 + @Override 54.128 + protected List<TypeBasedGuardingDynamicLinker> computeValue(Class<?> clazz) { 54.129 + List<TypeBasedGuardingDynamicLinker> list = NO_LINKER; 54.130 + for(int i = 0; i < linkers.length; ++i) { 54.131 + final TypeBasedGuardingDynamicLinker linker = linkers[i]; 54.132 + if(linker.canLinkType(clazz)) { 54.133 + switch(list.size()) { 54.134 + case 0: { 54.135 + list = singletonLinkers[i]; 54.136 + break; 54.137 + } 54.138 + case 1: { 54.139 + list = new LinkedList<>(list); 54.140 + } 54.141 + //$FALL-THROUGH$ 54.142 + default: { 54.143 + list.add(linker); 54.144 + } 54.145 + } 54.146 + } 54.147 + } 54.148 + return list; 54.149 + } 54.150 + } 54.151 + 54.152 + private final ClassValue<List<TypeBasedGuardingDynamicLinker>> classToLinker; 54.153 + 54.154 + /** 54.155 + * Creates a new composite type-based linker. 54.156 + * 54.157 + * @param linkers the component linkers 54.158 + */ 54.159 + public CompositeTypeBasedGuardingDynamicLinker(Iterable<? extends TypeBasedGuardingDynamicLinker> linkers) { 54.160 + final List<TypeBasedGuardingDynamicLinker> l = new LinkedList<>(); 54.161 + for(TypeBasedGuardingDynamicLinker linker: linkers) { 54.162 + l.add(linker); 54.163 + } 54.164 + this.classToLinker = new ClassToLinker(l.toArray(new TypeBasedGuardingDynamicLinker[l.size()])); 54.165 + } 54.166 + 54.167 + @Override 54.168 + public boolean canLinkType(Class<?> type) { 54.169 + return !classToLinker.get(type).isEmpty(); 54.170 + } 54.171 + 54.172 + @Override 54.173 + public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, final LinkerServices linkerServices) 54.174 + throws Exception { 54.175 + final Object obj = linkRequest.getReceiver(); 54.176 + if(obj == null) { 54.177 + return null; 54.178 + } 54.179 + for(TypeBasedGuardingDynamicLinker linker: classToLinker.get(obj.getClass())) { 54.180 + final GuardedInvocation invocation = linker.getGuardedInvocation(linkRequest, linkerServices); 54.181 + if(invocation != null) { 54.182 + return invocation; 54.183 + } 54.184 + } 54.185 + return null; 54.186 + } 54.187 + 54.188 + /** 54.189 + * Optimizes a list of type-based linkers. If a group of adjacent linkers in the list all implement 54.190 + * {@link TypeBasedGuardingDynamicLinker}, they will be replaced with a single instance of 54.191 + * {@link CompositeTypeBasedGuardingDynamicLinker} that contains them. 54.192 + * 54.193 + * @param linkers the list of linkers to optimize 54.194 + * @return the optimized list 54.195 + */ 54.196 + public static List<GuardingDynamicLinker> optimize(Iterable<? extends GuardingDynamicLinker> linkers) { 54.197 + final List<GuardingDynamicLinker> llinkers = new LinkedList<>(); 54.198 + final List<TypeBasedGuardingDynamicLinker> tblinkers = new LinkedList<>(); 54.199 + for(GuardingDynamicLinker linker: linkers) { 54.200 + if(linker instanceof TypeBasedGuardingDynamicLinker) { 54.201 + tblinkers.add((TypeBasedGuardingDynamicLinker)linker); 54.202 + } else { 54.203 + addTypeBased(llinkers, tblinkers); 54.204 + llinkers.add(linker); 54.205 + } 54.206 + } 54.207 + addTypeBased(llinkers, tblinkers); 54.208 + return llinkers; 54.209 + } 54.210 + 54.211 + private static void addTypeBased(List<GuardingDynamicLinker> llinkers, 54.212 + List<TypeBasedGuardingDynamicLinker> tblinkers) { 54.213 + switch(tblinkers.size()) { 54.214 + case 0: { 54.215 + break; 54.216 + } 54.217 + case 1: { 54.218 + llinkers.addAll(tblinkers); 54.219 + tblinkers.clear(); 54.220 + break; 54.221 + } 54.222 + default: { 54.223 + llinkers.add(new CompositeTypeBasedGuardingDynamicLinker(tblinkers)); 54.224 + tblinkers.clear(); 54.225 + break; 54.226 + } 54.227 + } 54.228 + } 54.229 +} 54.230 \ No newline at end of file
55.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 55.2 +++ b/src/jdk/internal/dynalink/support/DefaultCallSiteDescriptor.java Thu Feb 14 13:22:26 2013 +0100 55.3 @@ -0,0 +1,135 @@ 55.4 +/* 55.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 55.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 55.7 + * 55.8 + * This code is free software; you can redistribute it and/or modify it 55.9 + * under the terms of the GNU General Public License version 2 only, as 55.10 + * published by the Free Software Foundation. Oracle designates this 55.11 + * particular file as subject to the "Classpath" exception as provided 55.12 + * by Oracle in the LICENSE file that accompanied this code. 55.13 + * 55.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 55.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 55.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 55.17 + * version 2 for more details (a copy is included in the LICENSE file that 55.18 + * accompanied this code). 55.19 + * 55.20 + * You should have received a copy of the GNU General Public License version 55.21 + * 2 along with this work; if not, write to the Free Software Foundation, 55.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 55.23 + * 55.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 55.25 + * or visit www.oracle.com if you need additional information or have any 55.26 + * questions. 55.27 + */ 55.28 + 55.29 +/* 55.30 + * This file is available under and governed by the GNU General Public 55.31 + * License version 2 only, as published by the Free Software Foundation. 55.32 + * However, the following notice accompanied the original version of this 55.33 + * file, and Oracle licenses the original version of this file under the BSD 55.34 + * license: 55.35 + */ 55.36 +/* 55.37 + Copyright 2009-2013 Attila Szegedi 55.38 + 55.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 55.40 + and the BSD License (the "BSD License"), with licensee being free to 55.41 + choose either of the two at their discretion. 55.42 + 55.43 + You may not use this file except in compliance with either the Apache 55.44 + License or the BSD License. 55.45 + 55.46 + If you choose to use this file in compliance with the Apache License, the 55.47 + following notice applies to you: 55.48 + 55.49 + You may obtain a copy of the Apache License at 55.50 + 55.51 + http://www.apache.org/licenses/LICENSE-2.0 55.52 + 55.53 + Unless required by applicable law or agreed to in writing, software 55.54 + distributed under the License is distributed on an "AS IS" BASIS, 55.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 55.56 + implied. See the License for the specific language governing 55.57 + permissions and limitations under the License. 55.58 + 55.59 + If you choose to use this file in compliance with the BSD License, the 55.60 + following notice applies to you: 55.61 + 55.62 + Redistribution and use in source and binary forms, with or without 55.63 + modification, are permitted provided that the following conditions are 55.64 + met: 55.65 + * Redistributions of source code must retain the above copyright 55.66 + notice, this list of conditions and the following disclaimer. 55.67 + * Redistributions in binary form must reproduce the above copyright 55.68 + notice, this list of conditions and the following disclaimer in the 55.69 + documentation and/or other materials provided with the distribution. 55.70 + * Neither the name of the copyright holder nor the names of 55.71 + contributors may be used to endorse or promote products derived from 55.72 + this software without specific prior written permission. 55.73 + 55.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 55.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 55.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 55.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 55.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 55.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 55.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 55.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 55.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55.85 +*/ 55.86 + 55.87 +package jdk.internal.dynalink.support; 55.88 + 55.89 +import java.lang.invoke.MethodHandles.Lookup; 55.90 +import java.lang.invoke.MethodType; 55.91 +import jdk.internal.dynalink.CallSiteDescriptor; 55.92 + 55.93 + 55.94 +/** 55.95 + * A default, fairly light implementation of a call site descriptor used for describing non-standard operations. It does 55.96 + * not store {@link Lookup} objects but always returns the public lookup from its {@link #getLookup()} method. If you 55.97 + * need to support non-public lookup, you can use {@link LookupCallSiteDescriptor}. 55.98 + * @author Attila Szegedi 55.99 + */ 55.100 +class DefaultCallSiteDescriptor extends AbstractCallSiteDescriptor { 55.101 + 55.102 + private final String[] tokenizedName; 55.103 + private final MethodType methodType; 55.104 + 55.105 + DefaultCallSiteDescriptor(String[] tokenizedName, MethodType methodType) { 55.106 + this.tokenizedName = tokenizedName; 55.107 + this.methodType = methodType; 55.108 + } 55.109 + 55.110 + @Override 55.111 + public int getNameTokenCount() { 55.112 + return tokenizedName.length; 55.113 + } 55.114 + 55.115 + @Override 55.116 + public String getNameToken(int i) { 55.117 + try { 55.118 + return tokenizedName[i]; 55.119 + } catch(ArrayIndexOutOfBoundsException e) { 55.120 + throw new IllegalArgumentException(e.getMessage()); 55.121 + } 55.122 + } 55.123 + 55.124 + String[] getTokenizedName() { 55.125 + return tokenizedName; 55.126 + } 55.127 + 55.128 + @Override 55.129 + public MethodType getMethodType() { 55.130 + return methodType; 55.131 + } 55.132 + 55.133 + @Override 55.134 + public CallSiteDescriptor changeMethodType(MethodType newMethodType) { 55.135 + return CallSiteDescriptorFactory.getCanonicalPublicDescriptor(new DefaultCallSiteDescriptor(tokenizedName, 55.136 + newMethodType)); 55.137 + } 55.138 +}
56.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 56.2 +++ b/src/jdk/internal/dynalink/support/Guards.java Thu Feb 14 13:22:26 2013 +0100 56.3 @@ -0,0 +1,360 @@ 56.4 +/* 56.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 56.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 56.7 + * 56.8 + * This code is free software; you can redistribute it and/or modify it 56.9 + * under the terms of the GNU General Public License version 2 only, as 56.10 + * published by the Free Software Foundation. Oracle designates this 56.11 + * particular file as subject to the "Classpath" exception as provided 56.12 + * by Oracle in the LICENSE file that accompanied this code. 56.13 + * 56.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 56.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 56.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 56.17 + * version 2 for more details (a copy is included in the LICENSE file that 56.18 + * accompanied this code). 56.19 + * 56.20 + * You should have received a copy of the GNU General Public License version 56.21 + * 2 along with this work; if not, write to the Free Software Foundation, 56.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 56.23 + * 56.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 56.25 + * or visit www.oracle.com if you need additional information or have any 56.26 + * questions. 56.27 + */ 56.28 + 56.29 +/* 56.30 + * This file is available under and governed by the GNU General Public 56.31 + * License version 2 only, as published by the Free Software Foundation. 56.32 + * However, the following notice accompanied the original version of this 56.33 + * file, and Oracle licenses the original version of this file under the BSD 56.34 + * license: 56.35 + */ 56.36 +/* 56.37 + Copyright 2009-2013 Attila Szegedi 56.38 + 56.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 56.40 + and the BSD License (the "BSD License"), with licensee being free to 56.41 + choose either of the two at their discretion. 56.42 + 56.43 + You may not use this file except in compliance with either the Apache 56.44 + License or the BSD License. 56.45 + 56.46 + If you choose to use this file in compliance with the Apache License, the 56.47 + following notice applies to you: 56.48 + 56.49 + You may obtain a copy of the Apache License at 56.50 + 56.51 + http://www.apache.org/licenses/LICENSE-2.0 56.52 + 56.53 + Unless required by applicable law or agreed to in writing, software 56.54 + distributed under the License is distributed on an "AS IS" BASIS, 56.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 56.56 + implied. See the License for the specific language governing 56.57 + permissions and limitations under the License. 56.58 + 56.59 + If you choose to use this file in compliance with the BSD License, the 56.60 + following notice applies to you: 56.61 + 56.62 + Redistribution and use in source and binary forms, with or without 56.63 + modification, are permitted provided that the following conditions are 56.64 + met: 56.65 + * Redistributions of source code must retain the above copyright 56.66 + notice, this list of conditions and the following disclaimer. 56.67 + * Redistributions in binary form must reproduce the above copyright 56.68 + notice, this list of conditions and the following disclaimer in the 56.69 + documentation and/or other materials provided with the distribution. 56.70 + * Neither the name of the copyright holder nor the names of 56.71 + contributors may be used to endorse or promote products derived from 56.72 + this software without specific prior written permission. 56.73 + 56.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 56.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 56.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 56.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 56.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 56.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 56.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 56.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 56.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 56.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56.85 +*/ 56.86 + 56.87 +package jdk.internal.dynalink.support; 56.88 + 56.89 +import java.lang.invoke.MethodHandle; 56.90 +import java.lang.invoke.MethodHandles; 56.91 +import java.lang.invoke.MethodType; 56.92 +import java.util.logging.Level; 56.93 +import java.util.logging.Logger; 56.94 +import jdk.internal.dynalink.linker.LinkerServices; 56.95 + 56.96 + 56.97 +/** 56.98 + * Utility methods for creating typical guards. TODO: introduce reasonable caching of created guards. 56.99 + * 56.100 + * @author Attila Szegedi 56.101 + */ 56.102 +public class Guards { 56.103 + private static final Logger LOG = Logger 56.104 + .getLogger(Guards.class.getName(), "jdk.internal.dynalink.support.messages"); 56.105 + 56.106 + private Guards() { 56.107 + } 56.108 + 56.109 + /** 56.110 + * Creates a guard method handle with arguments of a specified type, but with boolean return value. When invoked, it 56.111 + * returns true if the first argument is of the specified class (exactly of it, not a subclass). The rest of the 56.112 + * arguments will be ignored. 56.113 + * 56.114 + * @param clazz the class of the first argument to test for 56.115 + * @param type the method type 56.116 + * @return a method handle testing whether its first argument is of the specified class. 56.117 + */ 56.118 + @SuppressWarnings("boxing") 56.119 + public static MethodHandle isOfClass(Class<?> clazz, MethodType type) { 56.120 + final Class<?> declaredType = type.parameterType(0); 56.121 + if(clazz == declaredType) { 56.122 + LOG.log(Level.WARNING, "isOfClassGuardAlwaysTrue", new Object[] { clazz.getName(), 0, type }); 56.123 + return constantTrue(type); 56.124 + } 56.125 + if(!declaredType.isAssignableFrom(clazz)) { 56.126 + LOG.log(Level.WARNING, "isOfClassGuardAlwaysFalse", new Object[] { clazz.getName(), 0, type }); 56.127 + return constantFalse(type); 56.128 + } 56.129 + return getClassBoundArgumentTest(IS_OF_CLASS, clazz, 0, type); 56.130 + } 56.131 + 56.132 + /** 56.133 + * Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it 56.134 + * returns true if the first argument is instance of the specified class or its subclass). The rest of the arguments 56.135 + * will be ignored. 56.136 + * 56.137 + * @param clazz the class of the first argument to test for 56.138 + * @param type the method type 56.139 + * @return a method handle testing whether its first argument is of the specified class or subclass. 56.140 + */ 56.141 + public static MethodHandle isInstance(Class<?> clazz, MethodType type) { 56.142 + return isInstance(clazz, 0, type); 56.143 + } 56.144 + 56.145 + /** 56.146 + * Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it 56.147 + * returns true if the n'th argument is instance of the specified class or its subclass). The rest of the arguments 56.148 + * will be ignored. 56.149 + * 56.150 + * @param clazz the class of the first argument to test for 56.151 + * @param pos the position on the argument list to test 56.152 + * @param type the method type 56.153 + * @return a method handle testing whether its first argument is of the specified class or subclass. 56.154 + */ 56.155 + @SuppressWarnings("boxing") 56.156 + public static MethodHandle isInstance(Class<?> clazz, int pos, MethodType type) { 56.157 + final Class<?> declaredType = type.parameterType(pos); 56.158 + if(clazz.isAssignableFrom(declaredType)) { 56.159 + LOG.log(Level.WARNING, "isInstanceGuardAlwaysTrue", new Object[] { clazz.getName(), pos, type }); 56.160 + return constantTrue(type); 56.161 + } 56.162 + if(!declaredType.isAssignableFrom(clazz)) { 56.163 + LOG.log(Level.WARNING, "isInstanceGuardAlwaysFalse", new Object[] { clazz.getName(), pos, type }); 56.164 + return constantFalse(type); 56.165 + } 56.166 + return getClassBoundArgumentTest(IS_INSTANCE, clazz, pos, type); 56.167 + } 56.168 + 56.169 + /** 56.170 + * Creates a method handle that returns true if the argument in the specified position is a Java array. 56.171 + * 56.172 + * @param pos the position in the argument lit 56.173 + * @param type the method type of the handle 56.174 + * @return a method handle that returns true if the argument in the specified position is a Java array; the rest of 56.175 + * the arguments are ignored. 56.176 + */ 56.177 + @SuppressWarnings("boxing") 56.178 + public static MethodHandle isArray(int pos, MethodType type) { 56.179 + final Class<?> declaredType = type.parameterType(pos); 56.180 + if(declaredType.isArray()) { 56.181 + LOG.log(Level.WARNING, "isArrayGuardAlwaysTrue", new Object[] { pos, type }); 56.182 + return constantTrue(type); 56.183 + } 56.184 + if(!declaredType.isAssignableFrom(Object[].class)) { 56.185 + LOG.log(Level.WARNING, "isArrayGuardAlwaysFalse", new Object[] { pos, type }); 56.186 + return constantFalse(type); 56.187 + } 56.188 + return asType(IS_ARRAY, pos, type); 56.189 + } 56.190 + 56.191 + /** 56.192 + * Return true if it is safe to strongly reference a class from the referred class loader from a class associated 56.193 + * with the referring class loader without risking a class loader memory leak. 56.194 + * 56.195 + * @param referrerLoader the referrer class loader 56.196 + * @param referredLoader the referred class loader 56.197 + * @return true if it is safe to strongly reference the class 56.198 + */ 56.199 + public static boolean canReferenceDirectly(ClassLoader referrerLoader, final ClassLoader referredLoader) { 56.200 + if(referredLoader == null) { 56.201 + // Can always refer directly to a system class 56.202 + return true; 56.203 + } 56.204 + if(referrerLoader == null) { 56.205 + // System classes can't refer directly to any non-system class 56.206 + return false; 56.207 + } 56.208 + // Otherwise, can only refer directly to classes residing in same or 56.209 + // parent class loader. 56.210 + 56.211 + ClassLoader referrer = referrerLoader; 56.212 + do { 56.213 + if(referrer == referredLoader) { 56.214 + return true; 56.215 + } 56.216 + referrer = referrer.getParent(); 56.217 + } while(referrer != null); 56.218 + return false; 56.219 + } 56.220 + 56.221 + private static MethodHandle getClassBoundArgumentTest(MethodHandle test, Class<?> clazz, int pos, MethodType type) { 56.222 + // Bind the class to the first argument of the test 56.223 + return asType(test.bindTo(clazz), pos, type); 56.224 + } 56.225 + 56.226 + /** 56.227 + * Takes a guard-test method handle, and adapts it to the requested type, returning a boolean. Only applies 56.228 + * conversions as per {@link MethodHandle#asType(MethodType)}. 56.229 + * @param test the test method handle 56.230 + * @param type the type to adapt the method handle to 56.231 + * @return the adapted method handle 56.232 + */ 56.233 + public static MethodHandle asType(MethodHandle test, MethodType type) { 56.234 + return test.asType(getTestType(test, type)); 56.235 + } 56.236 + 56.237 + /** 56.238 + * Takes a guard-test method handle, and adapts it to the requested type, returning a boolean. Applies the passed 56.239 + * {@link LinkerServices} object's {@link LinkerServices#asType(MethodHandle, MethodType)}. 56.240 + * @param linkerServices the linker services to use for type conversions 56.241 + * @param test the test method handle 56.242 + * @param type the type to adapt the method handle to 56.243 + * @return the adapted method handle 56.244 + */ 56.245 + public static MethodHandle asType(LinkerServices linkerServices, MethodHandle test, MethodType type) { 56.246 + return linkerServices.asType(test, getTestType(test, type)); 56.247 + } 56.248 + 56.249 + private static MethodType getTestType(MethodHandle test, MethodType type) { 56.250 + return type.dropParameterTypes(test.type().parameterCount(), 56.251 + type.parameterCount()).changeReturnType(boolean.class); 56.252 + } 56.253 + 56.254 + private static MethodHandle asType(MethodHandle test, int pos, MethodType type) { 56.255 + assert test != null; 56.256 + assert type != null; 56.257 + assert type.parameterCount() > 0; 56.258 + assert pos >= 0 && pos < type.parameterCount(); 56.259 + assert test.type().parameterCount() == 1; 56.260 + assert test.type().returnType() == Boolean.TYPE; 56.261 + return MethodHandles.permuteArguments(test.asType(test.type().changeParameterType(0, type.parameterType(pos))), 56.262 + type.changeReturnType(Boolean.TYPE), new int[] { pos }); 56.263 + } 56.264 + 56.265 + private static final MethodHandle IS_OF_CLASS = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, 56.266 + "isOfClass", MethodType.methodType(Boolean.TYPE, Class.class, Object.class)); 56.267 + 56.268 + private static final MethodHandle IS_INSTANCE = Lookup.PUBLIC.findVirtual(Class.class, "isInstance", 56.269 + MethodType.methodType(Boolean.TYPE, Object.class)); 56.270 + 56.271 + private static final MethodHandle IS_ARRAY = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, "isArray", 56.272 + MethodType.methodType(Boolean.TYPE, Object.class)); 56.273 + 56.274 + private static final MethodHandle IS_IDENTICAL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, 56.275 + "isIdentical", MethodType.methodType(Boolean.TYPE, Object.class, Object.class)); 56.276 + 56.277 + private static final MethodHandle IS_NULL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, 56.278 + "isNull", MethodType.methodType(Boolean.TYPE, Object.class)); 56.279 + 56.280 + private static final MethodHandle IS_NOT_NULL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, 56.281 + "isNotNull", MethodType.methodType(Boolean.TYPE, Object.class)); 56.282 + 56.283 + /** 56.284 + * Creates a guard method that tests its only argument for being of an exact particular class. 56.285 + * @param clazz the class to test for. 56.286 + * @return the desired guard method. 56.287 + */ 56.288 + public static MethodHandle getClassGuard(Class<?> clazz) { 56.289 + return IS_OF_CLASS.bindTo(clazz); 56.290 + } 56.291 + 56.292 + /** 56.293 + * Creates a guard method that tests its only argument for being an instance of a particular class. 56.294 + * @param clazz the class to test for. 56.295 + * @return the desired guard method. 56.296 + */ 56.297 + public static MethodHandle getInstanceOfGuard(Class<?> clazz) { 56.298 + return IS_INSTANCE.bindTo(clazz); 56.299 + } 56.300 + 56.301 + /** 56.302 + * Creates a guard method that tests its only argument for being referentially identical to another object 56.303 + * @param obj the object used as referential identity test 56.304 + * @return the desired guard method. 56.305 + */ 56.306 + public static MethodHandle getIdentityGuard(Object obj) { 56.307 + return IS_IDENTICAL.bindTo(obj); 56.308 + } 56.309 + 56.310 + /** 56.311 + * Returns a guard that tests whether the first argument is null. 56.312 + * @return a guard that tests whether the first argument is null. 56.313 + */ 56.314 + public static MethodHandle isNull() { 56.315 + return IS_NULL; 56.316 + } 56.317 + 56.318 + /** 56.319 + * Returns a guard that tests whether the first argument is not null. 56.320 + * @return a guard that tests whether the first argument is not null. 56.321 + */ 56.322 + public static MethodHandle isNotNull() { 56.323 + return IS_NOT_NULL; 56.324 + } 56.325 + 56.326 + @SuppressWarnings("unused") 56.327 + private static boolean isNull(Object obj) { 56.328 + return obj == null; 56.329 + } 56.330 + 56.331 + @SuppressWarnings("unused") 56.332 + private static boolean isNotNull(Object obj) { 56.333 + return obj != null; 56.334 + } 56.335 + 56.336 + @SuppressWarnings("unused") 56.337 + private static boolean isArray(Object o) { 56.338 + return o != null && o.getClass().isArray(); 56.339 + } 56.340 + 56.341 + @SuppressWarnings("unused") 56.342 + private static boolean isOfClass(Class<?> c, Object o) { 56.343 + return o != null && o.getClass() == c; 56.344 + } 56.345 + 56.346 + @SuppressWarnings("unused") 56.347 + private static boolean isIdentical(Object o1, Object o2) { 56.348 + return o1 == o2; 56.349 + } 56.350 + 56.351 + private static MethodHandle constantTrue(MethodType type) { 56.352 + return constantBoolean(Boolean.TRUE, type); 56.353 + } 56.354 + 56.355 + private static MethodHandle constantFalse(MethodType type) { 56.356 + return constantBoolean(Boolean.FALSE, type); 56.357 + } 56.358 + 56.359 + private static MethodHandle constantBoolean(Boolean value, MethodType type) { 56.360 + return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value), 56.361 + type.changeReturnType(Boolean.TYPE)); 56.362 + } 56.363 +} 56.364 \ No newline at end of file
57.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 57.2 +++ b/src/jdk/internal/dynalink/support/LinkRequestImpl.java Thu Feb 14 13:22:26 2013 +0100 57.3 @@ -0,0 +1,143 @@ 57.4 +/* 57.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 57.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 57.7 + * 57.8 + * This code is free software; you can redistribute it and/or modify it 57.9 + * under the terms of the GNU General Public License version 2 only, as 57.10 + * published by the Free Software Foundation. Oracle designates this 57.11 + * particular file as subject to the "Classpath" exception as provided 57.12 + * by Oracle in the LICENSE file that accompanied this code. 57.13 + * 57.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 57.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 57.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 57.17 + * version 2 for more details (a copy is included in the LICENSE file that 57.18 + * accompanied this code). 57.19 + * 57.20 + * You should have received a copy of the GNU General Public License version 57.21 + * 2 along with this work; if not, write to the Free Software Foundation, 57.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 57.23 + * 57.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 57.25 + * or visit www.oracle.com if you need additional information or have any 57.26 + * questions. 57.27 + */ 57.28 + 57.29 +/* 57.30 + * This file is available under and governed by the GNU General Public 57.31 + * License version 2 only, as published by the Free Software Foundation. 57.32 + * However, the following notice accompanied the original version of this 57.33 + * file, and Oracle licenses the original version of this file under the BSD 57.34 + * license: 57.35 + */ 57.36 +/* 57.37 + Copyright 2009-2013 Attila Szegedi 57.38 + 57.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 57.40 + and the BSD License (the "BSD License"), with licensee being free to 57.41 + choose either of the two at their discretion. 57.42 + 57.43 + You may not use this file except in compliance with either the Apache 57.44 + License or the BSD License. 57.45 + 57.46 + If you choose to use this file in compliance with the Apache License, the 57.47 + following notice applies to you: 57.48 + 57.49 + You may obtain a copy of the Apache License at 57.50 + 57.51 + http://www.apache.org/licenses/LICENSE-2.0 57.52 + 57.53 + Unless required by applicable law or agreed to in writing, software 57.54 + distributed under the License is distributed on an "AS IS" BASIS, 57.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 57.56 + implied. See the License for the specific language governing 57.57 + permissions and limitations under the License. 57.58 + 57.59 + If you choose to use this file in compliance with the BSD License, the 57.60 + following notice applies to you: 57.61 + 57.62 + Redistribution and use in source and binary forms, with or without 57.63 + modification, are permitted provided that the following conditions are 57.64 + met: 57.65 + * Redistributions of source code must retain the above copyright 57.66 + notice, this list of conditions and the following disclaimer. 57.67 + * Redistributions in binary form must reproduce the above copyright 57.68 + notice, this list of conditions and the following disclaimer in the 57.69 + documentation and/or other materials provided with the distribution. 57.70 + * Neither the name of the copyright holder nor the names of 57.71 + contributors may be used to endorse or promote products derived from 57.72 + this software without specific prior written permission. 57.73 + 57.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 57.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 57.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 57.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 57.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 57.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 57.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 57.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 57.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 57.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 57.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57.85 +*/ 57.86 + 57.87 +package jdk.internal.dynalink.support; 57.88 + 57.89 +import jdk.internal.dynalink.CallSiteDescriptor; 57.90 +import jdk.internal.dynalink.linker.LinkRequest; 57.91 + 57.92 +/** 57.93 + * Default implementation of the {@link LinkRequest}, representing a link request to a call site that passes no language 57.94 + * runtime specific native context arguments on the stack. 57.95 + * 57.96 + * @author Attila Szegedi 57.97 + */ 57.98 +public class LinkRequestImpl implements LinkRequest { 57.99 + 57.100 + private final CallSiteDescriptor callSiteDescriptor; 57.101 + private final Object[] arguments; 57.102 + private final boolean callSiteUnstable; 57.103 + 57.104 + /** 57.105 + * Creates a new link request. 57.106 + * 57.107 + * @param callSiteDescriptor the descriptor for the call site being linked 57.108 + * @param callSiteUnstable true if the call site being linked is considered unstable 57.109 + * @param arguments the arguments for the invocation 57.110 + */ 57.111 + public LinkRequestImpl(CallSiteDescriptor callSiteDescriptor, boolean callSiteUnstable, Object... arguments) { 57.112 + this.callSiteDescriptor = callSiteDescriptor; 57.113 + this.callSiteUnstable = callSiteUnstable; 57.114 + this.arguments = arguments; 57.115 + } 57.116 + 57.117 + @Override 57.118 + public Object[] getArguments() { 57.119 + return arguments != null ? arguments.clone() : null; 57.120 + } 57.121 + 57.122 + @Override 57.123 + public Object getReceiver() { 57.124 + return arguments != null && arguments.length > 0 ? arguments[0] : null; 57.125 + } 57.126 + 57.127 + @Override 57.128 + public CallSiteDescriptor getCallSiteDescriptor() { 57.129 + return callSiteDescriptor; 57.130 + } 57.131 + 57.132 + @Override 57.133 + public boolean isCallSiteUnstable() { 57.134 + return callSiteUnstable; 57.135 + } 57.136 + 57.137 + @Override 57.138 + public LinkRequest withoutRuntimeContext() { 57.139 + return this; 57.140 + } 57.141 + 57.142 + @Override 57.143 + public LinkRequest replaceArguments(CallSiteDescriptor newCallSiteDescriptor, Object[] newArguments) { 57.144 + return new LinkRequestImpl(newCallSiteDescriptor, callSiteUnstable, newArguments); 57.145 + } 57.146 +} 57.147 \ No newline at end of file
58.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 58.2 +++ b/src/jdk/internal/dynalink/support/LinkerServicesImpl.java Thu Feb 14 13:22:26 2013 +0100 58.3 @@ -0,0 +1,141 @@ 58.4 +/* 58.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 58.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 58.7 + * 58.8 + * This code is free software; you can redistribute it and/or modify it 58.9 + * under the terms of the GNU General Public License version 2 only, as 58.10 + * published by the Free Software Foundation. Oracle designates this 58.11 + * particular file as subject to the "Classpath" exception as provided 58.12 + * by Oracle in the LICENSE file that accompanied this code. 58.13 + * 58.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 58.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 58.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 58.17 + * version 2 for more details (a copy is included in the LICENSE file that 58.18 + * accompanied this code). 58.19 + * 58.20 + * You should have received a copy of the GNU General Public License version 58.21 + * 2 along with this work; if not, write to the Free Software Foundation, 58.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 58.23 + * 58.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 58.25 + * or visit www.oracle.com if you need additional information or have any 58.26 + * questions. 58.27 + */ 58.28 + 58.29 +/* 58.30 + * This file is available under and governed by the GNU General Public 58.31 + * License version 2 only, as published by the Free Software Foundation. 58.32 + * However, the following notice accompanied the original version of this 58.33 + * file, and Oracle licenses the original version of this file under the BSD 58.34 + * license: 58.35 + */ 58.36 +/* 58.37 + Copyright 2009-2013 Attila Szegedi 58.38 + 58.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 58.40 + and the BSD License (the "BSD License"), with licensee being free to 58.41 + choose either of the two at their discretion. 58.42 + 58.43 + You may not use this file except in compliance with either the Apache 58.44 + License or the BSD License. 58.45 + 58.46 + If you choose to use this file in compliance with the Apache License, the 58.47 + following notice applies to you: 58.48 + 58.49 + You may obtain a copy of the Apache License at 58.50 + 58.51 + http://www.apache.org/licenses/LICENSE-2.0 58.52 + 58.53 + Unless required by applicable law or agreed to in writing, software 58.54 + distributed under the License is distributed on an "AS IS" BASIS, 58.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 58.56 + implied. See the License for the specific language governing 58.57 + permissions and limitations under the License. 58.58 + 58.59 + If you choose to use this file in compliance with the BSD License, the 58.60 + following notice applies to you: 58.61 + 58.62 + Redistribution and use in source and binary forms, with or without 58.63 + modification, are permitted provided that the following conditions are 58.64 + met: 58.65 + * Redistributions of source code must retain the above copyright 58.66 + notice, this list of conditions and the following disclaimer. 58.67 + * Redistributions in binary form must reproduce the above copyright 58.68 + notice, this list of conditions and the following disclaimer in the 58.69 + documentation and/or other materials provided with the distribution. 58.70 + * Neither the name of the copyright holder nor the names of 58.71 + contributors may be used to endorse or promote products derived from 58.72 + this software without specific prior written permission. 58.73 + 58.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 58.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 58.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 58.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 58.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 58.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 58.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 58.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 58.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 58.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 58.85 +*/ 58.86 + 58.87 +package jdk.internal.dynalink.support; 58.88 + 58.89 +import java.lang.invoke.MethodHandle; 58.90 +import java.lang.invoke.MethodType; 58.91 +import jdk.internal.dynalink.linker.ConversionComparator.Comparison; 58.92 +import jdk.internal.dynalink.linker.GuardedInvocation; 58.93 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 58.94 +import jdk.internal.dynalink.linker.LinkRequest; 58.95 +import jdk.internal.dynalink.linker.LinkerServices; 58.96 + 58.97 + 58.98 +/** 58.99 + * Default implementation of the {@link LinkerServices} interface. 58.100 + * 58.101 + * @author Attila Szegedi 58.102 + */ 58.103 +public class LinkerServicesImpl implements LinkerServices { 58.104 + 58.105 + private final TypeConverterFactory typeConverterFactory; 58.106 + private final GuardingDynamicLinker topLevelLinker; 58.107 + 58.108 + /** 58.109 + * Creates a new linker services object. 58.110 + * 58.111 + * @param typeConverterFactory the type converter factory exposed by the services. 58.112 + * @param topLevelLinker the top level linker used by the services. 58.113 + */ 58.114 + public LinkerServicesImpl(final TypeConverterFactory typeConverterFactory, 58.115 + final GuardingDynamicLinker topLevelLinker) { 58.116 + this.typeConverterFactory = typeConverterFactory; 58.117 + this.topLevelLinker = topLevelLinker; 58.118 + } 58.119 + 58.120 + @Override 58.121 + public boolean canConvert(Class<?> from, Class<?> to) { 58.122 + return typeConverterFactory.canConvert(from, to); 58.123 + } 58.124 + 58.125 + @Override 58.126 + public MethodHandle asType(MethodHandle handle, MethodType fromType) { 58.127 + return typeConverterFactory.asType(handle, fromType); 58.128 + } 58.129 + 58.130 + @Override 58.131 + public MethodHandle getTypeConverter(Class<?> sourceType, Class<?> targetType) { 58.132 + return typeConverterFactory.getTypeConverter(sourceType, targetType); 58.133 + } 58.134 + 58.135 + @Override 58.136 + public Comparison compareConversion(Class<?> sourceType, Class<?> targetType1, Class<?> targetType2) { 58.137 + return typeConverterFactory.compareConversion(sourceType, targetType1, targetType2); 58.138 + } 58.139 + 58.140 + @Override 58.141 + public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest) throws Exception { 58.142 + return topLevelLinker.getGuardedInvocation(linkRequest, this); 58.143 + } 58.144 +} 58.145 \ No newline at end of file
59.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 59.2 +++ b/src/jdk/internal/dynalink/support/Lookup.java Thu Feb 14 13:22:26 2013 +0100 59.3 @@ -0,0 +1,361 @@ 59.4 +/* 59.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 59.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 59.7 + * 59.8 + * This code is free software; you can redistribute it and/or modify it 59.9 + * under the terms of the GNU General Public License version 2 only, as 59.10 + * published by the Free Software Foundation. Oracle designates this 59.11 + * particular file as subject to the "Classpath" exception as provided 59.12 + * by Oracle in the LICENSE file that accompanied this code. 59.13 + * 59.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 59.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 59.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 59.17 + * version 2 for more details (a copy is included in the LICENSE file that 59.18 + * accompanied this code). 59.19 + * 59.20 + * You should have received a copy of the GNU General Public License version 59.21 + * 2 along with this work; if not, write to the Free Software Foundation, 59.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 59.23 + * 59.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 59.25 + * or visit www.oracle.com if you need additional information or have any 59.26 + * questions. 59.27 + */ 59.28 + 59.29 +/* 59.30 + * This file is available under and governed by the GNU General Public 59.31 + * License version 2 only, as published by the Free Software Foundation. 59.32 + * However, the following notice accompanied the original version of this 59.33 + * file, and Oracle licenses the original version of this file under the BSD 59.34 + * license: 59.35 + */ 59.36 +/* 59.37 + Copyright 2009-2013 Attila Szegedi 59.38 + 59.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 59.40 + and the BSD License (the "BSD License"), with licensee being free to 59.41 + choose either of the two at their discretion. 59.42 + 59.43 + You may not use this file except in compliance with either the Apache 59.44 + License or the BSD License. 59.45 + 59.46 + If you choose to use this file in compliance with the Apache License, the 59.47 + following notice applies to you: 59.48 + 59.49 + You may obtain a copy of the Apache License at 59.50 + 59.51 + http://www.apache.org/licenses/LICENSE-2.0 59.52 + 59.53 + Unless required by applicable law or agreed to in writing, software 59.54 + distributed under the License is distributed on an "AS IS" BASIS, 59.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 59.56 + implied. See the License for the specific language governing 59.57 + permissions and limitations under the License. 59.58 + 59.59 + If you choose to use this file in compliance with the BSD License, the 59.60 + following notice applies to you: 59.61 + 59.62 + Redistribution and use in source and binary forms, with or without 59.63 + modification, are permitted provided that the following conditions are 59.64 + met: 59.65 + * Redistributions of source code must retain the above copyright 59.66 + notice, this list of conditions and the following disclaimer. 59.67 + * Redistributions in binary form must reproduce the above copyright 59.68 + notice, this list of conditions and the following disclaimer in the 59.69 + documentation and/or other materials provided with the distribution. 59.70 + * Neither the name of the copyright holder nor the names of 59.71 + contributors may be used to endorse or promote products derived from 59.72 + this software without specific prior written permission. 59.73 + 59.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 59.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 59.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 59.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 59.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 59.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 59.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 59.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 59.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 59.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 59.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59.85 +*/ 59.86 + 59.87 +package jdk.internal.dynalink.support; 59.88 + 59.89 +import java.lang.invoke.MethodHandle; 59.90 +import java.lang.invoke.MethodHandles; 59.91 +import java.lang.invoke.MethodType; 59.92 +import java.lang.reflect.Constructor; 59.93 +import java.lang.reflect.Field; 59.94 +import java.lang.reflect.Method; 59.95 +import java.lang.reflect.Modifier; 59.96 + 59.97 +/** 59.98 + * A wrapper around MethodHandles.Lookup that masks checked exceptions in those cases when you're looking up methods 59.99 + * within your own codebase (therefore it is an error if they are not present). 59.100 + * 59.101 + * @author Attila Szegedi 59.102 + */ 59.103 +public class Lookup { 59.104 + private final MethodHandles.Lookup lookup; 59.105 + 59.106 + /** 59.107 + * Creates a new instance, bound to an instance of {@link java.lang.invoke.MethodHandles.Lookup}. 59.108 + * 59.109 + * @param lookup the {@link java.lang.invoke.MethodHandles.Lookup} it delegates to. 59.110 + */ 59.111 + public Lookup(MethodHandles.Lookup lookup) { 59.112 + this.lookup = lookup; 59.113 + } 59.114 + 59.115 + /** 59.116 + * A canonical Lookup object that wraps {@link MethodHandles#publicLookup()}. 59.117 + */ 59.118 + public static final Lookup PUBLIC = new Lookup(MethodHandles.publicLookup()); 59.119 + 59.120 + /** 59.121 + * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflect(Method)}, converting any encountered 59.122 + * {@link IllegalAccessException} into an {@link IllegalAccessError}. 59.123 + * 59.124 + * @param m the method to unreflect 59.125 + * @return the unreflected method handle. 59.126 + */ 59.127 + public MethodHandle unreflect(Method m) { 59.128 + try { 59.129 + return lookup.unreflect(m); 59.130 + } catch(IllegalAccessException e) { 59.131 + final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect method " + m); 59.132 + ee.initCause(e); 59.133 + throw ee; 59.134 + } 59.135 + } 59.136 + 59.137 + 59.138 + /** 59.139 + * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectGetter(Field)}, converting any encountered 59.140 + * {@link IllegalAccessException} into an {@link IllegalAccessError}. 59.141 + * 59.142 + * @param f the field for which a getter is unreflected 59.143 + * @return the unreflected field getter handle. 59.144 + */ 59.145 + public MethodHandle unreflectGetter(Field f) { 59.146 + try { 59.147 + return lookup.unreflectGetter(f); 59.148 + } catch(IllegalAccessException e) { 59.149 + final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect getter for field " + f); 59.150 + ee.initCause(e); 59.151 + throw ee; 59.152 + } 59.153 + } 59.154 + 59.155 + /** 59.156 + * Performs a {@link java.lang.invoke.MethodHandles.Lookup#findGetter(Class, String, Class)}, converting any 59.157 + * encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and {@link NoSuchFieldException} 59.158 + * into a {@link NoSuchFieldError}. 59.159 + * 59.160 + * @param refc the class declaring the field 59.161 + * @param name the name of the field 59.162 + * @param type the type of the field 59.163 + * @return the unreflected field getter handle. 59.164 + * @throws IllegalAccessError if the field is inaccessible. 59.165 + * @throws NoSuchFieldError if the field does not exist. 59.166 + */ 59.167 + public MethodHandle findGetter(Class<?>refc, String name, Class<?> type) { 59.168 + try { 59.169 + return lookup.findGetter(refc, name, type); 59.170 + } catch(IllegalAccessException e) { 59.171 + final IllegalAccessError ee = new IllegalAccessError("Failed to access getter for field " + refc.getName() + 59.172 + "." + name + " of type " + type.getName()); 59.173 + ee.initCause(e); 59.174 + throw ee; 59.175 + } catch(NoSuchFieldException e) { 59.176 + final NoSuchFieldError ee = new NoSuchFieldError("Failed to find getter for field " + refc.getName() + 59.177 + "." + name + " of type " + type.getName()); 59.178 + ee.initCause(e); 59.179 + throw ee; 59.180 + } 59.181 + } 59.182 + 59.183 + /** 59.184 + * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectSetter(Field)}, converting any encountered 59.185 + * {@link IllegalAccessException} into an {@link IllegalAccessError}. 59.186 + * 59.187 + * @param f the field for which a setter is unreflected 59.188 + * @return the unreflected field setter handle. 59.189 + */ 59.190 + public MethodHandle unreflectSetter(Field f) { 59.191 + try { 59.192 + return lookup.unreflectSetter(f); 59.193 + } catch(IllegalAccessException e) { 59.194 + final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect setter for field " + f); 59.195 + ee.initCause(e); 59.196 + throw ee; 59.197 + } 59.198 + } 59.199 + 59.200 + /** 59.201 + * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor(Constructor)}, converting any 59.202 + * encountered {@link IllegalAccessException} into an {@link IllegalAccessError}. 59.203 + * 59.204 + * @param c the constructor to unreflect 59.205 + * @return the unreflected constructor handle. 59.206 + */ 59.207 + public MethodHandle unreflectConstructor(Constructor<?> c) { 59.208 + try { 59.209 + return lookup.unreflectConstructor(c); 59.210 + } catch(IllegalAccessException e) { 59.211 + final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect constructor " + c); 59.212 + ee.initCause(e); 59.213 + throw ee; 59.214 + } 59.215 + } 59.216 + 59.217 + /** 59.218 + * Performs a findSpecial on the underlying lookup, except for the backport where it rather uses unreflect. Converts 59.219 + * any encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and a 59.220 + * {@link NoSuchMethodException} into a {@link NoSuchMethodError}. 59.221 + * 59.222 + * @param declaringClass class declaring the method 59.223 + * @param name the name of the method 59.224 + * @param type the type of the method 59.225 + * @return a method handle for the method 59.226 + * @throws IllegalAccessError if the method is inaccessible. 59.227 + * @throws NoSuchMethodError if the method does not exist. 59.228 + */ 59.229 + public MethodHandle findSpecial(Class<?> declaringClass, String name, MethodType type) { 59.230 + try { 59.231 + if(Backport.inUse) { 59.232 + final Method m = declaringClass.getDeclaredMethod(name, type.parameterArray()); 59.233 + if(!Modifier.isPublic(declaringClass.getModifiers()) || !Modifier.isPublic(m.getModifiers())) { 59.234 + m.setAccessible(true); 59.235 + } 59.236 + return unreflect(m); 59.237 + } else { 59.238 + return lookup.findSpecial(declaringClass, name, type, declaringClass); 59.239 + } 59.240 + } catch(IllegalAccessException e) { 59.241 + final IllegalAccessError ee = new IllegalAccessError("Failed to access special method " + methodDescription( 59.242 + declaringClass, name, type)); 59.243 + ee.initCause(e); 59.244 + throw ee; 59.245 + } catch(NoSuchMethodException e) { 59.246 + final NoSuchMethodError ee = new NoSuchMethodError("Failed to find special method " + methodDescription( 59.247 + declaringClass, name, type)); 59.248 + ee.initCause(e); 59.249 + throw ee; 59.250 + } 59.251 + } 59.252 + 59.253 + private static String methodDescription(Class<?> declaringClass, String name, MethodType type) { 59.254 + return declaringClass.getName() + "#" + name + type; 59.255 + } 59.256 + 59.257 + /** 59.258 + * Performs a findStatic on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an 59.259 + * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}. 59.260 + * 59.261 + * @param declaringClass class declaring the method 59.262 + * @param name the name of the method 59.263 + * @param type the type of the method 59.264 + * @return a method handle for the method 59.265 + * @throws IllegalAccessError if the method is inaccessible. 59.266 + * @throws NoSuchMethodError if the method does not exist. 59.267 + */ 59.268 + public MethodHandle findStatic(Class<?> declaringClass, String name, MethodType type) { 59.269 + try { 59.270 + return lookup.findStatic(declaringClass, name, type); 59.271 + } catch(IllegalAccessException e) { 59.272 + final IllegalAccessError ee = new IllegalAccessError("Failed to access static method " + methodDescription( 59.273 + declaringClass, name, type)); 59.274 + ee.initCause(e); 59.275 + throw ee; 59.276 + } catch(NoSuchMethodException e) { 59.277 + final NoSuchMethodError ee = new NoSuchMethodError("Failed to find static method " + methodDescription( 59.278 + declaringClass, name, type)); 59.279 + ee.initCause(e); 59.280 + throw ee; 59.281 + } 59.282 + } 59.283 + 59.284 + /** 59.285 + * Performs a findVirtual on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an 59.286 + * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}. 59.287 + * 59.288 + * @param declaringClass class declaring the method 59.289 + * @param name the name of the method 59.290 + * @param type the type of the method 59.291 + * @return a method handle for the method 59.292 + * @throws IllegalAccessError if the method is inaccessible. 59.293 + * @throws NoSuchMethodError if the method does not exist. 59.294 + */ 59.295 + public MethodHandle findVirtual(Class<?> declaringClass, String name, MethodType type) { 59.296 + try { 59.297 + return lookup.findVirtual(declaringClass, name, type); 59.298 + } catch(IllegalAccessException e) { 59.299 + final IllegalAccessError ee = new IllegalAccessError("Failed to access virtual method " + methodDescription( 59.300 + declaringClass, name, type)); 59.301 + ee.initCause(e); 59.302 + throw ee; 59.303 + } catch(NoSuchMethodException e) { 59.304 + final NoSuchMethodError ee = new NoSuchMethodError("Failed to find virtual method " + methodDescription( 59.305 + declaringClass, name, type)); 59.306 + ee.initCause(e); 59.307 + throw ee; 59.308 + } 59.309 + } 59.310 + 59.311 + /** 59.312 + * Given a lookup, finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class. 59.313 + * Useful in classes' code for convenient linking to their own privates. 59.314 + * @param lookup the lookup for the class 59.315 + * @param name the name of the method 59.316 + * @param rtype the return type of the method 59.317 + * @param ptypes the parameter types of the method 59.318 + * @return the method handle for the method 59.319 + */ 59.320 + public static MethodHandle findOwnSpecial(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) { 59.321 + return new Lookup(lookup).findOwnSpecial(name, rtype, ptypes); 59.322 + } 59.323 + 59.324 + 59.325 + /** 59.326 + * Finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class. Useful in classes' 59.327 + * code for convenient linking to their own privates. It's easier to use than {@code findSpecial} in that you can 59.328 + * just list the parameter types, and don't have to specify lookup class. 59.329 + * @param name the name of the method 59.330 + * @param rtype the return type of the method 59.331 + * @param ptypes the parameter types of the method 59.332 + * @return the method handle for the method 59.333 + */ 59.334 + public MethodHandle findOwnSpecial(String name, Class<?> rtype, Class<?>... ptypes) { 59.335 + return findSpecial(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes)); 59.336 + } 59.337 + 59.338 + /** 59.339 + * Given a lookup, finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class. 59.340 + * Useful in classes' code for convenient linking to their own privates. It's easier to use than {@code findStatic} 59.341 + * in that you can just list the parameter types, and don't have to specify lookup class. 59.342 + * @param lookup the lookup for the class 59.343 + * @param name the name of the method 59.344 + * @param rtype the return type of the method 59.345 + * @param ptypes the parameter types of the method 59.346 + * @return the method handle for the method 59.347 + */ 59.348 + public static MethodHandle findOwnStatic(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) { 59.349 + return new Lookup(lookup).findOwnStatic(name, rtype, ptypes); 59.350 + } 59.351 + 59.352 + /** 59.353 + * Finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class. Useful in classes' 59.354 + * code for convenient linking to their own privates. It's easier to use than {@code findStatic} in that you can 59.355 + * just list the parameter types, and don't have to specify lookup class. 59.356 + * @param name the name of the method 59.357 + * @param rtype the return type of the method 59.358 + * @param ptypes the parameter types of the method 59.359 + * @return the method handle for the method 59.360 + */ 59.361 + public MethodHandle findOwnStatic(String name, Class<?> rtype, Class<?>... ptypes) { 59.362 + return findStatic(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes)); 59.363 + } 59.364 +}
60.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 60.2 +++ b/src/jdk/internal/dynalink/support/LookupCallSiteDescriptor.java Thu Feb 14 13:22:26 2013 +0100 60.3 @@ -0,0 +1,118 @@ 60.4 +/* 60.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 60.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 60.7 + * 60.8 + * This code is free software; you can redistribute it and/or modify it 60.9 + * under the terms of the GNU General Public License version 2 only, as 60.10 + * published by the Free Software Foundation. Oracle designates this 60.11 + * particular file as subject to the "Classpath" exception as provided 60.12 + * by Oracle in the LICENSE file that accompanied this code. 60.13 + * 60.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 60.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 60.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 60.17 + * version 2 for more details (a copy is included in the LICENSE file that 60.18 + * accompanied this code). 60.19 + * 60.20 + * You should have received a copy of the GNU General Public License version 60.21 + * 2 along with this work; if not, write to the Free Software Foundation, 60.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 60.23 + * 60.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 60.25 + * or visit www.oracle.com if you need additional information or have any 60.26 + * questions. 60.27 + */ 60.28 + 60.29 +/* 60.30 + * This file is available under and governed by the GNU General Public 60.31 + * License version 2 only, as published by the Free Software Foundation. 60.32 + * However, the following notice accompanied the original version of this 60.33 + * file, and Oracle licenses the original version of this file under the BSD 60.34 + * license: 60.35 + */ 60.36 +/* 60.37 + Copyright 2009-2013 Attila Szegedi 60.38 + 60.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 60.40 + and the BSD License (the "BSD License"), with licensee being free to 60.41 + choose either of the two at their discretion. 60.42 + 60.43 + You may not use this file except in compliance with either the Apache 60.44 + License or the BSD License. 60.45 + 60.46 + If you choose to use this file in compliance with the Apache License, the 60.47 + following notice applies to you: 60.48 + 60.49 + You may obtain a copy of the Apache License at 60.50 + 60.51 + http://www.apache.org/licenses/LICENSE-2.0 60.52 + 60.53 + Unless required by applicable law or agreed to in writing, software 60.54 + distributed under the License is distributed on an "AS IS" BASIS, 60.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 60.56 + implied. See the License for the specific language governing 60.57 + permissions and limitations under the License. 60.58 + 60.59 + If you choose to use this file in compliance with the BSD License, the 60.60 + following notice applies to you: 60.61 + 60.62 + Redistribution and use in source and binary forms, with or without 60.63 + modification, are permitted provided that the following conditions are 60.64 + met: 60.65 + * Redistributions of source code must retain the above copyright 60.66 + notice, this list of conditions and the following disclaimer. 60.67 + * Redistributions in binary form must reproduce the above copyright 60.68 + notice, this list of conditions and the following disclaimer in the 60.69 + documentation and/or other materials provided with the distribution. 60.70 + * Neither the name of the copyright holder nor the names of 60.71 + contributors may be used to endorse or promote products derived from 60.72 + this software without specific prior written permission. 60.73 + 60.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 60.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 60.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 60.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 60.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 60.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 60.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 60.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 60.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 60.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 60.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60.85 +*/ 60.86 + 60.87 +package jdk.internal.dynalink.support; 60.88 + 60.89 +import java.lang.invoke.MethodHandles.Lookup; 60.90 +import java.lang.invoke.MethodType; 60.91 +import jdk.internal.dynalink.CallSiteDescriptor; 60.92 + 60.93 + 60.94 +/** 60.95 + * A call site descriptor that stores a specific {@link Lookup}. It does not, however, store static bootstrap arguments. 60.96 + * @author Attila Szegedi 60.97 + */ 60.98 +class LookupCallSiteDescriptor extends DefaultCallSiteDescriptor { 60.99 + private Lookup lookup; 60.100 + 60.101 + /** 60.102 + * Create a new call site descriptor from explicit information. 60.103 + * @param tokenizedName the name of the method 60.104 + * @param methodType the method type 60.105 + * @param lookup the lookup 60.106 + */ 60.107 + LookupCallSiteDescriptor(String[] tokenizedName, MethodType methodType, Lookup lookup) { 60.108 + super(tokenizedName, methodType); 60.109 + this.lookup = lookup; 60.110 + } 60.111 + 60.112 + @Override 60.113 + public Lookup getLookup() { 60.114 + return lookup; 60.115 + } 60.116 + 60.117 + @Override 60.118 + public CallSiteDescriptor changeMethodType(MethodType newMethodType) { 60.119 + return new LookupCallSiteDescriptor(getTokenizedName(), newMethodType, lookup); 60.120 + } 60.121 +}
61.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 61.2 +++ b/src/jdk/internal/dynalink/support/NameCodec.java Thu Feb 14 13:22:26 2013 +0100 61.3 @@ -0,0 +1,218 @@ 61.4 +/* 61.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 61.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 61.7 + * 61.8 + * This code is free software; you can redistribute it and/or modify it 61.9 + * under the terms of the GNU General Public License version 2 only, as 61.10 + * published by the Free Software Foundation. Oracle designates this 61.11 + * particular file as subject to the "Classpath" exception as provided 61.12 + * by Oracle in the LICENSE file that accompanied this code. 61.13 + * 61.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 61.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 61.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 61.17 + * version 2 for more details (a copy is included in the LICENSE file that 61.18 + * accompanied this code). 61.19 + * 61.20 + * You should have received a copy of the GNU General Public License version 61.21 + * 2 along with this work; if not, write to the Free Software Foundation, 61.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 61.23 + * 61.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 61.25 + * or visit www.oracle.com if you need additional information or have any 61.26 + * questions. 61.27 + */ 61.28 + 61.29 +/* 61.30 + * This file is available under and governed by the GNU General Public 61.31 + * License version 2 only, as published by the Free Software Foundation. 61.32 + * However, the following notice accompanied the original version of this 61.33 + * file, and Oracle licenses the original version of this file under the BSD 61.34 + * license: 61.35 + */ 61.36 +/* 61.37 + Copyright 2009-2013 Attila Szegedi 61.38 + 61.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 61.40 + and the BSD License (the "BSD License"), with licensee being free to 61.41 + choose either of the two at their discretion. 61.42 + 61.43 + You may not use this file except in compliance with either the Apache 61.44 + License or the BSD License. 61.45 + 61.46 + If you choose to use this file in compliance with the Apache License, the 61.47 + following notice applies to you: 61.48 + 61.49 + You may obtain a copy of the Apache License at 61.50 + 61.51 + http://www.apache.org/licenses/LICENSE-2.0 61.52 + 61.53 + Unless required by applicable law or agreed to in writing, software 61.54 + distributed under the License is distributed on an "AS IS" BASIS, 61.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 61.56 + implied. See the License for the specific language governing 61.57 + permissions and limitations under the License. 61.58 + 61.59 + If you choose to use this file in compliance with the BSD License, the 61.60 + following notice applies to you: 61.61 + 61.62 + Redistribution and use in source and binary forms, with or without 61.63 + modification, are permitted provided that the following conditions are 61.64 + met: 61.65 + * Redistributions of source code must retain the above copyright 61.66 + notice, this list of conditions and the following disclaimer. 61.67 + * Redistributions in binary form must reproduce the above copyright 61.68 + notice, this list of conditions and the following disclaimer in the 61.69 + documentation and/or other materials provided with the distribution. 61.70 + * Neither the name of the copyright holder nor the names of 61.71 + contributors may be used to endorse or promote products derived from 61.72 + this software without specific prior written permission. 61.73 + 61.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 61.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 61.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 61.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 61.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 61.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 61.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 61.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 61.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 61.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61.85 +*/ 61.86 + 61.87 +package jdk.internal.dynalink.support; 61.88 + 61.89 +import jdk.internal.dynalink.CallSiteDescriptor; 61.90 + 61.91 +/** 61.92 + * Implements the name mangling and demangling as specified by John Rose's 61.93 + * <a href="https://blogs.oracle.com/jrose/entry/symbolic_freedom_in_the_vm" target="_blank">"Symbolic Freedom in the 61.94 + * VM"</a> article. It is recommended that implementers of languages on the JVM uniformly adopt this for symbolic 61.95 + * interoperability between languages. Normally, you would mangle the names as you're generating bytecode, and then 61.96 + * demangle them when you're creating {@link CallSiteDescriptor} objects. Note that you are expected to mangle 61.97 + * individual tokens, and not the whole name at the call site, i.e. the colon character normally separating the tokens 61.98 + * is never mangled. I.e. you wouldn't mangle {@code dyn:getProp:color} into {@code dyn\!getProp\!color}, but you would 61.99 + * mangle {@code dyn:getProp:color$} into {@code dyn:getProp:\=color\%} (only mangling the individual token containing 61.100 + * the symbol {@code color$}). {@link CallSiteDescriptorFactory#tokenizeName(String)} (and by implication, all call site 61.101 + * descriptors it creates) will automatically perform demangling on the passed names. If you use this factory, or you 61.102 + * have your own way of creating call site descriptors, but you still delegate to this method of the default factory 61.103 + * (it is recommended that you do), then you have demangling handled for you already, and only need to ensure that you 61.104 + * mangle the names when you're emitting them in the bytecode. 61.105 + * 61.106 + * @author Attila Szegedi 61.107 + */ 61.108 +public class NameCodec { 61.109 + private static final char ESCAPE_CHAR = '\\'; 61.110 + private static final char EMPTY_ESCAPE = '='; 61.111 + private static final String EMPTY_NAME = new String(new char[] { ESCAPE_CHAR, EMPTY_ESCAPE }); 61.112 + private static final char EMPTY_CHAR = 0xFEFF; 61.113 + 61.114 + private static final int MIN_ENCODING = '$'; 61.115 + private static final int MAX_ENCODING = ']'; 61.116 + private static final char[] ENCODING = new char[MAX_ENCODING - MIN_ENCODING + 1]; 61.117 + private static final int MIN_DECODING = '!'; 61.118 + private static final int MAX_DECODING = '}'; 61.119 + private static final char[] DECODING = new char[MAX_DECODING - MIN_DECODING + 1]; 61.120 + 61.121 + static { 61.122 + addEncoding('/', '|'); 61.123 + addEncoding('.', ','); 61.124 + addEncoding(';', '?'); 61.125 + addEncoding('$', '%'); 61.126 + addEncoding('<', '^'); 61.127 + addEncoding('>', '_'); 61.128 + addEncoding('[', '{'); 61.129 + addEncoding(']', '}'); 61.130 + addEncoding(':', '!'); 61.131 + addEncoding('\\', '-'); 61.132 + DECODING[EMPTY_ESCAPE - MIN_DECODING] = EMPTY_CHAR; 61.133 + } 61.134 + 61.135 + private NameCodec() { 61.136 + } 61.137 + 61.138 + /** 61.139 + * Encodes ("mangles") an unencoded symbolic name. 61.140 + * @param name the symbolic name to mangle 61.141 + * @return the mangled form of the symbolic name. 61.142 + */ 61.143 + public static String encode(String name) { 61.144 + final int l = name.length(); 61.145 + if(l == 0) { 61.146 + return EMPTY_NAME; 61.147 + } 61.148 + StringBuilder b = null; 61.149 + int lastEscape = -1; 61.150 + for(int i = 0; i < l; ++i) { 61.151 + final int encodeIndex = name.charAt(i) - MIN_ENCODING; 61.152 + if(encodeIndex >= 0 && encodeIndex < ENCODING.length) { 61.153 + final char e = ENCODING[encodeIndex]; 61.154 + if(e != 0) { 61.155 + if(b == null) { 61.156 + b = new StringBuilder(name.length() + 3); 61.157 + if(name.charAt(0) != ESCAPE_CHAR && i > 0) { 61.158 + b.append(EMPTY_NAME); 61.159 + } 61.160 + b.append(name, 0, i); 61.161 + } else { 61.162 + b.append(name, lastEscape + 1, i); 61.163 + } 61.164 + b.append(ESCAPE_CHAR).append(e); 61.165 + lastEscape = i; 61.166 + } 61.167 + } 61.168 + } 61.169 + if(b == null) { 61.170 + return name.toString(); 61.171 + } 61.172 + assert lastEscape != -1; 61.173 + b.append(name, lastEscape + 1, l); 61.174 + return b.toString(); 61.175 + } 61.176 + 61.177 + /** 61.178 + * Decodes ("demangles") an encoded symbolic name. 61.179 + * @param name the symbolic name to demangle 61.180 + * @return the demangled form of the symbolic name. 61.181 + */ 61.182 + public static String decode(String name) { 61.183 + if(name.charAt(0) != ESCAPE_CHAR) { 61.184 + return name; 61.185 + } 61.186 + final int l = name.length(); 61.187 + if(l == 2 && name.charAt(1) == EMPTY_CHAR) { 61.188 + return ""; 61.189 + } 61.190 + StringBuilder b = new StringBuilder(name.length()); 61.191 + int lastEscape = -2; 61.192 + int lastBackslash = -1; 61.193 + for(;;) { 61.194 + int nextBackslash = name.indexOf(ESCAPE_CHAR, lastBackslash + 1); 61.195 + if(nextBackslash == -1 || nextBackslash == l - 1) { 61.196 + break; 61.197 + } 61.198 + final int decodeIndex = name.charAt(nextBackslash + 1) - MIN_DECODING; 61.199 + if(decodeIndex >= 0 && decodeIndex < DECODING.length) { 61.200 + final char d = DECODING[decodeIndex]; 61.201 + if(d == EMPTY_CHAR) { 61.202 + // "\=" is only valid at the beginning of a mangled string 61.203 + if(nextBackslash == 0) { 61.204 + lastEscape = 0; 61.205 + } 61.206 + } else if(d != 0) { 61.207 + b.append(name, lastEscape + 2, nextBackslash).append(d); 61.208 + lastEscape = nextBackslash; 61.209 + } 61.210 + } 61.211 + lastBackslash = nextBackslash; 61.212 + } 61.213 + b.append(name, lastEscape + 2, l); 61.214 + return b.toString(); 61.215 + } 61.216 + 61.217 + private static void addEncoding(char from, char to) { 61.218 + ENCODING[from - MIN_ENCODING] = to; 61.219 + DECODING[to - MIN_DECODING] = from; 61.220 + } 61.221 +}
62.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 62.2 +++ b/src/jdk/internal/dynalink/support/NamedDynCallSiteDescriptor.java Thu Feb 14 13:22:26 2013 +0100 62.3 @@ -0,0 +1,118 @@ 62.4 +/* 62.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 62.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 62.7 + * 62.8 + * This code is free software; you can redistribute it and/or modify it 62.9 + * under the terms of the GNU General Public License version 2 only, as 62.10 + * published by the Free Software Foundation. Oracle designates this 62.11 + * particular file as subject to the "Classpath" exception as provided 62.12 + * by Oracle in the LICENSE file that accompanied this code. 62.13 + * 62.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 62.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 62.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 62.17 + * version 2 for more details (a copy is included in the LICENSE file that 62.18 + * accompanied this code). 62.19 + * 62.20 + * You should have received a copy of the GNU General Public License version 62.21 + * 2 along with this work; if not, write to the Free Software Foundation, 62.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 62.23 + * 62.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 62.25 + * or visit www.oracle.com if you need additional information or have any 62.26 + * questions. 62.27 + */ 62.28 + 62.29 +/* 62.30 + * This file is available under and governed by the GNU General Public 62.31 + * License version 2 only, as published by the Free Software Foundation. 62.32 + * However, the following notice accompanied the original version of this 62.33 + * file, and Oracle licenses the original version of this file under the BSD 62.34 + * license: 62.35 + */ 62.36 +/* 62.37 + Copyright 2009-2013 Attila Szegedi 62.38 + 62.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 62.40 + and the BSD License (the "BSD License"), with licensee being free to 62.41 + choose either of the two at their discretion. 62.42 + 62.43 + You may not use this file except in compliance with either the Apache 62.44 + License or the BSD License. 62.45 + 62.46 + If you choose to use this file in compliance with the Apache License, the 62.47 + following notice applies to you: 62.48 + 62.49 + You may obtain a copy of the Apache License at 62.50 + 62.51 + http://www.apache.org/licenses/LICENSE-2.0 62.52 + 62.53 + Unless required by applicable law or agreed to in writing, software 62.54 + distributed under the License is distributed on an "AS IS" BASIS, 62.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 62.56 + implied. See the License for the specific language governing 62.57 + permissions and limitations under the License. 62.58 + 62.59 + If you choose to use this file in compliance with the BSD License, the 62.60 + following notice applies to you: 62.61 + 62.62 + Redistribution and use in source and binary forms, with or without 62.63 + modification, are permitted provided that the following conditions are 62.64 + met: 62.65 + * Redistributions of source code must retain the above copyright 62.66 + notice, this list of conditions and the following disclaimer. 62.67 + * Redistributions in binary form must reproduce the above copyright 62.68 + notice, this list of conditions and the following disclaimer in the 62.69 + documentation and/or other materials provided with the distribution. 62.70 + * Neither the name of the copyright holder nor the names of 62.71 + contributors may be used to endorse or promote products derived from 62.72 + this software without specific prior written permission. 62.73 + 62.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 62.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 62.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 62.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 62.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 62.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 62.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 62.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 62.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 62.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62.85 +*/ 62.86 + 62.87 +package jdk.internal.dynalink.support; 62.88 + 62.89 +import java.lang.invoke.MethodType; 62.90 +import jdk.internal.dynalink.CallSiteDescriptor; 62.91 + 62.92 + 62.93 +class NamedDynCallSiteDescriptor extends UnnamedDynCallSiteDescriptor { 62.94 + private final String name; 62.95 + 62.96 + NamedDynCallSiteDescriptor(String op, String name, MethodType methodType) { 62.97 + super(op, methodType); 62.98 + this.name = name; 62.99 + } 62.100 + 62.101 + @Override 62.102 + public int getNameTokenCount() { 62.103 + return 3; 62.104 + } 62.105 + 62.106 + @Override 62.107 + public String getNameToken(int i) { 62.108 + switch(i) { 62.109 + case 0: return "dyn"; 62.110 + case 1: return getOp(); 62.111 + case 2: return name; 62.112 + default: throw new IndexOutOfBoundsException(String.valueOf(i)); 62.113 + } 62.114 + } 62.115 + 62.116 + @Override 62.117 + public CallSiteDescriptor changeMethodType(MethodType newMethodType) { 62.118 + return CallSiteDescriptorFactory.getCanonicalPublicDescriptor(new NamedDynCallSiteDescriptor(getOp(), name, 62.119 + newMethodType)); 62.120 + } 62.121 +}
63.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 63.2 +++ b/src/jdk/internal/dynalink/support/RuntimeContextLinkRequestImpl.java Thu Feb 14 13:22:26 2013 +0100 63.3 @@ -0,0 +1,142 @@ 63.4 +/* 63.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 63.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 63.7 + * 63.8 + * This code is free software; you can redistribute it and/or modify it 63.9 + * under the terms of the GNU General Public License version 2 only, as 63.10 + * published by the Free Software Foundation. Oracle designates this 63.11 + * particular file as subject to the "Classpath" exception as provided 63.12 + * by Oracle in the LICENSE file that accompanied this code. 63.13 + * 63.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 63.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 63.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 63.17 + * version 2 for more details (a copy is included in the LICENSE file that 63.18 + * accompanied this code). 63.19 + * 63.20 + * You should have received a copy of the GNU General Public License version 63.21 + * 2 along with this work; if not, write to the Free Software Foundation, 63.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 63.23 + * 63.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 63.25 + * or visit www.oracle.com if you need additional information or have any 63.26 + * questions. 63.27 + */ 63.28 + 63.29 +/* 63.30 + * This file is available under and governed by the GNU General Public 63.31 + * License version 2 only, as published by the Free Software Foundation. 63.32 + * However, the following notice accompanied the original version of this 63.33 + * file, and Oracle licenses the original version of this file under the BSD 63.34 + * license: 63.35 + */ 63.36 +/* 63.37 + Copyright 2009-2013 Attila Szegedi 63.38 + 63.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 63.40 + and the BSD License (the "BSD License"), with licensee being free to 63.41 + choose either of the two at their discretion. 63.42 + 63.43 + You may not use this file except in compliance with either the Apache 63.44 + License or the BSD License. 63.45 + 63.46 + If you choose to use this file in compliance with the Apache License, the 63.47 + following notice applies to you: 63.48 + 63.49 + You may obtain a copy of the Apache License at 63.50 + 63.51 + http://www.apache.org/licenses/LICENSE-2.0 63.52 + 63.53 + Unless required by applicable law or agreed to in writing, software 63.54 + distributed under the License is distributed on an "AS IS" BASIS, 63.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 63.56 + implied. See the License for the specific language governing 63.57 + permissions and limitations under the License. 63.58 + 63.59 + If you choose to use this file in compliance with the BSD License, the 63.60 + following notice applies to you: 63.61 + 63.62 + Redistribution and use in source and binary forms, with or without 63.63 + modification, are permitted provided that the following conditions are 63.64 + met: 63.65 + * Redistributions of source code must retain the above copyright 63.66 + notice, this list of conditions and the following disclaimer. 63.67 + * Redistributions in binary form must reproduce the above copyright 63.68 + notice, this list of conditions and the following disclaimer in the 63.69 + documentation and/or other materials provided with the distribution. 63.70 + * Neither the name of the copyright holder nor the names of 63.71 + contributors may be used to endorse or promote products derived from 63.72 + this software without specific prior written permission. 63.73 + 63.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 63.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 63.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 63.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 63.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 63.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 63.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 63.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 63.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 63.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 63.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 63.85 +*/ 63.86 + 63.87 +package jdk.internal.dynalink.support; 63.88 + 63.89 +import jdk.internal.dynalink.CallSiteDescriptor; 63.90 +import jdk.internal.dynalink.linker.LinkRequest; 63.91 + 63.92 +/** 63.93 + * A link request implementation for call sites that pass language runtime specific context arguments on the stack. The 63.94 + * context specific arguments should be the first "n" arguments. 63.95 + * 63.96 + * @author Attila Szegedi 63.97 + */ 63.98 +public class RuntimeContextLinkRequestImpl extends LinkRequestImpl { 63.99 + 63.100 + private final int runtimeContextArgCount; 63.101 + private LinkRequestImpl contextStrippedRequest; 63.102 + 63.103 + /** 63.104 + * Creates a new link request. 63.105 + * 63.106 + * @param callSiteDescriptor the descriptor for the call site being linked 63.107 + * @param arguments the arguments for the invocation 63.108 + * @param callSiteUnstable true if the call site being linked is considered unstable 63.109 + * @param runtimeContextArgCount the number of the leading arguments on the stack that represent the language 63.110 + * runtime specific context arguments. 63.111 + * @throws IllegalArgumentException if runtimeContextArgCount is less than 1. 63.112 + */ 63.113 + public RuntimeContextLinkRequestImpl(CallSiteDescriptor callSiteDescriptor, boolean callSiteUnstable, 63.114 + Object[] arguments, int runtimeContextArgCount) { 63.115 + super(callSiteDescriptor, callSiteUnstable, arguments); 63.116 + if(runtimeContextArgCount < 1) { 63.117 + throw new IllegalArgumentException("runtimeContextArgCount < 1"); 63.118 + } 63.119 + this.runtimeContextArgCount = runtimeContextArgCount; 63.120 + } 63.121 + 63.122 + @Override 63.123 + public LinkRequest withoutRuntimeContext() { 63.124 + if(contextStrippedRequest == null) { 63.125 + contextStrippedRequest = 63.126 + new LinkRequestImpl(CallSiteDescriptorFactory.dropParameterTypes(getCallSiteDescriptor(), 1, 63.127 + runtimeContextArgCount + 1), isCallSiteUnstable(), getTruncatedArguments()); 63.128 + } 63.129 + return contextStrippedRequest; 63.130 + } 63.131 + 63.132 + @Override 63.133 + public LinkRequest replaceArguments(CallSiteDescriptor callSiteDescriptor, Object[] arguments) { 63.134 + return new RuntimeContextLinkRequestImpl(callSiteDescriptor, isCallSiteUnstable(), arguments, 63.135 + runtimeContextArgCount); 63.136 + } 63.137 + 63.138 + private Object[] getTruncatedArguments() { 63.139 + final Object[] args = getArguments(); 63.140 + final Object[] newargs = new Object[args.length - runtimeContextArgCount]; 63.141 + newargs[0] = args[0]; // "this" remains at the 0th position 63.142 + System.arraycopy(args, runtimeContextArgCount + 1, newargs, 1, newargs.length - 1); 63.143 + return newargs; 63.144 + } 63.145 +} 63.146 \ No newline at end of file
64.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 64.2 +++ b/src/jdk/internal/dynalink/support/TypeConverterFactory.java Thu Feb 14 13:22:26 2013 +0100 64.3 @@ -0,0 +1,320 @@ 64.4 +/* 64.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 64.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 64.7 + * 64.8 + * This code is free software; you can redistribute it and/or modify it 64.9 + * under the terms of the GNU General Public License version 2 only, as 64.10 + * published by the Free Software Foundation. Oracle designates this 64.11 + * particular file as subject to the "Classpath" exception as provided 64.12 + * by Oracle in the LICENSE file that accompanied this code. 64.13 + * 64.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 64.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 64.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 64.17 + * version 2 for more details (a copy is included in the LICENSE file that 64.18 + * accompanied this code). 64.19 + * 64.20 + * You should have received a copy of the GNU General Public License version 64.21 + * 2 along with this work; if not, write to the Free Software Foundation, 64.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 64.23 + * 64.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 64.25 + * or visit www.oracle.com if you need additional information or have any 64.26 + * questions. 64.27 + */ 64.28 + 64.29 +/* 64.30 + * This file is available under and governed by the GNU General Public 64.31 + * License version 2 only, as published by the Free Software Foundation. 64.32 + * However, the following notice accompanied the original version of this 64.33 + * file, and Oracle licenses the original version of this file under the BSD 64.34 + * license: 64.35 + */ 64.36 +/* 64.37 + Copyright 2009-2013 Attila Szegedi 64.38 + 64.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 64.40 + and the BSD License (the "BSD License"), with licensee being free to 64.41 + choose either of the two at their discretion. 64.42 + 64.43 + You may not use this file except in compliance with either the Apache 64.44 + License or the BSD License. 64.45 + 64.46 + If you choose to use this file in compliance with the Apache License, the 64.47 + following notice applies to you: 64.48 + 64.49 + You may obtain a copy of the Apache License at 64.50 + 64.51 + http://www.apache.org/licenses/LICENSE-2.0 64.52 + 64.53 + Unless required by applicable law or agreed to in writing, software 64.54 + distributed under the License is distributed on an "AS IS" BASIS, 64.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 64.56 + implied. See the License for the specific language governing 64.57 + permissions and limitations under the License. 64.58 + 64.59 + If you choose to use this file in compliance with the BSD License, the 64.60 + following notice applies to you: 64.61 + 64.62 + Redistribution and use in source and binary forms, with or without 64.63 + modification, are permitted provided that the following conditions are 64.64 + met: 64.65 + * Redistributions of source code must retain the above copyright 64.66 + notice, this list of conditions and the following disclaimer. 64.67 + * Redistributions in binary form must reproduce the above copyright 64.68 + notice, this list of conditions and the following disclaimer in the 64.69 + documentation and/or other materials provided with the distribution. 64.70 + * Neither the name of the copyright holder nor the names of 64.71 + contributors may be used to endorse or promote products derived from 64.72 + this software without specific prior written permission. 64.73 + 64.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 64.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 64.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 64.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 64.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 64.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 64.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 64.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 64.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 64.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 64.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64.85 +*/ 64.86 + 64.87 +package jdk.internal.dynalink.support; 64.88 + 64.89 +import java.lang.invoke.MethodHandle; 64.90 +import java.lang.invoke.MethodHandles; 64.91 +import java.lang.invoke.MethodType; 64.92 +import java.lang.invoke.WrongMethodTypeException; 64.93 +import java.util.LinkedList; 64.94 +import java.util.List; 64.95 +import jdk.internal.dynalink.linker.ConversionComparator; 64.96 +import jdk.internal.dynalink.linker.ConversionComparator.Comparison; 64.97 +import jdk.internal.dynalink.linker.GuardedInvocation; 64.98 +import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; 64.99 +import jdk.internal.dynalink.linker.LinkerServices; 64.100 + 64.101 + 64.102 +/** 64.103 + * A factory for type converters. This class is the main implementation behind the 64.104 + * {@link LinkerServices#asType(MethodHandle, MethodType)}. It manages the known {@link GuardingTypeConverterFactory} 64.105 + * instances and creates appropriate converters for method handles. 64.106 + * 64.107 + * @author Attila Szegedi 64.108 + */ 64.109 +public class TypeConverterFactory { 64.110 + 64.111 + private final GuardingTypeConverterFactory[] factories; 64.112 + private final ConversionComparator[] comparators; 64.113 + 64.114 + private final ClassValue<ClassMap<MethodHandle>> converterMap = new ClassValue<ClassMap<MethodHandle>>() { 64.115 + @Override 64.116 + protected ClassMap<MethodHandle> computeValue(final Class<?> sourceType) { 64.117 + return new ClassMap<MethodHandle>(sourceType.getClassLoader()) { 64.118 + @Override 64.119 + protected MethodHandle computeValue(Class<?> targetType) { 64.120 + try { 64.121 + return createConverter(sourceType, targetType); 64.122 + } catch (RuntimeException e) { 64.123 + throw e; 64.124 + } catch (Exception e) { 64.125 + throw new RuntimeException(e); 64.126 + } 64.127 + } 64.128 + }; 64.129 + } 64.130 + }; 64.131 + 64.132 + private final ClassValue<ClassMap<MethodHandle>> converterIdentityMap = new ClassValue<ClassMap<MethodHandle>>() { 64.133 + @Override 64.134 + protected ClassMap<MethodHandle> computeValue(final Class<?> sourceType) { 64.135 + return new ClassMap<MethodHandle>(sourceType.getClassLoader()) { 64.136 + @Override 64.137 + protected MethodHandle computeValue(Class<?> targetType) { 64.138 + if(!canAutoConvert(sourceType, targetType)) { 64.139 + final MethodHandle converter = getTypeConverterNull(sourceType, targetType); 64.140 + if(converter != null) { 64.141 + return converter; 64.142 + } 64.143 + } 64.144 + return IDENTITY_CONVERSION.asType(MethodType.methodType(targetType, sourceType)); 64.145 + } 64.146 + }; 64.147 + } 64.148 + }; 64.149 + 64.150 + /** 64.151 + * Creates a new type converter factory from the available {@link GuardingTypeConverterFactory} instances. 64.152 + * 64.153 + * @param factories the {@link GuardingTypeConverterFactory} instances to compose. 64.154 + */ 64.155 + public TypeConverterFactory(Iterable<? extends GuardingTypeConverterFactory> factories) { 64.156 + final List<GuardingTypeConverterFactory> l = new LinkedList<>(); 64.157 + final List<ConversionComparator> c = new LinkedList<>(); 64.158 + for(GuardingTypeConverterFactory factory: factories) { 64.159 + l.add(factory); 64.160 + if(factory instanceof ConversionComparator) { 64.161 + c.add((ConversionComparator)factory); 64.162 + } 64.163 + } 64.164 + this.factories = l.toArray(new GuardingTypeConverterFactory[l.size()]); 64.165 + this.comparators = c.toArray(new ConversionComparator[c.size()]); 64.166 + 64.167 + } 64.168 + 64.169 + /** 64.170 + * Similar to {@link MethodHandle#asType(MethodType)} except it also hooks in method handles produced by 64.171 + * {@link GuardingTypeConverterFactory} implementations, providing for language-specific type coercing of 64.172 + * parameters. It will apply {@link MethodHandle#asType(MethodType)} for all primitive-to-primitive, 64.173 + * wrapper-to-primitive, primitive-to-wrapper conversions as well as for all upcasts. For all other conversions, 64.174 + * it'll insert {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)} with composite filters 64.175 + * provided by {@link GuardingTypeConverterFactory} implementations. 64.176 + * 64.177 + * @param handle target method handle 64.178 + * @param fromType the types of source arguments 64.179 + * @return a method handle that is a suitable combination of {@link MethodHandle#asType(MethodType)} and 64.180 + * {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)} with 64.181 + * {@link GuardingTypeConverterFactory} produced type converters as filters. 64.182 + */ 64.183 + public MethodHandle asType(MethodHandle handle, final MethodType fromType) { 64.184 + MethodHandle newHandle = handle; 64.185 + final MethodType toType = newHandle.type(); 64.186 + final int l = toType.parameterCount(); 64.187 + if(l != fromType.parameterCount()) { 64.188 + throw new WrongMethodTypeException("Parameter counts differ: " + handle.type() + " vs. " + fromType); 64.189 + } 64.190 + int pos = 0; 64.191 + final List<MethodHandle> converters = new LinkedList<>(); 64.192 + for(int i = 0; i < l; ++i) { 64.193 + final Class<?> fromParamType = fromType.parameterType(i); 64.194 + final Class<?> toParamType = toType.parameterType(i); 64.195 + if(canAutoConvert(fromParamType, toParamType)) { 64.196 + newHandle = applyConverters(newHandle, pos, converters); 64.197 + } else { 64.198 + final MethodHandle converter = getTypeConverterNull(fromParamType, toParamType); 64.199 + if(converter != null) { 64.200 + if(converters.isEmpty()) { 64.201 + pos = i; 64.202 + } 64.203 + converters.add(converter); 64.204 + } else { 64.205 + newHandle = applyConverters(newHandle, pos, converters); 64.206 + } 64.207 + } 64.208 + } 64.209 + newHandle = applyConverters(newHandle, pos, converters); 64.210 + 64.211 + // Convert return type 64.212 + final Class<?> fromRetType = fromType.returnType(); 64.213 + final Class<?> toRetType = toType.returnType(); 64.214 + if(fromRetType != Void.TYPE && toRetType != Void.TYPE) { 64.215 + if(!canAutoConvert(toRetType, fromRetType)) { 64.216 + final MethodHandle converter = getTypeConverterNull(toRetType, fromRetType); 64.217 + if(converter != null) { 64.218 + newHandle = MethodHandles.filterReturnValue(newHandle, converter); 64.219 + } 64.220 + } 64.221 + } 64.222 + 64.223 + // Take care of automatic conversions 64.224 + return newHandle.asType(fromType); 64.225 + } 64.226 + 64.227 + private static MethodHandle applyConverters(MethodHandle handle, int pos, List<MethodHandle> converters) { 64.228 + if(converters.isEmpty()) { 64.229 + return handle; 64.230 + } 64.231 + final MethodHandle newHandle = 64.232 + MethodHandles.filterArguments(handle, pos, converters.toArray(new MethodHandle[converters.size()])); 64.233 + converters.clear(); 64.234 + return newHandle; 64.235 + } 64.236 + 64.237 + /** 64.238 + * Returns true if there might exist a conversion between the requested types (either an automatic JVM conversion, 64.239 + * or one provided by any available {@link GuardingTypeConverterFactory}), or false if there definitely does not 64.240 + * exist a conversion between the requested types. Note that returning true does not guarantee that the conversion 64.241 + * will succeed at runtime (notably, if the "from" or "to" types are sufficiently generic), but returning false 64.242 + * guarantees that it would fail. 64.243 + * 64.244 + * @param from the source type for the conversion 64.245 + * @param to the target type for the conversion 64.246 + * @return true if there can be a conversion, false if there can not. 64.247 + */ 64.248 + public boolean canConvert(final Class<?> from, final Class<?> to) { 64.249 + return canAutoConvert(from, to) || getTypeConverterNull(from, to) != null; 64.250 + } 64.251 + 64.252 + /** 64.253 + * Determines which of the two type conversions from a source type to the two target types is preferred. This is 64.254 + * used for dynamic overloaded method resolution. If the source type is convertible to exactly one target type with 64.255 + * a method invocation conversion, it is chosen, otherwise available {@link ConversionComparator}s are consulted. 64.256 + * @param sourceType the source type. 64.257 + * @param targetType1 one potential target type 64.258 + * @param targetType2 another potential target type. 64.259 + * @return one of Comparison constants that establish which - if any - of the target types is preferable for the 64.260 + * conversion. 64.261 + */ 64.262 + public Comparison compareConversion(Class<?> sourceType, Class<?> targetType1, Class<?> targetType2) { 64.263 + for(ConversionComparator comparator: comparators) { 64.264 + final Comparison result = comparator.compareConversion(sourceType, targetType1, targetType2); 64.265 + if(result != Comparison.INDETERMINATE) { 64.266 + return result; 64.267 + } 64.268 + } 64.269 + if(TypeUtilities.isMethodInvocationConvertible(sourceType, targetType1)) { 64.270 + if(!TypeUtilities.isMethodInvocationConvertible(sourceType, targetType2)) { 64.271 + return Comparison.TYPE_1_BETTER; 64.272 + } 64.273 + } else if(TypeUtilities.isMethodInvocationConvertible(sourceType, targetType2)) { 64.274 + return Comparison.TYPE_2_BETTER; 64.275 + } 64.276 + return Comparison.INDETERMINATE; 64.277 + } 64.278 + 64.279 + /** 64.280 + * Determines whether it's safe to perform an automatic conversion between the source and target class. 64.281 + * 64.282 + * @param fromType convert from this class 64.283 + * @param toType convert to this class 64.284 + * @return true if it's safe to let MethodHandles.convertArguments() to handle this conversion. 64.285 + */ 64.286 + /*private*/ static boolean canAutoConvert(final Class<?> fromType, final Class<?> toType) { 64.287 + return TypeUtilities.isMethodInvocationConvertible(fromType, toType); 64.288 + } 64.289 + 64.290 + /*private*/ MethodHandle getTypeConverterNull(Class<?> sourceType, Class<?> targetType) { 64.291 + final MethodHandle converter = converterMap.get(sourceType).get(targetType); 64.292 + return converter == IDENTITY_CONVERSION ? null : converter; 64.293 + } 64.294 + 64.295 + /** 64.296 + * Given a source and target type, returns a method handle that converts between them. Never returns null; in worst 64.297 + * case it will return an identity conversion (that might fail for some values at runtime). You can use this method 64.298 + * if you have a piece of your program that is written in Java, and you need to reuse existing type conversion 64.299 + * machinery in a non-invokedynamic context. 64.300 + * @param sourceType the type to convert from 64.301 + * @param targetType the type to convert to 64.302 + * @return a method handle performing the conversion. 64.303 + */ 64.304 + public MethodHandle getTypeConverter(Class<?> sourceType, Class<?> targetType) { 64.305 + return converterIdentityMap.get(sourceType).get(targetType); 64.306 + } 64.307 + 64.308 + /*private*/ MethodHandle createConverter(Class<?> sourceType, Class<?> targetType) throws Exception { 64.309 + final MethodType type = MethodType.methodType(targetType, sourceType); 64.310 + final MethodHandle identity = IDENTITY_CONVERSION.asType(type); 64.311 + MethodHandle last = identity; 64.312 + for(int i = factories.length; i-- > 0;) { 64.313 + final GuardedInvocation next = factories[i].convertToType(sourceType, targetType); 64.314 + if(next != null) { 64.315 + next.assertType(type); 64.316 + last = next.compose(last); 64.317 + } 64.318 + } 64.319 + return last == identity ? IDENTITY_CONVERSION : last; 64.320 + } 64.321 + 64.322 + /*private*/ static final MethodHandle IDENTITY_CONVERSION = MethodHandles.identity(Object.class); 64.323 +} 64.324 \ No newline at end of file
65.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 65.2 +++ b/src/jdk/internal/dynalink/support/TypeUtilities.java Thu Feb 14 13:22:26 2013 +0100 65.3 @@ -0,0 +1,442 @@ 65.4 +/* 65.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 65.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 65.7 + * 65.8 + * This code is free software; you can redistribute it and/or modify it 65.9 + * under the terms of the GNU General Public License version 2 only, as 65.10 + * published by the Free Software Foundation. Oracle designates this 65.11 + * particular file as subject to the "Classpath" exception as provided 65.12 + * by Oracle in the LICENSE file that accompanied this code. 65.13 + * 65.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 65.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 65.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 65.17 + * version 2 for more details (a copy is included in the LICENSE file that 65.18 + * accompanied this code). 65.19 + * 65.20 + * You should have received a copy of the GNU General Public License version 65.21 + * 2 along with this work; if not, write to the Free Software Foundation, 65.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 65.23 + * 65.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 65.25 + * or visit www.oracle.com if you need additional information or have any 65.26 + * questions. 65.27 + */ 65.28 + 65.29 +/* 65.30 + * This file is available under and governed by the GNU General Public 65.31 + * License version 2 only, as published by the Free Software Foundation. 65.32 + * However, the following notice accompanied the original version of this 65.33 + * file, and Oracle licenses the original version of this file under the BSD 65.34 + * license: 65.35 + */ 65.36 +/* 65.37 + Copyright 2009-2013 Attila Szegedi 65.38 + 65.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 65.40 + and the BSD License (the "BSD License"), with licensee being free to 65.41 + choose either of the two at their discretion. 65.42 + 65.43 + You may not use this file except in compliance with either the Apache 65.44 + License or the BSD License. 65.45 + 65.46 + If you choose to use this file in compliance with the Apache License, the 65.47 + following notice applies to you: 65.48 + 65.49 + You may obtain a copy of the Apache License at 65.50 + 65.51 + http://www.apache.org/licenses/LICENSE-2.0 65.52 + 65.53 + Unless required by applicable law or agreed to in writing, software 65.54 + distributed under the License is distributed on an "AS IS" BASIS, 65.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 65.56 + implied. See the License for the specific language governing 65.57 + permissions and limitations under the License. 65.58 + 65.59 + If you choose to use this file in compliance with the BSD License, the 65.60 + following notice applies to you: 65.61 + 65.62 + Redistribution and use in source and binary forms, with or without 65.63 + modification, are permitted provided that the following conditions are 65.64 + met: 65.65 + * Redistributions of source code must retain the above copyright 65.66 + notice, this list of conditions and the following disclaimer. 65.67 + * Redistributions in binary form must reproduce the above copyright 65.68 + notice, this list of conditions and the following disclaimer in the 65.69 + documentation and/or other materials provided with the distribution. 65.70 + * Neither the name of the copyright holder nor the names of 65.71 + contributors may be used to endorse or promote products derived from 65.72 + this software without specific prior written permission. 65.73 + 65.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 65.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 65.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 65.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 65.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 65.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 65.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 65.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 65.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 65.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 65.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 65.85 +*/ 65.86 + 65.87 +package jdk.internal.dynalink.support; 65.88 + 65.89 +import java.util.ArrayList; 65.90 +import java.util.Collection; 65.91 +import java.util.Collections; 65.92 +import java.util.HashMap; 65.93 +import java.util.HashSet; 65.94 +import java.util.IdentityHashMap; 65.95 +import java.util.Iterator; 65.96 +import java.util.List; 65.97 +import java.util.Map; 65.98 +import java.util.Set; 65.99 + 65.100 +/** 65.101 + * Various static utility methods for testing type relationships. 65.102 + * 65.103 + * @author Attila Szegedi 65.104 + */ 65.105 +public class TypeUtilities { 65.106 + static final Class<Object> OBJECT_CLASS = Object.class; 65.107 + 65.108 + private TypeUtilities() { 65.109 + } 65.110 + 65.111 + /** 65.112 + * Given two types represented by c1 and c2, returns a type that is their most specific common superclass or 65.113 + * superinterface. 65.114 + * 65.115 + * @param c1 one type 65.116 + * @param c2 another type 65.117 + * @return their most common superclass or superinterface. If they have several unrelated superinterfaces as their 65.118 + * most specific common type, or the types themselves are completely unrelated interfaces, {@link java.lang.Object} 65.119 + * is returned. 65.120 + */ 65.121 + public static Class<?> getMostSpecificCommonType(Class<?> c1, Class<?> c2) { 65.122 + if(c1 == c2) { 65.123 + return c1; 65.124 + } 65.125 + Class<?> c3 = c2; 65.126 + if(c3.isPrimitive()) { 65.127 + if(c3 == Byte.TYPE) 65.128 + c3 = Byte.class; 65.129 + else if(c3 == Short.TYPE) 65.130 + c3 = Short.class; 65.131 + else if(c3 == Character.TYPE) 65.132 + c3 = Character.class; 65.133 + else if(c3 == Integer.TYPE) 65.134 + c3 = Integer.class; 65.135 + else if(c3 == Float.TYPE) 65.136 + c3 = Float.class; 65.137 + else if(c3 == Long.TYPE) 65.138 + c3 = Long.class; 65.139 + else if(c3 == Double.TYPE) 65.140 + c3 = Double.class; 65.141 + } 65.142 + Set<Class<?>> a1 = getAssignables(c1, c3); 65.143 + Set<Class<?>> a2 = getAssignables(c3, c1); 65.144 + a1.retainAll(a2); 65.145 + if(a1.isEmpty()) { 65.146 + // Can happen when at least one of the arguments is an interface, 65.147 + // as they don't have Object at the root of their hierarchy. 65.148 + return Object.class; 65.149 + } 65.150 + // Gather maximally specific elements. Yes, there can be more than one 65.151 + // thank to interfaces. I.e., if you call this method for String.class 65.152 + // and Number.class, you'll have Comparable, Serializable, and Object 65.153 + // as maximal elements. 65.154 + List<Class<?>> max = new ArrayList<>(); 65.155 + outer: for(Class<?> clazz: a1) { 65.156 + for(Iterator<Class<?>> maxiter = max.iterator(); maxiter.hasNext();) { 65.157 + Class<?> maxClazz = maxiter.next(); 65.158 + if(isSubtype(maxClazz, clazz)) { 65.159 + // It can't be maximal, if there's already a more specific 65.160 + // maximal than it. 65.161 + continue outer; 65.162 + } 65.163 + if(isSubtype(clazz, maxClazz)) { 65.164 + // If it's more specific than a currently maximal element, 65.165 + // that currently maximal is no longer a maximal. 65.166 + maxiter.remove(); 65.167 + } 65.168 + } 65.169 + // If we get here, no current maximal is more specific than the 65.170 + // current class, so it is considered maximal as well 65.171 + max.add(clazz); 65.172 + } 65.173 + if(max.size() > 1) { 65.174 + return OBJECT_CLASS; 65.175 + } 65.176 + return max.get(0); 65.177 + } 65.178 + 65.179 + private static Set<Class<?>> getAssignables(Class<?> c1, Class<?> c2) { 65.180 + Set<Class<?>> s = new HashSet<>(); 65.181 + collectAssignables(c1, c2, s); 65.182 + return s; 65.183 + } 65.184 + 65.185 + private static void collectAssignables(Class<?> c1, Class<?> c2, Set<Class<?>> s) { 65.186 + if(c1.isAssignableFrom(c2)) { 65.187 + s.add(c1); 65.188 + } 65.189 + Class<?> sc = c1.getSuperclass(); 65.190 + if(sc != null) { 65.191 + collectAssignables(sc, c2, s); 65.192 + } 65.193 + Class<?>[] itf = c1.getInterfaces(); 65.194 + for(int i = 0; i < itf.length; ++i) { 65.195 + collectAssignables(itf[i], c2, s); 65.196 + } 65.197 + } 65.198 + 65.199 + private static final Map<Class<?>, Class<?>> WRAPPER_TYPES = createWrapperTypes(); 65.200 + private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = invertMap(WRAPPER_TYPES); 65.201 + private static final Map<String, Class<?>> PRIMITIVE_TYPES_BY_NAME = createClassNameMapping(WRAPPER_TYPES.keySet()); 65.202 + 65.203 + private static Map<Class<?>, Class<?>> createWrapperTypes() { 65.204 + final Map<Class<?>, Class<?>> wrapperTypes = new IdentityHashMap<>(8); 65.205 + wrapperTypes.put(Boolean.TYPE, Boolean.class); 65.206 + wrapperTypes.put(Byte.TYPE, Byte.class); 65.207 + wrapperTypes.put(Character.TYPE, Character.class); 65.208 + wrapperTypes.put(Short.TYPE, Short.class); 65.209 + wrapperTypes.put(Integer.TYPE, Integer.class); 65.210 + wrapperTypes.put(Long.TYPE, Long.class); 65.211 + wrapperTypes.put(Float.TYPE, Float.class); 65.212 + wrapperTypes.put(Double.TYPE, Double.class); 65.213 + return Collections.unmodifiableMap(wrapperTypes); 65.214 + } 65.215 + 65.216 + private static Map<String, Class<?>> createClassNameMapping(Collection<Class<?>> classes) { 65.217 + final Map<String, Class<?>> map = new HashMap<>(); 65.218 + for(Class<?> clazz: classes) { 65.219 + map.put(clazz.getName(), clazz); 65.220 + } 65.221 + return map; 65.222 + } 65.223 + 65.224 + private static <K, V> Map<V, K> invertMap(Map<K, V> map) { 65.225 + final Map<V, K> inverted = new IdentityHashMap<>(map.size()); 65.226 + for(Map.Entry<K, V> entry: map.entrySet()) { 65.227 + inverted.put(entry.getValue(), entry.getKey()); 65.228 + } 65.229 + return Collections.unmodifiableMap(inverted); 65.230 + } 65.231 + 65.232 + /** 65.233 + * Determines whether one type can be converted to another type using a method invocation conversion, as per JLS 5.3 65.234 + * "Method Invocation Conversion". This is basically all conversions allowed by subtyping (see 65.235 + * {@link #isSubtype(Class, Class)}) as well as boxing conversion (JLS 5.1.7) optionally followed by widening 65.236 + * reference conversion and unboxing conversion (JLS 5.1.8) optionally followed by widening primitive conversion. 65.237 + * 65.238 + * @param callSiteType the parameter type at the call site 65.239 + * @param methodType the parameter type in the method declaration 65.240 + * @return true if callSiteType is method invocation convertible to the methodType. 65.241 + */ 65.242 + public static boolean isMethodInvocationConvertible(Class<?> callSiteType, Class<?> methodType) { 65.243 + if(methodType.isAssignableFrom(callSiteType)) { 65.244 + return true; 65.245 + } 65.246 + if(callSiteType.isPrimitive()) { 65.247 + if(methodType.isPrimitive()) { 65.248 + return isProperPrimitiveSubtype(callSiteType, methodType); 65.249 + } 65.250 + // Boxing + widening reference conversion 65.251 + return methodType.isAssignableFrom(WRAPPER_TYPES.get(callSiteType)); 65.252 + } 65.253 + if(methodType.isPrimitive()) { 65.254 + final Class<?> unboxedCallSiteType = PRIMITIVE_TYPES.get(callSiteType); 65.255 + return unboxedCallSiteType != null 65.256 + && (unboxedCallSiteType == methodType || isProperPrimitiveSubtype(unboxedCallSiteType, methodType)); 65.257 + } 65.258 + return false; 65.259 + } 65.260 + 65.261 + /** 65.262 + * Determines whether one type can be potentially converted to another type at runtime. Allows a conversion between 65.263 + * any subtype and supertype in either direction, and also allows a conversion between any two primitive types, as 65.264 + * well as between any primitive type and any reference type that can hold a boxed primitive. 65.265 + * 65.266 + * @param callSiteType the parameter type at the call site 65.267 + * @param methodType the parameter type in the method declaration 65.268 + * @return true if callSiteType is potentially convertible to the methodType. 65.269 + */ 65.270 + public static boolean isPotentiallyConvertible(Class<?> callSiteType, Class<?> methodType) { 65.271 + // Widening or narrowing reference conversion 65.272 + if(methodType.isAssignableFrom(callSiteType) || callSiteType.isAssignableFrom(methodType)) { 65.273 + return true; 65.274 + } 65.275 + if(callSiteType.isPrimitive()) { 65.276 + // Allow any conversion among primitives, as well as from any 65.277 + // primitive to any type that can receive a boxed primitive. 65.278 + // TODO: narrow this a bit, i.e. allow, say, boolean to Character? 65.279 + // MethodHandles.convertArguments() allows it, so we might need to 65.280 + // too. 65.281 + return methodType.isPrimitive() || isAssignableFromBoxedPrimitive(methodType); 65.282 + } 65.283 + if(methodType.isPrimitive()) { 65.284 + // Allow conversion from any reference type that can contain a 65.285 + // boxed primitive to any primitive. 65.286 + // TODO: narrow this a bit too? 65.287 + return isAssignableFromBoxedPrimitive(callSiteType); 65.288 + } 65.289 + return false; 65.290 + } 65.291 + 65.292 + /** 65.293 + * Determines whether one type is a subtype of another type, as per JLS 4.10 "Subtyping". Note: this is not strict 65.294 + * or proper subtype, therefore true is also returned for identical types; to be completely precise, it allows 65.295 + * identity conversion (JLS 5.1.1), widening primitive conversion (JLS 5.1.2) and widening reference conversion (JLS 65.296 + * 5.1.5). 65.297 + * 65.298 + * @param subType the supposed subtype 65.299 + * @param superType the supposed supertype of the subtype 65.300 + * @return true if subType can be converted by identity conversion, widening primitive conversion, or widening 65.301 + * reference conversion to superType. 65.302 + */ 65.303 + public static boolean isSubtype(Class<?> subType, Class<?> superType) { 65.304 + // Covers both JLS 4.10.2 "Subtyping among Class and Interface Types" 65.305 + // and JLS 4.10.3 "Subtyping among Array Types", as well as primitive 65.306 + // type identity. 65.307 + if(superType.isAssignableFrom(subType)) { 65.308 + return true; 65.309 + } 65.310 + // JLS 4.10.1 "Subtyping among Primitive Types". Note we don't test for 65.311 + // identity, as identical types were taken care of in the 65.312 + // isAssignableFrom test. As per 4.10.1, the supertype relation is as 65.313 + // follows: 65.314 + // double > float 65.315 + // float > long 65.316 + // long > int 65.317 + // int > short 65.318 + // int > char 65.319 + // short > byte 65.320 + if(superType.isPrimitive() && subType.isPrimitive()) { 65.321 + return isProperPrimitiveSubtype(subType, superType); 65.322 + } 65.323 + return false; 65.324 + } 65.325 + 65.326 + /** 65.327 + * Returns true if a supposed primitive subtype is a proper subtype ( meaning, subtype and not identical) of the 65.328 + * supposed primitive supertype 65.329 + * 65.330 + * @param subType the supposed subtype 65.331 + * @param superType the supposed supertype 65.332 + * @return true if subType is a proper (not identical to) primitive subtype of the superType 65.333 + */ 65.334 + private static boolean isProperPrimitiveSubtype(Class<?> subType, Class<?> superType) { 65.335 + if(superType == boolean.class || subType == boolean.class) { 65.336 + return false; 65.337 + } 65.338 + if(subType == byte.class) { 65.339 + return superType != char.class; 65.340 + } 65.341 + if(subType == char.class) { 65.342 + return superType != short.class && superType != byte.class; 65.343 + } 65.344 + if(subType == short.class) { 65.345 + return superType != char.class && superType != byte.class; 65.346 + } 65.347 + if(subType == int.class) { 65.348 + return superType == long.class || superType == float.class || superType == double.class; 65.349 + } 65.350 + if(subType == long.class) { 65.351 + return superType == float.class || superType == double.class; 65.352 + } 65.353 + if(subType == float.class) { 65.354 + return superType == double.class; 65.355 + } 65.356 + return false; 65.357 + } 65.358 + 65.359 + private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPES = createWrapperToPrimitiveTypes(); 65.360 + 65.361 + private static Map<Class<?>, Class<?>> createWrapperToPrimitiveTypes() { 65.362 + final Map<Class<?>, Class<?>> classes = new IdentityHashMap<>(); 65.363 + classes.put(Void.class, Void.TYPE); 65.364 + classes.put(Boolean.class, Boolean.TYPE); 65.365 + classes.put(Byte.class, Byte.TYPE); 65.366 + classes.put(Character.class, Character.TYPE); 65.367 + classes.put(Short.class, Short.TYPE); 65.368 + classes.put(Integer.class, Integer.TYPE); 65.369 + classes.put(Long.class, Long.TYPE); 65.370 + classes.put(Float.class, Float.TYPE); 65.371 + classes.put(Double.class, Double.TYPE); 65.372 + return classes; 65.373 + } 65.374 + 65.375 + private static final Set<Class<?>> PRIMITIVE_WRAPPER_TYPES = createPrimitiveWrapperTypes(); 65.376 + 65.377 + private static Set<Class<?>> createPrimitiveWrapperTypes() { 65.378 + final Map<Class<?>, Class<?>> classes = new IdentityHashMap<>(); 65.379 + addClassHierarchy(classes, Boolean.class); 65.380 + addClassHierarchy(classes, Byte.class); 65.381 + addClassHierarchy(classes, Character.class); 65.382 + addClassHierarchy(classes, Short.class); 65.383 + addClassHierarchy(classes, Integer.class); 65.384 + addClassHierarchy(classes, Long.class); 65.385 + addClassHierarchy(classes, Float.class); 65.386 + addClassHierarchy(classes, Double.class); 65.387 + return classes.keySet(); 65.388 + } 65.389 + 65.390 + private static void addClassHierarchy(Map<Class<?>, Class<?>> map, Class<?> clazz) { 65.391 + if(clazz == null) { 65.392 + return; 65.393 + } 65.394 + map.put(clazz, clazz); 65.395 + addClassHierarchy(map, clazz.getSuperclass()); 65.396 + for(Class<?> itf: clazz.getInterfaces()) { 65.397 + addClassHierarchy(map, itf); 65.398 + } 65.399 + } 65.400 + 65.401 + /** 65.402 + * Returns true if the class can be assigned from any boxed primitive. 65.403 + * 65.404 + * @param clazz the class 65.405 + * @return true if the class can be assigned from any boxed primitive. Basically, it is true if the class is any 65.406 + * primitive wrapper class, or a superclass or superinterface of any primitive wrapper class. 65.407 + */ 65.408 + private static boolean isAssignableFromBoxedPrimitive(Class<?> clazz) { 65.409 + return PRIMITIVE_WRAPPER_TYPES.contains(clazz); 65.410 + } 65.411 + 65.412 + /** 65.413 + * Given a name of a primitive type (except "void"), returns the class representing it. I.e. when invoked with 65.414 + * "int", returns {@link Integer#TYPE}. 65.415 + * @param name the name of the primitive type 65.416 + * @return the class representing the primitive type, or null if the name does not correspond to a primitive type 65.417 + * or is "void". 65.418 + */ 65.419 + public static Class<?> getPrimitiveTypeByName(String name) { 65.420 + return PRIMITIVE_TYPES_BY_NAME.get(name); 65.421 + } 65.422 + 65.423 + /** 65.424 + * When passed a class representing a wrapper for a primitive type, returns the class representing the corresponding 65.425 + * primitive type. I.e. calling it with {@code Integer.class} will return {@code Integer.TYPE}. If passed a class 65.426 + * that is not a wrapper for primitive type, returns null. 65.427 + * @param wrapperType the class object representing a wrapper for a primitive type 65.428 + * @return the class object representing the primitive type, or null if the passed class is not a primitive wrapper. 65.429 + */ 65.430 + public static Class<?> getPrimitiveType(Class<?> wrapperType) { 65.431 + return WRAPPER_TO_PRIMITIVE_TYPES.get(wrapperType); 65.432 + } 65.433 + 65.434 + 65.435 + /** 65.436 + * When passed a class representing a primitive type, returns the class representing the corresponding 65.437 + * wrapper type. I.e. calling it with {@code int.class} will return {@code Integer.class}. If passed a class 65.438 + * that is not a primitive type, returns null. 65.439 + * @param primitiveType the class object representing a primitive type 65.440 + * @return the class object representing the wrapper type, or null if the passed class is not a primitive. 65.441 + */ 65.442 + public static Class<?> getWrapperType(Class<?> primitiveType) { 65.443 + return WRAPPER_TYPES.get(primitiveType); 65.444 + } 65.445 +} 65.446 \ No newline at end of file
66.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 66.2 +++ b/src/jdk/internal/dynalink/support/UnnamedDynCallSiteDescriptor.java Thu Feb 14 13:22:26 2013 +0100 66.3 @@ -0,0 +1,127 @@ 66.4 +/* 66.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 66.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 66.7 + * 66.8 + * This code is free software; you can redistribute it and/or modify it 66.9 + * under the terms of the GNU General Public License version 2 only, as 66.10 + * published by the Free Software Foundation. Oracle designates this 66.11 + * particular file as subject to the "Classpath" exception as provided 66.12 + * by Oracle in the LICENSE file that accompanied this code. 66.13 + * 66.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 66.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 66.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 66.17 + * version 2 for more details (a copy is included in the LICENSE file that 66.18 + * accompanied this code). 66.19 + * 66.20 + * You should have received a copy of the GNU General Public License version 66.21 + * 2 along with this work; if not, write to the Free Software Foundation, 66.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 66.23 + * 66.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 66.25 + * or visit www.oracle.com if you need additional information or have any 66.26 + * questions. 66.27 + */ 66.28 + 66.29 +/* 66.30 + * This file is available under and governed by the GNU General Public 66.31 + * License version 2 only, as published by the Free Software Foundation. 66.32 + * However, the following notice accompanied the original version of this 66.33 + * file, and Oracle licenses the original version of this file under the BSD 66.34 + * license: 66.35 + */ 66.36 +/* 66.37 + Copyright 2009-2013 Attila Szegedi 66.38 + 66.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 66.40 + and the BSD License (the "BSD License"), with licensee being free to 66.41 + choose either of the two at their discretion. 66.42 + 66.43 + You may not use this file except in compliance with either the Apache 66.44 + License or the BSD License. 66.45 + 66.46 + If you choose to use this file in compliance with the Apache License, the 66.47 + following notice applies to you: 66.48 + 66.49 + You may obtain a copy of the Apache License at 66.50 + 66.51 + http://www.apache.org/licenses/LICENSE-2.0 66.52 + 66.53 + Unless required by applicable law or agreed to in writing, software 66.54 + distributed under the License is distributed on an "AS IS" BASIS, 66.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 66.56 + implied. See the License for the specific language governing 66.57 + permissions and limitations under the License. 66.58 + 66.59 + If you choose to use this file in compliance with the BSD License, the 66.60 + following notice applies to you: 66.61 + 66.62 + Redistribution and use in source and binary forms, with or without 66.63 + modification, are permitted provided that the following conditions are 66.64 + met: 66.65 + * Redistributions of source code must retain the above copyright 66.66 + notice, this list of conditions and the following disclaimer. 66.67 + * Redistributions in binary form must reproduce the above copyright 66.68 + notice, this list of conditions and the following disclaimer in the 66.69 + documentation and/or other materials provided with the distribution. 66.70 + * Neither the name of the copyright holder nor the names of 66.71 + contributors may be used to endorse or promote products derived from 66.72 + this software without specific prior written permission. 66.73 + 66.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 66.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 66.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 66.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 66.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 66.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 66.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 66.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 66.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 66.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 66.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 66.85 +*/ 66.86 + 66.87 +package jdk.internal.dynalink.support; 66.88 + 66.89 +import java.lang.invoke.MethodType; 66.90 +import jdk.internal.dynalink.CallSiteDescriptor; 66.91 + 66.92 + 66.93 +class UnnamedDynCallSiteDescriptor extends AbstractCallSiteDescriptor { 66.94 + private final MethodType methodType; 66.95 + private final String op; 66.96 + 66.97 + UnnamedDynCallSiteDescriptor(String op, MethodType methodType) { 66.98 + this.op = op; 66.99 + this.methodType = methodType; 66.100 + } 66.101 + 66.102 + @Override 66.103 + public int getNameTokenCount() { 66.104 + return 2; 66.105 + } 66.106 + 66.107 + String getOp() { 66.108 + return op; 66.109 + } 66.110 + 66.111 + @Override 66.112 + public String getNameToken(int i) { 66.113 + switch(i) { 66.114 + case 0: return "dyn"; 66.115 + case 1: return op; 66.116 + default: throw new IndexOutOfBoundsException(String.valueOf(i)); 66.117 + } 66.118 + } 66.119 + 66.120 + @Override 66.121 + public MethodType getMethodType() { 66.122 + return methodType; 66.123 + } 66.124 + 66.125 + @Override 66.126 + public CallSiteDescriptor changeMethodType(MethodType newMethodType) { 66.127 + return CallSiteDescriptorFactory.getCanonicalPublicDescriptor(new UnnamedDynCallSiteDescriptor(op, 66.128 + newMethodType)); 66.129 + } 66.130 +}
67.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 67.2 +++ b/src/jdk/internal/dynalink/support/messages.properties Thu Feb 14 13:22:26 2013 +0100 67.3 @@ -0,0 +1,86 @@ 67.4 +# Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 67.5 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 67.6 +# 67.7 +# This code is free software; you can redistribute it and/or modify it 67.8 +# under the terms of the GNU General Public License version 2 only, as 67.9 +# published by the Free Software Foundation. Oracle designates this 67.10 +# particular file as subject to the "Classpath" exception as provided 67.11 +# by Oracle in the LICENSE file that accompanied this code. 67.12 +# 67.13 +# This code is distributed in the hope that it will be useful, but WITHOUT 67.14 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 67.15 +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 67.16 +# version 2 for more details (a copy is included in the LICENSE file that 67.17 +# accompanied this code). 67.18 +# 67.19 +# You should have received a copy of the GNU General Public License version 67.20 +# 2 along with this work; if not, write to the Free Software Foundation, 67.21 +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 67.22 +# 67.23 +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 67.24 +# or visit www.oracle.com if you need additional information or have any 67.25 +# questions. 67.26 + 67.27 +# This file is available under and governed by the GNU General Public 67.28 +# License version 2 only, as published by the Free Software Foundation. 67.29 +# However, the following notice accompanied the original version of this 67.30 +# file, and Oracle licenses the original version of this file under the BSD 67.31 +# license: 67.32 +# 67.33 +# Copyright 2009-2013 Attila Szegedi 67.34 +# 67.35 +# Licensed under both the Apache License, Version 2.0 (the "Apache License") 67.36 +# and the BSD License (the "BSD License"), with licensee being free to 67.37 +# choose either of the two at their discretion. 67.38 +# 67.39 +# You may not use this file except in compliance with either the Apache 67.40 +# License or the BSD License. 67.41 +# 67.42 +# If you choose to use this file in compliance with the Apache License, the 67.43 +# following notice applies to you: 67.44 +# 67.45 +# You may obtain a copy of the Apache License at 67.46 +# 67.47 +# http://www.apache.org/licenses/LICENSE-2.0 67.48 +# 67.49 +# Unless required by applicable law or agreed to in writing, software 67.50 +# distributed under the License is distributed on an "AS IS" BASIS, 67.51 +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 67.52 +# implied. See the License for the specific language governing 67.53 +# permissions and limitations under the License. 67.54 +# 67.55 +# If you choose to use this file in compliance with the BSD License, the 67.56 +# following notice applies to you: 67.57 +# 67.58 +# Redistribution and use in source and binary forms, with or without 67.59 +# modification, are permitted provided that the following conditions are 67.60 +# met: 67.61 +# * Redistributions of source code must retain the above copyright 67.62 +# notice, this list of conditions and the following disclaimer. 67.63 +# * Redistributions in binary form must reproduce the above copyright 67.64 +# notice, this list of conditions and the following disclaimer in the 67.65 +# documentation and/or other materials provided with the distribution. 67.66 +# * Neither the name of the copyright holder nor the names of 67.67 +# contributors may be used to endorse or promote products derived from 67.68 +# this software without specific prior written permission. 67.69 +# 67.70 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 67.71 +# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 67.72 +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 67.73 +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 67.74 +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 67.75 +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 67.76 +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 67.77 +# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 67.78 +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 67.79 +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 67.80 +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 67.81 + 67.82 +isInstanceGuardAlwaysTrue=isInstance guard for {0} in position {1} in method type {2} will always return true 67.83 +isInstanceGuardAlwaysFalse=isInstance guard for {0} in position {1} in method type {2} will always return false 67.84 + 67.85 +isOfClassGuardAlwaysTrue=isOfClass guard for {0} in position {1} in method type {2} will always return true 67.86 +isOfClassGuardAlwaysFalse=isOfClass guard for {0} in position {1} in method type {2} will always return false 67.87 + 67.88 +isArrayGuardAlwaysTrue=isArray guard in position {0} in method type {1} will always return true 67.89 +isArrayGuardAlwaysFalse=isArray guard in position {0} in method type {1} will always return false 67.90 \ No newline at end of file
68.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 68.2 +++ b/src/jdk/internal/dynalink/support/package.html Thu Feb 14 13:22:26 2013 +0100 68.3 @@ -0,0 +1,89 @@ 68.4 +<!-- 68.5 + Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 68.6 + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 68.7 + 68.8 + This code is free software; you can redistribute it and/or modify it 68.9 + under the terms of the GNU General Public License version 2 only, as 68.10 + published by the Free Software Foundation. Oracle designates this 68.11 + particular file as subject to the "Classpath" exception as provided 68.12 + by Oracle in the LICENSE file that accompanied this code. 68.13 + 68.14 + This code is distributed in the hope that it will be useful, but WITHOUT 68.15 + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 68.16 + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 68.17 + version 2 for more details (a copy is included in the LICENSE file that 68.18 + accompanied this code). 68.19 + 68.20 + You should have received a copy of the GNU General Public License version 68.21 + 2 along with this work; if not, write to the Free Software Foundation, 68.22 + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 68.23 + 68.24 + Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 68.25 + or visit www.oracle.com if you need additional information or have any 68.26 + questions. 68.27 +--> 68.28 + 68.29 +<!-- 68.30 + This file is available under and governed by the GNU General Public 68.31 + License version 2 only, as published by the Free Software Foundation. 68.32 + However, the following notice accompanied the original version of this 68.33 + file, and Oracle licenses the original version of this file under the BSD 68.34 + license: 68.35 + 68.36 + Copyright 2009-2013 Attila Szegedi 68.37 + 68.38 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 68.39 + and the BSD License (the "BSD License"), with licensee being free to 68.40 + choose either of the two at their discretion. 68.41 + 68.42 + You may not use this file except in compliance with either the Apache 68.43 + License or the BSD License. 68.44 + 68.45 + If you choose to use this file in compliance with the Apache License, the 68.46 + following notice applies to you: 68.47 + 68.48 + You may obtain a copy of the Apache License at 68.49 + 68.50 + http://www.apache.org/licenses/LICENSE-2.0 68.51 + 68.52 + Unless required by applicable law or agreed to in writing, software 68.53 + distributed under the License is distributed on an "AS IS" BASIS, 68.54 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 68.55 + implied. See the License for the specific language governing 68.56 + permissions and limitations under the License. 68.57 + 68.58 + If you choose to use this file in compliance with the BSD License, the 68.59 + following notice applies to you: 68.60 + 68.61 + Redistribution and use in source and binary forms, with or without 68.62 + modification, are permitted provided that the following conditions are 68.63 + met: 68.64 + * Redistributions of source code must retain the above copyright 68.65 + notice, this list of conditions and the following disclaimer. 68.66 + * Redistributions in binary form must reproduce the above copyright 68.67 + notice, this list of conditions and the following disclaimer in the 68.68 + documentation and/or other materials provided with the distribution. 68.69 + * Neither the name of the copyright holder nor the names of 68.70 + contributors may be used to endorse or promote products derived from 68.71 + this software without specific prior written permission. 68.72 + 68.73 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 68.74 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 68.75 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 68.76 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 68.77 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 68.78 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 68.79 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 68.80 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 68.81 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 68.82 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 68.83 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 68.84 +--> 68.85 + <body> 68.86 + <p> 68.87 + Contains supporting classes for other packages. There is no guarantee that 68.88 + any of these classes or interfaces will not change in a manner that breaks 68.89 + backwards compatibility; they are not considered to be part of the public 68.90 + API. 68.91 + </p> 68.92 + </body>
69.1 --- a/src/jdk/nashorn/internal/codegen/Compiler.java Thu Feb 14 13:01:52 2013 +0100 69.2 +++ b/src/jdk/nashorn/internal/codegen/Compiler.java Thu Feb 14 13:22:26 2013 +0100 69.3 @@ -31,6 +31,7 @@ 69.4 import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE; 69.5 import static jdk.nashorn.internal.codegen.CompilerConstants.SOURCE; 69.6 import static jdk.nashorn.internal.codegen.CompilerConstants.THIS; 69.7 + 69.8 import java.io.File; 69.9 import java.util.Arrays; 69.10 import java.util.EnumSet; 69.11 @@ -40,6 +41,7 @@ 69.12 import java.util.Map; 69.13 import java.util.Map.Entry; 69.14 import java.util.Set; 69.15 +import jdk.internal.dynalink.support.NameCodec; 69.16 import jdk.nashorn.internal.codegen.ClassEmitter.Flag; 69.17 import jdk.nashorn.internal.codegen.types.Type; 69.18 import jdk.nashorn.internal.ir.FunctionNode; 69.19 @@ -49,7 +51,6 @@ 69.20 import jdk.nashorn.internal.runtime.DebugLogger; 69.21 import jdk.nashorn.internal.runtime.Source; 69.22 import jdk.nashorn.internal.runtime.options.Options; 69.23 -import org.dynalang.dynalink.support.NameCodec; 69.24 69.25 /** 69.26 * Responsible for converting JavaScripts to java byte code. Main entry
70.1 --- a/src/jdk/nashorn/internal/codegen/MethodEmitter.java Thu Feb 14 13:01:52 2013 +0100 70.2 +++ b/src/jdk/nashorn/internal/codegen/MethodEmitter.java Thu Feb 14 13:22:26 2013 +0100 70.3 @@ -71,6 +71,7 @@ 70.4 import java.util.ArrayDeque; 70.5 import java.util.EnumSet; 70.6 import java.util.Iterator; 70.7 +import jdk.internal.dynalink.support.NameCodec; 70.8 import jdk.internal.org.objectweb.asm.Handle; 70.9 import jdk.internal.org.objectweb.asm.MethodVisitor; 70.10 import jdk.nashorn.internal.codegen.ClassEmitter.Flag; 70.11 @@ -94,7 +95,6 @@ 70.12 import jdk.nashorn.internal.runtime.ScriptObject; 70.13 import jdk.nashorn.internal.runtime.linker.Bootstrap; 70.14 import jdk.nashorn.internal.runtime.options.Options; 70.15 -import org.dynalang.dynalink.support.NameCodec; 70.16 70.17 /** 70.18 * This is the main function responsible for emitting method code
71.1 --- a/src/jdk/nashorn/internal/codegen/WeighNodes.java Thu Feb 14 13:01:52 2013 +0100 71.2 +++ b/src/jdk/nashorn/internal/codegen/WeighNodes.java Thu Feb 14 13:22:26 2013 +0100 71.3 @@ -27,7 +27,6 @@ 71.4 71.5 import java.util.List; 71.6 import java.util.Map; 71.7 - 71.8 import jdk.nashorn.internal.codegen.types.Type; 71.9 import jdk.nashorn.internal.ir.AccessNode; 71.10 import jdk.nashorn.internal.ir.BinaryNode;
72.1 --- a/src/jdk/nashorn/internal/ir/Block.java Thu Feb 14 13:01:52 2013 +0100 72.2 +++ b/src/jdk/nashorn/internal/ir/Block.java Thu Feb 14 13:22:26 2013 +0100 72.3 @@ -39,7 +39,6 @@ 72.4 import java.util.Comparator; 72.5 import java.util.HashMap; 72.6 import java.util.List; 72.7 - 72.8 import jdk.nashorn.internal.codegen.Frame; 72.9 import jdk.nashorn.internal.codegen.MethodEmitter.Label; 72.10 import jdk.nashorn.internal.ir.annotations.Ignore;
73.1 --- a/src/jdk/nashorn/internal/ir/CallNode.java Thu Feb 14 13:01:52 2013 +0100 73.2 +++ b/src/jdk/nashorn/internal/ir/CallNode.java Thu Feb 14 13:22:26 2013 +0100 73.3 @@ -28,7 +28,6 @@ 73.4 import java.util.ArrayList; 73.5 import java.util.Collections; 73.6 import java.util.List; 73.7 - 73.8 import jdk.nashorn.internal.codegen.types.Type; 73.9 import jdk.nashorn.internal.ir.annotations.Ignore; 73.10 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
74.1 --- a/src/jdk/nashorn/internal/ir/Node.java Thu Feb 14 13:01:52 2013 +0100 74.2 +++ b/src/jdk/nashorn/internal/ir/Node.java Thu Feb 14 13:22:26 2013 +0100 74.3 @@ -26,7 +26,6 @@ 74.4 package jdk.nashorn.internal.ir; 74.5 74.6 import java.util.IdentityHashMap; 74.7 - 74.8 import jdk.nashorn.internal.codegen.types.Type; 74.9 import jdk.nashorn.internal.ir.visitor.NodeVisitor; 74.10 import jdk.nashorn.internal.parser.Token;
75.1 --- a/src/jdk/nashorn/internal/ir/Symbol.java Thu Feb 14 13:01:52 2013 +0100 75.2 +++ b/src/jdk/nashorn/internal/ir/Symbol.java Thu Feb 14 13:22:26 2013 +0100 75.3 @@ -29,7 +29,6 @@ 75.4 import java.util.HashSet; 75.5 import java.util.Set; 75.6 import java.util.StringTokenizer; 75.7 - 75.8 import jdk.nashorn.internal.codegen.types.Type; 75.9 import jdk.nashorn.internal.runtime.Context; 75.10 import jdk.nashorn.internal.runtime.options.Options;
76.1 --- a/src/jdk/nashorn/internal/objects/Global.java Thu Feb 14 13:01:52 2013 +0100 76.2 +++ b/src/jdk/nashorn/internal/objects/Global.java Thu Feb 14 13:22:26 2013 +0100 76.3 @@ -37,6 +37,8 @@ 76.4 import java.util.LinkedHashMap; 76.5 import java.util.List; 76.6 import java.util.Map; 76.7 +import jdk.internal.dynalink.linker.GuardedInvocation; 76.8 +import jdk.internal.dynalink.linker.LinkRequest; 76.9 import jdk.nashorn.internal.objects.annotations.Attribute; 76.10 import jdk.nashorn.internal.objects.annotations.Property; 76.11 import jdk.nashorn.internal.objects.annotations.ScriptClass; 76.12 @@ -57,8 +59,6 @@ 76.13 import jdk.nashorn.internal.runtime.Source; 76.14 import jdk.nashorn.internal.runtime.linker.InvokeByName; 76.15 import jdk.nashorn.internal.scripts.JO$; 76.16 -import org.dynalang.dynalink.linker.GuardedInvocation; 76.17 -import org.dynalang.dynalink.linker.LinkRequest; 76.18 76.19 /** 76.20 * Representation of global scope.
77.1 --- a/src/jdk/nashorn/internal/objects/NativeBoolean.java Thu Feb 14 13:01:52 2013 +0100 77.2 +++ b/src/jdk/nashorn/internal/objects/NativeBoolean.java Thu Feb 14 13:22:26 2013 +0100 77.3 @@ -30,6 +30,8 @@ 77.4 77.5 import java.lang.invoke.MethodHandle; 77.6 import java.lang.invoke.MethodHandles; 77.7 +import jdk.internal.dynalink.linker.GuardedInvocation; 77.8 +import jdk.internal.dynalink.linker.LinkRequest; 77.9 import jdk.nashorn.internal.objects.annotations.Attribute; 77.10 import jdk.nashorn.internal.objects.annotations.Constructor; 77.11 import jdk.nashorn.internal.objects.annotations.Function; 77.12 @@ -39,8 +41,6 @@ 77.13 import jdk.nashorn.internal.runtime.ScriptRuntime; 77.14 import jdk.nashorn.internal.runtime.linker.MethodHandleFactory; 77.15 import jdk.nashorn.internal.runtime.linker.PrimitiveLookup; 77.16 -import org.dynalang.dynalink.linker.GuardedInvocation; 77.17 -import org.dynalang.dynalink.linker.LinkRequest; 77.18 77.19 /** 77.20 * ECMA 15.6 Boolean Objects.
78.1 --- a/src/jdk/nashorn/internal/objects/NativeJSAdapter.java Thu Feb 14 13:01:52 2013 +0100 78.2 +++ b/src/jdk/nashorn/internal/objects/NativeJSAdapter.java Thu Feb 14 13:22:26 2013 +0100 78.3 @@ -35,6 +35,9 @@ 78.4 import java.util.ArrayList; 78.5 import java.util.Iterator; 78.6 import java.util.List; 78.7 +import jdk.internal.dynalink.CallSiteDescriptor; 78.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 78.9 +import jdk.internal.dynalink.linker.LinkRequest; 78.10 import jdk.nashorn.internal.objects.annotations.Constructor; 78.11 import jdk.nashorn.internal.objects.annotations.ScriptClass; 78.12 import jdk.nashorn.internal.runtime.FindProperty; 78.13 @@ -45,9 +48,6 @@ 78.14 import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator; 78.15 import jdk.nashorn.internal.runtime.linker.Lookup; 78.16 import jdk.nashorn.internal.runtime.linker.MethodHandleFactory; 78.17 -import org.dynalang.dynalink.CallSiteDescriptor; 78.18 -import org.dynalang.dynalink.linker.GuardedInvocation; 78.19 -import org.dynalang.dynalink.linker.LinkRequest; 78.20 78.21 /** 78.22 * This class is the implementation of the Nashorn-specific global object named {@code JSAdapter}. It can be
79.1 --- a/src/jdk/nashorn/internal/objects/NativeJava.java Thu Feb 14 13:01:52 2013 +0100 79.2 +++ b/src/jdk/nashorn/internal/objects/NativeJava.java Thu Feb 14 13:22:26 2013 +0100 79.3 @@ -30,6 +30,8 @@ 79.4 79.5 import java.lang.reflect.Array; 79.6 import java.util.Collection; 79.7 +import jdk.internal.dynalink.beans.StaticClass; 79.8 +import jdk.internal.dynalink.support.TypeUtilities; 79.9 import jdk.nashorn.internal.objects.annotations.Attribute; 79.10 import jdk.nashorn.internal.objects.annotations.Function; 79.11 import jdk.nashorn.internal.objects.annotations.ScriptClass; 79.12 @@ -37,8 +39,6 @@ 79.13 import jdk.nashorn.internal.runtime.JSType; 79.14 import jdk.nashorn.internal.runtime.ScriptObject; 79.15 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; 79.16 -import org.dynalang.dynalink.beans.StaticClass; 79.17 -import org.dynalang.dynalink.support.TypeUtilities; 79.18 79.19 /** 79.20 * This class is the implementation for the {@code Java} global object exposed to programs running under Nashorn. This
80.1 --- a/src/jdk/nashorn/internal/objects/NativeJavaImporter.java Thu Feb 14 13:01:52 2013 +0100 80.2 +++ b/src/jdk/nashorn/internal/objects/NativeJavaImporter.java Thu Feb 14 13:22:26 2013 +0100 80.3 @@ -25,16 +25,16 @@ 80.4 80.5 package jdk.nashorn.internal.objects; 80.6 80.7 +import jdk.internal.dynalink.CallSiteDescriptor; 80.8 +import jdk.internal.dynalink.beans.StaticClass; 80.9 +import jdk.internal.dynalink.linker.GuardedInvocation; 80.10 +import jdk.internal.dynalink.linker.LinkRequest; 80.11 import jdk.nashorn.internal.objects.annotations.Attribute; 80.12 import jdk.nashorn.internal.objects.annotations.Constructor; 80.13 import jdk.nashorn.internal.objects.annotations.Function; 80.14 import jdk.nashorn.internal.objects.annotations.ScriptClass; 80.15 import jdk.nashorn.internal.runtime.NativeJavaPackage; 80.16 import jdk.nashorn.internal.runtime.ScriptObject; 80.17 -import org.dynalang.dynalink.CallSiteDescriptor; 80.18 -import org.dynalang.dynalink.beans.StaticClass; 80.19 -import org.dynalang.dynalink.linker.GuardedInvocation; 80.20 -import org.dynalang.dynalink.linker.LinkRequest; 80.21 80.22 /** 80.23 * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names.
81.1 --- a/src/jdk/nashorn/internal/objects/NativeNumber.java Thu Feb 14 13:01:52 2013 +0100 81.2 +++ b/src/jdk/nashorn/internal/objects/NativeNumber.java Thu Feb 14 13:22:26 2013 +0100 81.3 @@ -36,6 +36,8 @@ 81.4 import java.lang.invoke.MethodHandles; 81.5 import java.text.NumberFormat; 81.6 import java.util.Locale; 81.7 +import jdk.internal.dynalink.linker.GuardedInvocation; 81.8 +import jdk.internal.dynalink.linker.LinkRequest; 81.9 import jdk.nashorn.internal.objects.annotations.Attribute; 81.10 import jdk.nashorn.internal.objects.annotations.Constructor; 81.11 import jdk.nashorn.internal.objects.annotations.Function; 81.12 @@ -47,8 +49,6 @@ 81.13 import jdk.nashorn.internal.runtime.ScriptRuntime; 81.14 import jdk.nashorn.internal.runtime.linker.MethodHandleFactory; 81.15 import jdk.nashorn.internal.runtime.linker.PrimitiveLookup; 81.16 -import org.dynalang.dynalink.linker.GuardedInvocation; 81.17 -import org.dynalang.dynalink.linker.LinkRequest; 81.18 81.19 /** 81.20 * ECMA 15.7 Number Objects.
82.1 --- a/src/jdk/nashorn/internal/objects/NativeString.java Thu Feb 14 13:01:52 2013 +0100 82.2 +++ b/src/jdk/nashorn/internal/objects/NativeString.java Thu Feb 14 13:22:26 2013 +0100 82.3 @@ -39,6 +39,9 @@ 82.4 import java.util.LinkedList; 82.5 import java.util.List; 82.6 import java.util.regex.Pattern; 82.7 +import jdk.internal.dynalink.CallSiteDescriptor; 82.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 82.9 +import jdk.internal.dynalink.linker.LinkRequest; 82.10 import jdk.nashorn.internal.objects.annotations.Attribute; 82.11 import jdk.nashorn.internal.objects.annotations.Constructor; 82.12 import jdk.nashorn.internal.objects.annotations.Function; 82.13 @@ -56,9 +59,6 @@ 82.14 import jdk.nashorn.internal.runtime.linker.MethodHandleFactory; 82.15 import jdk.nashorn.internal.runtime.linker.NashornGuards; 82.16 import jdk.nashorn.internal.runtime.linker.PrimitiveLookup; 82.17 -import org.dynalang.dynalink.CallSiteDescriptor; 82.18 -import org.dynalang.dynalink.linker.GuardedInvocation; 82.19 -import org.dynalang.dynalink.linker.LinkRequest; 82.20 82.21 82.22 /**
83.1 --- a/src/jdk/nashorn/internal/parser/Lexer.java Thu Feb 14 13:01:52 2013 +0100 83.2 +++ b/src/jdk/nashorn/internal/parser/Lexer.java Thu Feb 14 13:22:26 2013 +0100 83.3 @@ -31,6 +31,7 @@ 83.4 import static jdk.nashorn.internal.parser.TokenType.EOL; 83.5 import static jdk.nashorn.internal.parser.TokenType.ERROR; 83.6 import static jdk.nashorn.internal.parser.TokenType.ESCSTRING; 83.7 +import static jdk.nashorn.internal.parser.TokenType.EXECSTRING; 83.8 import static jdk.nashorn.internal.parser.TokenType.FLOATING; 83.9 import static jdk.nashorn.internal.parser.TokenType.HEXADECIMAL; 83.10 import static jdk.nashorn.internal.parser.TokenType.LBRACE; 83.11 @@ -40,7 +41,6 @@ 83.12 import static jdk.nashorn.internal.parser.TokenType.REGEX; 83.13 import static jdk.nashorn.internal.parser.TokenType.RPAREN; 83.14 import static jdk.nashorn.internal.parser.TokenType.STRING; 83.15 -import static jdk.nashorn.internal.parser.TokenType.EXECSTRING; 83.16 import static jdk.nashorn.internal.parser.TokenType.XML; 83.17 83.18 import jdk.nashorn.internal.runtime.ECMAErrors;
84.1 --- a/src/jdk/nashorn/internal/runtime/GlobalObject.java Thu Feb 14 13:01:52 2013 +0100 84.2 +++ b/src/jdk/nashorn/internal/runtime/GlobalObject.java Thu Feb 14 13:22:26 2013 +0100 84.3 @@ -26,8 +26,8 @@ 84.4 package jdk.nashorn.internal.runtime; 84.5 84.6 import java.lang.invoke.MethodHandle; 84.7 -import org.dynalang.dynalink.linker.GuardedInvocation; 84.8 -import org.dynalang.dynalink.linker.LinkRequest; 84.9 +import jdk.internal.dynalink.linker.GuardedInvocation; 84.10 +import jdk.internal.dynalink.linker.LinkRequest; 84.11 84.12 /** 84.13 * Runtime interface to the global scope of the current context.
85.1 --- a/src/jdk/nashorn/internal/runtime/JSType.java Thu Feb 14 13:01:52 2013 +0100 85.2 +++ b/src/jdk/nashorn/internal/runtime/JSType.java Thu Feb 14 13:22:26 2013 +0100 85.3 @@ -28,9 +28,9 @@ 85.4 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall; 85.5 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 85.6 85.7 +import jdk.internal.dynalink.beans.StaticClass; 85.8 import jdk.nashorn.internal.codegen.CompilerConstants.Call; 85.9 import jdk.nashorn.internal.parser.Lexer; 85.10 -import org.dynalang.dynalink.beans.StaticClass; 85.11 85.12 /** 85.13 * Representation for ECMAScript types - this maps directly to the ECMA script standard
86.1 --- a/src/jdk/nashorn/internal/runtime/NativeJavaPackage.java Thu Feb 14 13:01:52 2013 +0100 86.2 +++ b/src/jdk/nashorn/internal/runtime/NativeJavaPackage.java Thu Feb 14 13:22:26 2013 +0100 86.3 @@ -25,13 +25,13 @@ 86.4 86.5 package jdk.nashorn.internal.runtime; 86.6 86.7 +import jdk.internal.dynalink.CallSiteDescriptor; 86.8 +import jdk.internal.dynalink.beans.StaticClass; 86.9 +import jdk.internal.dynalink.linker.GuardedInvocation; 86.10 +import jdk.internal.dynalink.linker.LinkRequest; 86.11 import jdk.nashorn.internal.objects.NativeJava; 86.12 import jdk.nashorn.internal.objects.annotations.Attribute; 86.13 import jdk.nashorn.internal.objects.annotations.Function; 86.14 -import org.dynalang.dynalink.CallSiteDescriptor; 86.15 -import org.dynalang.dynalink.beans.StaticClass; 86.16 -import org.dynalang.dynalink.linker.GuardedInvocation; 86.17 -import org.dynalang.dynalink.linker.LinkRequest; 86.18 86.19 /** 86.20 * An object that exposes Java packages and classes as its properties. Packages are exposed as objects that have further
87.1 --- a/src/jdk/nashorn/internal/runtime/ScriptFunction.java Thu Feb 14 13:01:52 2013 +0100 87.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptFunction.java Thu Feb 14 13:22:26 2013 +0100 87.3 @@ -33,14 +33,14 @@ 87.4 import java.lang.invoke.MethodHandle; 87.5 import java.lang.invoke.MethodHandles; 87.6 import java.lang.invoke.MethodType; 87.7 +import jdk.internal.dynalink.CallSiteDescriptor; 87.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 87.9 +import jdk.internal.dynalink.linker.LinkRequest; 87.10 import jdk.nashorn.internal.codegen.CompilerConstants.Call; 87.11 import jdk.nashorn.internal.objects.annotations.SpecializedFunction; 87.12 import jdk.nashorn.internal.runtime.linker.MethodHandleFactory; 87.13 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 87.14 import jdk.nashorn.internal.runtime.linker.NashornGuards; 87.15 -import org.dynalang.dynalink.CallSiteDescriptor; 87.16 -import org.dynalang.dynalink.linker.GuardedInvocation; 87.17 -import org.dynalang.dynalink.linker.LinkRequest; 87.18 87.19 /** 87.20 * Runtime representation of a JavaScript function.
88.1 --- a/src/jdk/nashorn/internal/runtime/ScriptLoader.java Thu Feb 14 13:01:52 2013 +0100 88.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptLoader.java Thu Feb 14 13:22:26 2013 +0100 88.3 @@ -39,6 +39,12 @@ 88.4 super(parent, context); 88.5 } 88.6 88.7 + @Override 88.8 + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { 88.9 + checkPackageAccess(name); 88.10 + return super.loadClassTrusted(name, resolve); 88.11 + } 88.12 + 88.13 // package-private and private stuff below this point 88.14 88.15 /**
89.1 --- a/src/jdk/nashorn/internal/runtime/ScriptObject.java Thu Feb 14 13:01:52 2013 +0100 89.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java Thu Feb 14 13:22:26 2013 +0100 89.3 @@ -55,6 +55,10 @@ 89.4 import java.util.List; 89.5 import java.util.Map; 89.6 import java.util.Set; 89.7 +import jdk.internal.dynalink.CallSiteDescriptor; 89.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 89.9 +import jdk.internal.dynalink.linker.LinkRequest; 89.10 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 89.11 import jdk.nashorn.internal.codegen.CompilerConstants.Call; 89.12 import jdk.nashorn.internal.codegen.objects.ObjectClassGenerator; 89.13 import jdk.nashorn.internal.objects.AccessorPropertyDescriptor; 89.14 @@ -65,10 +69,6 @@ 89.15 import jdk.nashorn.internal.runtime.linker.MethodHandleFactory; 89.16 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 89.17 import jdk.nashorn.internal.runtime.linker.NashornGuards; 89.18 -import org.dynalang.dynalink.CallSiteDescriptor; 89.19 -import org.dynalang.dynalink.linker.GuardedInvocation; 89.20 -import org.dynalang.dynalink.linker.LinkRequest; 89.21 -import org.dynalang.dynalink.support.CallSiteDescriptorFactory; 89.22 89.23 /** 89.24 * Base class for generic JavaScript objects.
90.1 --- a/src/jdk/nashorn/internal/runtime/ScriptRuntime.java Thu Feb 14 13:01:52 2013 +0100 90.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptRuntime.java Thu Feb 14 13:22:26 2013 +0100 90.3 @@ -38,11 +38,11 @@ 90.4 import java.util.Iterator; 90.5 import java.util.NoSuchElementException; 90.6 import java.util.Objects; 90.7 +import jdk.internal.dynalink.beans.StaticClass; 90.8 import jdk.nashorn.internal.codegen.CompilerConstants.Call; 90.9 import jdk.nashorn.internal.ir.debug.JSONWriter; 90.10 import jdk.nashorn.internal.parser.Lexer; 90.11 import jdk.nashorn.internal.runtime.linker.Bootstrap; 90.12 -import org.dynalang.dynalink.beans.StaticClass; 90.13 90.14 90.15 /**
91.1 --- a/src/jdk/nashorn/internal/runtime/SetMethodCreator.java Thu Feb 14 13:01:52 2013 +0100 91.2 +++ b/src/jdk/nashorn/internal/runtime/SetMethodCreator.java Thu Feb 14 13:22:26 2013 +0100 91.3 @@ -29,18 +29,17 @@ 91.4 import static jdk.nashorn.internal.runtime.linker.Lookup.MH; 91.5 91.6 import java.lang.invoke.MethodHandle; 91.7 - 91.8 +import jdk.internal.dynalink.CallSiteDescriptor; 91.9 +import jdk.internal.dynalink.linker.GuardedInvocation; 91.10 import jdk.nashorn.internal.codegen.objects.ObjectClassGenerator; 91.11 import jdk.nashorn.internal.runtime.linker.Lookup; 91.12 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 91.13 import jdk.nashorn.internal.runtime.linker.NashornGuards; 91.14 91.15 -import org.dynalang.dynalink.CallSiteDescriptor; 91.16 -import org.dynalang.dynalink.linker.GuardedInvocation; 91.17 91.18 /** 91.19 * Instances of this class are quite ephemeral; they only exist for the duration of an invocation of 91.20 - * {@link ScriptObject#findSetMethod(CallSiteDescriptor, org.dynalang.dynalink.linker.LinkRequest)} and 91.21 + * {@link ScriptObject#findSetMethod(CallSiteDescriptor, jdk.internal.dynalink.linker.LinkRequest)} and 91.22 * serve as the actual encapsulation of the algorithm for creating an appropriate property setter method. 91.23 */ 91.24 final class SetMethodCreator {
92.1 --- a/src/jdk/nashorn/internal/runtime/StructureLoader.java Thu Feb 14 13:01:52 2013 +0100 92.2 +++ b/src/jdk/nashorn/internal/runtime/StructureLoader.java Thu Feb 14 13:22:26 2013 +0100 92.3 @@ -58,8 +58,6 @@ 92.4 92.5 @Override 92.6 protected synchronized Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException { 92.7 - checkPackageAccess(name); 92.8 - 92.9 // check the cache first 92.10 final Class<?> loadedClass = findLoadedClass(name); 92.11 if (loadedClass != null) {
93.1 --- a/src/jdk/nashorn/internal/runtime/Undefined.java Thu Feb 14 13:01:52 2013 +0100 93.2 +++ b/src/jdk/nashorn/internal/runtime/Undefined.java Thu Feb 14 13:22:26 2013 +0100 93.3 @@ -31,10 +31,10 @@ 93.4 import java.lang.invoke.MethodHandle; 93.5 import java.lang.invoke.MethodHandles; 93.6 import java.lang.invoke.MethodType; 93.7 -import org.dynalang.dynalink.CallSiteDescriptor; 93.8 -import org.dynalang.dynalink.linker.GuardedInvocation; 93.9 -import org.dynalang.dynalink.support.CallSiteDescriptorFactory; 93.10 -import org.dynalang.dynalink.support.Guards; 93.11 +import jdk.internal.dynalink.CallSiteDescriptor; 93.12 +import jdk.internal.dynalink.linker.GuardedInvocation; 93.13 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 93.14 +import jdk.internal.dynalink.support.Guards; 93.15 93.16 /** 93.17 * Unique instance of this class is used to represent JavaScript undefined.
94.1 --- a/src/jdk/nashorn/internal/runtime/WithObject.java Thu Feb 14 13:01:52 2013 +0100 94.2 +++ b/src/jdk/nashorn/internal/runtime/WithObject.java Thu Feb 14 13:22:26 2013 +0100 94.3 @@ -29,11 +29,11 @@ 94.4 94.5 import java.lang.invoke.MethodHandle; 94.6 import java.lang.invoke.MethodHandles; 94.7 +import jdk.internal.dynalink.CallSiteDescriptor; 94.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 94.9 +import jdk.internal.dynalink.linker.LinkRequest; 94.10 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 94.11 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 94.12 -import org.dynalang.dynalink.CallSiteDescriptor; 94.13 -import org.dynalang.dynalink.linker.GuardedInvocation; 94.14 -import org.dynalang.dynalink.linker.LinkRequest; 94.15 -import org.dynalang.dynalink.support.CallSiteDescriptorFactory; 94.16 94.17 94.18 /**
95.1 --- a/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java Thu Feb 14 13:01:52 2013 +0100 95.2 +++ b/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java Thu Feb 14 13:22:26 2013 +0100 95.3 @@ -32,15 +32,15 @@ 95.4 import java.lang.invoke.MethodHandles; 95.5 import java.lang.invoke.MethodHandles.Lookup; 95.6 import java.lang.invoke.MethodType; 95.7 +import jdk.internal.dynalink.CallSiteDescriptor; 95.8 +import jdk.internal.dynalink.DynamicLinker; 95.9 +import jdk.internal.dynalink.DynamicLinkerFactory; 95.10 +import jdk.internal.dynalink.beans.BeansLinker; 95.11 +import jdk.internal.dynalink.linker.GuardedInvocation; 95.12 +import jdk.internal.dynalink.linker.LinkerServices; 95.13 import jdk.nashorn.internal.codegen.CompilerConstants.Call; 95.14 import jdk.nashorn.internal.codegen.RuntimeCallSite; 95.15 import jdk.nashorn.internal.runtime.options.Options; 95.16 -import org.dynalang.dynalink.CallSiteDescriptor; 95.17 -import org.dynalang.dynalink.DynamicLinker; 95.18 -import org.dynalang.dynalink.DynamicLinkerFactory; 95.19 -import org.dynalang.dynalink.beans.BeansLinker; 95.20 -import org.dynalang.dynalink.linker.GuardedInvocation; 95.21 -import org.dynalang.dynalink.linker.LinkerServices; 95.22 95.23 /** 95.24 * This class houses bootstrap method for invokedynamic instructions generated by compiler.
96.1 --- a/src/jdk/nashorn/internal/runtime/linker/JSObjectLinker.java Thu Feb 14 13:01:52 2013 +0100 96.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JSObjectLinker.java Thu Feb 14 13:22:26 2013 +0100 96.3 @@ -29,14 +29,14 @@ 96.4 import java.lang.invoke.MethodHandles; 96.5 import java.lang.invoke.MethodType; 96.6 import java.util.Objects; 96.7 +import jdk.internal.dynalink.CallSiteDescriptor; 96.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 96.9 +import jdk.internal.dynalink.linker.LinkRequest; 96.10 +import jdk.internal.dynalink.linker.LinkerServices; 96.11 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 96.12 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 96.13 import jdk.nashorn.internal.runtime.JSType; 96.14 import netscape.javascript.JSObject; 96.15 -import org.dynalang.dynalink.CallSiteDescriptor; 96.16 -import org.dynalang.dynalink.linker.GuardedInvocation; 96.17 -import org.dynalang.dynalink.linker.LinkRequest; 96.18 -import org.dynalang.dynalink.linker.LinkerServices; 96.19 -import org.dynalang.dynalink.linker.TypeBasedGuardingDynamicLinker; 96.20 -import org.dynalang.dynalink.support.CallSiteDescriptorFactory; 96.21 96.22 /** 96.23 * A Dynalink linker to handle web browser built-in JS (DOM etc.) objects as well
97.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java Thu Feb 14 13:01:52 2013 +0100 97.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java Thu Feb 14 13:22:26 2013 +0100 97.3 @@ -70,6 +70,8 @@ 97.4 import java.util.Map; 97.5 import java.util.Random; 97.6 import java.util.Set; 97.7 +import jdk.internal.dynalink.beans.StaticClass; 97.8 +import jdk.internal.dynalink.support.LinkRequestImpl; 97.9 import jdk.internal.org.objectweb.asm.ClassWriter; 97.10 import jdk.internal.org.objectweb.asm.Label; 97.11 import jdk.internal.org.objectweb.asm.Opcodes; 97.12 @@ -83,8 +85,6 @@ 97.13 import jdk.nashorn.internal.runtime.ScriptObject; 97.14 import jdk.nashorn.internal.runtime.ScriptRuntime; 97.15 import jdk.nashorn.internal.runtime.Undefined; 97.16 -import org.dynalang.dynalink.beans.StaticClass; 97.17 -import org.dynalang.dynalink.support.LinkRequestImpl; 97.18 97.19 /** 97.20 * A factory class that generates adapter classes. Adapter classes allow implementation of Java interfaces and
98.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaArgumentConverters.java Thu Feb 14 13:01:52 2013 +0100 98.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaArgumentConverters.java Thu Feb 14 13:22:26 2013 +0100 98.3 @@ -33,10 +33,10 @@ 98.4 import java.lang.invoke.MethodHandles; 98.5 import java.util.HashMap; 98.6 import java.util.Map; 98.7 +import jdk.internal.dynalink.support.TypeUtilities; 98.8 import jdk.nashorn.internal.runtime.ConsString; 98.9 import jdk.nashorn.internal.runtime.JSType; 98.10 import jdk.nashorn.internal.runtime.ScriptObject; 98.11 -import org.dynalang.dynalink.support.TypeUtilities; 98.12 98.13 /** 98.14 * Utility class shared by {@link NashornLinker} and {@code NashornPrimitiveLinker} for converting JS values to Java
99.1 --- a/src/jdk/nashorn/internal/runtime/linker/LinkerCallSite.java Thu Feb 14 13:01:52 2013 +0100 99.2 +++ b/src/jdk/nashorn/internal/runtime/linker/LinkerCallSite.java Thu Feb 14 13:22:26 2013 +0100 99.3 @@ -43,14 +43,14 @@ 99.4 import java.util.Random; 99.5 import java.util.Set; 99.6 import java.util.concurrent.atomic.AtomicInteger; 99.7 +import jdk.internal.dynalink.ChainedCallSite; 99.8 +import jdk.internal.dynalink.DynamicLinker; 99.9 +import jdk.internal.dynalink.linker.GuardedInvocation; 99.10 import jdk.nashorn.internal.runtime.Context; 99.11 import jdk.nashorn.internal.runtime.Debug; 99.12 import jdk.nashorn.internal.runtime.ScriptObject; 99.13 import jdk.nashorn.internal.runtime.ScriptRuntime; 99.14 import jdk.nashorn.internal.runtime.options.Options; 99.15 -import org.dynalang.dynalink.ChainedCallSite; 99.16 -import org.dynalang.dynalink.DynamicLinker; 99.17 -import org.dynalang.dynalink.linker.GuardedInvocation; 99.18 99.19 99.20 /**
100.1 --- a/src/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java Thu Feb 14 13:01:52 2013 +0100 100.2 +++ b/src/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java Thu Feb 14 13:22:26 2013 +0100 100.3 @@ -30,13 +30,13 @@ 100.4 import static jdk.nashorn.internal.runtime.linker.Lookup.MH; 100.5 100.6 import java.lang.invoke.MethodHandle; 100.7 +import jdk.internal.dynalink.CallSiteDescriptor; 100.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 100.9 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 100.10 +import jdk.internal.dynalink.linker.LinkRequest; 100.11 +import jdk.internal.dynalink.linker.LinkerServices; 100.12 +import jdk.internal.dynalink.support.Guards; 100.13 import jdk.nashorn.internal.runtime.ScriptRuntime; 100.14 -import org.dynalang.dynalink.CallSiteDescriptor; 100.15 -import org.dynalang.dynalink.linker.GuardedInvocation; 100.16 -import org.dynalang.dynalink.linker.GuardingDynamicLinker; 100.17 -import org.dynalang.dynalink.linker.LinkRequest; 100.18 -import org.dynalang.dynalink.linker.LinkerServices; 100.19 -import org.dynalang.dynalink.support.Guards; 100.20 100.21 /** 100.22 * Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other
101.1 --- a/src/jdk/nashorn/internal/runtime/linker/NashornCallSiteDescriptor.java Thu Feb 14 13:01:52 2013 +0100 101.2 +++ b/src/jdk/nashorn/internal/runtime/linker/NashornCallSiteDescriptor.java Thu Feb 14 13:22:26 2013 +0100 101.3 @@ -28,9 +28,9 @@ 101.4 import java.lang.invoke.MethodType; 101.5 import java.lang.ref.WeakReference; 101.6 import java.util.WeakHashMap; 101.7 -import org.dynalang.dynalink.CallSiteDescriptor; 101.8 -import org.dynalang.dynalink.support.AbstractCallSiteDescriptor; 101.9 -import org.dynalang.dynalink.support.CallSiteDescriptorFactory; 101.10 +import jdk.internal.dynalink.CallSiteDescriptor; 101.11 +import jdk.internal.dynalink.support.AbstractCallSiteDescriptor; 101.12 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 101.13 101.14 /** 101.15 * Nashorn-specific implementation of Dynalink's {@link CallSiteDescriptor}. The reason we have our own subclass is that
102.1 --- a/src/jdk/nashorn/internal/runtime/linker/NashornLinker.java Thu Feb 14 13:01:52 2013 +0100 102.2 +++ b/src/jdk/nashorn/internal/runtime/linker/NashornLinker.java Thu Feb 14 13:22:26 2013 +0100 102.3 @@ -29,23 +29,22 @@ 102.4 102.5 import java.lang.invoke.MethodHandle; 102.6 import java.lang.invoke.MethodHandles; 102.7 - 102.8 +import jdk.internal.dynalink.CallSiteDescriptor; 102.9 +import jdk.internal.dynalink.linker.ConversionComparator; 102.10 +import jdk.internal.dynalink.linker.GuardedInvocation; 102.11 +import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; 102.12 +import jdk.internal.dynalink.linker.LinkRequest; 102.13 +import jdk.internal.dynalink.linker.LinkerServices; 102.14 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 102.15 +import jdk.internal.dynalink.support.Guards; 102.16 import jdk.nashorn.internal.runtime.ScriptFunction; 102.17 import jdk.nashorn.internal.runtime.ScriptObject; 102.18 import jdk.nashorn.internal.runtime.Undefined; 102.19 -import org.dynalang.dynalink.CallSiteDescriptor; 102.20 -import org.dynalang.dynalink.linker.ConversionComparator; 102.21 -import org.dynalang.dynalink.linker.GuardedInvocation; 102.22 -import org.dynalang.dynalink.linker.GuardingTypeConverterFactory; 102.23 -import org.dynalang.dynalink.linker.LinkRequest; 102.24 -import org.dynalang.dynalink.linker.LinkerServices; 102.25 -import org.dynalang.dynalink.linker.TypeBasedGuardingDynamicLinker; 102.26 -import org.dynalang.dynalink.support.Guards; 102.27 102.28 /** 102.29 * This is the main dynamic linker for Nashorn. It is used for linking all {@link ScriptObject} and its subclasses (this 102.30 * includes {@link ScriptFunction} and its subclasses) as well as {@link Undefined}. This linker is exported to other 102.31 - * language runtimes by being declared in {@code META-INF/services/org.dynalang.dynalink.linker.GuardingDynamicLinker} 102.32 + * language runtimes by being declared in {@code META-INF/services/jdk.internal.dynalink.linker.GuardingDynamicLinker} 102.33 * file of Nashorn's distribution. 102.34 */ 102.35 public class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory, ConversionComparator {
103.1 --- a/src/jdk/nashorn/internal/runtime/linker/NashornPrimitiveLinker.java Thu Feb 14 13:01:52 2013 +0100 103.2 +++ b/src/jdk/nashorn/internal/runtime/linker/NashornPrimitiveLinker.java Thu Feb 14 13:22:26 2013 +0100 103.3 @@ -29,16 +29,16 @@ 103.4 103.5 import java.lang.invoke.MethodHandle; 103.6 import java.lang.invoke.MethodHandles; 103.7 +import jdk.internal.dynalink.linker.ConversionComparator; 103.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 103.9 +import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; 103.10 +import jdk.internal.dynalink.linker.LinkRequest; 103.11 +import jdk.internal.dynalink.linker.LinkerServices; 103.12 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 103.13 +import jdk.internal.dynalink.support.TypeUtilities; 103.14 import jdk.nashorn.internal.runtime.ConsString; 103.15 import jdk.nashorn.internal.runtime.Context; 103.16 import jdk.nashorn.internal.runtime.GlobalObject; 103.17 -import org.dynalang.dynalink.linker.ConversionComparator; 103.18 -import org.dynalang.dynalink.linker.GuardedInvocation; 103.19 -import org.dynalang.dynalink.linker.GuardingTypeConverterFactory; 103.20 -import org.dynalang.dynalink.linker.LinkRequest; 103.21 -import org.dynalang.dynalink.linker.LinkerServices; 103.22 -import org.dynalang.dynalink.linker.TypeBasedGuardingDynamicLinker; 103.23 -import org.dynalang.dynalink.support.TypeUtilities; 103.24 103.25 /** 103.26 * Internal linker for String, Boolean, and Number objects, only ever used by Nashorn engine and not exposed to other 103.27 @@ -90,7 +90,7 @@ 103.28 * @param sourceType the source type to convert from 103.29 * @param targetType1 one candidate target type 103.30 * @param targetType2 another candidate target type 103.31 - * @return one of {@link org.dynalang.dynalink.linker.ConversionComparator.Comparison} values signifying which 103.32 + * @return one of {@link jdk.internal.dynalink.linker.ConversionComparator.Comparison} values signifying which 103.33 * target type should be favored for conversion. 103.34 */ 103.35 @Override
104.1 --- a/src/jdk/nashorn/internal/runtime/linker/NashornStaticClassLinker.java Thu Feb 14 13:01:52 2013 +0100 104.2 +++ b/src/jdk/nashorn/internal/runtime/linker/NashornStaticClassLinker.java Thu Feb 14 13:22:26 2013 +0100 104.3 @@ -25,16 +25,16 @@ 104.4 104.5 package jdk.nashorn.internal.runtime.linker; 104.6 104.7 +import jdk.internal.dynalink.CallSiteDescriptor; 104.8 +import jdk.internal.dynalink.beans.BeansLinker; 104.9 +import jdk.internal.dynalink.beans.StaticClass; 104.10 +import jdk.internal.dynalink.linker.GuardedInvocation; 104.11 +import jdk.internal.dynalink.linker.GuardingDynamicLinker; 104.12 +import jdk.internal.dynalink.linker.LinkRequest; 104.13 +import jdk.internal.dynalink.linker.LinkerServices; 104.14 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 104.15 +import jdk.internal.dynalink.support.Guards; 104.16 import jdk.nashorn.internal.runtime.ECMAErrors; 104.17 -import org.dynalang.dynalink.CallSiteDescriptor; 104.18 -import org.dynalang.dynalink.beans.BeansLinker; 104.19 -import org.dynalang.dynalink.beans.StaticClass; 104.20 -import org.dynalang.dynalink.linker.GuardedInvocation; 104.21 -import org.dynalang.dynalink.linker.GuardingDynamicLinker; 104.22 -import org.dynalang.dynalink.linker.LinkRequest; 104.23 -import org.dynalang.dynalink.linker.LinkerServices; 104.24 -import org.dynalang.dynalink.linker.TypeBasedGuardingDynamicLinker; 104.25 -import org.dynalang.dynalink.support.Guards; 104.26 104.27 /** 104.28 * Internal linker for {@link StaticClass} objects, only ever used by Nashorn engine and not exposed to other engines.
105.1 --- a/src/jdk/nashorn/internal/runtime/linker/PrimitiveLookup.java Thu Feb 14 13:01:52 2013 +0100 105.2 +++ b/src/jdk/nashorn/internal/runtime/linker/PrimitiveLookup.java Thu Feb 14 13:22:26 2013 +0100 105.3 @@ -29,12 +29,12 @@ 105.4 105.5 import java.lang.invoke.MethodHandle; 105.6 import java.lang.invoke.MethodType; 105.7 +import jdk.internal.dynalink.CallSiteDescriptor; 105.8 +import jdk.internal.dynalink.linker.GuardedInvocation; 105.9 +import jdk.internal.dynalink.linker.LinkRequest; 105.10 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 105.11 +import jdk.internal.dynalink.support.Guards; 105.12 import jdk.nashorn.internal.runtime.ScriptObject; 105.13 -import org.dynalang.dynalink.CallSiteDescriptor; 105.14 -import org.dynalang.dynalink.linker.GuardedInvocation; 105.15 -import org.dynalang.dynalink.linker.LinkRequest; 105.16 -import org.dynalang.dynalink.support.CallSiteDescriptorFactory; 105.17 -import org.dynalang.dynalink.support.Guards; 105.18 105.19 /** 105.20 * Implements lookup of methods to link for dynamic operations on JavaScript primitive values (booleans, strings, and
106.1 --- a/src/jdk/nashorn/internal/runtime/options/Options.java Thu Feb 14 13:01:52 2013 +0100 106.2 +++ b/src/jdk/nashorn/internal/runtime/options/Options.java Thu Feb 14 13:22:26 2013 +0100 106.3 @@ -44,7 +44,6 @@ 106.4 import java.util.TimeZone; 106.5 import java.util.TreeMap; 106.6 import java.util.TreeSet; 106.7 - 106.8 import jdk.nashorn.internal.runtime.Logging; 106.9 import jdk.nashorn.internal.runtime.QuotedStringTokenizer; 106.10
107.1 --- a/test/script/sandbox/nashorninternals.js Thu Feb 14 13:01:52 2013 +0100 107.2 +++ b/test/script/sandbox/nashorninternals.js Thu Feb 14 13:22:26 2013 +0100 107.3 @@ -55,3 +55,7 @@ 107.4 checkClass("jdk.nashorn.internal.parser.JSONParser"); 107.5 checkClass("jdk.nashorn.internal.parser.Lexer"); 107.6 checkClass("jdk.nashorn.internal.parser.Scanner"); 107.7 +checkClass("jdk.internal.dynalink.CallSiteDescriptor"); 107.8 +checkClass("jdk.internal.dynalink.beans.StaticClass"); 107.9 +checkClass("jdk.internal.dynalink.linker.LinkRequest"); 107.10 +checkClass("jdk.internal.dynalink.support.AbstractRelinkableCallSite");
108.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Thu Feb 14 13:01:52 2013 +0100 108.2 +++ b/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Thu Feb 14 13:22:26 2013 +0100 108.3 @@ -26,8 +26,8 @@ 108.4 package jdk.nashorn.api.scripting; 108.5 108.6 import static org.testng.Assert.assertEquals; 108.7 +import static org.testng.Assert.assertFalse; 108.8 import static org.testng.Assert.assertNotNull; 108.9 -import static org.testng.Assert.assertFalse; 108.10 import static org.testng.Assert.assertTrue; 108.11 import static org.testng.Assert.fail; 108.12
109.1 --- a/test/src/jdk/nashorn/internal/runtime/ContextTest.java Thu Feb 14 13:01:52 2013 +0100 109.2 +++ b/test/src/jdk/nashorn/internal/runtime/ContextTest.java Thu Feb 14 13:22:26 2013 +0100 109.3 @@ -35,7 +35,6 @@ 109.4 import javax.script.ScriptEngineManager; 109.5 import javax.script.ScriptException; 109.6 import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 109.7 -import jdk.nashorn.api.scripting.ScriptEngineTest; 109.8 import jdk.nashorn.internal.runtime.options.Options; 109.9 import org.testng.annotations.Test; 109.10
110.1 --- a/test/src/jdk/nashorn/internal/runtime/JSTypeTest.java Thu Feb 14 13:01:52 2013 +0100 110.2 +++ b/test/src/jdk/nashorn/internal/runtime/JSTypeTest.java Thu Feb 14 13:22:26 2013 +0100 110.3 @@ -26,8 +26,8 @@ 110.4 package jdk.nashorn.internal.runtime; 110.5 110.6 import static org.testng.Assert.assertEquals; 110.7 +import static org.testng.Assert.assertFalse; 110.8 import static org.testng.Assert.assertTrue; 110.9 -import static org.testng.Assert.assertFalse; 110.10 110.11 import org.testng.annotations.Test; 110.12