If you want an Optional, you can use findFirst() on a stream, Optional<ModuleReference> ref = ModuleFinder.of( jar ).findAll().stream().findFirst();
Rémi ----- Mail original ----- > De: "Gunnar Morling" <gun...@hibernate.org> > À: "Alan Bateman" <alan.bate...@oracle.com> > Cc: "jigsaw-dev" <jigsaw-dev@openjdk.java.net> > Envoyé: Mardi 25 Avril 2017 09:10:45 > Objet: Re: Getting the automatic module name of non-modular JAR > I see; thanks, Alan. > > I wanted to avoid using a regex or similar, in order to make sure the > JDK's own automatic naming rules are applied instead of > "re-implementing" them. I was kinda hoping for a method like > > Path jar = ...; > Optional<ModuleReference> ref = ModuleReference.of( jar ); > > > 2017-04-25 8:49 GMT+02:00 Alan Bateman <alan.bate...@oracle.com>: >> On 24/04/2017 21:23, Gunnar Morling wrote: >> >>> Hi, >>> >>> Given a non-modular JAR (e.g. represented as Path), what's the easiest >>> way to obtain the automatic module name derived for this JAR? >> >> If you just want the name then it might be more efficient to do it with a >> regular expression. >> >>> >>> I found the following: >>> >>> Path nonModularJar = ...; >>> String automaticModuleName = ModuleFinder.of( nonModularJar ) >>> .findAll() >>> .iterator() >>> .next() >>> .descriptor() >>> .name(); >>> >>> Is this the best I can do? >>> >>> More generally speaking, is using ModuleFinder with a single path the >>> only way to obtain a ModuleReference/ModuleDescriptor for a specific >>> JAR? >> >> Yes, ModuleFinder is the only way (it might be more succulent to use stream >> + findFirst but that is just detail). If you are only interested in the name >> then you could of course open the JAR file. If it contains module-info.class >> then read it with ModuleDescriptor.read, otherwise use a regex to derive the >> name. >> > > -Alan