Optional dependencies

Peter Levart peter.levart at gmail.com
Fri Dec 4 20:48:06 UTC 2015

On 12/04/2015 06:07 PM, Ali Ebrahimi wrote:
> Hi,
> On Fri, Dec 4, 2015 at 8:23 PM, Stephen Colebourne <scolebourne at joda.org>
> wrote:
>> I've been pondering whether -addReads is sufficient for this use case.
>> While type 1 of my classification below (annotations) would work using
>> compile-time -addReads, I don't think types 2 and 3 would. (Type 2 is
>> reflection based, type 3 is usage based)
>> Here is the setup:
>> module user {
>>    requires org.joda.beans
>>    requires com.google.guava
>> }
>> module org.joda.beans {
>>    // optionally requires com.google.guava
>> }
>> Proposed solution:
>> compile module with -addReads=com.google.guavea
>> The proposed solution would allow the Joda-Beans module to be
>> successfully compiled. But it seems that it would not be able to see
>> the Guava code at runtime (as the module does not have the
>> dependency). Note that there is source code in Joda-Beans that
>> actively calls Guava clases in addition to checking they exist by
>> reflection.
> I don't think this is optional dependency if you have static references to
> Guava's types in source code.

Everything can be optional (at runtime). JVM is very lazy at resolving 
types. If code that refers to a particular type is never executed, then 
that type is never looked-up. So everything depends on how you structure 
your code. If you are sure that parts of code that need guava will only 
be executed when the user of your code explicitly requests a particular 
minor feature, you can make guava an optional dependency which is needed 
only if user needs this particular feature.

But you have to be careful then that you don't use guava in other parts 
of your code that is executed for providing the "main" functionality of 
your module. Neither javac nor jlink will warn you if you make a 
mistake. You're on your own, because you asked for it!

Regards, Peter


More information about the jigsaw-dev mailing list