Avoiding sun.misc.Unsafe and embracing modules in Java libraries: missing links

Rafael Winterhalter rafael.wth at gmail.com
Thu Apr 12 16:06:13 UTC 2018


I have not thought of that but you are of course right, that works. The
solution is however far from pretty as it needs a case basis since the
resolution of the proxy class is only possible if the proxy is in fact
loaded into an unnamed module what is not always the case. This does
however cover all cases for Java 9 and Java 10 proxies. I do however hope
Java 11 can ship with something more convenient then this:
https://gist.github.com/raphw/c1faf2f40e80afce6f13511098cfb90f

Thanks for guiding me through that!
Best regards, Rafael

2018-04-12 10:40 GMT+02:00 Alan Bateman <Alan.Bateman at oracle.com>:

> On 11/04/2018 21:07, Rafael Winterhalter wrote:
>
>> I do not think that this is possible. If the module containing the
>> interface does not open a package, I cannot change the privileges of the
>> main module such that I can resolve a method handle for invoking the
>> special invocation.
>>
>> I just tried this out too and I did not find a way, could you suggest how
>> to change my code for being able to do so?
>>
> If the interface is public in an exported package (no need for the package
> to be open) then the proxy will be generated into the unnamed module. So
> easy to get a Lookup to the proxy class and you can use this as the special
> caller. Can you change your invocation handler to the following and try it:
>
> Class<?> proxyClass = proxy.getClass();
> Main.class.getModule().addReads(proxyClass.getModule());
> Lookup lookup = MethodHandles.privateLookupIn(proxyClass,
> MethodHandles.lookup());
> MethodType mt = MethodType.methodType(String.class);
> return lookup.findSpecial(iface, "foo", mt, proxyClass).bindTo(proxy).invo
> keWithArguments();
>
> -Alan
>


More information about the jigsaw-dev mailing list