It is safe to use same classes from same modules in different layers

Alan Bateman alan.bateman at oracle.com
Mon Sep 9 07:35:54 UTC 2024


On 08/09/2024 08:59, PavelTurk wrote:
> Hello everyone,
>
> Lets consider the following situation:
>
> Bootlayer (module Core) is a main application
> - child layer A (module A, module Foo) is a plugin A
> - child layer B (module B, module Foo) is a plugin B.
>
> So, the SAME module Foo (with class Foo) is used in two different 
> child layers.
>
> Module Core provides this service {
>        Object getInterPluginObject();
>        void setInterPluginObject(Object obj);
> }
>
> So, in plugin A I do
> var foo = new Foo();
> coreService.setInterPluginObject(foo);
>
> and in plugin B I do
> var foo = (Foo) coreService.getInterPluginObject();
>
> Could anyone say if it should work, and if yes, then how safe this 
> approach is?
> Please note, that module Foo CAN NOT be added to bootlayer because main
> application shouldn't know anything about it.

I assume this arrangement will fail with ClassCastException as B can't 
cast a A-Foo to a B-Foo.

If Core wants to provides a system-wide registry (which I think is what 
you are doing here) then it will need to be a Foo interface that is 
visible and accessible to all all users of that registry.

-Alan








More information about the jigsaw-dev mailing list