ModuleLayer.Controller strongly references Module

Michał Kłeczek michal at kleczek.org
Mon Nov 22 07:23:11 UTC 2021


From OpenJDK discuss:

> On 21 Nov 2021, at 21:55, Alan Bateman <Alan.Bateman at oracle.com> wrote:
> 
> On 21/11/2021 13:27, Michał Kłeczek wrote:
>> Hello All,
>> 
>> I am looking for a way to associate ModuleLayer.Controller with a Module (or ModuleLayer) in such a way that it won’t disable garbage collection of Modules and ModuleLayers.
>> WeakHashMap<ModuleLayer, ModuleLayer.Controller> won’t work as Controller strongly references its ModuleLayer (which in turn strongly references Modules).
>> WeakHashMap<ModuleLayer, WeakReference<ModuleLayer.Controller>> won’t work either cause controller will be garbage collected.
>> 
>> So far I only came up with subclassing a ModuleReference and keeping the reference to the controller there. This won’t work in case when ModuleLayer.configuration() is used a a parent configuration.
>> 
>> Am I missing something obvious here?
> You are correct that a module layer doesn't keep the ML.Controller alive. A ML.Controller is somewhat niche object to expand the readability graph or maybe add additional edges to open modules, and then be thrown away. It sounds like you want to keep it around in order to do further opening when additional child layers come into being. One way you could do this is create a module with a holder class to keep the reference. So rather than keeping the reference in the model world (ModuleReference), instead put it as a field in a reified module. With the right encapsulation it should give you the module layer -> Controller without compromising the integrity of the layers. If you want to discuss further then it's probably best to follow-up on jigsaw-dev rather here.
> 


Indeed - a synthetic module providing a holder service might do the job.

Seems somewhat cumbersome though.

Would it be too much to ask for accepting a patch adding a reference to ML.Controller to ModuleLayer?
It wouldn’t require changes to the spec I think as the lifetime of ML.Controller is unspecified.

— Michal



More information about the jigsaw-dev mailing list