Skip to content

Commit 9485afa

Browse files
committed
WIP: make EclipseHelper with with Java 11+
1 parent ecbbba3 commit 9485afa

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,19 @@
219219
<artifactId>exec-maven-plugin</artifactId>
220220
<executions>
221221
<execution>
222+
<id>index-main-classes</id>
222223
<phase>process-classes</phase>
223224
<goals>
224225
<goal>java</goal>
225226
</goals>
226227
</execution>
228+
<execution>
229+
<id>index-test-classes</id>
230+
<phase>process-test-classes</phase>
231+
<goals>
232+
<goal>java</goal>
233+
</goals>
234+
</execution>
227235
</executions>
228236
<configuration>
229237
<mainClass>org.scijava.annotations.EclipseHelper</mainClass>

src/main/java/org/scijava/annotations/EclipseHelper.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131

3232
import java.io.File;
3333
import java.io.IOException;
34+
import java.lang.reflect.Field;
35+
import java.lang.reflect.InvocationTargetException;
36+
import java.lang.reflect.Method;
3437
import java.net.MalformedURLException;
3538
import java.net.URL;
3639
import java.net.URLClassLoader;
40+
import java.util.Arrays;
3741
import java.util.HashSet;
3842
import java.util.Set;
3943
import java.util.jar.Attributes.Name;
@@ -100,8 +104,8 @@ public class EclipseHelper extends DirectoryIndexer {
100104
static Set<URL> indexed = new HashSet<>();
101105
private boolean bannerShown;
102106

103-
private static boolean debug =
104-
"debug".equals(System.getProperty("scijava.log.level"));
107+
private static boolean debug = true;
108+
//"debug".equals(System.getProperty("scijava.log.level"));
105109
private boolean autoDetectEclipse = true;
106110

107111
private static void debug(final String message) {
@@ -125,18 +129,16 @@ private static void debug(final String message) {
125129
*/
126130
public static void updateAnnotationIndex(final ClassLoader loader) {
127131
debug("Checking class loader: " + loader);
128-
if (loader == null ||
129-
!(loader instanceof URLClassLoader))
130-
{
131-
debug("Not an URLClassLoader: " + loader);
132+
if (loader == null) {
133+
debug("Null ClassLoader!");
132134
return;
133135
}
134136
EclipseHelper helper = new EclipseHelper();
135137
if (Boolean.getBoolean(FORCE_ANNOTATION_INDEX_PROPERTY)) {
136138
helper.autoDetectEclipse = false;
137139
}
138140
boolean first = true;
139-
for (final URL url : ((URLClassLoader) loader).getURLs()) {
141+
for (final URL url : getURLs(loader)) {
140142
debug("Checking URL: " + url);
141143
if (helper.autoDetectEclipse && first) {
142144
if (!"file".equals(url.getProtocol()) ||
@@ -282,6 +284,31 @@ else if (file.isDirectory()) {
282284
return true;
283285
}
284286

287+
private static URL[] getURLs(final ClassLoader classLoader) {
288+
// -- Java 8 --
289+
if (classLoader instanceof URLClassLoader) {
290+
return ((URLClassLoader) classLoader).getURLs();
291+
}
292+
293+
// -- Java 11+ --
294+
try {
295+
// URLClassPath ucp = classLoader.ucp;
296+
final Field ucpField = classLoader.getClass().getDeclaredField("ucp");
297+
ucpField.setAccessible(true);
298+
final Object ucp = ucpField.get(classLoader);
299+
300+
// return ucp.getURLs();
301+
final Method getURLs = ucp.getClass().getDeclaredMethod("getURLs");
302+
return (URL[]) getURLs.invoke(ucp);
303+
}
304+
catch (final NoSuchMethodException | IllegalAccessException |
305+
InvocationTargetException | NoSuchFieldException e)
306+
{
307+
e.printStackTrace();
308+
return new URL[0];
309+
}
310+
}
311+
285312
/**
286313
* Updates the annotation index in the current Eclipse project.
287314
* <p>

0 commit comments

Comments
 (0)