question on exports to
Peter Levart
peter.levart at gmail.com
Wed Jun 1 15:04:42 UTC 2016
On 06/01/2016 04:53 PM, Alan Bateman wrote:
>
>
> On 01/06/2016 13:43, Peter Levart wrote:
>>
>> I also don't wish to cause further confusion, but I have a feeling
>> that Jochen might have the following situation:
>>
>> - MyOtherLib contains classes and code that is using these classes
>> (invoking their methods), but it wishes to call those methods via an
>> indirection through a GeneralInvoker that happens to live in another
>> module
>> - MyOtherLib is also using classes (invoking methods) from other
>> modules that it already has direct access to, but it wishes to call
>> those methods via an indirection through a GeneralInvoker too
>>
>> If that is true, then perhaps there is a simpler solution that
>> doesn't require modifying the exports of any module.
>>
>> Make your TheInvoker take another argument of type MethodHandles.Lookup:
>>
>> public class TheInvoker{
>> public static Object invoke(MethodHandles.Lookup lookup, Object
>> receiver, String name, Object... args) throws Throwable {
>> Method m = receiver.getClass().getDeclaredMethod(name,
>> toClass(args));
>> MethodHandle mh = lookup.unreflect(m).bindTo(receiver);
>> return mh.invokeWithArguments(args);
>> }
>> ...
>> }
>>
>>
>> Then pass the appropriate lookup to it from where you call
>> TheInvoker.invoke (from MyOtherLib):
>>
>> TheInvoker.invoke(MethodHandles.lookup(), receiver, "methodName",
>> arguments...);
>>
>>
>> Would that work?
> Using a Lookup is clever. Some exports are still needed of course.
> GeneralInvoker will need to export the package with TheInvoker, that
> may be exported already. Also something must have invoked the entry
> point in MyOtherLib and so the package with thaht "entry point" must
> be exported to the caller module.
>
> -Alan
>
Of course, you have to trust TheInvoker to not do anything stupid with
your Lookup. By giving away your Lookup, you are giving away your access
rights to some other party.
Regards, Peter
More information about the jigsaw-dev
mailing list