5.75. kink/javahost/METHOD_HANDLE¶
Provides utility functions to manipulate java.lang.MethodHandle.
5.75.1. METHOD_HANDLE.constructor(Owner_class Arg_classes ...[$config_fun={}])¶
`constructor` wraps MethodHandles.publicLookup().findConstructor. `constructor` searches for a publicly available constructor of Owner_class, which has arguments typed as Arg_classes.
Preconditions
• Owner_class must be a `java` val of java.lang.Class.
• Arg_classes must be a vec of instances of java.lang.Class.
• $config_fun must be a fun which takes a find_invocable_config val.
Result
If the specified constructor is found, and it has public accessibility, `constructor` tail-calls the success cont with the MethodHandle of the constructor.
if NoSuchMethodException is thrown, `constructor` tail-calls the cont of NoSuchMethodException with the thrown exception.
If IllegalAccessException is thrown, `constructor` tail-calls the cont of IllegalAccessException with the thrown exception.
5.75.2. METHOD_HANDLE.static_method(Owner_class Method_name Arg_classes Ret_class ...[$config_fun])¶
static_method wraps MethodHandles.publicLookup().findVirtual. static_method searches for a publicly available static method of Owner_class, which has Method_name, arguments typed as Arg_classes, and the return value typed as Ret_class.
Preconditions
• Owner_class must be a java val of java.lang.Class.
• Method_name must be a str.
• Arg_classes must be a vec of instances of java.lang.Class.
• Ret_class must be a java val of java.lang.Class.
Result
If the specified method is found, and it has public accessibility, static_method tail-calls the success cont with the MethodHandle of the method.
if NoSuchMethodException is thrown, static_method tail-calls the cont of NoSuchMethodException with the thrown exception.
If IllegalAccessException is thrown, static_method tail-calls the cont of IllegalAccessException with the thrown exception.
5.75.3. METHOD_HANDLE.virtual_method(Owner_class Method_name Arg_classes Ret_class ...[$config_fun])¶
virtual_method wraps MethodHandles.publicLookup().findVirtual. virtual_method searches for a publicly available static method of Owner_class, which has Method_name, arguments typed as Arg_classes, and the return value typed as Ret_class.
Preconditions
• Owner_class must be a java val of java.lang.Class.
• Method_name must be a str.
• Arg_classes must be a vec of instances of java.lang.Class.
• Ret_class must be a java val of java.lang.Class.
Result
If the specified method is found, and it has public accessibility, virtual_method tail-calls the success cont with the MethodHandle of the method.
if NoSuchMethodException is thrown, virtual_method tail-calls the cont of NoSuchMethodException with the thrown exception.
If IllegalAccessException is thrown, virtual_method tail-calls the cont of IllegalAccessException with the thrown exception.
5.75.4. type find_invocable_config¶
find_invocable_config is a type of config vals of METHOD_HANDLE.constructor, METHOD_HANDLE.static_method, and METHOD_HANDLE.virtual_method.
5.75.4.1. C.on_success($success)¶
on_success specifies $success as the success cont. If on_success is not called, VAL.identity is used as the default success cont.
5.75.4.2. C.on_no_such_method($no_such_method)¶
on_no_such_method specifies $no_such_method as the cont of NoSuchMethodException. If on_no_such_method is not called, a fun which raises an exception is used as the default cont.
5.75.4.3. C.on_illegal_access($illegal_access)¶
on_illegal_access specifies $illegal_access as the cont of IllegalAccessException. If on_illegal_access is not called, a fun which raises an exception is used as the default cont.
5.75.5. METHOD_HANDLE.throw(Java_exc Arg_classes Ret_class)¶
`throw` returns a MethodHandle to throw Java_exc of type Arg_classes -> Ret_class.
Preconditions
• Java_exc must be a java val of an instance of java.lang.Throwable
• Arg_classes must be a vec of instances of java.lang.Class.
• Ret_class must be a java val of an instance of java.lang.Class.
5.75.6. METHOD_HANDLE.return(Ret_val Arg_classes Ret_class)¶
`return` returns a MethodHandle to return a value, with type Arg_classes -> Ret_class.
Preconditions
• Ret_val must be a java val, which is typable as Ret_class.
• Arg_classes must be a vec of instances of java.lang.Class.
• Ret_class must be a java val of an instance of java.lang.Class.
5.75.7. METHOD_HANDLE.nop(Arg_classes)¶
`nop` returns a MethodHandles which does nothing, with type Arg_classes -> void.
Preconditions
• Arg_classes must be a vec of instances of java.lang.Class.
5.75.8. METHOD_HANDLE.params(Mh)¶
`params` returns a vec of parameter classes of the method handle.
The result is a vec of classes, which are statically typed as java.lang.Class.
Precondition
• `Mh` must be an instance of java.lang.invoke.MethodHandle.
5.75.9. METHOD_HANDLE.result(Mh)¶
`result` returns the result class of the method handle.
Precondition
• `Mh` must be an instance of java.lang.invoke.MethodHandle.
5.75.10. METHOD_HANDLE.invoke(Mh Args ...[$config_fun={}])¶
`invoke` invokes the method handle `Mh`, using MethodHandle.invokeWithArguments. `Args` is passed as the arguments.
Preconditions
• `Mh` must be an instance of MethodHandle.
• `Args` must be a vec of java vals. The dynamic types of `Args` must be typable as the argument types of `Mh.
• $config_fun must be a fun which takes a val of java_call_config type.
See kink/javahost/JAVA for java_call_config type.
Result
1) If the return type is a reference type, and the invocation terminates without throwing an exception, `invoke` tail-calls the success cont with a java val (resultObj, returnType), where `resultObj` is the return value of the method, and `returnType` is the return type of `Mh`.
2) If the return type is char, boolean, byte, short, int, long, float or double, and the invocation terminates without throwing an exception, `invoke` tail-calls the success cont with a java val (resultObj, returnType), where `resultObj` is a wrapper instance of the return value, and `returnType` is the declared return type of `Mh`.
3) If the return type is void, and the invocation terminates without throwing exception, `invoke` tail-calls the success cont with no arg.
4) If the invocation terminates throwing a Java exception, and the exception is an instance of one or more `Exception_class` types specified by invocations of C.catch, the error cont corresponding to the most specific exception type is tail-called with a java val (exceptionObj, Exception_class), where `exceptionObj` is the thrown exception, and `Exception_class` is the class which is specified by `catch`.
5) If the invocation terminates throwing a Java exception, and no `Exception_class` of C.catch matches the thrown exception, `invoke` raises an exception.