3131
3232import java .io .File ;
3333import java .io .IOException ;
34+ import java .lang .reflect .Field ;
35+ import java .lang .reflect .InvocationTargetException ;
36+ import java .lang .reflect .Method ;
3437import java .net .MalformedURLException ;
3538import java .net .URL ;
3639import java .net .URLClassLoader ;
40+ import java .util .Arrays ;
3741import java .util .HashSet ;
3842import java .util .Set ;
3943import 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