Why do we need requires static in java-9 module system? [duplicate]
Asked Answered
B

2

11

I started to learn jigsaw java-9 feature and read some articles/video.

I can't understand concept of optional dependencies(requires static)

quote from article:

When a module needs to be compiled against types from another module but does not want to depend on it at run time, it can use a requires static clause. If foo requires static bar, the module system behaves different at compile and run time:

At compile time, bar must be present or there will be an error. During compilation bar is readable by foo.
At run time, bar might be absent and that will cause neither error nor warning. If it is present, it is readable by foo.

So I want to know couple of things:

  1. What the reason to make module dependable on another module during compile time but not in runtime? any examples? instruments like lombok?

  2. Any analogs of optional dependencies in java prior java-9 ?

P.S.

I found one more explanation: quote from article:

Sometimes we write code that references another module, but that users of our library will never want to use.

For instance, we might write a utility function that pretty-prints our internal state when another logging module is present. But, not every consumer of our library will want this functionality, and they don’t want to include an extra logging library.

In these cases, we want to use an optional dependency. By using the requires static directive, we create a compile-time-only dependency:

module my.module {
    requires static module.name;
}

But it is absolutely unclear for me. Could anyone explain it in a simple way?

Burgher answered 16/5, 2019 at 14:0 Comment(2)
static mean optional only compile time requires and not runtime.Cavity
Obviously my quiestion is not duplicate. My question about real life use cases WHEN and WHY should I use optional dependencies!!!Burgher
T
11
  1. There are a decent number of libraries out there where it only makes sense to have them at compile time. Mostly this deals with annotations that only exist to help during development (e.g. prevent bugs, reduce boilerplate). Some examples include:


    These annotations tend to have a RetentionPolicy of SOURCE or CLASS, which means they aren't useful (or even available) at runtime. Why ship these dependencies with the rest of your application when you deploy? Without requires static you would be forced to include them when you deploy, otherwise your application would fail to start due to missing dependencies.

  2. You would declare these dependencies as optional pre-Java 9 as well. Many Java projects of any significance use a build tool such as Maven or Gradle. In addition to those tools automatically building and testing your project, a large part of what they do is dependency management. I'm not familiar enough with Maven, but when using Gradle one would use:

    dependencies {
        compileOnly 'group.id:artifact-id:version'
    }
    

    To declare dependencies that are not needed at runtime.

Trichinosis answered 16/5, 2019 at 14:13 Comment(3)
Isn't runtime-optionality also ideal for modules that might not be needed by certain users, i.e. with SLF4J most users might prefer the Logback connector while some might prefer JUL or the kind. The SLF4J project will for sure release all connectors simultaneously while only a limited set might be used at runtimeHorowitz
Ideally, in such a situation the module would only requires the SLF4J API module, which itself would expose an SPI. Then the implementation modules would implement the SPI which gets found and used at runtime. In other words, your module never requires the implementation modules directly.Trichinosis
That said, there are situations where a module can provide a functionality, but only when certain dependencies are present. But again, in an ideal world, such a module should be split up into multiple modules.Trichinosis
C
1

If Dependent Module should be available at compile time but optional at rumtime, then such type of Dependency is called Optional Dependency. We can Specify optional dependency by using static keyword.

Note The static keyword is used to say that "This dependency check is mandatory at compile time and optional at runtime."

Eg.1

module moduleB {
  requires moduleA;
}

moudleA should be available at the time of compilation & rumtime. it is not Optional Dependency.

Eg2.

module moduleB {
   requires static moduleA;
}

At the time of compilation moduleA should be available, but at runtime it is optional ie, at runtime even moduleA is not avaiable JVM will execute code.

Cavity answered 16/5, 2019 at 15:15 Comment(2)
more info by optional dependency : baeldung.com/java-9-modularityCavity
For instance, we might write a utility function that pretty-prints our internal state when another logging module is present. But, not every consumer of our library will want this functionality, and they don’t want to include an extra logging library. In these cases, we want to use an optional dependency. By using the requires static directive, we create a compile-time-only dependency: It is absolutely unclearBurgher

© 2022 - 2024 — McMap. All rights reserved.