Package com.headius.invokebinder
Class Signature
java.lang.Object
com.headius.invokebinder.Signature
Signature represents a series of method arguments plus their symbolic names.
In order to make it easier to permute arguments, track their flow, and debug
cases where reordering or permuting fails to work properly, the Signature
class also tracks symbolic names for all arguments. This allows permuting
by name or by name pattern, avoiding the error-prone juggling of int[] for
the standard MethodHandles.permuteArguments call.
A Signature is created starting using #thatReturns method, and expanded using
#withArgument for each named argument in sequence. Order is preserved.
A Signature can be mutated into another by manipuating the argument list as
with java.lang.invoke.MethodType, but using argument names and name patterns
instead of integer offsets.
Two signatures can be used to produce a permute array suitable for use in
java.lang.invoke.MethodHandles#permuteArguments using the #to methods. The
#to method can also accept a list of argument names, as a shortcut.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new signature with the given return value.Construct a new signature with the given return value, argument types, and argument names.Signature
(MethodType methodType, String... argNames) Construct a new signature with the given method type and argument names. -
Method Summary
Modifier and TypeMethodDescriptionAppend an argument (name + type) to the signature.appendArgs
(String[] names, Class... types) Append an argument (name + type) to the signature.argName
(int index) Retrieve the name of the argument at the given index.String[]
argNames()
The current argument names for this signature.int
Retrieve the offset of the given argument name in this signature's arguments.int
argOffsets
(String pattern) Retrieve the offset of the given argument name in this signature's arguments.argType
(int index) Get the argument type at the given index.Produce a new signature based on this one with a different return type.changeReturn
(Class retval) Create a new signature based on this one with a different return type.dropArg
(int index) Drops the argument at the given index.Drops the first argument with the given name.Drop the first argument from this signature.dropLast()
Drop the last argument from this signature.Create a new signature containing the same return value as this one, but omitting the specified arguments.Get the first argument name.Get the first argument type.Insert an argument (name + type) into the signature.Insert an argument (name + type) into the signature before the argument with the given name.insertArgs
(int index, String[] names, Class... types) Insert arguments (names + types) into the signature.insertArgs
(String beforeName, String[] names, Class... types) Insert arguments (names + types) into the signature before the argument with the given name.Get the last argument name.Get the last argument type.private int[]
nonMatchingTo
(String... otherArgNames) Create a new signature containing the same return value as this one, but only the specified arguments.permuteWith
(SmartHandle target) Produce a new SmartHandle by permuting this Signature's arguments to the Signature of a target SmartHandle.permuteWith
(MethodHandle target, String... permuteArgs) Produce a method handle permuting the arguments in this signature using the given permute arguments and targeting the given java.lang.invoke.MethodHandle.prependArg
(String name, Class type) Prepend an argument (name + type) to the signature.prependArgs
(String[] names, Class[] types) Prepend an argument (name + type) to the signature.replaceArg
(String oldName, String newName, Class newType) Replace the named argument with a new name and type.static Signature
Create a new signature returning the given type.Spread the trailing [] argument into its component type assigning given names.Spread the trailing [] argument into the given argument typesSpread the trailing [] argument into its component type assigning given names.int[]
Generate an array of argument offsets based on permuting this signature to the given signature.int[]
Generate an array of argument offsets based on permuting this signature to the given signature.toString()
Produce a human-readable representation of this signature.type()
The current java.lang.invoke.MethodType for this Signature.
-
Field Details
-
methodType
-
argNames
-
-
Constructor Details
-
Signature
Signature(Class retval) Construct a new signature with the given return value.- Parameters:
retval
- the return value for the new signature
-
Signature
Construct a new signature with the given return value, argument types, and argument names.- Parameters:
retval
- the return value for the new signatureargTypes
- the argument types for the new signatureargNames
- the argument names for the new signature
-
Signature
Signature(MethodType methodType, String... argNames) Construct a new signature with the given method type and argument names.- Parameters:
methodType
- the method type for the new signatureargNames
- the argument names for the new signature
-
-
Method Details
-
toString
Produce a human-readable representation of this signature. This representation uses Class#getSimpleName to improve readability. -
returning
Create a new signature returning the given type.- Parameters:
retval
- the return type for the new signature- Returns:
- the new signature
-
changeReturn
Create a new signature based on this one with a different return type.- Parameters:
retval
- the class for the new signature's return type- Returns:
- the new signature
-
asFold
Produce a new signature based on this one with a different return type.- Parameters:
retval
- the new return type for the new signature- Returns:
- the new signature
-
appendArg
Append an argument (name + type) to the signature.- Parameters:
name
- the name of the argumenttype
- the type of the argument- Returns:
- a new signature
-
appendArgs
Append an argument (name + type) to the signature.- Parameters:
name
- the name of the argumenttype
- the type of the argument- Returns:
- a new signature
-
prependArg
Prepend an argument (name + type) to the signature.- Parameters:
name
- the name of the argumenttype
- the type of the argument- Returns:
- a new signature
-
prependArgs
Prepend an argument (name + type) to the signature.- Parameters:
name
- the name of the argumenttype
- the type of the argument- Returns:
- a new signature
-
insertArg
Insert an argument (name + type) into the signature.- Parameters:
index
- the index at which to insertname
- the name of the new argumenttype
- the type of the new argument- Returns:
- a new signature
-
insertArg
Insert an argument (name + type) into the signature before the argument with the given name.- Parameters:
beforeName
- the name of the argument before which to insertname
- the name of the new argumenttype
- the type of the new argument- Returns:
- a new signature
-
insertArgs
Insert arguments (names + types) into the signature.- Parameters:
index
- the index at which to insertnames
- the names of the new argumentstypes
- the types of the new arguments- Returns:
- a new signature
-
insertArgs
Insert arguments (names + types) into the signature before the argument with the given name.- Parameters:
beforeName
- the name of the argument before which to insertnames
- the names of the new argumentstypes
- the types of the new arguments- Returns:
-
dropArg
Drops the first argument with the given name.- Parameters:
name
- the name of the argument to drop- Returns:
- a new signature
-
dropArg
Drops the argument at the given index.- Parameters:
index
- the index of the argument to drop- Returns:
- a new signature
-
dropLast
Drop the last argument from this signature.- Returns:
- a new signature
-
dropFirst
Drop the first argument from this signature.- Returns:
- a new signature
-
replaceArg
Replace the named argument with a new name and type.- Parameters:
oldName
- the old name of the argumentnewName
- the new name of the argument; can be the same as oldoldName
- the new type of the argument; can be the same as old
-
spread
Spread the trailing [] argument into the given argument types -
spread
Spread the trailing [] argument into its component type assigning given names. -
spread
Spread the trailing [] argument into its component type assigning given names. -
type
The current java.lang.invoke.MethodType for this Signature.- Returns:
- the current method type
-
argNames
The current argument names for this signature.- Returns:
- the current argument names
-
argName
Retrieve the name of the argument at the given index.- Parameters:
index
- the index from which to get the argument name- Returns:
- the argument name
-
argOffset
Retrieve the offset of the given argument name in this signature's arguments. If the argument name is not in the argument list, returns -1.- Parameters:
name
- the argument name to search for- Returns:
- the offset at which the argument name was found or -1
-
argOffsets
Retrieve the offset of the given argument name in this signature's arguments. If the argument name is not in the argument list, returns -1.- Parameters:
name
- the argument name to search for- Returns:
- the offset at which the argument name was found or -1
-
firstArgName
Get the first argument name.- Returns:
- the first argument name
-
lastArgName
Get the last argument name.- Returns:
- the last argument name
-
argType
Get the argument type at the given index.- Parameters:
index
- the index from which to get the argument type- Returns:
- the argument type
-
firstArgType
Get the first argument type.- Returns:
- the first argument type
-
lastArgType
Get the last argument type.- Returns:
- the last argument type
-
permute
Create a new signature containing the same return value as this one, but only the specified arguments.- Parameters:
permuteArgs
- the names of the arguments to preserve- Returns:
- the new signature
-
exclude
Create a new signature containing the same return value as this one, but omitting the specified arguments. Blacklisting to #permute's whitelisting.- Parameters:
excludeArgs
- the names of the arguments to exclude- Returns:
- the new signature
-
permuteWith
Produce a method handle permuting the arguments in this signature using the given permute arguments and targeting the given java.lang.invoke.MethodHandle. Example:Signature sig = Signature.returning(String.class).appendArg("a", int.class).appendArg("b", int.class); MethodHandle handle = handleThatTakesOneInt(); MethodHandle newHandle = sig.permuteTo(handle, "b");
- Parameters:
target
- the method handle to targetpermuteArgs
- the arguments to permute- Returns:
- a new handle that permutes appropriate positions based on the given permute args
-
permuteWith
Produce a new SmartHandle by permuting this Signature's arguments to the Signature of a target SmartHandle. The new SmartHandle's signature will match this one, permuting those arguments and invoking the target handle.- Parameters:
target
- the SmartHandle to use as a permutation target- Returns:
- a new SmartHandle that permutes this Signature's args into a call to the target SmartHandle.
- See Also:
-
to
Generate an array of argument offsets based on permuting this signature to the given signature.- Parameters:
other
- the signature to target- Returns:
- an array of argument offsets that will permute to the given signature
-
to
Generate an array of argument offsets based on permuting this signature to the given signature. Repeats are permitted, and the patterns will be matched against actual argument names using regex matching.- Parameters:
otherArgPatterns
- the argument name patterns to permute- Returns:
- an array of argument offsets that will permute to the matching argument names
-
nonMatchingTo
-