Skip to content

Instantly share code, notes, and snippets.

@lucamolteni
Created April 9, 2025 12:51
Show Gist options
  • Save lucamolteni/bcf034a4f5e86a862c3dc1114bfeb5bc to your computer and use it in GitHub Desktop.
Save lucamolteni/bcf034a4f5e86a862c3dc1114bfeb5bc to your computer and use it in GitHub Desktop.
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
public class CheckSplitPackages {
public static void main(String[] args) throws IOException {
var rootDir = Paths.get(args.length > 0 ? args[0] : ".");
System.out.println("πŸ” Scanning all JARs under: " + rootDir + " (excluding test JARs)");
var jarFiles = Files.walk(rootDir)
.filter(p -> p.toString().endsWith(".jar"))
.filter(p -> !p.getFileName().toString().endsWith("-tests.jar"))
.toList();
System.out.println("Found " + jarFiles.size() + " non-test JAR files to analyze");
var packageMap = new HashMap<String, String>();
var seen = new HashMap<String, Set<String>>();
var foundConflict = false;
for (var jarPath : jarFiles) {
var jarName = jarPath.getFileName().toString();
try (var jar = new JarFile(jarPath.toFile())) {
jar.stream()
.filter(e -> !e.isDirectory() && e.getName().endsWith(".class"))
.map(e -> {
var pkg = e.getName().contains("/") ?
e.getName().substring(0, e.getName().lastIndexOf('/')).replace('/', '.') :
"";
return pkg;
})
.filter(pkg -> !pkg.isEmpty())
.forEach(pkg -> {
var previous = packageMap.putIfAbsent(pkg, jarName);
if (previous != null && !previous.equals(jarName)) {
var jars = seen.computeIfAbsent(pkg, k -> new LinkedHashSet<>());
if (jars.add(jarName)) {
if (jars.size() == 1) {
System.out.println("🚨 Split package detected: " + pkg);
System.out.println(" ↳ in: " + previous);
System.out.println(" ↳ and: " + jarName);
} else {
System.out.println(" ↳ also in: " + jarName);
}
foundConflict = true;
}
}
});
}
}
if (!foundConflict) {
System.out.println("βœ… No split packages found!");
} else {
System.out.println("❌ Split packages detected β€” please fix before modularizing.");
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment