Skip to content

Commit

Permalink
Merge pull request #54 from skalarproduktraum/master
Browse files Browse the repository at this point in the history
JPMS-safe getClasspathElements()
  • Loading branch information
ctrueden authored Sep 27, 2023
2 parents bea0de6 + 07837b7 commit 9484376
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 49 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
<groupId>net.imagej</groupId>
<artifactId>ij</artifactId>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.162</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
Expand Down
58 changes: 9 additions & 49 deletions src/main/java/net/imagej/patcher/LegacyHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

package net.imagej.patcher;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;

import java.awt.event.KeyEvent;
import java.io.ByteArrayInputStream;
import java.io.File;
Expand Down Expand Up @@ -590,57 +593,14 @@ public static Collection<File> getClasspathElements(
final ClassLoader fromClassLoader, final StringBuilder errors,
final ClassLoader... excludeClassLoaders)
{
final Set<ClassLoader> exclude =
new HashSet<ClassLoader>(Arrays.asList(excludeClassLoaders));
final List<File> result = new ArrayList<File>();
for (ClassLoader loader = fromClassLoader; loader != null; loader =
loader.getParent()) {
if (exclude.contains(loader)) break;

if (!(loader instanceof URLClassLoader)) {
errors.append("Cannot add class path from ClassLoader of type ")
.append(fromClassLoader.getClass().getName()).append("\n");
continue;
}
try( ScanResult result = new ClassGraph()
// .verbose()
// .enableAllInfo()
.acceptPackages()
.scan()) {

for (final URL url : ((URLClassLoader) loader).getURLs()) {
if (!"file".equals(url.getProtocol())) {
errors.append("Not a file URL! ").append(url).append("\n");
continue;
}
result.add(new File(url.getPath()));
final String path = url.getPath();
if (path.matches(".*/target/surefire/surefirebooter[0-9]*\\.jar")) try {
final JarFile jar = new JarFile(path);
final Manifest manifest = jar.getManifest();
if (manifest != null) {
final String classPath =
manifest.getMainAttributes().getValue(Name.CLASS_PATH);
if (classPath != null) {
for (final String element : classPath.split(" +"))
try {
final URL url2 = new URL(element);
if (!"file".equals(url2.getProtocol())) {
errors.append("Not a file URL! ").append(url2).append("\n");
continue;
}
result.add(new File(url2.getPath()));
}
catch (final MalformedURLException e) {
e.printStackTrace();
}
}
}
}
catch (final IOException e) {
System.err
.println("Warning: could not add plugin class path due to ");
e.printStackTrace();
}

}
return result.getClasspathFiles();
}
return result;
}

/**
Expand Down

0 comments on commit 9484376

Please sign in to comment.