Top Description Fields Constructors Methods
com.headius.invokebinder

public Class Signature

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandle, .MethodHandles, .MethodType, java.lang.reflect.Array, java.util.ArrayList, .Arrays, .List, java.util.regex.Matcher, .Pattern

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.
Author
headius

Field Summary

Modifier and TypeField and Description
private final String[]
private final MethodType

Constructor Summary

AccessConstructor and Description
pack-priv
Signature(Class<?>
the return value for the new signature
retval
)

Construct a new signature with the given return value.

pack-priv
Signature(Class<?>
the return value for the new signature
retval
,
Class<?>[]
the argument types for the new signature
argTypes
,
String...
the argument names for the new signature
argNames
)

Construct a new signature with the given return value, argument types, and argument names.

pack-priv
Signature(Class<?>
the return value for the new signature
retval
,
Class<?>
the first argument type, often the receiver of an instance method
firstArg
,
Class<?>[]
the remaining argument types for the new signature
restArgs
,
String...
the argument names for the new signature
argNames
)

Construct a new signature with the given return value, argument types, and argument names.

pack-priv
Signature(MethodType
the method type for the new signature
methodType
,
String...
the argument names for the new signature
argNames
)

Construct a new signature with the given method type and argument names.

pack-priv
Signature(MethodType
the method type for the new signature
methodType
,
String
the first argument name for the new signature; for eventual instance methods, it can be "this"
firstName
,
String...
the remaining argument names for the new signature
restNames
)

Construct a new signature with the given method type and argument names.

Method Summary

Modifier and TypeMethod and Description
public Signature

Returns:

a new signature with the added arguments
appendArg
(String
the name of the argument
name
,
Class<?>
the type of the argument
type
)

Append an argument (name + type) to the signature.

public Signature

Returns:

a new signature with the added arguments
appendArgs
(String[]
the names of the arguments
names
,
Class<?>...
the types of the argument
types
)

Append an argument (name + type) to the signature.

public int

Returns:

argument count of this signature
argCount
()

The current argument count.

public String

Returns:

the argument name
argName
(int
the index from which to get the argument name
index
)

Retrieve the name of the argument at the given index.

public Signature

Returns:

a new signature with the given name at the given index
argName
(int
the index at which to set the argument name
index
,
String
the name to set
name
)

Set the argument name at the given index.

public String[]

Returns:

the current argument names
argNames
()

The current argument names for this signature.

public int

Returns:

the offset at which the argument name was found or -1
argOffset
(String
the argument name to search for
name
)

Retrieve the offset of the given argument name in this signature's arguments.

public int

Returns:

the offset at which the argument name was found or -1
argOffsets
(String
the argument name to search for
pattern
)

Retrieve the offset of the given argument name in this signature's arguments.

public Class<?>

Returns:

the argument type
argType
(int
the index from which to get the argument type
index
)

Get the argument type at the given index.

public Signature

Returns:

a new signature with the given type at the given index
argType
(int
the index at which to set the argument type
index
,
Class<?>
the type to set
type
)

Set the argument type at the given index.

public Signature

Returns:

a new signature with the added argument
asFold
(Class<?>
the new return type for the new signature
retval
)

Produce a new signature based on this one with a different return type.

public Signature

Returns:

the new signature with modified return value
changeReturn
(Class<?>
the class for the new signature's return type
retval
)

Create a new signature based on this one with a different return type.

public Signature

Returns:

a new signature with an array argument where the collected arguments were
collect
(String
the name of the new array argument
newName
,
String
the pattern of arguments to collect
oldPattern
)

Collect sequential arguments matching pattern into an array.

public Signature

Returns:

a new signature
dropArg
(String
the name of the argument to drop
name
)

Drops the first argument with the given name.

public Signature

Returns:

a new signature
dropArg
(int
the index of the argument to drop
index
)

Drops the argument at the given index.

public Signature

Returns:

a new signature
dropFirst
()

Drop the first argument from this signature.

public Signature

Returns:

a new signature
dropFirst
(int
number of arguments to drop
n
)

Drop the specified number of first arguments from this signature.

public Signature

Returns:

a new signature
dropLast
()

Drop the last argument from this signature.

public Signature

Returns:

a new signature
dropLast
(int
number of arguments to drop
n
)

Drop the specified number of last arguments from this signature.

public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public Signature

Returns:

the new signature
exclude
(String...
the names of the arguments to exclude
excludeArgs
)

Create a new signature containing the same return value as this one, but omitting the specified arguments.

public String

Returns:

the first argument name
firstArgName
()

Get the first argument name.

public Class<?>

Returns:

the first argument type
firstArgType
()

Get the first argument type.

public static Signature

Returns:

a new Signature
from
(Class<?>
the type of the return value
retval
,
Class<?>[]
the types of the arguments
argTypes
,
String...
the names of the arguments
argNames
)

Create a new signature based on the given return value, argument types, and argument names

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public Signature

Returns:

a new signature with the added arguments
insertArg
(int
the index at which to insert
index
,
String
the name of the new argument
name
,
Class<?>
the type of the new argument
type
)

Insert an argument (name + type) into the signature.

public Signature

Returns:

a new signature with the added arguments
insertArg
(String
the name of the argument before which to insert
beforeName
,
String
the name of the new argument
name
,
Class<?>
the type of the new argument
type
)

Insert an argument (name + type) into the signature before the argument with the given name.

public Signature

Returns:

a new signature with the added arguments
insertArgs
(int
the index at which to insert
index
,
String[]
the names of the new arguments
names
,
Class<?>...
the types of the new arguments
types
)

Insert arguments (names + types) into the signature.

public Signature

Returns:

a new Signature with the added arguments
insertArgs
(String
the name of the argument before which to insert
beforeName
,
String[]
the names of the new arguments
names
,
Class<?>...
the types of the new arguments
types
)

Insert arguments (names + types) into the signature before the argument with the given name.

public String

Returns:

the last argument name
lastArgName
()

Get the last argument name.

public Class<?>

Returns:

the last argument type
lastArgType
()

Get the last argument type.

private int[]
nonMatchingTo(String... otherArgNames)

public Signature

Returns:

the new signature
permute
(String...
the names of the arguments to preserve
permuteArgs
)

Create a new signature containing the same return value as this one, but only the specified arguments.

public MethodHandle

Returns:

a new handle that permutes appropriate positions based on the given permute args
permuteWith
(MethodHandle
the method handle to target
target
,
String...
the arguments to permute
permuteArgs
)

Produce a method handle permuting the arguments in this signature using the given permute arguments and targeting the given java.lang.invoke.MethodHandle.

public SmartHandle

Returns:

a new SmartHandle that permutes this Signature's args into a call to the target SmartHandle.
permuteWith
(SmartHandle
the SmartHandle to use as a permutation target
target
)

Produce a new SmartHandle by permuting this Signature's arguments to the Signature of a target SmartHandle.

public Signature

Returns:

a new signature with the added arguments
prependArg
(String
the name of the argument
name
,
Class<?>
the type of the argument
type
)

Prepend an argument (name + type) to the signature.

public Signature

Returns:

a new signature with the added arguments
prependArgs
(String[]
the names of the arguments
names
,
Class<?>...
the types of the arguments
types
)

Prepend arguments (names + types) to the signature.

public Signature

Returns:

a new signature with the modified argument
replaceArg
(String
the old name of the argument
oldName
,
String
the new name of the argument; can be the same as old
newName
,
Class<?>
the new type of the argument; can be the same as old
newType
)

Replace the named argument with a new name and type.

public static Signature

Returns:

the new signature
returning
(Class<?>
the return type for the new signature
retval
)

Create a new signature returning the given type.

public Signature

Returns:

a new signature with decomposed arguments in place of the trailing array
spread
(String[]
names to use for the decomposed arguments
names
,
Class<?>...
types to use for the decomposed arguments
types
)

Spread the trailing [] argument into its component type assigning given names.

public Signature

Returns:

a new signature with decomposed arguments in place of the trailing array
spread
(String...
names to use for the decomposed arguments
names
)

Spread the trailing [] argument into its component type assigning given names.

public Signature

Returns:

a new signature with decomposed arguments in place of the trailing array
spread
(String
base name of the spread arguments
baseName
,
int
number of arguments into which the last argument will decompose
count
)

Spread the trailing [] argument into its component type assigning given names.

public int[]

Returns:

an array of argument offsets that will permute to the given signature
to
(Signature
the signature to target
other
)

Generate an array of argument offsets based on permuting this signature to the given signature.

public int[]

Returns:

an array of argument offsets that will permute to the matching argument names
to
(String...
the argument name patterns to permute
otherArgPatterns
)

Generate an array of argument offsets based on permuting this signature to the given signature.

public String

Returns:

a human-readable representation of the signature
toString
()

Overrides java.lang.Object.toString.

Produce a human-readable representation of this signature.
public MethodType

Returns:

the current method type
type
()

The current java.lang.invoke.MethodType for this Signature.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

argNamesback to summary
private final String[] argNames
methodTypeback to summary
private final MethodType methodType

Constructor Detail

Signatureback to summary
pack-priv Signature(Class<?> retval)

Construct a new signature with the given return value.

Parameters
retval:Class<?>

the return value for the new signature

Signatureback to summary
pack-priv Signature(Class<?> retval, Class<?>[] argTypes, String... argNames)

Construct a new signature with the given return value, argument types, and argument names.

Parameters
retval:Class<?>

the return value for the new signature

argTypes:Class<?>[]

the argument types for the new signature

argNames:String[]

the argument names for the new signature

Signatureback to summary
pack-priv Signature(Class<?> retval, Class<?> firstArg, Class<?>[] restArgs, String... argNames)

Construct a new signature with the given return value, argument types, and argument names.

Parameters
retval:Class<?>

the return value for the new signature

firstArg:Class<?>

the first argument type, often the receiver of an instance method

restArgs:Class<?>[]

the remaining argument types for the new signature

argNames:String[]

the argument names for the new signature

Signatureback to summary
pack-priv Signature(MethodType methodType, String... argNames)

Construct a new signature with the given method type and argument names.

Parameters
methodType:MethodType

the method type for the new signature

argNames:String[]

the argument names for the new signature

Signatureback to summary
pack-priv Signature(MethodType methodType, String firstName, String... restNames)

Construct a new signature with the given method type and argument names.

Parameters
methodType:MethodType

the method type for the new signature

firstName:String

the first argument name for the new signature; for eventual instance methods, it can be "this"

restNames:String[]

the remaining argument names for the new signature

Method Detail

appendArgback to summary
public Signature appendArg(String name, Class<?> type)

Append an argument (name + type) to the signature.

Parameters
name:String

the name of the argument

type:Class<?>

the type of the argument

Returns:Signature

a new signature with the added arguments

appendArgsback to summary
public Signature appendArgs(String[] names, Class<?>... types)

Append an argument (name + type) to the signature.

Parameters
names:String[]

the names of the arguments

types:Class<?>[]

the types of the argument

Returns:Signature

a new signature with the added arguments

argCountback to summary
public int argCount()

The current argument count.

Returns:int

argument count of this signature

argNameback to summary
public String argName(int index)

Retrieve the name of the argument at the given index.

Parameters
index:int

the index from which to get the argument name

Returns:String

the argument name

argNameback to summary
public Signature argName(int index, String name)

Set the argument name at the given index.

Parameters
index:int

the index at which to set the argument name

name:String

the name to set

Returns:Signature

a new signature with the given name at the given index

argNamesback to summary
public String[] argNames()

The current argument names for this signature.

Returns:String[]

the current argument names

argOffsetback to summary
public int argOffset(String name)

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:String

the argument name to search for

Returns:int

the offset at which the argument name was found or -1

argOffsetsback to summary
public int argOffsets(String pattern)

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
pattern:String

the argument name to search for

Returns:int

the offset at which the argument name was found or -1

argTypeback to summary
public Class<?> argType(int index)

Get the argument type at the given index.

Parameters
index:int

the index from which to get the argument type

Returns:Class<?>

the argument type

argTypeback to summary
public Signature argType(int index, Class<?> type)

Set the argument type at the given index.

Parameters
index:int

the index at which to set the argument type

type:Class<?>

the type to set

Returns:Signature

a new signature with the given type at the given index

asFoldback to summary
public Signature asFold(Class<?> retval)

Produce a new signature based on this one with a different return type.

Parameters
retval:Class<?>

the new return type for the new signature

Returns:Signature

a new signature with the added argument

changeReturnback to summary
public Signature changeReturn(Class<?> retval)

Create a new signature based on this one with a different return type.

Parameters
retval:Class<?>

the class for the new signature's return type

Returns:Signature

the new signature with modified return value

collectback to summary
public Signature collect(String newName, String oldPattern)

Collect sequential arguments matching pattern into an array. They must have the same type.

Parameters
newName:String

the name of the new array argument

oldPattern:String

the pattern of arguments to collect

Returns:Signature

a new signature with an array argument where the collected arguments were

dropArgback to summary
public Signature dropArg(String name)

Drops the first argument with the given name.

Parameters
name:String

the name of the argument to drop

Returns:Signature

a new signature

dropArgback to summary
public Signature dropArg(int index)

Drops the argument at the given index.

Parameters
index:int

the index of the argument to drop

Returns:Signature

a new signature

dropFirstback to summary
public Signature dropFirst()

Drop the first argument from this signature.

Returns:Signature

a new signature

dropFirstback to summary
public Signature dropFirst(int n)

Drop the specified number of first arguments from this signature.

Parameters
n:int

number of arguments to drop

Returns:Signature

a new signature

dropLastback to summary
public Signature dropLast()

Drop the last argument from this signature.

Returns:Signature

a new signature

dropLastback to summary
public Signature dropLast(int n)

Drop the specified number of last arguments from this signature.

Parameters
n:int

number of arguments to drop

Returns:Signature

a new signature

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
excludeback to summary
public Signature exclude(String... excludeArgs)

Create a new signature containing the same return value as this one, but omitting the specified arguments. Blacklisting to #permute's whitelisting.

Parameters
excludeArgs:String[]

the names of the arguments to exclude

Returns:Signature

the new signature

firstArgNameback to summary
public String firstArgName()

Get the first argument name.

Returns:String

the first argument name

firstArgTypeback to summary
public Class<?> firstArgType()

Get the first argument type.

Returns:Class<?>

the first argument type

fromback to summary
public static Signature from(Class<?> retval, Class<?>[] argTypes, String... argNames)

Create a new signature based on the given return value, argument types, and argument names

Parameters
retval:Class<?>

the type of the return value

argTypes:Class<?>[]

the types of the arguments

argNames:String[]

the names of the arguments

Returns:Signature

a new Signature

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
insertArgback to summary
public Signature insertArg(int index, String name, Class<?> type)

Insert an argument (name + type) into the signature.

Parameters
index:int

the index at which to insert

name:String

the name of the new argument

type:Class<?>

the type of the new argument

Returns:Signature

a new signature with the added arguments

insertArgback to summary
public Signature insertArg(String beforeName, String name, Class<?> type)

Insert an argument (name + type) into the signature before the argument with the given name.

Parameters
beforeName:String

the name of the argument before which to insert

name:String

the name of the new argument

type:Class<?>

the type of the new argument

Returns:Signature

a new signature with the added arguments

insertArgsback to summary
public Signature insertArgs(int index, String[] names, Class<?>... types)

Insert arguments (names + types) into the signature.

Parameters
index:int

the index at which to insert

names:String[]

the names of the new arguments

types:Class<?>[]

the types of the new arguments

Returns:Signature

a new signature with the added arguments

insertArgsback to summary
public Signature insertArgs(String beforeName, String[] names, Class<?>... types)

Insert arguments (names + types) into the signature before the argument with the given name.

Parameters
beforeName:String

the name of the argument before which to insert

names:String[]

the names of the new arguments

types:Class<?>[]

the types of the new arguments

Returns:Signature

a new Signature with the added arguments

lastArgNameback to summary
public String lastArgName()

Get the last argument name.

Returns:String

the last argument name

lastArgTypeback to summary
public Class<?> lastArgType()

Get the last argument type.

Returns:Class<?>

the last argument type

nonMatchingToback to summary
private int[] nonMatchingTo(String... otherArgNames)
permuteback to summary
public Signature permute(String... permuteArgs)

Create a new signature containing the same return value as this one, but only the specified arguments.

Parameters
permuteArgs:String[]

the names of the arguments to preserve

Returns:Signature

the new signature

permuteWithback to summary
public MethodHandle 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. 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:MethodHandle

the method handle to target

permuteArgs:String[]

the arguments to permute

Returns:MethodHandle

a new handle that permutes appropriate positions based on the given permute args

permuteWithback to summary
public SmartHandle permuteWith(SmartHandle target)

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:SmartHandle

the SmartHandle to use as a permutation target

Returns:SmartHandle

a new SmartHandle that permutes this Signature's args into a call to the target SmartHandle.

See Also
Signature#permuteWith(java.lang.invoke.MethodHandle, java.lang.String[])
prependArgback to summary
public Signature prependArg(String name, Class<?> type)

Prepend an argument (name + type) to the signature.

Parameters
name:String

the name of the argument

type:Class<?>

the type of the argument

Returns:Signature

a new signature with the added arguments

prependArgsback to summary
public Signature prependArgs(String[] names, Class<?>... types)

Prepend arguments (names + types) to the signature.

Parameters
names:String[]

the names of the arguments

types:Class<?>[]

the types of the arguments

Returns:Signature

a new signature with the added arguments

replaceArgback to summary
public Signature replaceArg(String oldName, String newName, Class<?> newType)

Replace the named argument with a new name and type.

Parameters
oldName:String

the old name of the argument

newName:String

the new name of the argument; can be the same as old

newType:Class<?>

the new type of the argument; can be the same as old

Returns:Signature

a new signature with the modified argument

returningback to summary
public static Signature returning(Class<?> retval)

Create a new signature returning the given type.

Parameters
retval:Class<?>

the return type for the new signature

Returns:Signature

the new signature

spreadback to summary
public Signature spread(String[] names, Class<?>... types)

Spread the trailing [] argument into its component type assigning given names.

Parameters
names:String[]

names to use for the decomposed arguments

types:Class<?>[]

types to use for the decomposed arguments

Returns:Signature

a new signature with decomposed arguments in place of the trailing array

spreadback to summary
public Signature spread(String... names)

Spread the trailing [] argument into its component type assigning given names.

Parameters
names:String[]

names to use for the decomposed arguments

Returns:Signature

a new signature with decomposed arguments in place of the trailing array

spreadback to summary
public Signature spread(String baseName, int count)

Spread the trailing [] argument into its component type assigning given names.

Parameters
baseName:String

base name of the spread arguments

count:int

number of arguments into which the last argument will decompose

Returns:Signature

a new signature with decomposed arguments in place of the trailing array

toback to summary
public int[] to(Signature other)

Generate an array of argument offsets based on permuting this signature to the given signature.

Parameters
other:Signature

the signature to target

Returns:int[]

an array of argument offsets that will permute to the given signature

toback to summary
public int[] to(String... otherArgPatterns)

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:String[]

the argument name patterns to permute

Returns:int[]

an array of argument offsets that will permute to the matching argument names

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Produce a human-readable representation of this signature. This representation uses Class#getSimpleName to improve readability.

Returns:String

a human-readable representation of the signature

typeback to summary
public MethodType type()

The current java.lang.invoke.MethodType for this Signature.

Returns:MethodType

the current method type