Requires optional permits <was> Re: Service provider module resolution
Alan Bateman
Alan.Bateman at oracle.com
Wed Jul 11 07:24:07 PDT 2012
On 11/07/2012 10:19, Neil Bartlett wrote:
> You don't have to interact with the optional dependency purely with
> reflection. You could check for the availability of the class first
> with ClassLoader.loadClass() or Class.forName(), and if that doesn't
> throw an exception then you can proceed and use the class in the
> normal way. Of course this has to be done before calling any code that
> has the dependency, whether directly or indirectly e.g. via a
> supertype.
>
> I have sometimes seen this done in OSGi, alhough optionality at the
> level of static class/package dependencies is widely considered to be
> a bad practice.
>
> If you do this then you clearly need the dependency at compile time
> but it is not needed at runtime. This is one of several reasons why
> compile dependencies differ from runtime dependencies.
>
I realize you're not advocating this approach but it involves
assumptions that may not be true everywhere. In particular it assumes
that VM is not doing eager resolution as in theory you may get the CNF
before the Class.forName executes. Precompilation and other install time
optimizations have the potential to complicate things too. In any case,
it's still all very fragile as the code compiles but may fail with CNF
at runtime.
-Alan.
More information about the jigsaw-dev
mailing list