Path: blob/master/src/jdk.dynalink/share/classes/module-info.java
41144 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26* Defines the API for dynamic linking of high-level operations on objects.27* <p>28* Dynalink is a library for dynamic linking of high-level operations on objects.29* These operations include "read a property",30* "write a property", "invoke a function" and so on. Dynalink is primarily31* useful for implementing programming languages where at least some expressions32* have dynamic types (that is, types that can not be decided statically), and33* the operations on dynamic types are expressed as34* {@linkplain java.lang.invoke.CallSite call sites}. These call sites will be35* linked to appropriate target {@linkplain java.lang.invoke.MethodHandle method handles}36* at run time based on actual types of the values the expressions evaluated to.37* These can change between invocations, necessitating relinking the call site38* multiple times to accommodate new types; Dynalink handles all that and more.39* <p>40* Dynalink supports implementation of programming languages with object models41* that differ (even radically) from the JVM's class-based model and have their42* custom type conversions.43* <p>44* Dynalink is closely related to, and relies on, the {@link java.lang.invoke}45* package.46* <p>47*48* While {@link java.lang.invoke} provides a low level API for dynamic linking49* of {@code invokedynamic} call sites, it does not provide a way to express50* higher level operations on objects, nor methods that implement them. These51* operations are the usual ones in object-oriented environments: property52* access, access of elements of collections, invocation of methods and53* constructors (potentially with multiple dispatch, e.g. link- and run-time54* equivalents of Java overloaded method resolution). These are all functions55* that are normally desired in a language on the JVM. If a language is56* statically typed and its type system matches that of the JVM, it can57* accomplish this with use of the usual invocation, field access, etc.58* instructions (e.g. {@code invokevirtual}, {@code getfield}). However, if the59* language is dynamic (hence, types of some expressions are not known until60* evaluated at run time), or its object model or type system don't match61* closely that of the JVM, then it should use {@code invokedynamic} call sites62* instead and let Dynalink manage them.63* <h2>Example</h2>64* Dynalink is probably best explained by an example showing its use. Let's65* suppose you have a program in a language where you don't have to declare the66* type of an object and you want to access a property on it:67* <pre>68* var color = obj.color;69* </pre>70* If you generated a Java class to represent the above one-line program, its71* bytecode would look something like this:72* <pre>73* aload 2 // load "obj" on stack74* invokedynamic "GET:PROPERTY:color"(Object)Object // invoke property getter on object of unknown type75* astore 3 // store the return value into local variable "color"76* </pre>77* In order to link the {@code invokedynamic} instruction, we need a bootstrap78* method. A minimalist bootstrap method with Dynalink could look like this:79* <pre>80* import java.lang.invoke.*;81* import jdk.dynalink.*;82* import jdk.dynalink.support.*;83*84* class MyLanguageRuntime {85* private static final DynamicLinker dynamicLinker = new DynamicLinkerFactory().createLinker();86*87* public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType type) {88* return dynamicLinker.link(89* new SimpleRelinkableCallSite(90* new CallSiteDescriptor(lookup, parseOperation(name), type)));91* }92*93* private static Operation parseOperation(String name) {94* ...95* }96* }97* </pre>98* There are several objects of significance in the above code snippet:99* <ul>100* <li>{@link jdk.dynalink.DynamicLinker} is the main object in Dynalink, it101* coordinates the linking of call sites to method handles that implement the102* operations named in them. It is configured and created using a103* {@link jdk.dynalink.DynamicLinkerFactory}.</li>104* <li>When the bootstrap method is invoked, it needs to create a105* {@link java.lang.invoke.CallSite} object. In Dynalink, these call sites need106* to additionally implement the {@link jdk.dynalink.RelinkableCallSite}107* interface. "Relinkable" here alludes to the fact that if the call site108* encounters objects of different types at run time, its target will be changed109* to a method handle that can perform the operation on the newly encountered110* type. {@link jdk.dynalink.support.SimpleRelinkableCallSite} and111* {@link jdk.dynalink.support.ChainedCallSite} (not used in the above example)112* are two implementations already provided by the library.</li>113* <li>Dynalink uses {@link jdk.dynalink.CallSiteDescriptor} objects to114* preserve the parameters to the bootstrap method: the lookup and the method type,115* as it will need them whenever it needs to relink a call site.</li>116* <li>Dynalink uses {@link jdk.dynalink.Operation} objects to express117* dynamic operations. It does not prescribe how would you encode the operations118* in your call site, though. That is why in the above example the119* {@code parseOperation} function is left empty, and you would be expected to120* provide the code to parse the string {@code "GET:PROPERTY:color"}121* in the call site's name into a named property getter operation object as122* {@code StandardOperation.GET.withNamespace(StandardNamespace.PROPERTY).named("color")}.123* </ul>124* <p>What can you already do with the above setup? {@code DynamicLinkerFactory}125* by default creates a {@code DynamicLinker} that can link Java objects with the126* usual Java semantics. If you have these three simple classes:127* <pre>128* public class A {129* public String color;130* public A(String color) { this.color = color; }131* }132*133* public class B {134* private String color;135* public B(String color) { this.color = color; }136* public String getColor() { return color; }137* }138*139* public class C {140* private int color;141* public C(int color) { this.color = color; }142* public int getColor() { return color; }143* }144* </pre>145* and you somehow create their instances and pass them to your call site in your146* programming language:147* <pre>148* for each(var obj in [new A("red"), new B("green"), new C(0x0000ff)]) {149* print(obj.color);150* }151* </pre>152* then on first invocation, Dynalink will link the {@code .color} getter153* operation to a field getter for {@code A.color}, on second invocation it will154* relink it to {@code B.getColor()} returning a {@code String}, and finally on155* third invocation it will relink it to {@code C.getColor()} returning an {@code int}.156* The {@code SimpleRelinkableCallSite} we used above only remembers the linkage157* for the last encountered type (it implements what is known as a <i>monomorphic158* inline cache</i>). Another already provided implementation,159* {@link jdk.dynalink.support.ChainedCallSite} will remember linkages for160* several different types (it is a <i>polymorphic inline cache</i>) and is161* probably a better choice in serious applications.162* <h2>Dynalink and bytecode creation</h2>163* {@code CallSite} objects are usually created as part of bootstrapping164* {@code invokedynamic} instructions in bytecode. Hence, Dynalink is typically165* used as part of language runtimes that compile programs into Java166* {@code .class} bytecode format. Dynalink does not address the aspects of167* either creating bytecode classes or loading them into the JVM. That said,168* Dynalink can also be used without bytecode compilation (e.g. in language169* interpreters) by creating {@code CallSite} objects explicitly and associating170* them with representations of dynamic operations in the interpreted program171* (e.g. a typical representation would be some node objects in a syntax tree).172* <h2>Available operations</h2>173* Dynalink defines several standard operations in its174* {@link jdk.dynalink.StandardOperation} class. The linker for Java175* objects can link all of these operations, and you are encouraged to at176* minimum support and use these operations in your language too. The177* standard operations {@code GET} and {@code SET} need to be combined with178* at least one {@link jdk.dynalink.Namespace} to be useful, e.g. to express a179* property getter, you'd use {@code StandardOperation.GET.withNamespace(StandardNamespace.PROPERTY)}.180* Dynalink defines three standard namespaces in the {@link jdk.dynalink.StandardNamespace} class.181* To associate a fixed name with an operation, you can use182* {@link jdk.dynalink.NamedOperation} as in the previous example:183* {@code StandardOperation.GET.withNamespace(StandardNamespace.PROPERTY).named("color")}184* expresses a getter for the property named "color".185* <h2>Operations on multiple namespaces</h2>186* Some languages might not have separate namespaces on objects for187* properties, elements, and methods, and a source language construct might188* address several of them at once. Dynalink supports specifying multiple189* {@link jdk.dynalink.Namespace} objects with {@link jdk.dynalink.NamespaceOperation}.190* <h2>Language-specific linkers</h2>191* Languages that define their own object model different than the JVM192* class-based model and/or use their own type conversions will need to create193* their own language-specific linkers. See the {@link jdk.dynalink.linker}194* package and specifically the {@link jdk.dynalink.linker.GuardingDynamicLinker}195* interface to get started.196* <h2>Dynalink and Java objects</h2>197* The {@code DynamicLinker} objects created by {@code DynamicLinkerFactory} by198* default contain an internal instance of199* {@code BeansLinker}, which is a language-specific linker200* that implements the usual Java semantics for all of the above operations and201* can link any Java object that no other language-specific linker has managed202* to link. This way, all language runtimes have built-in interoperability with203* ordinary Java objects. See {@link jdk.dynalink.beans.BeansLinker} for details204* on how it links the various operations.205* <h2>Cross-language interoperability</h2>206* A {@code DynamicLinkerFactory} can be configured with a207* {@linkplain jdk.dynalink.DynamicLinkerFactory#setClassLoader(ClassLoader) class208* loader}. It will try to instantiate all209* {@link jdk.dynalink.linker.GuardingDynamicLinkerExporter} classes visible to210* that class loader and compose the linkers they provide into the211* {@code DynamicLinker} it creates. This allows for interoperability between212* languages: if you have two language runtimes A and B deployed in your JVM and213* they export their linkers through the above mechanism, language runtime A214* will have a language-specific linker instance from B and vice versa inside215* their {@code DynamicLinker} objects. This means that if an object from216* language runtime B gets passed to code from language runtime A, the linker217* from B will get a chance to link the call site in A when it encounters the218* object from B.219*220* @uses jdk.dynalink.linker.GuardingDynamicLinkerExporter221*222* @moduleGraph223* @since 9224*/225module jdk.dynalink {226requires java.logging;227228exports jdk.dynalink;229exports jdk.dynalink.beans;230exports jdk.dynalink.linker;231exports jdk.dynalink.linker.support;232exports jdk.dynalink.support;233234uses jdk.dynalink.linker.GuardingDynamicLinkerExporter;235}236237238239