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

Reply via email to