4141import java .nio .file .attribute .BasicFileAttributes ;
4242import java .security .CodeSource ;
4343import java .util .ArrayList ;
44- import java .util .Arrays ;
44+ import java .util .Comparator ;
4545import java .util .List ;
4646import java .util .jar .JarEntry ;
4747import java .util .jar .JarFile ;
@@ -209,17 +209,16 @@ private String extractModuleNameFromJar(File file, JarFile jarFile) throws IOExc
209209 * @return a {@link StringBuilder} with the {@code META-INF/versions/<version number>} if it exists; otherwise null
210210 */
211211 private static StringBuilder versionDirectoryIfExists (JarFile jarFile ) {
212+ Comparator <Integer > numericOrder = Integer ::compareTo ;
212213 List <Integer > versions = jarFile .stream ()
213214 .filter (je -> je .getName ().startsWith (META_INF_VERSIONS_PREFIX ) && je .getName ().endsWith ("/module-info.class" ))
214215 .map (
215216 je -> Integer .parseInt (
216217 je .getName ().substring (META_INF_VERSIONS_PREFIX .length (), je .getName ().length () - META_INF_VERSIONS_PREFIX .length ())
217218 )
218219 )
220+ .sorted (numericOrder .reversed ())
219221 .toList ();
220- versions = new ArrayList <>(versions );
221- versions .sort (Integer ::compareTo );
222- versions = versions .reversed ();
223222 int major = Runtime .version ().feature ();
224223 StringBuilder path = new StringBuilder (META_INF_VERSIONS_PREFIX );
225224 for (int version : versions ) {
@@ -300,7 +299,10 @@ private String extractClassNameFromDirectory(File dir) throws IOException {
300299 public @ NotNull FileVisitResult visitFile (@ NotNull Path candidate , @ NotNull BasicFileAttributes attrs ) {
301300 String name = candidate .getFileName ().toString (); // Just the part after the last dir separator
302301 if (name .endsWith (".class" ) && (name .equals ("module-info.class" ) || name .contains ("$" )) == false ) {
303- result = candidate .toAbsolutePath ().toString ().substring (dir .getAbsolutePath ().length () + 1 );
302+ result = candidate .toAbsolutePath ()
303+ .toString ()
304+ .substring (dir .getAbsolutePath ().length () + 1 )
305+ .replace (File .separatorChar , '/' );
304306 return TERMINATE ;
305307 } else {
306308 return CONTINUE ;
@@ -316,20 +318,24 @@ private String extractClassNameFromDirectory(File dir) throws IOException {
316318 * if it exists or the preset one derived from the jar task
317319 */
318320 private String extractModuleNameFromDirectory (File dir ) throws IOException {
319- List <File > files = new ArrayList <>(List .of (dir ));
320- while (files .isEmpty () == false ) {
321- File find = files .removeFirst ();
322- if (find .exists ()) {
323- if (find .getName ().equals ("module-info.class" )) {
324- try (InputStream inputStream = new FileInputStream (find )) {
325- return extractModuleNameFromModuleInfo (inputStream );
321+ var visitor = new SimpleFileVisitor <Path >() {
322+ private String result = getModuleName ().getOrNull ();
323+
324+ @ Override
325+ public @ NotNull FileVisitResult visitFile (@ NotNull Path candidate , @ NotNull BasicFileAttributes attrs ) throws IOException {
326+ String name = candidate .getFileName ().toString (); // Just the part after the last dir separator
327+ if (name .equals ("module-info.class" )) {
328+ try (InputStream inputStream = new FileInputStream (candidate .toFile ())) {
329+ result = extractModuleNameFromModuleInfo (inputStream );
330+ return TERMINATE ;
326331 }
327- } else if ( find . isDirectory ()) {
328- files . addAll ( Arrays . asList ( find . listFiles ())) ;
332+ } else {
333+ return CONTINUE ;
329334 }
330335 }
331- }
332- return getModuleName ().getOrNull ();
336+ };
337+ Files .walkFileTree (dir .toPath (), visitor );
338+ return visitor .result ;
333339 }
334340
335341 /**
0 commit comments