ModuleLayer.Controller strongly references Module
Alan Bateman
Alan.Bateman at oracle.com
Sun Nov 21 20:55:27 UTC 2021
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.
-Alan.
More information about the discuss
mailing list