Skip to content

Commit

Permalink
Remove duplicate from classpath after adding the transformed
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Jul 26, 2024
1 parent 8b889bb commit b79d342
Showing 1 changed file with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void main(String[] args) throws Throwable {
}
}
}
List<Path> tmpJars = new ArrayList<>();
List<Map.Entry<Path, PathWithTransformersEntry>> tmpJars = new ArrayList<>();
classpathProvider = ReadClasspathProvider.of(ClasspathProvider.fromProperties().filter(path -> {
File file = path.toFile().getAbsoluteFile();
for (PathWithTransformersEntry path1 : toTransform) {
Expand All @@ -152,7 +152,7 @@ public static void main(String[] args) throws Throwable {
Map<String, String> classRedefineCache = new HashMap<>();
DirectoryFileAccess debugOut = debugOuts.get(entry.getPath());
Path tmpJar = Files.createTempFile(null, ".jar");
tmpJars.add(tmpJar);
tmpJars.add(new AbstractMap.SimpleEntry<>(tmpJar, entry));
Files.deleteIfExists(tmpJar);
try (OpenedFileAccess outputInterface = OpenedFileAccess.ofJar(tmpJar)) {
try (OpenedFileAccess og = OpenedFileAccess.ofJar(entry.getPath())) {
Expand Down Expand Up @@ -208,9 +208,10 @@ public static void main(String[] args) throws Throwable {
}

List<String> cp = new ArrayList<>(Arrays.asList(System.getProperty("java.class.path", "").split(File.pathSeparator)));
for (Path tmpJar : tmpJars) {
cp.add(tmpJar.toAbsolutePath().toString());
for (Map.Entry<Path, PathWithTransformersEntry> tmpJar : tmpJars) {
cp.add(tmpJar.getKey().toAbsolutePath().toString());
}
removeDuplicates(cp, tmpJars);
System.setProperty("java.class.path", String.join(File.pathSeparator, cp));

Path mainClassPath = Paths.get(System.getProperty(MAIN_CLASS));
Expand All @@ -219,6 +220,57 @@ public static void main(String[] args) throws Throwable {
handle.invokeExact((String[]) argsList.toArray(new String[0]));
}

private static void removeDuplicates(List<String> cpList, List<Map.Entry<Path, PathWithTransformersEntry>> tmpJars) throws IOException {
List<Set<String>> tmpModules = new ArrayList<>();
for (Map.Entry<Path, PathWithTransformersEntry> tmpJar : tmpJars) {
try (OpenedFileAccess og = OpenedFileAccess.ofJar(tmpJar.getValue().path)) {
Set<String> modules = new HashSet<>();
og.handle(path -> {
if (path.endsWith(".class")) {
// Get package name, i.e. "dev/architectury/transformer/TransformerRuntime.class" -> "dev.architectury.transformer"
String packageName = Transform.trimLeadingSlash(path).substring(0, Transform.trimLeadingSlash(path).lastIndexOf('/')).replace('/', '.');
modules.add(packageName);
}
});
tmpModules.add(modules);
Logger.debug("Temporary jar [%s] contains packages: %s", tmpJar.getKey(), String.join(", ", modules));
}
}

cpList.removeIf(cp -> {
try {
Path cpPath = Paths.get(cp);
if (Files.isDirectory(cpPath)) {
try (OpenedFileAccess ac = OpenedFileAccess.ofDirectory(cpPath)) {
Set<String> modules = new HashSet<>();
ac.handle(path -> {
String relative = Transform.trimLeadingSlash(cpPath.relativize(Paths.get(path)).toString());
if (relative.endsWith(".class")) {
// Get package name, i.e. "dev/architectury/transformer/TransformerRuntime.class" -> "dev.architectury.transformer"
String packageName = relative.substring(0, relative.lastIndexOf('/')).replace('/', '.');
modules.add(packageName);
}
});
Logger.debug("Classpath entry [%s] contains packages: %s", cp, String.join(", ", modules));
if (modules.isEmpty()) {
return false;
}
for (Set<String> module : tmpModules) {
if (module.equals(modules)) {
Logger.info("Removing duplicate classpath entry: " + cp);
return true;
}
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}

return false;
});
}

private static List<PathWithTransformersEntry> parsePathWithTransformersEntries(String configText) throws IOException {
Map<Path, List<TransformerPair>> map;
try (TransformersReader reader = new TransformersReader(new StringReader(configText))) {
Expand Down

0 comments on commit b79d342

Please sign in to comment.