Skip to content

Commit 9484376

Browse files
authored
Merge pull request #54 from skalarproduktraum/master
JPMS-safe getClasspathElements()
2 parents bea0de6 + 07837b7 commit 9484376

File tree

2 files changed

+14
-49
lines changed

2 files changed

+14
-49
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@
9797
<groupId>net.imagej</groupId>
9898
<artifactId>ij</artifactId>
9999
</dependency>
100+
<dependency>
101+
<groupId>io.github.classgraph</groupId>
102+
<artifactId>classgraph</artifactId>
103+
<version>4.8.162</version>
104+
</dependency>
100105
<dependency>
101106
<groupId>org.javassist</groupId>
102107
<artifactId>javassist</artifactId>

src/main/java/net/imagej/patcher/LegacyHooks.java

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
package net.imagej.patcher;
3131

32+
import io.github.classgraph.ClassGraph;
33+
import io.github.classgraph.ScanResult;
34+
3235
import java.awt.event.KeyEvent;
3336
import java.io.ByteArrayInputStream;
3437
import java.io.File;
@@ -590,57 +593,14 @@ public static Collection<File> getClasspathElements(
590593
final ClassLoader fromClassLoader, final StringBuilder errors,
591594
final ClassLoader... excludeClassLoaders)
592595
{
593-
final Set<ClassLoader> exclude =
594-
new HashSet<ClassLoader>(Arrays.asList(excludeClassLoaders));
595-
final List<File> result = new ArrayList<File>();
596-
for (ClassLoader loader = fromClassLoader; loader != null; loader =
597-
loader.getParent()) {
598-
if (exclude.contains(loader)) break;
599-
600-
if (!(loader instanceof URLClassLoader)) {
601-
errors.append("Cannot add class path from ClassLoader of type ")
602-
.append(fromClassLoader.getClass().getName()).append("\n");
603-
continue;
604-
}
596+
try( ScanResult result = new ClassGraph()
597+
// .verbose()
598+
// .enableAllInfo()
599+
.acceptPackages()
600+
.scan()) {
605601

606-
for (final URL url : ((URLClassLoader) loader).getURLs()) {
607-
if (!"file".equals(url.getProtocol())) {
608-
errors.append("Not a file URL! ").append(url).append("\n");
609-
continue;
610-
}
611-
result.add(new File(url.getPath()));
612-
final String path = url.getPath();
613-
if (path.matches(".*/target/surefire/surefirebooter[0-9]*\\.jar")) try {
614-
final JarFile jar = new JarFile(path);
615-
final Manifest manifest = jar.getManifest();
616-
if (manifest != null) {
617-
final String classPath =
618-
manifest.getMainAttributes().getValue(Name.CLASS_PATH);
619-
if (classPath != null) {
620-
for (final String element : classPath.split(" +"))
621-
try {
622-
final URL url2 = new URL(element);
623-
if (!"file".equals(url2.getProtocol())) {
624-
errors.append("Not a file URL! ").append(url2).append("\n");
625-
continue;
626-
}
627-
result.add(new File(url2.getPath()));
628-
}
629-
catch (final MalformedURLException e) {
630-
e.printStackTrace();
631-
}
632-
}
633-
}
634-
}
635-
catch (final IOException e) {
636-
System.err
637-
.println("Warning: could not add plugin class path due to ");
638-
e.printStackTrace();
639-
}
640-
641-
}
602+
return result.getClasspathFiles();
642603
}
643-
return result;
644604
}
645605

646606
/**

0 commit comments

Comments
 (0)