provides and requires static ... runtime error
Alan Bateman
Alan.Bateman at oracle.com
Tue Apr 18 08:05:06 UTC 2023
On 17/04/2023 15:20, Rob Bygrave wrote:
> /:/
>
>
> > /is it reasonable to consider that ServiceLoader is the *only* vector
> by which the implementation class will be instantiated?/
>
> In my view this is expected and imo I get there by thinking in the
> opposite direction from the provides p.S type to the requires static
> rather than the other way around. That is, the p.S type is only
> available via requires static hence it is expected to potentially not
> exist at runtime in the module-path. That is, if p.S was expected to
> exist at runtime it would be "read" via a requires or requires
> transient clause and not via a requires static - /the use of requires
> static for this case is explicit and intentional/.
>
> In using requires static ... imo we are explicitly going
> out-of-our-way to say "the types here might not be available at
> runtime" and the classic case for this as I see it is this case of
> providing an optional service, that will only be service loaded if the
> user of that service is in the classpath / module-path. /IF/ the
> module that is the user of a service is in the classpath / module-path
> then that module will ensure that the p.S type is in the module-path.
>
In your example, module io.avaje.config declares that it provides an
implementation of io.avaje.inject.spi.PropertyRequiresPlugin. There is
nothing to connect this to `requires static io.avaje.inject`. The module
system would need to search "far and wide" for io.avaje.inject to see if
exports io.avaje.inject.spi to io.avaje.config, otherwise there is no
way for it to know that the "missing package" is in a module that is not
required to be present at run-time. In other words, `requires static
io.avaje.inject` does not convey to the module system that
io.avaje.inject exports packages with service types.
I understand there is a temptation to compare `requires static` with
optional dependences in Maven but they are not the same thing. `requires
static` is more for use-cases like annotations that do not need to be
present at run-time. It could of course be extended but it's a slippery
slope that ultimately amounts to giving up on reliability. In this case,
it amounts to giving up on a post resolution check and allowing all
service providers to have a dangling reference to a service type.
-Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jigsaw-dev/attachments/20230418/98576b53/attachment.htm>
More information about the jigsaw-dev
mailing list