Migrating to uses from an existing module system

Thomas Watson tjwatson at us.ibm.com
Tue Nov 29 22:45:05 UTC 2016


I have a usecase where I am attempting to map an existing module system's 
class loaders to JPMS modules by using the Layer API.  In this case the 
existing module system is OSGi.  I have run into an issue when an existing 
OSGi bundle contains code that uses the ServiceLoader.  For the sake of 
discussion the existing code could look like this:

        ServiceLoader<PrintServiceLookup> sl = 
ServiceLoader.load(PrintServiceLookup.class);
        System.out.println(sl.findFirst().orElseGet(() -> {return 
null;}));

Without mapping OSGi to JPMS modules this code works fine.  This is 
because the code ends up being loaded by an unnamed module for which JPMS 
assumes is allowed to use any service through the ServiceLoader.  But once 
I map the OSGi bundle class loader to a JPMS module the code above fails 
to work with an exception:

java.util.ServiceConfigurationError: javax.print.PrintServiceLookup: 
module test.service.loader does not declare `uses`

Is there any way around this?  I know there is an addUses method, and I 
also know that I can generate ModuleDescriptors for my Layer that specify 
a proper uses requirement.  But that would require a code scan to discover 
all possible calls to the ServiceLoader in order to declare the uses.

Tom





More information about the jigsaw-dev mailing list