proxy an interface and call a default method
Peter Levart
peter.levart at gmail.com
Thu Jun 2 13:23:44 UTC 2016
Hi Remi, Jochen,
On 06/02/2016 11:15 AM, forax at univ-mlv.fr wrote:
>> >The solution could be for Proxy API to provide a MH that was already
>> >bound to the Proxy instance. Such pre-bound MH could not be abused then.
> independently of any security issue, it may be a good idea but doing a partial evaluation on a MH is not cheap.
>
I created a prototype for this:
http://cr.openjdk.java.net/~plevart/jdk9-dev/Proxy.invokeSuperDefaults/webrev.01/
Example usage is as follows:
public class Test {
interface I {
default void m() {
System.out.println("default I.m() called");
}
}
public static void main(String[] args) {
InvocationHandler h = (proxy, method, params) -> {
System.out.println("InvocationHandler called for: " + method);
MethodHandle superM = ((Proxy) proxy).findSuper(I.class,
"m", MethodType.methodType(void.class));
return superM.invokeWithArguments(params);
};
I i = (I) Proxy.newProxyInstance(
I.class.getClassLoader(), new Class<?>[]{I.class}, h);
i.m();
}
}
It works, but in order for this to have adequate performance, caching
would have to be added. But caching a pre-bound MH would require caching
on per-proxy-instance basis, which would not be very efficient. So
perhaps, instead of providing a Proxy::findSuper method that returns a
pre-bound MH, there could simply be a method like the following in the
Proxy class:
public final Object invokeSuper(Class<?> interfaze, String methodName,
MethodType methodType, Object ... args) { ... }
What do you think?
Regards, Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20160602/3d19f263/attachment-0001.html>
More information about the mlvm-dev
mailing list