|
Java™ Platform Standard Ed. 7 DRAFT ea-b76 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.dyn.MethodHandles
public class MethodHandles extends Object
Fundamental operations and utilities for MethodHandle.
API Note: The matching of method types in this API cannot be completely checked by Java's generic type system for three reasons:
| Modifier and Type | Class and Description |
|---|---|
static class |
MethodHandles.Lookup
PROVISIONAL API, WORK IN PROGRESS: A factory object for creating method handles, when the creation requires access checking. |
| Modifier and Type | Method and Description |
|---|---|
static MethodHandle |
arrayElementGetter(Class<?> arrayClass)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle giving read access to elements of an array. |
static MethodHandle |
arrayElementSetter(Class<?> arrayClass)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle giving write access to elements of an array. |
static MethodHandle |
collectArguments(MethodHandle target,
MethodType newType)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle which adapts the type of the given method handle to a new type, by collecting a series of trailing arguments into an array. |
static MethodHandle |
combineArguments(MethodHandle target,
int pos,
MethodHandle combiner)
PROVISIONAL API, WORK IN PROGRESS: Adapt a target method handle target by first processing
its arguments, and then calling the target. |
static MethodHandle |
convertArguments(MethodHandle target,
MethodType newType)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle which adapts the type of the given method handle to a new type, by pairwise argument conversion, and/or varargs conversion. |
static MethodHandle |
dropArguments(MethodHandle target,
int pos,
Class<?>... valueTypes)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle which calls the original method handle, after dropping the given argument(s) at the given position. |
static MethodHandle |
exactInvoker(MethodType type)
PROVISIONAL API, WORK IN PROGRESS: Give a method handle which will take a invoke any method handle of the given type. |
static MethodHandle |
genericInvoker(MethodType type,
int objectArgCount,
boolean varargs)
PROVISIONAL API, WORK IN PROGRESS: Give a method handle which will invoke any method handle of the given type on a standard set of Object type arguments. |
static MethodHandle |
guardWithTest(MethodHandle test,
MethodHandle target,
MethodHandle fallback)
PROVISIONAL API, WORK IN PROGRESS: Make a method handle which adapts a target method handle, by guarding it with a test, a boolean-valued method handle. |
static MethodHandle |
insertArgument(MethodHandle target,
int pos,
Object value)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle which calls the original method handle, after inserting the given argument at the given position. |
static Object |
invoke_0(MethodHandle target)
|
static Object |
invoke_1(MethodHandle target,
Object a0)
|
static Object |
invoke_2(MethodHandle target,
Object a0,
Object a1)
|
static Object |
invoke_3(MethodHandle target,
Object a0,
Object a1,
Object a2)
|
static Object |
invoke_4(MethodHandle target,
Object a0,
Object a1,
Object a2,
Object a3)
|
static Object |
invoke(MethodHandle target,
Object... arguments)
PROVISIONAL API, WORK IN PROGRESS: Call the invoke method of a given method handle,
with arguments that exactly match the parameter types of the method handle. |
static MethodHandles.Lookup |
lookup()
|
static MethodHandle |
permuteArguments(MethodHandle target,
MethodType newType,
int[] reorder)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle which adapts the calling sequence of the given method handle to a new type, by reordering the arguments. |
static MethodHandle |
spreadArguments(MethodHandle target,
MethodType newType)
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle which adapts the type of the given method handle to a new type, by spreading the final argument. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static MethodHandles.Lookup lookup()
public static MethodHandle arrayElementGetter(Class<?> arrayClass)
throws IllegalArgumentException
int.
arrayClass - an array typeIllegalArgumentException - if arrayClass is not an array type
public static MethodHandle arrayElementSetter(Class<?> arrayClass)
throws IllegalArgumentException
IllegalArgumentException - if arrayClass is not an array type
public static Object invoke(MethodHandle target,
Object... arguments)
invoke method of a given method handle,
with arguments that exactly match the parameter types of the method handle.
The length of the arguments array must equal the parameter count
of the target's type.
The arguments array is spread into separate arguments, and
basic reference and unboxing conversions are applied.
In order to match the type of the target, the following argument conversions are applied as necessary:
byte to int
This call is a convenience method for the following code:
MethodHandle invoker = MethodHandles.genericInvoker(target.type(), 0, true); Object result = invoker.invoke(arguments);
target - the method handle to invokearguments - the arguments to pass to the targetpublic static Object invoke_0(MethodHandle target)
public static Object invoke_1(MethodHandle target,
Object a0)
public static Object invoke_2(MethodHandle target,
Object a0,
Object a1)
public static Object invoke_3(MethodHandle target,
Object a0,
Object a1,
Object a2)
public static Object invoke_4(MethodHandle target,
Object a0,
Object a1,
Object a2,
Object a3)
public static MethodHandle genericInvoker(MethodType type,
int objectArgCount,
boolean varargs)
Object type arguments.
The the resulting invoker will be a method handle with the following
arguments:
MethodHandle target
Object values
Object[] array containing more arguments
Object reference,
boxing a primitive value if the original type returns a primitive,
and always null if the original type returns void.
This is a convenience method equivalent to the following code:
MethodHandle invoker = exactInvoker(type);
MethodType genericType = MethodType.makeGeneric(objectArgCount, varargs);
genericType = genericType.insertParameterType(0, MethodHandle.class);
if (!varargs)
return convertArguments(invoker, genericType);
else
return spreadArguments(invoker, genericType);
type - the desired target typeobjectArgCount - number of fixed (non-varargs) Object argumentsvarargs - if true, the invoker will accept a final Object[] argumentpublic static MethodHandle exactInvoker(MethodType type)
MethodHandle.
This is a convenience method equivalent to the following code:
MethodHandles.lookup().findVirtual(MethodHandle.class, "invoke", type);
type - the desired target type
public static MethodHandle convertArguments(MethodHandle target,
MethodType newType)
If the original type and new type are equal, returns target.
The following conversions are applied as needed both to arguments and return types. Let T0 and T1 be the differing new and old parameter types (or old and new return types) for corresponding values passed by the new and old method types.
If an ordinary (non-varargs) parameter of the new type is to be boxed in a varargs parameter of the old type of type T1[], then T1 is the element type of the varargs array. Otherwise, if a varargs parameter of the new type of type T0[] is to be spread into one or more outgoing old type parameters, then T0 is the element type of the If the new type is varargs and the old type is not, the varargs argument will be checked and must be a non-null array of exactly the right length. If there are no parameters in the old type corresponding to the new varargs parameter, the varargs argument is also allowed to be null.
Given those types T0, T1, one of the following conversions is applied if possible:
target - the method handle to invoke after arguments are retypednewType - the expected type of the new method handletarget after performing
any necessary argument conversions, and arranges for any
necessary return value conversionsWrongMethodTypeException - if the conversion cannot be made
public static MethodHandle permuteArguments(MethodHandle target,
MethodType newType,
int[] reorder)
The given array controls the reordering.
Call #I the number of incoming parameters (the value
newType.parameterCount(), and call #O the number
of outgoing parameters (the value target.type().parameterCount()).
Then the length of the reordering array must be #O,
and each element must be a non-negative number less than #I.
For every N less than #O, the N-th
outgoing argument will be taken from the I-th incoming
argument, where I is reorder[N].
The reordering array need not specify an actual permutation. An incoming argument will be duplicated if its index appears more than once in the array, and an incoming argument will be dropped if its index does not appear in the array.
Pairwise conversions are applied as needed to arguments and return
values, as with convertArguments(java.dyn.MethodHandle, java.dyn.MethodType).
target - the method handle to invoke after arguments are reorderednewType - the expected type of the new method handlereorder - a string which controls the reorderingtarget after performing
any necessary argument motion and conversions, and arranges for any
necessary return value conversions
public static MethodHandle spreadArguments(MethodHandle target,
MethodType newType)
The final parameter type of the new type must be an array type T[]. This is the type of what is called the spread argument. All other arguments of the new type are called ordinary arguments.
The ordinary arguments of the new type are pairwise converted
to the initial parameter types of the old type, according to the
rules in convertArguments(java.dyn.MethodHandle, java.dyn.MethodType).
Any additional arguments in the old type
are converted from the array element type T,
again according to the rules in convertArguments(java.dyn.MethodHandle, java.dyn.MethodType).
The return value is converted according likewise.
The call verifies that the spread argument is in fact an array of exactly the type length, i.e., the excess number of arguments in the old type over the ordinary arguments in the new type. If there are no excess arguments, the spread argument is also allowed to be null.
target - the method handle to invoke after the argument is prependednewType - the expected type of the new method handle
public static MethodHandle collectArguments(MethodHandle target,
MethodType newType)
This method is inverse to spreadArguments(java.dyn.MethodHandle, java.dyn.MethodType).
The final parameter type of the old type must be an array type T[],
which is the type of what is called the spread argument.
The trailing arguments of the new type which correspond to
the spread argument are all converted to type T and collected
into an array before the original method is called.
ISSUE: Unify this with combineArguments. CollectArguments is combineArguments with (a) new Object[]{...} as a combiner, and (b) the combined arguments dropped, in favor of the combined result.
target - the method handle to invoke after the argument is prependednewType - the expected type of the new method handle
public static MethodHandle insertArgument(MethodHandle target,
int pos,
Object value)
The given argument object must match the dropped argument type. If the dropped argument type is a primitive, the argument object must be a wrapper, and is unboxed to produce the primitive.
The pos may range between zero and N (inclusively), where N is the number of argument types in target, meaning to insert the new argument as the first or last (respectively), or somewhere in between.
target - the method handle to invoke after the argument is insertedpos - where to insert the argument (zero for the first)value - the argument to insert
public static MethodHandle dropArguments(MethodHandle target,
int pos,
Class<?>... valueTypes)
The pos may range between zero and N-1, where N is the number of argument types in target, meaning to drop the first or last argument (respectively), or an argument somewhere in between.
target - the method handle to invoke after the argument is droppedvalueTypes - the type(s) of the argument to droppos - which argument to drop (zero for the first)
public static MethodHandle guardWithTest(MethodHandle test,
MethodHandle target,
MethodHandle fallback)
Here is pseudocode for the resulting adapter:
signature T(A...);
boolean test(A...);
T target(A...);
T fallback(A...);
T adapter(A... a) {
if (test(a...))
return target(a...);
else
return fallback(a...);
}
test - method handle used for test, must return booleantarget - method handle to call if test passesfallback - method handle to call if test failsIllegalArgumentException - if test does not return boolean,
or if all three method types do not match (with the return
type of test changed to match that of target).
public static MethodHandle combineArguments(MethodHandle target,
int pos,
MethodHandle combiner)
target by first processing
its arguments, and then calling the target.
The initial processing is performed by a second method handle, the combiner.
After this, control passes to the target, with the same arguments.
The return value of the combiner is inserted into the argument list
for the target at the indicated position pos, if it is non-negative.
Except for this inserted argument (if any), the argument types of
the target target and the combiner must be identical.
(Note that dropArguments(java.dyn.MethodHandle, int, java.lang.Class>...) can be used to remove any arguments
that either the combiner or target does not wish to receive.)
The combiner handle must have the same argument types as the
target handle, but must return MethodHandle instead of
the ultimate return type. The returned method handle, in turn,
is required to have exactly the given final method type.
Here is pseudocode for the resulting adapter:
signature V(A[pos]..., B...);
signature T(A[pos]..., V, B...);
T target(A... a, V v, B... b);
V combiner(A..., B...);
T adapter(A... a, B... b) {
V v = combiner(a..., b...);
return target(a..., v, b...);
}
target - the method handle to invoke after arguments are combinedpos - where the return value of combiner is to
be inserted as an argument to targetcombiner - method handle to call initially on the incoming argumentsIllegalArgumentException - if combiner does not itself
return either void or the pos-th argument of target,
or does not have the same argument types as target
(minus the inserted argument)
|
Java™ Platform Standard Ed. 7 DRAFT ea-b76 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Use is subject to license terms.