Are modifiers `static` and `transitive` incompatible?

Piotr P. Karwasz piotr.karwasz at gmail.com
Wed Jan 31 07:47:30 UTC 2024


Hello,

The `requires` instruction can have two qualifiers `static` and
`transitive` that separately make perfect sense. For a module `foo`:

 * `requires static bar;`, makes resolution of the `bar` module
optional at runtime,
 * `requires transitive bar;`, allows each module that reads `foo` to
also read `bar`. As far as I understand this means that `bar` must be
present at both compile time and runtime.

There is however a third option allowed:

```java
module foo {
    requires static transitive bar;
}
```

What is this combination supposed to do? Why is it allowed?

According to the Javadoc:

> `requires` directives that have the `static` modifier express an optional dependence at run time. If a module declares that it `requires static M` then resolution does not search the observable modules for M to satisfy the dependency. However, if M is recursively enumerated at step 1 then all modules that are enumerated and `requires static M` will read M.

where "step 1" describes the computation of the closure of the set of
root modules by the `X requires transitive Y` relation.

If I interpret this correctly, if a `requires` directive is both
`static` and `transitive`, the `foo` module is **not optional**, since
"step 1" requires its presence.

We often end up with such directives, when using the
`bnd-maven-plugin`, which maps the OSGi `resolution:=optional`
directive to `static` and the `uses:=...` directive to `transitive`.
If we mark a package as optional, but the package uses public types
from `bar`, we end up with `static transitive`.

Is this a bug of the plugin or does `static transitive` have a meaning?

Piotr

[1] https://github.com/bndtools/bnd/tree/master/maven-plugins/bnd-maven-plugin


More information about the jigsaw-dev mailing list