Top Description Fields Constructors Methods
com.headius.invokebinder

public Class SmartHandle

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandle, .MethodHandles, .MethodHandles.Lookup, .MethodType

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

Field Summary

Modifier and TypeField and Description
private final MethodHandle
handle

The MethodHandle associated with this smart handle

private final Signature
signature

The signature associated with this smart handle

Constructor Summary

AccessConstructor and Description
pack-priv
SmartHandle(Signature signature, MethodHandle handle)

Method Summary

Modifier and TypeMethod and Description
public SmartHandle

Returns:

a new SmartHandle that already has applied the given argument
apply
(int
the index of the argument in the new SmartHandle's Signature
index
,
Object
the argument value
arg
)

Apply an argument into the handle at the given index, returning a new SmartHandle.

public SmartHandle

Returns:

a new SmartHandle that already has applied the given argument
apply
(String
the name of the argument in the new SmartHandle's Signature
name
,
Object
the argument value
arg
)

Apply an argument into the handle at the given name, returning a new SmartHandle.

public SmartHandle

Returns:

a new SmartHandle that already has applied the given argument
applyLast
(Object
the argument value
arg
)

Apply an argument into the handle at the end, returning a new SmartHandle.

public SmartHandle

Returns:

a new SmartHandle with the first argument dropped in favor of obj
bindTo
(Object
the object to which to bind this handle's first argument
obj
)

Bind the first argument of this SmartHandle to the given object, returning a new adapted handle.

public SmartHandle

Returns:

a new SmartHandle that accepts the given argument types
cast
(MethodType
the target MethodType from which arguments will be converted
incoming
)

Create a new SmartHandle that casts arguments from the given type to the current signature's type, using the same argument names.

public SmartHandle

Returns:

a new SmartHandle that accepts the given argument types
cast
(Signature
the target MethodType from which arguments will be converted
incoming
)

Create a new SmartHandle that casts arguments from the given signature to the current signature's type with the new argument names.

public SmartHandle

Returns:

a new SmartHandle that accepts the given argument types
cast
(Class<?>
the return type of the new handle
returnType
,
Class<?>...
the argument types of the new handle
argTypes
)

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.

public SmartHandle

Returns:

a new SmartHandle that accepts the given argument types
convert
(MethodType
the target MethodType from which arguments will be converted
incoming
)

Create a new SmartHandle that converts arguments from the given type to the current signature's type, using the same argument names.

public SmartHandle

Returns:

a new SmartHandle that accepts the given argument types
convert
(Class<?>
the return type of the new handle
returnType
,
Class<?>...
the argument types of the new handle
argTypes
)

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.

public SmartHandle

Returns:

a new SmartHandle that accepts the given argument types
convert
(Signature
the target MethodType from which arguments will be converted
incoming
)

Create a new SmartHandle that converts arguments from the given signature to the current signature's type with the new argument names.

public SmartHandle

Returns:

a new SmartHandle with the additional argument
drop
(String
name before which the dropped argument goes
beforeName
,
String
name of the argument
newName
,
Class<?>
type of the argument
type
)

Drop an argument name and type from the handle at the given index, returning a new SmartHandle.

public SmartHandle

Returns:

a new SmartHandle with the additional argument
drop
(int
index before which the dropped argument goes
index
,
String
name of the argument
newName
,
Class<?>
type of the argument
type
)

Drop an argument from the handle at the given index, returning a new SmartHandle.

public SmartHandle

Returns:

a new SmartHandle with the additional argument
dropLast
(String
name of the argument
newName
,
Class<?>
type of the argument
type
)

Drop an argument from the handle at the end, returning a new SmartHandle.

public static SmartHandle

Returns:

a new SmartHandle based on the signature and looked-up MethodHandle
findStaticQuiet
(MethodHandles.Lookup
the MethodHandles.Lookup object to use
lookup
,
Class<?>
the class where the method is located
target
,
String
the name of the method
name
,
Signature
the signature of the method
signature
)

Create a new SmartHandle by performing a lookup on the given target class for the given method name with the given signature.

public static SmartHandle

Returns:

a new SmartHandle
from
(Signature
the signature for the new smart handle
signature
,
MethodHandle
the method handle for the new smart handle
handle
)

Create a new SmartHandle from the given Signature and MethodHandle.

public MethodHandle

Returns:

a MethodHandle that performs the test and branch
guard
(MethodHandle
the "true" path for this handle's test
target
,
MethodHandle
the "false" path for this handle's test
fallback
)

Use this SmartHandle as a test to guard target and fallback handles.

public SmartHandle

Returns:

a new SmartHandle that performs the test and branch
guard
(SmartHandle
the "true" path for this handle's test
target
,
SmartHandle
the "false" path for this handle's test
fallback
)

Use this SmartHandle as a test to guard target and fallback handles.

public MethodHandle

Returns:

the MethodHandle of this SmartHandle
handle
()

Get the MethodHandle of this SmartHandle.

public SmartHandle

Returns:

a new SmartHandle that returns the given value
returnValue
(Class<?>
the type for the new return value
type
,
Object
the new value to return
value
)

Replace the return value with the given value, performing no other processing of the original value.

public Signature

Returns:

the Signature of this SmartHandle
signature
()

Get the Signature of this SmartHandle.

public String

Returns:

a String representation of this handle
toString
()

Overrides java.lang.Object.toString.

A human-readable String representation of this SamrtHandle.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

handleback to summary
private final MethodHandle handle

The MethodHandle associated with this smart handle

signatureback to summary
private final Signature signature

The signature associated with this smart handle

Constructor Detail

SmartHandleback to summary
pack-priv SmartHandle(Signature signature, MethodHandle handle)

Method Detail

applyback to summary
public SmartHandle apply(int index, Object arg)

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

the index of the argument in the new SmartHandle's Signature

arg:Object

the argument value

Returns:SmartHandle

a new SmartHandle that already has applied the given argument

applyback to summary
public SmartHandle apply(String name, Object arg)

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

the name of the argument in the new SmartHandle's Signature

arg:Object

the argument value

Returns:SmartHandle

a new SmartHandle that already has applied the given argument

applyLastback to summary
public SmartHandle applyLast(Object arg)

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

the argument value

Returns:SmartHandle

a new SmartHandle that already has applied the given argument

bindToback to summary
public SmartHandle bindTo(Object obj)

Bind the first argument of this SmartHandle to the given object, returning a new adapted handle.

Parameters
obj:Object

the object to which to bind this handle's first argument

Returns:SmartHandle

a new SmartHandle with the first argument dropped in favor of obj

castback to summary
public SmartHandle 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. This casting is equivalent to MethodHandles#explicitCastArguments.

Parameters
incoming:MethodType

the target MethodType from which arguments will be converted

Returns:SmartHandle

a new SmartHandle that accepts the given argument types

castback to summary
public SmartHandle cast(Signature incoming)

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

the target MethodType from which arguments will be converted

Returns:SmartHandle

a new SmartHandle that accepts the given argument types

castback to summary
public SmartHandle cast(Class<?> returnType, Class<?>... argTypes)

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:Class<?>

the return type of the new handle

argTypes:Class<?>[]

the argument types of the new handle

Returns:SmartHandle

a new SmartHandle that accepts the given argument types

convertback to summary
public SmartHandle 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. This conversion is equivalent to MethodHandle#asType.

Parameters
incoming:MethodType

the target MethodType from which arguments will be converted

Returns:SmartHandle

a new SmartHandle that accepts the given argument types

convertback to summary
public SmartHandle convert(Class<?> returnType, Class<?>... argTypes)

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:Class<?>

the return type of the new handle

argTypes:Class<?>[]

the argument types of the new handle

Returns:SmartHandle

a new SmartHandle that accepts the given argument types

convertback to summary
public SmartHandle convert(Signature incoming)

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

the target MethodType from which arguments will be converted

Returns:SmartHandle

a new SmartHandle that accepts the given argument types

dropback to summary
public SmartHandle drop(String beforeName, String newName, Class<?> type)

Drop an argument name and type from the handle at the given index, returning a new SmartHandle.

Parameters
beforeName:String

name before which the dropped argument goes

newName:String

name of the argument

type:Class<?>

type of the argument

Returns:SmartHandle

a new SmartHandle with the additional argument

dropback to summary
public SmartHandle drop(int index, String newName, Class<?> type)

Drop an argument from the handle at the given index, returning a new SmartHandle.

Parameters
index:int

index before which the dropped argument goes

newName:String

name of the argument

type:Class<?>

type of the argument

Returns:SmartHandle

a new SmartHandle with the additional argument

dropLastback to summary
public SmartHandle dropLast(String newName, Class<?> type)

Drop an argument from the handle at the end, returning a new SmartHandle.

Parameters
newName:String

name of the argument

type:Class<?>

type of the argument

Returns:SmartHandle

a new SmartHandle with the additional argument

findStaticQuietback to summary
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:MethodHandles.Lookup

the MethodHandles.Lookup object to use

target:Class<?>

the class where the method is located

name:String

the name of the method

signature:Signature

the signature of the method

Returns:SmartHandle

a new SmartHandle based on the signature and looked-up MethodHandle

fromback to summary
public static SmartHandle from(Signature signature, MethodHandle handle)

Create a new SmartHandle from the given Signature and MethodHandle.

Parameters
signature:Signature

the signature for the new smart handle

handle:MethodHandle

the method handle for the new smart handle

Returns:SmartHandle

a new SmartHandle

guardback to summary
public MethodHandle guard(MethodHandle target, MethodHandle fallback)

Use this SmartHandle as a test to guard target and fallback handles.

Parameters
target:MethodHandle

the "true" path for this handle's test

fallback:MethodHandle

the "false" path for this handle's test

Returns:MethodHandle

a MethodHandle that performs the test and branch

guardback to summary
public SmartHandle guard(SmartHandle target, SmartHandle fallback)

Use this SmartHandle as a test to guard target and fallback handles.

Parameters
target:SmartHandle

the "true" path for this handle's test

fallback:SmartHandle

the "false" path for this handle's test

Returns:SmartHandle

a new SmartHandle that performs the test and branch

handleback to summary
public MethodHandle handle()

Get the MethodHandle of this SmartHandle.

Returns:MethodHandle

the MethodHandle of this SmartHandle

returnValueback to summary
public SmartHandle returnValue(Class<?> type, Object value)

Replace the return value with the given value, performing no other processing of the original value.

Parameters
type:Class<?>

the type for the new return value

value:Object

the new value to return

Returns:SmartHandle

a new SmartHandle that returns the given value

signatureback to summary
public Signature signature()

Get the Signature of this SmartHandle.

Returns:Signature

the Signature of this SmartHandle

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

A human-readable String representation of this SamrtHandle.

Returns:String

a String representation of this handle

Annotations
@Override