public class SmartHandle extends Object
// 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);
Modifier and Type | Method and Description |
---|---|
SmartHandle |
apply(int index,
Object arg)
Apply an argument into the handle at the given index, returning a new
SmartHandle.
|
SmartHandle |
apply(String name,
Object arg)
Apply an argument into the handle at the given name, returning a new
SmartHandle.
|
SmartHandle |
applyLast(Object arg)
Apply an argument into the handle at the end, returning a new
SmartHandle.
|
SmartHandle |
bindTo(Object obj)
Bind the first argument of this SmartHandle to the given object,
returning a new adapted handle.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
SmartHandle |
drop(int index,
String newName,
Class type)
Insert an argument into the handle at the given index, returning a new
SmartHandle.
|
SmartHandle |
drop(String beforeName,
String newName,
Class type)
Insert an argument into the handle at the given index, returning a new
SmartHandle.
|
SmartHandle |
dropLast(String newName,
Class type)
Insert an argument into the handle at the given index, 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.
|
MethodHandle |
guard(MethodHandle target,
MethodHandle fallback)
Use this SmartHandle as a test to guard target and fallback handles.
|
SmartHandle |
guard(SmartHandle target,
SmartHandle fallback)
Use this SmartHandle as a test to guard target and fallback handles.
|
MethodHandle |
handle()
Get the MethodHandle of this SmartHandle.
|
SmartHandle |
returnValue(Class type,
Object value)
Replace the return value with the given value, performing no other
processing of the original value.
|
Signature |
signature()
Get the Signature of this SmartHandle.
|
String |
toString()
A human-readable String representation of this SamrtHandle.
|
public static SmartHandle from(Signature signature, MethodHandle handle)
signature
- the signature for the new smart handlehandle
- the method handle for the new smart handlepublic static SmartHandle findStaticQuiet(MethodHandles.Lookup lookup, Class target, String name, Signature signature)
lookup
- the MethodHandles.Lookup object to usetarget
- the class where the method is locatedname
- the name of the methodsignature
- the signature of the methodpublic Signature signature()
public MethodHandle handle()
public SmartHandle apply(int index, Object arg)
name
- the name of the argument in the new SmartHandle's Signaturearg
- the argument valuepublic SmartHandle apply(String name, Object arg)
name
- the name of the argument in the new SmartHandle's Signaturearg
- the argument valuepublic SmartHandle applyLast(Object arg)
name
- the name of the argument in the new SmartHandle's Signaturearg
- the argument valuepublic SmartHandle drop(String beforeName, String newName, Class type)
name
- the name of the argument in the new SmartHandle's Signaturearg
- the argument valuepublic SmartHandle drop(int index, String newName, Class type)
name
- the name of the argument in the new SmartHandle's Signaturearg
- the argument valuepublic SmartHandle dropLast(String newName, Class type)
name
- the name of the argument in the new SmartHandle's Signaturearg
- the argument valuepublic MethodHandle guard(MethodHandle target, MethodHandle fallback)
target
- the "true" path for this handle's testfallback
- the "false" path for this handle's testpublic SmartHandle guard(SmartHandle target, SmartHandle fallback)
target
- the "true" path for this handle's testfallback
- the "false" path for this handle's testpublic SmartHandle bindTo(Object obj)
obj
- the object to which to bind this handle's first argumentpublic SmartHandle convert(MethodType incoming)
incoming
- the target MethodType from which arguments will be convertedpublic SmartHandle convert(Class returnType, Class... argTypes)
returnType
- the return type of the new handleargTypes
- the argument types of the new handlepublic SmartHandle convert(Signature incoming)
incoming
- the target MethodType from which arguments will be convertedpublic SmartHandle cast(MethodType incoming)
incoming
- the target MethodType from which arguments will be convertedpublic SmartHandle cast(Signature incoming)
incoming
- the target MethodType from which arguments will be convertedpublic SmartHandle cast(Class returnType, Class... argTypes)
returnType
- the return type of the new handleargTypes
- the argument types of the new handlepublic SmartHandle returnValue(Class type, Object value)
value
- the type for the new return valuevalue
- the new value to returnCopyright © 2015. All rights reserved.