2

Before java9 modules there was an option to generate both service provider implementation classes and META-INF/services files. With this option service provider classes generated during annotation processing were accessible via ServiceLoader in runtime (if an appropriate META-INF/services resource files were generated as well).

In java9 (with enabled modules) new ServiceLoader mechanic was introduced. No more META-INF/services, all services and service providers should be explicitly declared in module-info.java. Does it mean that java9 (with enabled modules) restricts from generating both service provider classes and their declarations or am I missing something?

Thanks.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Not really sure, what you mean by annotation processing here, but seems like you're mostly looking for the [ServiceLoader explanation.](https://docs.oracle.com/javase/10/docs/api/java/util/ServiceLoader.html)... the section specific to "Deploying service providers on the class path" and "Deploying service providers as modules" should be of your interest. – Naman Aug 16 '18 at 14:42
  • @nullpointer, annotation processing is a standard framework tool which allows you to hook into the compilation process and modify the AST. My use case is that I'm generating service providers classes on compile time. The point is that during compilation time I can also generate META-INF/services for all newly generated impls but with enabled java9 modules this won't work. – Nikolay Papakha Aug 16 '18 at 15:05
  • What's failing for you while you try and implement the same old code of yours would have been more of my interest to look into. Could have given an insight of what exactly are you seeking further off from there. – Naman Aug 16 '18 at 15:10
  • @nullpointer, for example, Google Auto Service (https://github.com/google/auto/tree/master/service) employs the same idea - generates META-INF/services files but for source files. With java9 modules enabled ServiceLoader loads modules declared in java-module file only, it ignores META-INF/services at all. – Nikolay Papakha Aug 16 '18 at 15:21
  • According to the doc that I've linked in my earlier comment, I think it should still be feasible to find out the providers of the API and create a corresponding META-INF/services file for it. – Naman Aug 16 '18 at 15:24
  • @nullpointer, When you specify a module descriptor, java-module file, then service loader won't require the META-INF/services files for that module it will look for provider declarations in module descriptor. – Nikolay Papakha Aug 16 '18 at 15:41
  • @nullpointer, So either there should be another option to make generated providers(from a Named Application Module) available for service loader or this is a limitation by design – Nikolay Papakha Aug 16 '18 at 15:44
  • 1
    Sounds like the problem is to generate classes to be put into an already existing module (whose `provides` declaration you can’t change). What is stopping you from putting the generated classes into a generated module with a generated `provides` declaration? – Holger Jan 06 '20 at 17:35

0 Answers0