author | dholmes |
Tue, 21 Dec 2010 23:39:42 -0500 | |
changeset 7691 | ec07a04e74e7 |
parent 7052 | 963a5baf2ba3 |
permissions | -rw-r--r-- |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1 |
/* |
5506 | 2 |
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
4 |
* |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
10 |
* |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
15 |
* accompanied this code). |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
16 |
* |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
20 |
* |
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
24 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
25 |
|
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5725
diff
changeset
|
26 |
package sun.dyn; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
27 |
|
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5725
diff
changeset
|
28 |
import java.dyn.*; |
4537 | 29 |
import sun.dyn.Access; |
30 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
31 |
/** |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
32 |
* A Java method handle is a deprecated proposal for extending |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
33 |
* the basic method handle type with additional |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
34 |
* programmer defined methods and fields. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
35 |
* Its behavior as a method handle is determined at instance creation time, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
36 |
* by providing the new instance with an "entry point" method handle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
37 |
* to handle calls. This entry point must accept a leading argument |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
38 |
* whose type is the Java method handle itself or a supertype, and the |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
39 |
* entry point is always called with the Java method handle itself as |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
40 |
* the first argument. This is similar to ordinary virtual methods, which also |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
41 |
* accept the receiver object {@code this} as an implicit leading argument. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
42 |
* The {@code MethodType} of the Java method handle is the same as that |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
43 |
* of the entry point method handle, with the leading parameter type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
44 |
* omitted. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
45 |
* <p> |
4537 | 46 |
* Here is an example of usage, creating a hybrid object/functional datum: |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
47 |
* <p><blockquote><pre> |
4537 | 48 |
* class Greeter extends JavaMethodHandle { |
49 |
* private String greeting = "hello"; |
|
50 |
* public void setGreeting(String s) { greeting = s; } |
|
51 |
* public void run() { System.out.println(greeting+", "+greetee); } |
|
52 |
* private final String greetee; |
|
53 |
* Greeter(String greetee) { |
|
54 |
* super(RUN); // alternatively, super("run") |
|
55 |
* this.greetee = greetee; |
|
56 |
* } |
|
57 |
* // the entry point function is computed once: |
|
58 |
* private static final MethodHandle RUN |
|
59 |
* = MethodHandles.lookup().findVirtual(Greeter.class, "run", |
|
60 |
* MethodType.make(void.class)); |
|
61 |
* } |
|
62 |
* // class Main { public static void main(String... av) { ... |
|
63 |
* Greeter greeter = new Greeter("world"); |
|
64 |
* greeter.run(); // prints "hello, world" |
|
65 |
* // Statically typed method handle invocation (most direct): |
|
66 |
* MethodHandle mh = greeter; |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
67 |
* mh.<void>invokeExact(); // also prints "hello, world" |
4537 | 68 |
* // Dynamically typed method handle invocation: |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
69 |
* MethodHandles.invokeExact(greeter); // also prints "hello, world" |
4537 | 70 |
* greeter.setGreeting("howdy"); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
71 |
* mh.invokeExact(); // prints "howdy, world" (object-like mutable behavior) |
4537 | 72 |
* </pre></blockquote> |
73 |
* <p> |
|
74 |
* In the example of {@code Greeter}, the method {@code run} provides the entry point. |
|
75 |
* The entry point need not be a constant value; it may be independently |
|
76 |
* computed in each call to the constructor. The entry point does not |
|
77 |
* even need to be a method on the {@code Greeter} class, though |
|
78 |
* that is the typical case. |
|
79 |
* <p> |
|
80 |
* The entry point may also be provided symbolically, in which case the the |
|
81 |
* {@code JavaMethodHandle} constructor performs the lookup of the entry point. |
|
82 |
* This makes it possible to use {@code JavaMethodHandle} to create an anonymous |
|
83 |
* inner class: |
|
84 |
* <p><blockquote><pre> |
|
85 |
* // We can also do this with symbolic names and/or inner classes: |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
86 |
* MethodHandles.invokeExact(new JavaMethodHandle("yow") { |
4537 | 87 |
* void yow() { System.out.println("yow, world"); } |
88 |
* }); |
|
89 |
* </pre></blockquote> |
|
90 |
* <p> |
|
91 |
* Here is similar lower-level code which works in terms of a bound method handle. |
|
92 |
* <p><blockquote><pre> |
|
93 |
* class Greeter { |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
94 |
* public void run() { System.out.println("hello, "+greetee); } |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
95 |
* private final String greetee; |
4537 | 96 |
* Greeter(String greetee) { this.greetee = greetee; } |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
97 |
* // the entry point function is computed once: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
98 |
* private static final MethodHandle RUN |
4537 | 99 |
* = MethodHandles.findVirtual(Greeter.class, "run", |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
100 |
* MethodType.make(void.class)); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
101 |
* } |
4537 | 102 |
* // class Main { public static void main(String... av) { ... |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
103 |
* Greeter greeter = new Greeter("world"); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
104 |
* greeter.run(); // prints "hello, world" |
4537 | 105 |
* MethodHandle mh = MethodHanndles.insertArgument(Greeter.RUN, 0, greeter); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
106 |
* mh.invokeExact(); // also prints "hello, world" |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
107 |
* </pre></blockquote> |
4537 | 108 |
* Note that the method handle must be separately created as a view on the base object. |
109 |
* This increases footprint, complexity, and dynamic indirections. |
|
110 |
* <p> |
|
111 |
* Here is a pure functional value expressed most concisely as an anonymous inner class: |
|
112 |
* <p><blockquote><pre> |
|
113 |
* // class Main { public static void main(String... av) { ... |
|
114 |
* final String greetee = "world"; |
|
115 |
* MethodHandle greeter = new JavaMethodHandle("run") { |
|
116 |
* private void run() { System.out.println("hello, "+greetee); } |
|
117 |
* } |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
118 |
* greeter.invokeExact(); // prints "hello, world" |
4537 | 119 |
* </pre></blockquote> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
120 |
* <p> |
4537 | 121 |
* Here is an abstract parameterized lvalue, efficiently expressed as a subtype of MethodHandle, |
122 |
* and instantiated as an anonymous class. The data structure is a handle to 1-D array, |
|
123 |
* with a specialized index type (long). It is created by inner class, and uses |
|
124 |
* signature-polymorphic APIs throughout. |
|
125 |
* <p><blockquote><pre> |
|
126 |
* abstract class AssignableMethodHandle extends JavaMethodHandle { |
|
127 |
* private final MethodHandle setter; |
|
128 |
* public MethodHandle setter() { return setter; } |
|
129 |
* public AssignableMethodHandle(String get, String set) { |
|
130 |
* super(get); |
|
131 |
* MethodType getType = this.type(); |
|
132 |
* MethodType setType = getType.insertParameterType(getType.parameterCount(), getType.returnType()).changeReturnType(void.class); |
|
133 |
* this.setter = MethodHandles.publicLookup().bind(this, set, setType); |
|
134 |
* } |
|
135 |
* } |
|
136 |
* // class Main { public static void main(String... av) { ... |
|
137 |
* final Number[] stuff = { 123, 456 }; |
|
138 |
* AssignableMethodHandle stuffPtr = new AssignableMethodHandle("get", "set") { |
|
139 |
* public Number get(long i) { return stuff[(int)i]; } |
|
140 |
* public void set(long i, Object x) { stuff[(int)i] = x; } |
|
141 |
* } |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
142 |
* int x = (Integer) stuffPtr.<Number>invokeExact(1L); // 456 |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
143 |
* stuffPtr.setter().<void>invokeExact(0L, (Number) 789); // replaces 123 with 789 |
4537 | 144 |
* </pre></blockquote> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
145 |
* @see MethodHandle |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
146 |
* @deprecated The JSR 292 EG intends to replace {@code JavaMethodHandle} with |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
147 |
* an interface-based API for mixing method handle behavior with other classes. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
148 |
* @author John Rose, JSR 292 EG |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
149 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
150 |
public abstract class JavaMethodHandle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
151 |
// Note: This is an implementation inheritance hack, and will be removed |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
152 |
// with a JVM change which moves the required hidden behavior onto this class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
153 |
extends sun.dyn.BoundMethodHandle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
154 |
{ |
4537 | 155 |
private static final Access IMPL_TOKEN = Access.getToken(); |
156 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
157 |
/** |
4537 | 158 |
* When creating a {@code JavaMethodHandle}, the actual method handle |
159 |
* invocation behavior will be delegated to the specified {@code entryPoint}. |
|
160 |
* This may be any method handle which can take the newly constructed object |
|
161 |
* as a leading parameter. |
|
162 |
* <p> |
|
163 |
* The method handle type of {@code this} (i.e, the fully constructed object) |
|
164 |
* will be {@code entryPoint}, minus the leading argument. |
|
165 |
* The leading argument will be bound to {@code this} on every method |
|
166 |
* handle invocation. |
|
167 |
* @param entryPoint the method handle to handle calls |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
168 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
169 |
protected JavaMethodHandle(MethodHandle entryPoint) { |
4537 | 170 |
super(entryPoint); |
171 |
} |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
172 |
} |