|
| 1 | +package org.mangorage.mangobotlaunch; |
| 2 | + |
| 3 | +import org.mangorage.bootstrap.api.dependency.ModuleNameOrigin; |
| 4 | +import org.mangorage.mangobotlaunch.util.Result; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.UncheckedIOException; |
| 8 | +import java.lang.module.ModuleFinder; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.jar.JarFile; |
| 11 | +import java.util.jar.Manifest; |
| 12 | + |
| 13 | +public final class JarHandler { |
| 14 | + |
| 15 | + public static Result resolveModuleName(Path jarPath) { |
| 16 | + try (JarFile jarFile = new JarFile(jarPath.toFile())) { |
| 17 | + |
| 18 | + String moduleName = null; |
| 19 | + |
| 20 | + try { |
| 21 | + moduleName = ModuleFinder.of(jarPath) |
| 22 | + .findAll() |
| 23 | + .iterator() |
| 24 | + .next() |
| 25 | + .descriptor() |
| 26 | + .name(); |
| 27 | + } catch (Exception ignore) {} |
| 28 | + |
| 29 | + // 1. Proper JPMS module |
| 30 | + if (jarFile.getEntry("module-info.class") != null) { |
| 31 | + return new Result( |
| 32 | + ModuleFinder.of(jarPath) |
| 33 | + .findAll() |
| 34 | + .iterator() |
| 35 | + .next() |
| 36 | + .descriptor() |
| 37 | + .name(), |
| 38 | + ModuleNameOrigin.MODULE_INFO, |
| 39 | + jarPath |
| 40 | + ); |
| 41 | + } else if (jarFile.isMultiRelease() && moduleName != null) { |
| 42 | + return new Result(moduleName, ModuleNameOrigin.MULTI_RELEASE, jarPath); |
| 43 | + } |
| 44 | + |
| 45 | + // 2. Check MANIFEST.MF for Automatic-Module-Name |
| 46 | + Manifest manifest = jarFile.getManifest(); |
| 47 | + |
| 48 | + if (manifest != null) { |
| 49 | + String autoName = manifest.getMainAttributes() |
| 50 | + .getValue("Automatic-Module-Name"); |
| 51 | + |
| 52 | + if (autoName != null && !autoName.isBlank()) { |
| 53 | + return new Result( |
| 54 | + autoName, |
| 55 | + ModuleNameOrigin.MANIFEST, |
| 56 | + jarPath |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + final var found = ModuleFinder.of(jarPath).findAll(); |
| 62 | + if (found != null) { |
| 63 | + final var foundModule = found.stream().findAny(); |
| 64 | + if (foundModule.isPresent()) |
| 65 | + System.out.println(foundModule.get()); |
| 66 | + return new Result( |
| 67 | + foundModule.get().descriptor().name(), |
| 68 | + ModuleNameOrigin.MODULE_FINDER, |
| 69 | + jarPath |
| 70 | + ); |
| 71 | + } |
| 72 | + } catch (Exception ignore) { |
| 73 | + ignore.printStackTrace(); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + String symbolicName = manifest.getMainAttributes() |
| 78 | + .getValue("Bundle-SymbolicName"); |
| 79 | + |
| 80 | + if (symbolicName != null) { |
| 81 | + return new Result( |
| 82 | + symbolicName, |
| 83 | + ModuleNameOrigin.MANIFEST_BUNDLE_SYMBOLIC_NAME, |
| 84 | + jarPath |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + // 3. Fallback: filename heuristic (aka desperation mode) |
| 91 | + String filename = jarPath.getFileName().toString(); |
| 92 | + |
| 93 | + String cleanedName = filename |
| 94 | + .replaceAll("-[\\d\\.]+.*\\.jar$", "") // Remove version and extension |
| 95 | + .replaceAll("\\.jar$", "") // Remove extension if no version |
| 96 | + .replace('-', '.'); // Convert hyphens to dots |
| 97 | + |
| 98 | + return new Result( |
| 99 | + cleanedName, |
| 100 | + ModuleNameOrigin.GUESSED, |
| 101 | + jarPath |
| 102 | + ); |
| 103 | + |
| 104 | + } catch (IOException e) { |
| 105 | + throw new UncheckedIOException("Failed to read JAR: " + jarPath, e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments