ModuleLayer.defineModules function clf must be stateful?

Thomas Watson tjwats at
Tue Feb 26 19:38:13 UTC 2019

I noticed that the clf function passed to
ModuleLayer.defineModules(Configuration, List<ModuleLayer>,
Function<String, ClassLoader>) is called at least twice for every module
name contained in the new ModuleLayer.  Once for the call
to java.lang.ModuleLayer.checkForDuplicatePkgs(Configuration,
Function<String, ClassLoader>) and once for the call
to java.lang.Module.defineModules(Configuration, Function<String,
ClassLoader>, ModuleLayer)

In my case I was returning a new ClassLoader object each call to my clf
function.  But I noticed that I was creating twice as many class loaders
than needed for the ModuleLayer.  Ultimately it looks like the
checkForDuplciatePkgs method throws away the created ClassLoader and then
the second call to my clf in defineModules gets the actual class loader
that will be used by the module.  My question: is this expected?  Is my
function expected to hold state and return the same class loader object
each time it is called for a specific module name?


More information about the jigsaw-dev mailing list