LJC/vJUG Hackday query: ServiceLoader class

Chris Hegarty chris.hegarty at oracle.com
Fri May 12 14:53:21 UTC 2017


Mani,

On 12/05/17 12:48, Mani Sarkar wrote:
> Since I don’t have an IDE compatible con Java9 I don’t know if
> ServiceLoader class has a way to explicitly ask for an implementation on an
> explicit module
>
> *Anonymous: *
>
> See new constructors in JavaDoc:
> http://download.java.net/java/jdk9/docs/api/java/util/ServiceLoader.html
>
> Mani:  would the OpenJDK / Java 9 team please want to add anything else to
> the above

I added the following concrete example to the online doc.

---

It is possible to use the new-in-9 ServiceLoader::stream method to 
return a stream of ServiceLoader.Provider’s. Filtering based on 
type/module can be done on the stream. For example:

   // List all ToolProvider service providers in the jdk.jdeps module.
   ServiceLoader<?> sl = 
ServiceLoader.load(java.util.spi.ToolProvider.class);
   sl.stream()
     .map(p -> p.type())
     .filter(t -> t.getModule().getName().equals("jdk.jdeps"))
     .map(t -> t.getName())
     .forEach(System.out::println);

-Chris.


More information about the jdk9-dev mailing list