Package com.headius.invokebinder
Class Binder
java.lang.Object
com.headius.invokebinder.Binder
The Binder class provides a DSL for building a chain of MethodHandles using
various of the adaptations provided by java.lang.invoke.MethodHandles. The
transformations are pushed into a stack, allowing the DSL to operate forward
from an incoming signature rather than backward from a target handle. This
is often conceptually easier to understand, and certainly easier to read.
The transformations are also applied simultaneously to the starting
java.lang.invoke.MethodType, allowing Binder to check at each step whether
the adaptation is valid.
Here's a typical use, starting with a signature that takes two Strings and
returns a String, dropping and inserting arguments, casting to a target
signature, and finally calling a target handle with that signature.
MethodHandle mh = Binder .from(String.class, String.class, String.class) // String w(String, String) .drop(1, String.class) // String x(String) .insert(0, 'hello') // String y(String, String) .cast(String.class, CharSequence.class, Object.class) // String z(CharSequence, Object)String .invoke(someTargetHandle);
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new Binder using the given invokebinder.Construct a new Binder using the given invokebinder plus an additional transformBinder
(Binder source, Transform transform, MethodType type) Construct a new Binder using the given invokebinder plus an additional transform and current typeBinder
(MethodHandles.Lookup lookup, Binder source) Construct a new Binder using the given Lookup and invokebinder.Binder
(MethodHandles.Lookup lookup, MethodType start) Construct a new Binder, starting from a given Lookup and MethodType.Binder
(MethodType start) Construct a new Binder, starting from a given MethodType. -
Method Summary
Modifier and TypeMethodDescriptionappend
(boolean value) Append to the argument list the given boolean value.append
(byte value) Append to the argument list the given byte value.append
(char value) Append to the argument list the given char value.append
(double value) Append to the argument list the given double value.append
(float value) Append to the argument list the given float value.append
(int value) Append to the argument list the given int value.append
(long value) Append to the argument list the given long value.append
(short value) Append to the argument list the given short value.Append to the argument list the given argument values with the specified types.Append to the argument list the given argument value with the specified type.Append to the argument list the given argument value(s).Apply the chain of transforms and bind them to an array varhandle operation.arrayGet()
Apply the chain of transforms and bind them to an array element get.Apply the chain of transforms and bind them to an acquire-fenced array element get.Apply the chain of transforms and bind them to an opaque (no ordering guarantee) array element get.Apply the chain of transforms and bind them to a volatile array element get.arraySet()
Apply the chain of transforms and bind them to an array element set.Apply the chain of transforms and bind them to a release-fenced array element set.Apply the chain of transforms and bind them to an opaque (no ordering guarantee) array element set.Apply the chain of transforms and bind them to a volatile array element set.branch
(MethodHandle test, MethodHandle truePath, MethodHandle falsePath) Apply the chain of transforms and bind them to a boolean branch as from java.lang.invoke.MethodHandles.guardWithTest.Cast the incoming arguments to the given MethodType.cast
(MethodType type) Cast the incoming arguments to the given MethodType.castVirtual
(Class<?> returnType, Class<?> firstType, Class<?>... restTypes) Cast the incoming arguments to the given MethodType.catchException
(Class<? extends Throwable> throwable, MethodHandle function) Catch the given exception type from the downstream chain and handle it with the given function.Box a range of incoming arguments into the given array type.collect
(int index, int count, Class<?> type, MethodHandle collector) Box a range of incoming arguments into the given array type using the given constructor to construct the array.Box all incoming arguments from the given position onward into the given array type.collect
(int index, Class<?> type, MethodHandle collector) Box all incoming arguments from the given position onward into the given array type.Apply the tranforms, binding them to a constant value that will propagate back through the chain.Convert the incoming arguments to the given MethodType.convert
(MethodType target) Convert the incoming arguments to the given MethodType.drop
(int index) Drop a single argument at the given index.drop
(int index, int count) Drop from the given index a number of arguments.dropAll()
Drop all arguments from this handle chainDrop a single argument at the beginning of the argument list.dropFirst
(int count) Drop from the end of the argument list a number of arguments.dropLast()
Drop a single argument at the end of the argument list.dropLast
(int count) Drop from the end of the argument list a number of arguments.filter
(int index, MethodHandle... functions) Filter incoming arguments, from the given index, replacing each with the result of calling the associated function in the given list.filterForward
(int index, MethodHandle... functions) Filter incoming arguments, from the given index, replacing each with the result of calling the associated function in the given list.filterReturn
(MethodHandle function) Filter return value, using a function that produces the current return type from another type.fold
(MethodHandle function) Process the incoming arguments using the given handle, inserting the result as the first argument.foldStatic
(Class<?> target, String method) Process the incoming arguments by calling the given static method on the given class, inserting the result as the first argument.foldStatic
(MethodHandles.Lookup lookup, Class<?> target, String method) Process the incoming arguments by calling the given static method on the given class, inserting the result as the first argument.foldVirtual
(MethodHandles.Lookup lookup, String method) Process the incoming arguments by calling the given method on the first argument, inserting the result as the first argument.foldVirtual
(String method) Process the incoming arguments by calling the given method on the first argument, inserting the result as the first argument.foldVoid
(MethodHandle function) Process the incoming arguments using the given handle, leaving the argument list unmodified.static Binder
Construct a new Binder, starting from a given invokebinder.static Binder
Construct a new Binder using a return type.static Binder
Construct a new Binder using a return type and argument types.static Binder
Construct a new Binder using a return type and argument types.static Binder
from
(MethodHandles.Lookup lookup, Binder start) Construct a new Binder, starting from a given invokebinder.static Binder
from
(MethodHandles.Lookup lookup, Class<?> returnType) Construct a new Binder using a return type.static Binder
from
(MethodHandles.Lookup lookup, Class<?> returnType, Class<?>[] argTypes) Construct a new Binder using a return type and argument types.static Binder
from
(MethodHandles.Lookup lookup, Class<?> returnType, Class<?> argType0, Class<?>... argTypes) Construct a new Binder using a return type and argument types.static Binder
from
(MethodHandles.Lookup lookup, MethodType start) Construct a new Binder, starting from a given MethodType.static Binder
from
(MethodType start) Construct a new Binder, starting from a given MethodType.getField
(MethodHandles.Lookup lookup, String name) Apply the chain of transforms and bind them to an object field retrieval specified using the end signature plus the given class and name.getFieldQuiet
(MethodHandles.Lookup lookup, String name) Apply the chain of transforms and bind them to an object field retrieval specified using the end signature plus the given class and name.getStatic
(MethodHandles.Lookup lookup, Class<?> target, String name) Apply the chain of transforms and bind them to a static field retrieval specified using the end signature plus the given class and name.getStaticQuiet
(MethodHandles.Lookup lookup, Class<?> target, String name) Apply the chain of transforms and bind them to a static field retrieval specified using the end signature plus the given class and name.identity()
Apply the tranforms, binding them to a handle that will simply return its sole argument as its return value.insert
(int index, boolean value) Insert at the given index the given boolean value.insert
(int index, byte value) Insert at the given index the given byte value.insert
(int index, char value) Insert at the given index the given char value.insert
(int index, double value) Insert at the given index the given double value.insert
(int index, float value) Insert at the given index the given float value.insert
(int index, int value) Insert at the given index the given int value.insert
(int index, long value) Insert at the given index the given long value.insert
(int index, short value) Insert at the given index the given short value.Insert at the given index the given argument value(s).Insert at the given index the given argument value.Insert at the given index the given argument value(s).invoke
(MethodHandle target) Apply the chain of transforms with the target method handle as the final endpoint.invoke
(MethodHandles.Lookup lookup, Method method) Apply the chain of transforms and bind them to a static method specified using the end signature plus the given class and method.invokeConstructor
(MethodHandles.Lookup lookup, Class<?> target) Apply the chain of transforms and bind them to a constructor specified using the end signature plus the given class.invokeConstructorQuiet
(MethodHandles.Lookup lookup, Class<?> target) Apply the chain of transforms and bind them to a constructor specified using the end signature plus the given class.invokeQuiet
(MethodHandles.Lookup lookup, Method method) Apply the chain of transforms and bind them to a static method specified using the end signature plus the given class and method.invoker()
Produce a MethodHandle that invokes its leading MethodHandle argument with the remaining arguments, returning the result.invokeSpecial
(MethodHandles.Lookup lookup, String name, Class<?> caller) Apply the chain of transforms and bind them to a special method specified using the end signature plus the given class and name.invokeSpecialQuiet
(MethodHandles.Lookup lookup, String name, Class<?> caller) Apply the chain of transforms and bind them to a special method specified using the end signature plus the given class and name.invokeStatic
(MethodHandles.Lookup lookup, Class<?> target, String name) Apply the chain of transforms and bind them to a static method specified using the end signature plus the given class and name.invokeStaticQuiet
(MethodHandles.Lookup lookup, Class<?> target, String name) Apply the chain of transforms and bind them to a static method specified using the end signature plus the given class and name.invokeVirtual
(MethodHandles.Lookup lookup, String name) Apply the chain of transforms and bind them to a virtual method specified using the end signature plus the given class and name.invokeVirtualQuiet
(MethodHandles.Lookup lookup, String name) Apply the chain of transforms and bind them to a virtual method specified using the end signature plus the given class and name.logType()
Log the current MethodType as info.nop()
Apply all transforms to an endpoint that does absolutely nothing.permute
(int... reorder) Permute the incoming arguments to a new sequence specified by the given values.prepend
(boolean value) Prepend to the argument list the given boolean value.prepend
(byte value) Prepend to the argument list the given byte value.prepend
(char value) Prepend to the argument list the given char value.prepend
(double value) Prepend to the argument list the given double value.prepend
(float value) Prepend to the argument list the given float value.prepend
(int value) Prepend to the argument list the given int value.prepend
(long value) Prepend to the argument list the given long value.prepend
(short value) Prepend to the argument list the given short value.Prepend to the argument list the given argument values with the specified types.Prepend to the argument list the given argument value with the specified typePrepend to the argument list the given argument value(s).Println the current MethodType to stdout.printType
(PrintStream ps) Println the current MethodType to the given stream.setField
(MethodHandles.Lookup lookup, String name) Apply the chain of transforms and bind them to an object field assignment specified using the end signature plus the given class and name.setFieldQuiet
(MethodHandles.Lookup lookup, String name) Apply the chain of transforms and bind them to an object field assignment specified using the end signature plus the given class and name.setStatic
(MethodHandles.Lookup lookup, Class<?> target, String name) Apply the chain of transforms and bind them to an object field assignment specified using the end signature plus the given class and name.setStaticQuiet
(MethodHandles.Lookup lookup, Class<?> target, String name) Apply the chain of transforms and bind them to an object field assignment specified using the end signature plus the given class and name.spread
(int count) Spread a trailing array argument into the given number of arguments of the type of the array.Spread a trailing array argument into the specified argument types.Throw the current signature's sole Throwable argument.Join this binder to an existing one by applying its transformations after this one.toJava
(MethodType incoming) Produce Java code that would perform equivalent operations to this binder.tryFinally
(MethodHandle post) Apply transforms to run the given handle's logic as a "finally" block.type()
The current MethodType, were the handle chain to terminate at this point.Box all incoming arguments from the given position onward into the given array type.withLookup
(MethodHandles.Lookup lookup) Use an alternate java.lang.invoke.MethodHandles.Lookup as the default for any direct handles created.
-
Constructor Details
-
Binder
Construct a new Binder, starting from a given MethodType. Use a public java.lang.invoke.MethodHandles.Lookup for retrieving direct handles.- Parameters:
start
- the starting MethodType, for calls entering the eventual chain
-
Binder
Construct a new Binder, starting from a given Lookup and MethodType.- Parameters:
lookup
- the Lookup context to use for direct handlesstart
- the starting MethodType, for calls entering the eventual chain
-
Binder
Construct a new Binder using the given invokebinder.- Parameters:
source
- a Binder to duplicate
-
Binder
Construct a new Binder using the given Lookup and invokebinder.- Parameters:
lookup
- the Lookup context to use for direct handlessource
- the source Binder
-
Binder
Construct a new Binder using the given invokebinder plus an additional transform- Parameters:
source
- the source Bindertransform
- the additional Transform
-
Binder
Construct a new Binder using the given invokebinder plus an additional transform and current type- Parameters:
source
- the source Bindertransform
- the additional Transformtype
- the new current type resulting from the transform
-
-
Method Details
-
from
Construct a new Binder, starting from a given MethodType.- Parameters:
start
- the starting MethodType, for calls entering the eventual chain- Returns:
- the Binder object
-
from
Construct a new Binder, starting from a given MethodType.- Parameters:
lookup
- the Lookup context to use for direct handlesstart
- the starting MethodType, for calls entering the eventual chain- Returns:
- the Binder object
-
from
Construct a new Binder using a return type.- Parameters:
returnType
- the return type of the incoming signature- Returns:
- the Binder object
-
from
Construct a new Binder using a return type.- Parameters:
lookup
- the Lookup context to use for direct handlesreturnType
- the return type of the incoming signature- Returns:
- the Binder object
-
from
Construct a new Binder using a return type and argument types.- Parameters:
returnType
- the return type of the incoming signatureargTypes
- the argument types of the incoming signature- Returns:
- the Binder object
-
from
Construct a new Binder using a return type and argument types.- Parameters:
lookup
- the Lookup context to use for direct handlesreturnType
- the return type of the incoming signatureargTypes
- the argument types of the incoming signature- Returns:
- the Binder object
-
from
Construct a new Binder using a return type and argument types.- Parameters:
returnType
- the return type of the incoming signatureargType0
- the first argument type of the incoming signatureargTypes
- the remaining argument types of the incoming signature- Returns:
- the Binder object
-
from
public static Binder from(MethodHandles.Lookup lookup, Class<?> returnType, Class<?> argType0, Class<?>... argTypes) Construct a new Binder using a return type and argument types.- Parameters:
lookup
- the Lookup context to use for direct handlesreturnType
- the return type of the incoming signatureargType0
- the first argument type of the incoming signatureargTypes
- the remaining argument types of the incoming signature- Returns:
- the Binder object
-
from
Construct a new Binder, starting from a given invokebinder.- Parameters:
start
- the starting invokebinder; the new one will start with the current endpoint type of the given invokebinder- Returns:
- the Binder object
-
from
Construct a new Binder, starting from a given invokebinder.- Parameters:
lookup
- the Lookup context to use for direct handlesstart
- the starting invokebinder; the new one will start with the current endpoint type of the given invokebinder- Returns:
- the Binder object
-
to
Join this binder to an existing one by applying its transformations after this one.- Parameters:
other
- the Binder containing the set of transformations to append- Returns:
- a new Binder combining this Binder with the target Binder
-
withLookup
Use an alternate java.lang.invoke.MethodHandles.Lookup as the default for any direct handles created.- Parameters:
lookup
- the new Lookup context to use- Returns:
- a new Binder with the given lookup
-
type
The current MethodType, were the handle chain to terminate at this point.- Returns:
- the current MethodType
-
printType
Println the current MethodType to the given stream.- Parameters:
ps
- a PrintStream to which to println the current MethodType- Returns:
- this Binding
-
printType
Println the current MethodType to stdout.- Returns:
- this Binding
-
logType
Log the current MethodType as info.- Returns:
- this Binding
-
insert
Insert at the given index the given boolean value.- Parameters:
index
- the index at which to insert the argument valuevalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given byte value.- Parameters:
index
- the index at which to insert the argument valuevalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given short value.- Parameters:
index
- the index at which to insert the argument valuevalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given char value.- Parameters:
index
- the index at which to insert the argument valuevalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given int value.- Parameters:
index
- the index at which to insert the argument valuevalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given long value.- Parameters:
index
- the index at which to insert the argument valuevalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given float value.- Parameters:
index
- the index at which to insert the argument valuevalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given double value.- Parameters:
index
- the index at which to insert the argument valuevalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given argument value(s).- Parameters:
index
- the index at which to insert the argument valuevalues
- the value(s) to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given argument value.- Parameters:
index
- the index at which to insert the argument valuetype
- the actual type to use, rather than getClassvalue
- the value to insert- Returns:
- a new Binder
-
insert
Insert at the given index the given argument value(s).- Parameters:
index
- the index at which to insert the argument valuetypes
- the actual types to use, rather than getClassvalues
- the value(s) to insert- Returns:
- a new Binder
-
append
Append to the argument list the given boolean value.- Parameters:
value
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given byte value.- Parameters:
value
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given short value.- Parameters:
value
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given char value.- Parameters:
value
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given int value.- Parameters:
value
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given long value.- Parameters:
value
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given float value.- Parameters:
value
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given double value.- Parameters:
value
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given argument value(s).- Parameters:
values
- the value(s) to append- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given boolean value.- Parameters:
value
- the value to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given byte value.- Parameters:
value
- the value to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given short value.- Parameters:
value
- the value to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given char value.- Parameters:
value
- the value to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given int value.- Parameters:
value
- the value to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given long value.- Parameters:
value
- the value to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given float value.- Parameters:
value
- the value to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given double value.- Parameters:
value
- the value to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given argument value(s).- Parameters:
values
- the value(s) to prepend- Returns:
- a new Binder
-
append
Append to the argument list the given argument value with the specified type.- Parameters:
type
- the actual type to use, rather than getClassvalue
- the value to append- Returns:
- a new Binder
-
append
Append to the argument list the given argument values with the specified types.- Parameters:
types
- the actual types to use, rather than getClassvalues
- the value(s) to append- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given argument value with the specified type- Parameters:
type
- the actual type to use, rather than getClassvalue
- the value(s) to prepend- Returns:
- a new Binder
-
prepend
Prepend to the argument list the given argument values with the specified types.- Parameters:
types
- the actual types to use, rather than getClassvalues
- the value(s) to prepend- Returns:
- a new Binder
-
drop
Drop a single argument at the given index.- Parameters:
index
- the index at which to drop an argument- Returns:
- a new Binder
-
drop
Drop from the given index a number of arguments.- Parameters:
index
- the index at which to start droppingcount
- the number of arguments to drop- Returns:
- a new Binder
-
dropLast
Drop a single argument at the end of the argument list.- Returns:
- a new Binder
-
dropLast
Drop from the end of the argument list a number of arguments.- Parameters:
count
- the number of arguments to drop- Returns:
- a new Binder
-
dropFirst
Drop a single argument at the beginning of the argument list.- Returns:
- a new Binder
-
dropFirst
Drop from the end of the argument list a number of arguments.- Parameters:
count
- the number of arguments to drop- Returns:
- a new Binder
-
dropAll
Drop all arguments from this handle chain- Returns:
- a new Binder
-
convert
Convert the incoming arguments to the given MethodType. The conversions applied are equivalent to those in MethodHandle.asType(MethodType).- Parameters:
target
- the target MethodType- Returns:
- a new Binder
-
convert
Convert the incoming arguments to the given MethodType. The conversions applied are equivalent to those in MethodHandle.asType(MethodType).- Parameters:
returnType
- the target return typeargTypes
- the target argument types- Returns:
- a new Binder
-
cast
Cast the incoming arguments to the given MethodType. The casts applied are equivalent to those in MethodHandles.explicitCastArguments(mh, MethodType).- Parameters:
type
- the target MethodType- Returns:
- a new Binder
-
cast
Cast the incoming arguments to the given MethodType. The casts applied are equivalent to those in MethodHandle.explicitCastArguments(MethodType).- Parameters:
returnType
- the target return typeargTypes
- the target argument types- Returns:
- a new Binder
-
castVirtual
Cast the incoming arguments to the given MethodType. The casts applied are equivalent to those in MethodHandle.explicitCastArguments(MethodType).- Parameters:
returnType
- the target return typefirstType
- the first argument type, usually a target typerestTypes
- the remaining target argument types- Returns:
- a new Binder
-
spread
Spread a trailing array argument into the specified argument types.- Parameters:
spreadTypes
- the types into which to spread the incoming Object[]- Returns:
- a new Binder
-
spread
Spread a trailing array argument into the given number of arguments of the type of the array.- Parameters:
count
- the new count of arguments to spread from the trailing array- Returns:
- a new Binder
-
collect
Box all incoming arguments from the given position onward into the given array type.- Parameters:
index
- the index from which to start boxing argstype
- the array type into which the args will be boxed- Returns:
- a new Binder
-
collect
Box all incoming arguments from the given position onward into the given array type.- Parameters:
index
- the index from which to start boxing argstype
- the array type into which the args will be boxedcollector
- a function to use for collecting the arguments- Returns:
- a new Binder
-
collect
Box a range of incoming arguments into the given array type.- Parameters:
index
- the index from which to start boxing argscount
- the count of arguments to boxtype
- the array type into which the args will be boxed- Returns:
- a new Binder
-
collect
Box a range of incoming arguments into the given array type using the given constructor to construct the array. The collector signature should match (T1, ..., Tn) where T is the type of the arguments being collected and n is the number of arguments being collected.- Parameters:
index
- the index from which to start boxing argscount
- the count of arguments to boxtype
- the array type into which the args will be boxedcollector
- a function to use for collecting the arguments- Returns:
- a new Binder
-
varargs
Box all incoming arguments from the given position onward into the given array type. This version accepts a variable number of incoming arguments.- Parameters:
index
- the index from which to start boxing argstype
- the array type into which the args will be boxed- Returns:
- a new Binder
-
permute
Permute the incoming arguments to a new sequence specified by the given values. Arguments may be duplicated or dropped in this sequence.- Parameters:
reorder
- the int offsets of the incoming arguments in the desired permutation- Returns:
- a new Binder
-
fold
Process the incoming arguments using the given handle, inserting the result as the first argument.- Parameters:
function
- the function that will process the incoming arguments. Its signature must match the current signature's arguments exactly.- Returns:
- a new Binder
-
foldVoid
Process the incoming arguments using the given handle, leaving the argument list unmodified.- Parameters:
function
- the function that will process the incoming arguments. Its signature must match the current signature's arguments exactly.- Returns:
- a new Binder
-
foldStatic
Process the incoming arguments by calling the given static method on the given class, inserting the result as the first argument.- Parameters:
lookup
- the java.lang.invoke.MethodHandles.Lookup to usetarget
- the class on which the method is definedmethod
- the method to invoke on the first argument- Returns:
- a new Binder
-
foldStatic
Process the incoming arguments by calling the given static method on the given class, inserting the result as the first argument.- Parameters:
target
- the class on which the method is definedmethod
- the method to invoke on the first argument- Returns:
- a new Binder
-
foldVirtual
Process the incoming arguments by calling the given method on the first argument, inserting the result as the first argument.- Parameters:
lookup
- the java.lang.invoke.MethodHandles.Lookup to usemethod
- the method to invoke on the first argument- Returns:
- a new Binder
-
foldVirtual
Process the incoming arguments by calling the given method on the first argument, inserting the result as the first argument.- Parameters:
method
- the method to invoke on the first argument- Returns:
- a new Binder
-
filter
Filter incoming arguments, from the given index, replacing each with the result of calling the associated function in the given list. Note that the order in which the filters are applied is undefined; OpenJDK produces handles that execute them in reverse order.- Parameters:
index
- the index of the first argument to filterfunctions
- the array of functions to transform the arguments- Returns:
- a new Binder
- See Also:
-
filterForward
Filter incoming arguments, from the given index, replacing each with the result of calling the associated function in the given list. This version guarantees left-to-right evaluation of filter functions, potentially at the cost of a more complex handle tree.- Parameters:
index
- the index of the first argument to filterfunctions
- the array of functions to transform the arguments- Returns:
- a new Binder
-
filterReturn
Filter return value, using a function that produces the current return type from another type. The new endpoint will have the return value that the filter function accepts as an argument.- Parameters:
function
- the array of functions to transform the arguments- Returns:
- a new Binder
-
tryFinally
Apply transforms to run the given handle's logic as a "finally" block. try { some_code // your eventual endpoint } finally { finally_logic // the given handle } The layering uses a combination of catch and fold to reuse the same target handle for both exceptional and non-exceptional paths. In essence, the result is equivalent to using the given post logic as both an exception handler (using catchException) and a "post fold" that runs after the main downstream handles have run.- Parameters:
post
- the logic that would live inside the "finally" block- Returns:
- a new Binder
-
catchException
Catch the given exception type from the downstream chain and handle it with the given function.- Parameters:
throwable
- the exception type to catchfunction
- the function to use for handling the exception- Returns:
- a new Binder
-
nop
Apply all transforms to an endpoint that does absolutely nothing. Useful for creating exception handlers in void methods that simply ignore the exception.- Returns:
- a handle that has all transforms applied and does nothing at its endpoint
-
throwException
Throw the current signature's sole Throwable argument. Return type does not matter, since it will never return.- Returns:
- a handle that has all transforms applied and which will eventually throw an exception
-
constant
Apply the tranforms, binding them to a constant value that will propagate back through the chain. The chain's expected return type at that point must be compatible with the given value's type.- Parameters:
value
- the constant value to put at the end of the chain- Returns:
- a handle that has all transforms applied in sequence up to the constant
-
identity
Apply the tranforms, binding them to a handle that will simply return its sole argument as its return value. The endpoint signature must have a single argument of the same type as its return type.- Returns:
- a handle that has all transforms applied in sequence
-
invoke
Apply the chain of transforms with the target method handle as the final endpoint. Produces a handle that has the transforms in given sequence. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
target
- the endpoint handle to bind to- Returns:
- a handle that has all transforms applied in sequence up to endpoint
-
invoke
public MethodHandle invoke(MethodHandles.Lookup lookup, Method method) throws IllegalAccessException Apply the chain of transforms and bind them to a static method specified using the end signature plus the given class and method. The method will be retrieved using the given Lookup and must match the end signature exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to unreflect the methodmethod
- the Method to unreflect- Returns:
- the full handle chain, bound to the given method
- Throws:
IllegalAccessException
- if the method is not accessible
-
invokeQuiet
Apply the chain of transforms and bind them to a static method specified using the end signature plus the given class and method. The method will be retrieved using the given Lookup and must match the end signature exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to unreflect the methodmethod
- the Method to unreflect- Returns:
- the full handle chain, bound to the given method
-
invokeStatic
public MethodHandle invokeStatic(MethodHandles.Lookup lookup, Class<?> target, String name) throws NoSuchMethodException, IllegalAccessException Apply the chain of transforms and bind them to a static method specified using the end signature plus the given class and name. The method will be retrieved using the given Lookup and must match the end signature exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to unreflect the methodtarget
- the class in which to find the methodname
- the name of the method to invoke- Returns:
- the full handle chain, bound to the given method
- Throws:
NoSuchMethodException
- if the method does not existIllegalAccessException
- if the method is not accessible
-
invokeStaticQuiet
Apply the chain of transforms and bind them to a static method specified using the end signature plus the given class and name. The method will be retrieved using the given Lookup and must match the end signature exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the methodtarget
- the class in which to find the methodname
- the name of the method to invoke- Returns:
- the full handle chain, bound to the given method
-
invokeVirtual
public MethodHandle invokeVirtual(MethodHandles.Lookup lookup, String name) throws NoSuchMethodException, IllegalAccessException Apply the chain of transforms and bind them to a virtual method specified using the end signature plus the given class and name. The method will be retrieved using the given Lookup and must match the end signature exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the methodname
- the name of the method to invoke- Returns:
- the full handle chain, bound to the given method
- Throws:
NoSuchMethodException
- if the method does not existIllegalAccessException
- if the method is not accessible
-
invokeVirtualQuiet
Apply the chain of transforms and bind them to a virtual method specified using the end signature plus the given class and name. The method will be retrieved using the given Lookup and must match the end signature exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the methodname
- the name of the method to invoke- Returns:
- the full handle chain, bound to the given method
-
invokeSpecial
public MethodHandle invokeSpecial(MethodHandles.Lookup lookup, String name, Class<?> caller) throws NoSuchMethodException, IllegalAccessException Apply the chain of transforms and bind them to a special method specified using the end signature plus the given class and name. The method will be retrieved using the given Lookup and must match the end signature exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the methodname
- the name of the method to invokecaller
- the calling class- Returns:
- the full handle chain, bound to the given method
- Throws:
NoSuchMethodException
- if the method does not existIllegalAccessException
- if the method is not accessible
-
invokeSpecialQuiet
Apply the chain of transforms and bind them to a special method specified using the end signature plus the given class and name. The method will be retrieved using the given Lookup and must match the end signature exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the methodname
- the name of the method to invokecaller
- the calling class- Returns:
- the full handle chain, bound to the given method
-
invokeConstructor
public MethodHandle invokeConstructor(MethodHandles.Lookup lookup, Class<?> target) throws NoSuchMethodException, IllegalAccessException Apply the chain of transforms and bind them to a constructor specified using the end signature plus the given class. The constructor will be retrieved using the given Lookup and must match the end signature's arguments exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the constructortarget
- the constructor's class- Returns:
- the full handle chain, bound to the given constructor
- Throws:
NoSuchMethodException
- if the constructor does not existIllegalAccessException
- if the constructor is not accessible
-
invokeConstructorQuiet
Apply the chain of transforms and bind them to a constructor specified using the end signature plus the given class. The constructor will be retrieved using the given Lookup and must match the end signature's arguments exactly. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the constructortarget
- the constructor's class- Returns:
- the full handle chain, bound to the given constructor
-
getField
public MethodHandle getField(MethodHandles.Lookup lookup, String name) throws NoSuchFieldException, IllegalAccessException Apply the chain of transforms and bind them to an object field retrieval specified using the end signature plus the given class and name. The field must match the end signature's return value and the end signature must take the target class or a subclass as its only argument. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the fieldname
- the field's name- Returns:
- the full handle chain, bound to the given field access
- Throws:
NoSuchFieldException
- if the field does not existIllegalAccessException
- if the field is not accessibleNoSuchFieldException
- if the field does not existIllegalAccessException
- if the field is not accessible
-
getFieldQuiet
Apply the chain of transforms and bind them to an object field retrieval specified using the end signature plus the given class and name. The field must match the end signature's return value and the end signature must take the target class or a subclass as its only argument. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the fieldname
- the field's name- Returns:
- the full handle chain, bound to the given field access
-
getStatic
public MethodHandle getStatic(MethodHandles.Lookup lookup, Class<?> target, String name) throws NoSuchFieldException, IllegalAccessException Apply the chain of transforms and bind them to a static field retrieval specified using the end signature plus the given class and name. The field must match the end signature's return value and the end signature must take no arguments. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the fieldtarget
- the class in which the field is definedname
- the field's name- Returns:
- the full handle chain, bound to the given field access
- Throws:
NoSuchFieldException
- if the field does not existIllegalAccessException
- if the field is not accessible or cannot be modified
-
getStaticQuiet
Apply the chain of transforms and bind them to a static field retrieval specified using the end signature plus the given class and name. The field must match the end signature's return value and the end signature must take no arguments. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the fieldtarget
- the class in which the field is definedname
- the field's name- Returns:
- the full handle chain, bound to the given field access
-
setField
public MethodHandle setField(MethodHandles.Lookup lookup, String name) throws NoSuchFieldException, IllegalAccessException Apply the chain of transforms and bind them to an object field assignment specified using the end signature plus the given class and name. The end signature must take the target class or a subclass and the field's type as its arguments, and its return type must be compatible with void. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the fieldname
- the field's name- Returns:
- the full handle chain, bound to the given field assignment
- Throws:
NoSuchFieldException
- if the field does not existIllegalAccessException
- if the field is not accessible or cannot be modified
-
setFieldQuiet
Apply the chain of transforms and bind them to an object field assignment specified using the end signature plus the given class and name. The end signature must take the target class or a subclass and the field's type as its arguments, and its return type must be compatible with void. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the fieldname
- the field's name- Returns:
- the full handle chain, bound to the given field assignment
-
setStatic
public MethodHandle setStatic(MethodHandles.Lookup lookup, Class<?> target, String name) throws NoSuchFieldException, IllegalAccessException Apply the chain of transforms and bind them to an object field assignment specified using the end signature plus the given class and name. The end signature must take the target class or a subclass and the field's type as its arguments, and its return type must be compatible with void. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the fieldtarget
- the class in which the field is definedname
- the field's name- Returns:
- the full handle chain, bound to the given field assignment
- Throws:
NoSuchFieldException
- if the field does not existIllegalAccessException
- if the field is not accessible or cannot be modified
-
setStaticQuiet
Apply the chain of transforms and bind them to an object field assignment specified using the end signature plus the given class and name. The end signature must take the target class or a subclass and the field's type as its arguments, and its return type must be compatible with void. If the final handle's type does not exactly match the initial type for this Binder, an additional cast will be attempted. This version is "quiet" in that it throws an unchecked InvalidTransformException if the target method does not exist or is inaccessible.- Parameters:
lookup
- the MethodHandles.Lookup to use to look up the fieldtarget
- the class in which the field is definedname
- the field's name- Returns:
- the full handle chain, bound to the given field assignment
-
arraySet
Apply the chain of transforms and bind them to an array element set. The signature at the endpoint must return void and receive the array type, int index, and array element type.- Returns:
- the full handle chain, bound to an array element set.
-
arraySetVolatile
Apply the chain of transforms and bind them to a volatile array element set.- See Also:
-
arraySetAcquire
Apply the chain of transforms and bind them to a release-fenced array element set.- See Also:
-
arraySetOpaque
Apply the chain of transforms and bind them to an opaque (no ordering guarantee) array element set.- See Also:
-
arrayGet
Apply the chain of transforms and bind them to an array element get. The signature at the endpoint must return the array element type and receive the array type and int index.- Returns:
- the full handle chain, bound to an array element get.
-
arrayGetVolatile
Apply the chain of transforms and bind them to a volatile array element get.- See Also:
-
arrayGetAcquire
Apply the chain of transforms and bind them to an acquire-fenced array element get.- See Also:
-
arrayGetOpaque
Apply the chain of transforms and bind them to an opaque (no ordering guarantee) array element get.- See Also:
-
arrayAccess
Apply the chain of transforms and bind them to an array varhandle operation. The signature at the endpoint must match the VarHandle access type passed in. -
branch
Apply the chain of transforms and bind them to a boolean branch as from java.lang.invoke.MethodHandles.guardWithTest. As with GWT, the current endpoint signature must match the given target and fallback signatures.- Parameters:
test
- the test handletruePath
- the target handlefalsePath
- the fallback handle- Returns:
- the full handle chain bound to a branch
-
invoker
Produce a MethodHandle that invokes its leading MethodHandle argument with the remaining arguments, returning the result.- Returns:
- a new handle that invokes its leading MethodHandle argument
-
toJava
Produce Java code that would perform equivalent operations to this binder.- Returns:
- Java code for the handle adaptations this Binder would produce.
-