3.37. kink/host_lang/java/JAVA_PROXY

JAVA_PROXY mod provides a way to make a Java dynamic proxies (java.lang.reflect.Proxy).

See the documentation of Proxy class:


3.37.1. JAVA_PROXY.new(Class_loader Intfs $handler)

JAVA_PROXY.new makes a Java dynamic proxy, whose action is defined by $handler fun.


• Class_loader must be a java_val (loader, any type), where loader is an instance of java.lang.ClassLoader.

• Intfs must be a vec of java_vals, object references of which are instances of java.lang.Class.

• The class loader and the class objects must satisfy conditions described in https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Proxy.html

$handler is invoked when a method of the proxy is invoked. The parameters of $handler are Method and Args, where:

• Method is a java_val (method, java.lang.reflect.Method), where method is the invoked method.

• Args is a vec of java_vals, object references of which are arguments passed to the method, or boxing instances of them, and static types of which are the declared types of the parameters of the method.

When $handler terminates throwing a Java exception as described in Java.throw and Java.call_method, invocation of the Java method throws the Java exception.

JAVA_PROXY.new returns a java_val(proxy, proxyClass), where proxy is the created dynamic proxy, and proxyClass is the runtime class of the proxy instance.

If instantiation of a proxy fails with a Java exception, JAVA_PROXY.new raises a Kink exception, instead of throwing a Java exception.


:Runnable_class <- JAVA.class('java.lang.Runnable')
:Cl <- JAVA.wrap(new_val).dynamic_type.call_method('getClassLoader')
:Run <- JAVA_PROXY.new(Cl [Runnable_class]){(:Method :Args)
  Method.call_method('getName').to_kink_str == 'run' || raise(
    'unknown method {}'.format(Method.repr))
:Thread <- JAVA.class('java.lang.Thread').new(Run)
# Output:
#   hey