Skip to content

Commit

Permalink
Fix LinkageError in rare cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ceze88 committed Jan 23, 2024
1 parent 5850e1a commit 52b93e8
Showing 1 changed file with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,14 @@
public class DependencyLoader {

private static final Logger logger = LoggerFactory.getLogger(DependencyLoader.class);
private static final Set<Dependency> loadedDependencies = new HashSet<>();
private static LibraryLoader libraryLoader;
private static final LibraryLoader libraryLoader = new LibraryLoader(new File("craftaro"));

public static LibraryLoader getLibraryLoader() {
return libraryLoader;
}

public static void initParentClassLoader(ClassLoader parent) {
libraryLoader = new LibraryLoader(parent, new File("craftaro"));
}

public static void loadDependencies(Set<Dependency> dependencies) {
for (Dependency dependency : dependencies) {
if (loadedDependencies.contains(dependency)) {
continue;
}
loadDependency(dependency);
}
}
Expand All @@ -69,6 +61,11 @@ public static void loadDependency(Dependency dependency) {
File outputFile = new File(libraryLoader.getLibFolder(), dependency.getGroupId().replace(".", File.separator) + File.separator + dependency.getArtifactId().replace(".", File.separator) + File.separator + dependency.getVersion() + File.separator + "raw-" + name + ".jar");
File relocatedFile = new File(outputFile.getParentFile(), name.replace("raw-", "") + ".jar");
if (relocatedFile.exists()) {
//Check if the file is already loaded to the classpath
if (isLoaded(relocatedFile)) {
return;
}

//Load dependency into the classpath
loadJarIntoClasspath(relocatedFile, dependency);
return;
Expand Down Expand Up @@ -148,7 +145,11 @@ public static void loadJarIntoClasspath(File file, Dependency dependency) {
}
}

libraryLoader.load(new LibraryLoader.Dependency(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getRepositoryUrl()), true);
try {
libraryLoader.load(new LibraryLoader.Dependency(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getRepositoryUrl()), true);
} catch (InvalidDependencyException e) {
//already loaded
}

} catch (Exception e) {
logger.error("[CraftaroCore] Failed to load dependency " + file, e);
Expand All @@ -170,4 +171,30 @@ private static boolean isRelocated(File file) {
}
return false;
}

private static boolean isLoaded(File file) {
//Find the first class file in the jar and try Class.forName
try (ZipFile zipFile = new ZipFile(file)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().startsWith("META-INF")) {
continue;
}

if (entry.getName().endsWith(".class")) {
String className = entry.getName().replace("/", ".").replace(".class", "");
try {
Class.forName(className);
return true;
} catch (Exception | Error e) {
return false;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}

0 comments on commit 52b93e8

Please sign in to comment.