Package com.headius.invokebinder
Class SmartHandle
java.lang.Object
com.headius.invokebinder.SmartHandle
A tuple of a Signature and a java.lang.invoke.MethodHandle, providing
features of both plus a number of MethodHandles.* methods in a simpler form.
SmartHandle is provided as a way to couple a given MethodHandle to a
Signature, allowing future adaptation of the MethodHandle to proceed using
Signature's various shortcuts and conveniences.
Example:
// A signature that only wants the "context" and "args" arguments
public static final Signature ARG_COUNT_CHECK_FOLD = Signature
.returning(void.class)
.appendArg("args", Object[].class);
// The actual target signature for arg count checking, with min and max ints
public static final Signature ARG_COUNT_CHECK_SIGNATURE = Signature
.returning(int.class)
.appendArg("args", Object[].class)
.appendArg("min", int.class)
.appendArg("max", int.class);
// A SmartHandle for the arity-checking method, using the fold and signature
// from above and inserting 1, 3 for min, max
SmartHandle arityCheck = SmartBinder
.from(ARITY_CHECK_FOLD)
.append("min", 1)
.append("max", 3)
.cast(ARITY_CHECK_SIGNATURE)
.invokeStaticQuiet(LOOKUP, ArgCountChecker.class, "checkArgumentCount");
// The variable-arity call contaings other arguments plus the Object[] args.
// Here, we can just fold with our arityCheck SmartHandle, which drops args
// we are not interested in, passes along the args array, and ignores the
// return value.
variableCall = SmartBinder
.from(VARIABLE_ARITY_SIGNATURE)
.foldVoid(arityCheck)
.invoke(directCall);
- Author:
- headius
-
Method Summary
Modifier and TypeMethodDescriptionApply an argument into the handle at the given index, returning a new SmartHandle.Apply an argument into the handle at the given name, returning a new SmartHandle.Apply an argument into the handle at the end, returning a new SmartHandle.Bind the first argument of this SmartHandle to the given object, returning a new adapted handle.Create a new SmartHandle that casts arguments from the given signature to the current signature's type with the new argument names.Create a new SmartHandle that casts arguments from the given return type and argument types to the current signature's type, using the same argument names.cast
(MethodType incoming) Create a new SmartHandle that casts arguments from the given type to the current signature's type, using the same argument names.Create a new SmartHandle that converts arguments from the given signature to the current signature's type with the new argument names.Create a new SmartHandle that converts arguments from the given return type and argument types to the current signature's type, using the same argument names.convert
(MethodType incoming) Create a new SmartHandle that converts arguments from the given type to the current signature's type, using the same argument names.Drop an argument from the handle at the given index, returning a new SmartHandle.Drop an argument name and type from the handle at the given index, returning a new SmartHandle.Drop an argument from the handle at the end, returning a new SmartHandle.static SmartHandle
findStaticQuiet
(MethodHandles.Lookup lookup, Class<?> target, String name, Signature signature) Create a new SmartHandle by performing a lookup on the given target class for the given method name with the given signature.static SmartHandle
from
(Signature signature, MethodHandle handle) Create a new SmartHandle from the given Signature and MethodHandle.guard
(SmartHandle target, SmartHandle fallback) Use this SmartHandle as a test to guard target and fallback handles.guard
(MethodHandle target, MethodHandle fallback) Use this SmartHandle as a test to guard target and fallback handles.handle()
Get the MethodHandle of this SmartHandle.returnValue
(Class<?> type, Object value) Replace the return value with the given value, performing no other processing of the original value.Get the Signature of this SmartHandle.toString()
A human-readable String representation of this SamrtHandle.
-
Method Details
-
from
Create a new SmartHandle from the given Signature and MethodHandle.- Parameters:
signature
- the signature for the new smart handlehandle
- the method handle for the new smart handle- Returns:
- a new SmartHandle
-
findStaticQuiet
public static SmartHandle findStaticQuiet(MethodHandles.Lookup lookup, Class<?> target, String name, Signature signature) Create a new SmartHandle by performing a lookup on the given target class for the given method name with the given signature.- Parameters:
lookup
- the MethodHandles.Lookup object to usetarget
- the class where the method is locatedname
- the name of the methodsignature
- the signature of the method- Returns:
- a new SmartHandle based on the signature and looked-up MethodHandle
-
signature
Get the Signature of this SmartHandle.- Returns:
- the Signature of this SmartHandle
-
handle
Get the MethodHandle of this SmartHandle.- Returns:
- the MethodHandle of this SmartHandle
-
apply
Apply an argument into the handle at the given index, returning a new SmartHandle. The new handle will use the given value for the argument at the given index, accepting one fewer argument as a result. In other words, fix that argument (partial application) into the given handle.- Parameters:
index
- the index of the argument in the new SmartHandle's Signaturearg
- the argument value- Returns:
- a new SmartHandle that already has applied the given argument
-
apply
Apply an argument into the handle at the given name, returning a new SmartHandle. The new handle will use the given value for the argument at the given index, accepting one fewer argument as a result. In other words, fix that argument (partial application) into the given handle.- Parameters:
name
- the name of the argument in the new SmartHandle's Signaturearg
- the argument value- Returns:
- a new SmartHandle that already has applied the given argument
-
applyLast
Apply an argument into the handle at the end, returning a new SmartHandle. The new handle will use the given value for the last argument, accepting one fewer argument as a result. In other words, fix that argument (partial application) into the given handle.- Parameters:
arg
- the argument value- Returns:
- a new SmartHandle that already has applied the given argument
-
drop
Drop an argument name and type from the handle at the given index, returning a new SmartHandle.- Parameters:
beforeName
- name before which the dropped argument goesnewName
- name of the argumenttype
- type of the argument- Returns:
- a new SmartHandle with the additional argument
-
drop
Drop an argument from the handle at the given index, returning a new SmartHandle.- Parameters:
index
- index before which the dropped argument goesnewName
- name of the argumenttype
- type of the argument- Returns:
- a new SmartHandle with the additional argument
-
dropLast
Drop an argument from the handle at the end, returning a new SmartHandle.- Parameters:
newName
- name of the argumenttype
- type of the argument- Returns:
- a new SmartHandle with the additional argument
-
guard
Use this SmartHandle as a test to guard target and fallback handles.- Parameters:
target
- the "true" path for this handle's testfallback
- the "false" path for this handle's test- Returns:
- a MethodHandle that performs the test and branch
-
guard
Use this SmartHandle as a test to guard target and fallback handles.- Parameters:
target
- the "true" path for this handle's testfallback
- the "false" path for this handle's test- Returns:
- a new SmartHandle that performs the test and branch
-
bindTo
Bind the first argument of this SmartHandle to the given object, returning a new adapted handle.- Parameters:
obj
- the object to which to bind this handle's first argument- Returns:
- a new SmartHandle with the first argument dropped in favor of obj
-
convert
Create a new SmartHandle that converts arguments from the given type to the current signature's type, using the same argument names. This conversion is equivalent to MethodHandle#asType.- Parameters:
incoming
- the target MethodType from which arguments will be converted- Returns:
- a new SmartHandle that accepts the given argument types
-
convert
Create a new SmartHandle that converts arguments from the given return type and argument types to the current signature's type, using the same argument names. This conversion is equivalent to MethodHandle#asType.- Parameters:
returnType
- the return type of the new handleargTypes
- the argument types of the new handle- Returns:
- a new SmartHandle that accepts the given argument types
-
convert
Create a new SmartHandle that converts arguments from the given signature to the current signature's type with the new argument names. This conversion is equivalent to MethodHandle#asType.- Parameters:
incoming
- the target MethodType from which arguments will be converted- Returns:
- a new SmartHandle that accepts the given argument types
-
cast
Create a new SmartHandle that casts arguments from the given type to the current signature's type, using the same argument names. This casting is equivalent to MethodHandles#explicitCastArguments.- Parameters:
incoming
- the target MethodType from which arguments will be converted- Returns:
- a new SmartHandle that accepts the given argument types
-
cast
Create a new SmartHandle that casts arguments from the given signature to the current signature's type with the new argument names. This casting is equivalent to MethodHandle#asType.- Parameters:
incoming
- the target MethodType from which arguments will be converted- Returns:
- a new SmartHandle that accepts the given argument types
-
cast
Create a new SmartHandle that casts arguments from the given return type and argument types to the current signature's type, using the same argument names. This casting is equivalent to MethodHandle#asType.- Parameters:
returnType
- the return type of the new handleargTypes
- the argument types of the new handle- Returns:
- a new SmartHandle that accepts the given argument types
-
returnValue
Replace the return value with the given value, performing no other processing of the original value.- Parameters:
type
- the type for the new return valuevalue
- the new value to return- Returns:
- a new SmartHandle that returns the given value
-
toString
A human-readable String representation of this SamrtHandle.
-