It is safe to use same classes from same modules in different layers
Johannes Spangenberg
johannes.spangenberg at hotmail.de
Sun Sep 8 19:37:48 UTC 2024
> 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.
If I understand you correctly, you would add module Foo to the module
path of both child layers? In this case, both layers would have access
to a class called Foo, but it would not be the same class in both
layers. This makes sense if you consider that both layers might just as
well depend on two different versions of module Foo, so they cannot
share the same loaded classes. As a result, your code would run into a
ClassCastException.
> and in plugin B I do
> var foo = (Foo) coreService.getInterPluginObject();
However, note that the JPMS is not limited to a single parent layer. You
could add a separate layer containing Foo, which could be an additional
parent for both, layer A and B. Depending on your context, plugin B may
also declare a dependency on plugin A, and therefore get child layer A
as a parent. I don't think that I can give you a proper recommendation
as I don't know much about your project. (Also not that I am NOT
affiliated with the OpenJDK project, I am just a fellow subscriber to
this mailing list.)
More information about the jigsaw-dev
mailing list