author | never |
Mon, 12 Jul 2010 22:27:18 -0700 | |
changeset 5926 | a36f90d986b6 |
parent 5506 | 202f599c92aa |
child 5725 | 16c1792b2ee6 |
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 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
26 |
package java.dyn; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
27 |
|
4537 | 28 |
import sun.dyn.Access; |
29 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
30 |
/** |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
31 |
* A Java method handle extends the basic method handle type with additional |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
32 |
* programmer defined methods and fields. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
33 |
* 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
|
34 |
* 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
|
35 |
* 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
|
36 |
* 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
|
37 |
* 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
|
38 |
* 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
|
39 |
* 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
|
40 |
* 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
|
41 |
* 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
|
42 |
* omitted. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
43 |
* <p> |
4537 | 44 |
* 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
|
45 |
* <p><blockquote><pre> |
4537 | 46 |
* class Greeter extends JavaMethodHandle { |
47 |
* private String greeting = "hello"; |
|
48 |
* public void setGreeting(String s) { greeting = s; } |
|
49 |
* public void run() { System.out.println(greeting+", "+greetee); } |
|
50 |
* private final String greetee; |
|
51 |
* Greeter(String greetee) { |
|
52 |
* super(RUN); // alternatively, super("run") |
|
53 |
* this.greetee = greetee; |
|
54 |
* } |
|
55 |
* // the entry point function is computed once: |
|
56 |
* private static final MethodHandle RUN |
|
57 |
* = MethodHandles.lookup().findVirtual(Greeter.class, "run", |
|
58 |
* MethodType.make(void.class)); |
|
59 |
* } |
|
60 |
* // class Main { public static void main(String... av) { ... |
|
61 |
* Greeter greeter = new Greeter("world"); |
|
62 |
* greeter.run(); // prints "hello, world" |
|
63 |
* // Statically typed method handle invocation (most direct): |
|
64 |
* MethodHandle mh = greeter; |
|
65 |
* mh.<void>invoke(); // also prints "hello, world" |
|
66 |
* // Dynamically typed method handle invocation: |
|
67 |
* MethodHandles.invoke(greeter); // also prints "hello, world" |
|
68 |
* greeter.setGreeting("howdy"); |
|
69 |
* mh.invoke(); // prints "howdy, world" (object-like mutable behavior) |
|
70 |
* </pre></blockquote> |
|
71 |
* <p> |
|
72 |
* In the example of {@code Greeter}, the method {@code run} provides the entry point. |
|
73 |
* The entry point need not be a constant value; it may be independently |
|
74 |
* computed in each call to the constructor. The entry point does not |
|
75 |
* even need to be a method on the {@code Greeter} class, though |
|
76 |
* that is the typical case. |
|
77 |
* <p> |
|
78 |
* The entry point may also be provided symbolically, in which case the the |
|
79 |
* {@code JavaMethodHandle} constructor performs the lookup of the entry point. |
|
80 |
* This makes it possible to use {@code JavaMethodHandle} to create an anonymous |
|
81 |
* inner class: |
|
82 |
* <p><blockquote><pre> |
|
83 |
* // We can also do this with symbolic names and/or inner classes: |
|
84 |
* MethodHandles.invoke(new JavaMethodHandle("yow") { |
|
85 |
* void yow() { System.out.println("yow, world"); } |
|
86 |
* }); |
|
87 |
* </pre></blockquote> |
|
88 |
* <p> |
|
89 |
* Here is similar lower-level code which works in terms of a bound method handle. |
|
90 |
* <p><blockquote><pre> |
|
91 |
* class Greeter { |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
92 |
* public void run() { System.out.println("hello, "+greetee); } |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
93 |
* private final String greetee; |
4537 | 94 |
* Greeter(String greetee) { this.greetee = greetee; } |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
95 |
* // the entry point function is computed once: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
96 |
* private static final MethodHandle RUN |
4537 | 97 |
* = MethodHandles.findVirtual(Greeter.class, "run", |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
98 |
* MethodType.make(void.class)); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
99 |
* } |
4537 | 100 |
* // class Main { public static void main(String... av) { ... |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
101 |
* Greeter greeter = new Greeter("world"); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
102 |
* greeter.run(); // prints "hello, world" |
4537 | 103 |
* MethodHandle mh = MethodHanndles.insertArgument(Greeter.RUN, 0, greeter); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
104 |
* mh.invoke(); // also prints "hello, world" |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
105 |
* </pre></blockquote> |
4537 | 106 |
* Note that the method handle must be separately created as a view on the base object. |
107 |
* This increases footprint, complexity, and dynamic indirections. |
|
108 |
* <p> |
|
109 |
* Here is a pure functional value expressed most concisely as an anonymous inner class: |
|
110 |
* <p><blockquote><pre> |
|
111 |
* // class Main { public static void main(String... av) { ... |
|
112 |
* final String greetee = "world"; |
|
113 |
* MethodHandle greeter = new JavaMethodHandle("run") { |
|
114 |
* private void run() { System.out.println("hello, "+greetee); } |
|
115 |
* } |
|
116 |
* greeter.invoke(); // prints "hello, world" |
|
117 |
* </pre></blockquote> |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
118 |
* <p> |
4537 | 119 |
* Here is an abstract parameterized lvalue, efficiently expressed as a subtype of MethodHandle, |
120 |
* and instantiated as an anonymous class. The data structure is a handle to 1-D array, |
|
121 |
* with a specialized index type (long). It is created by inner class, and uses |
|
122 |
* signature-polymorphic APIs throughout. |
|
123 |
* <p><blockquote><pre> |
|
124 |
* abstract class AssignableMethodHandle extends JavaMethodHandle { |
|
125 |
* private final MethodHandle setter; |
|
126 |
* public MethodHandle setter() { return setter; } |
|
127 |
* public AssignableMethodHandle(String get, String set) { |
|
128 |
* super(get); |
|
129 |
* MethodType getType = this.type(); |
|
130 |
* MethodType setType = getType.insertParameterType(getType.parameterCount(), getType.returnType()).changeReturnType(void.class); |
|
131 |
* this.setter = MethodHandles.publicLookup().bind(this, set, setType); |
|
132 |
* } |
|
133 |
* } |
|
134 |
* // class Main { public static void main(String... av) { ... |
|
135 |
* final Number[] stuff = { 123, 456 }; |
|
136 |
* AssignableMethodHandle stuffPtr = new AssignableMethodHandle("get", "set") { |
|
137 |
* public Number get(long i) { return stuff[(int)i]; } |
|
138 |
* public void set(long i, Object x) { stuff[(int)i] = x; } |
|
139 |
* } |
|
140 |
* int x = (Integer) stuffPtr.<Number>invoke(1L); // 456 |
|
141 |
* stuffPtr.setter().<void>invoke(0L, (Number) 789); // replaces 123 with 789 |
|
142 |
* </pre></blockquote> |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
143 |
* @see MethodHandle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
144 |
* @author John Rose, JSR 292 EG |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
145 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
146 |
public abstract class JavaMethodHandle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
147 |
// 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
|
148 |
// 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
|
149 |
extends sun.dyn.BoundMethodHandle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
150 |
{ |
4537 | 151 |
private static final Access IMPL_TOKEN = Access.getToken(); |
152 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
153 |
/** |
4537 | 154 |
* When creating a {@code JavaMethodHandle}, the actual method handle |
155 |
* invocation behavior will be delegated to the specified {@code entryPoint}. |
|
156 |
* This may be any method handle which can take the newly constructed object |
|
157 |
* as a leading parameter. |
|
158 |
* <p> |
|
159 |
* The method handle type of {@code this} (i.e, the fully constructed object) |
|
160 |
* will be {@code entryPoint}, minus the leading argument. |
|
161 |
* The leading argument will be bound to {@code this} on every method |
|
162 |
* handle invocation. |
|
163 |
* @param entryPoint the method handle to handle calls |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
164 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
165 |
protected JavaMethodHandle(MethodHandle entryPoint) { |
4537 | 166 |
super(entryPoint); |
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Create a method handle whose entry point is a non-static method |
|
171 |
* visible in the exact (most specific) class of |
|
172 |
* the newly constructed object. |
|
173 |
* <p> |
|
174 |
* The method is specified by name and type, as if via this expression: |
|
175 |
* {@code MethodHandles.lookup().findVirtual(this.getClass(), name, type)}. |
|
176 |
* The class defining the method might be an anonymous inner class. |
|
177 |
* <p> |
|
178 |
* The method handle type of {@code this} (i.e, the fully constructed object) |
|
179 |
* will be the given method handle type. |
|
180 |
* A call to {@code this} will invoke the selected method. |
|
181 |
* The receiver argument will be bound to {@code this} on every method |
|
182 |
* handle invocation. |
|
183 |
* <p> |
|
184 |
* <i>Rationale:</i> |
|
185 |
* Although this constructor may seem to be a mere luxury, |
|
186 |
* it is not subsumed by the more general constructor which |
|
187 |
* takes any {@code MethodHandle} as the entry point argument. |
|
188 |
* In order to convert an entry point name to a method handle, |
|
189 |
* the self-class of the object is required (in order to do |
|
190 |
* the lookup). The self-class, in turn, is generally not |
|
191 |
* available at the time of the constructor invocation, |
|
192 |
* due to the rules of Java and the JVM verifier. |
|
193 |
* One cannot call {@code this.getClass()}, because |
|
194 |
* the value of {@code this} is inaccessible at the point |
|
195 |
* of the constructor call. (Changing this would require |
|
196 |
* change to the Java language, verifiers, and compilers.) |
|
197 |
* In particular, this constructor allows {@code JavaMethodHandle}s |
|
198 |
* to be created in combination with the anonymous inner class syntax. |
|
199 |
* @param entryPointName the name of the entry point method |
|
200 |
* @param type (optional) the desired type of the method handle |
|
201 |
*/ |
|
202 |
protected JavaMethodHandle(String entryPointName, MethodType type) { |
|
203 |
super(entryPointName, type, true); |
|
204 |
||
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Create a method handle whose entry point is a non-static method |
|
209 |
* visible in the exact (most specific) class of |
|
210 |
* the newly constructed object. |
|
211 |
* <p> |
|
212 |
* The method is specified only by name. |
|
213 |
* There must be exactly one method of that name visible in the object class, |
|
214 |
* either inherited or locally declared. |
|
215 |
* (That is, the method must not be overloaded.) |
|
216 |
* <p> |
|
217 |
* The method handle type of {@code this} (i.e, the fully constructed object) |
|
218 |
* will be the same as the type of the selected non-static method. |
|
219 |
* The receiver argument will be bound to {@code this} on every method |
|
220 |
* handle invocation. |
|
221 |
* <p>ISSUE: This signature wildcarding feature does not correspond to |
|
222 |
* any MethodHandles.Lookup API element. Can we eliminate it? |
|
223 |
* Alternatively, it is useful for naming non-overloaded methods. |
|
224 |
* Shall we make type arguments optional in the Lookup methods, |
|
225 |
* throwing an error in cases of ambiguity? |
|
226 |
* <p> |
|
227 |
* For this method's rationale, see the documentation |
|
228 |
* for {@link #JavaMethodHandle(String,MethodType)}. |
|
229 |
* @param entryPointName the name of the entry point method |
|
230 |
*/ |
|
231 |
protected JavaMethodHandle(String entryPointName) { |
|
232 |
super(entryPointName, (MethodType) null, false); |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
233 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
234 |
} |