0

Let's suppose we have JPMS modules - A,B, C etc. A defines some service a.spi.SomeService and all there modules have implementation of this service. For example, module B has an implementation of this service b.spi.SomeServiceImpl, so, in module-info of B we have:

provides a.spi.SomeService with b.spi.SomeServiceImpl; 

Can we make the service of A, which provides module B, C etc available only to module A, so as all other modules, even if they provides a.spi.SomeService too, couldn't use it?

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • [Spec](https://docs.oracle.com/javase/specs/jls/se13/html/jls-7.html#jls-7.7) confirms `provides TypeName with TypeName {, TypeName} ;`, you cannot expose it to a closed set of modules similar to exporting a package as in `exports PackageName [to ModuleName {, ModuleName}] ;`. So just and restrict that package of your provide to be exported/opened to a specific module, wouldn't that work? Or is it a cycle? Complete descriptors would help better analyse. – Naman Mar 30 '20 at 11:57
  • *Can we make the service, which provides module B available only to module A?* .. If you need the implementation provided by `B` in `A`. It would be a cyclic dependency in my opinion. What use case drives this? Why can't `A` have the implementation in itself and `export` it for `B` to re-use. Or else can your question be simplified to - "Is it possible to restrict the usage of service provided by a module."? Regarding the renaming, for the sake of the question, you can name it to `a.spi.SomeServiceProvider` and `b.SomeServiceProviderImpl` (but that's not important here imo.) – Naman Mar 30 '20 at 12:11
  • @Naman I have the following situation - one module has service interface and this module is the service consumer. All other modules provides this service. And I want that only module A could use provided services. – Pavel_K Mar 30 '20 at 12:15

1 Answers1

1

You can export a package only to certain modules:

module A {
  exports a.spi to B
}
Lazar Petrovic
  • 537
  • 3
  • 8
  • This solution is absolutely wrong. You allow access to `a.spi` only to one module - moudle B. The question is that N modules can implement the interface from module A, but only module A can use implemented services. – Pavel_K Mar 30 '20 at 12:06