jdk/src/java.base/share/classes/jdk/internal/loader/BuiltinClassLoader.java
changeset 45004 ea3137042a61
parent 44359 c6761862ca0b
child 45652 33342314ce89
equal deleted inserted replaced
44789:73fd39e0702e 45004:ea3137042a61
   170     boolean hasClassPath() {
   170     boolean hasClassPath() {
   171         return ucp != null;
   171         return ucp != null;
   172     }
   172     }
   173 
   173 
   174     /**
   174     /**
   175      * Register a module this this class loader. This has the effect of making
   175      * Register a module this class loader. This has the effect of making the
   176      * the types in the module visible.
   176      * types in the module visible.
   177      */
   177      */
   178     public void loadModule(ModuleReference mref) {
   178     public void loadModule(ModuleReference mref) {
   179         assert !VM.isModuleSystemInited();
       
   180 
       
   181         String mn = mref.descriptor().name();
   179         String mn = mref.descriptor().name();
   182         if (nameToModule.putIfAbsent(mn, mref) != null) {
   180         if (nameToModule.putIfAbsent(mn, mref) != null) {
   183             throw new InternalError(mn + " already defined to this loader");
   181             throw new InternalError(mn + " already defined to this loader");
   184         }
   182         }
   185 
   183 
   188             LoadedModule other = packageToModule.putIfAbsent(pn, loadedModule);
   186             LoadedModule other = packageToModule.putIfAbsent(pn, loadedModule);
   189             if (other != null) {
   187             if (other != null) {
   190                 throw new InternalError(pn + " in modules " + mn + " and "
   188                 throw new InternalError(pn + " in modules " + mn + " and "
   191                                         + other.mref().descriptor().name());
   189                                         + other.mref().descriptor().name());
   192             }
   190             }
       
   191         }
       
   192 
       
   193         // clear resources cache if VM is already initialized
       
   194         if (VM.isModuleSystemInited() && resourceCache != null) {
       
   195             resourceCache = null;
   193         }
   196         }
   194     }
   197     }
   195 
   198 
   196     /**
   199     /**
   197      * Returns the {@code ModuleReference} for the named module defined to
   200      * Returns the {@code ModuleReference} for the named module defined to
   353      * The cache used by this method avoids repeated searching of all modules.
   356      * The cache used by this method avoids repeated searching of all modules.
   354      */
   357      */
   355     private List<URL> findMiscResource(String name) throws IOException {
   358     private List<URL> findMiscResource(String name) throws IOException {
   356         SoftReference<Map<String, List<URL>>> ref = this.resourceCache;
   359         SoftReference<Map<String, List<URL>>> ref = this.resourceCache;
   357         Map<String, List<URL>> map = (ref != null) ? ref.get() : null;
   360         Map<String, List<URL>> map = (ref != null) ? ref.get() : null;
   358         if (map != null) {
   361         if (map == null) {
       
   362             map = new ConcurrentHashMap<>();
       
   363             this.resourceCache = new SoftReference<>(map);
       
   364         } else {
   359             List<URL> urls = map.get(name);
   365             List<URL> urls = map.get(name);
   360             if (urls != null)
   366             if (urls != null)
   361                 return urls;
   367                 return urls;
   362         }
   368         }
   363 
   369 
   379                                 } catch (MalformedURLException |
   385                                 } catch (MalformedURLException |
   380                                          IllegalArgumentException e) {
   386                                          IllegalArgumentException e) {
   381                                 }
   387                                 }
   382                             }
   388                             }
   383                         }
   389                         }
   384                         return result;
   390                         return (result != null) ? result : Collections.emptyList();
   385                     }
   391                     }
   386                 });
   392                 });
   387         } catch (PrivilegedActionException pae) {
   393         } catch (PrivilegedActionException pae) {
   388             throw (IOException) pae.getCause();
   394             throw (IOException) pae.getCause();
   389         }
   395         }
   390 
   396 
   391         // only cache resources after all modules have been defined
   397         // only cache resources after VM is fully initialized
   392         if (VM.isModuleSystemInited()) {
   398         if (VM.isModuleSystemInited()) {
   393             if (map == null) {
       
   394                 map = new ConcurrentHashMap<>();
       
   395                 this.resourceCache = new SoftReference<>(map);
       
   396             }
       
   397             if (urls == null)
       
   398                 urls = Collections.emptyList();
       
   399             map.putIfAbsent(name, urls);
   399             map.putIfAbsent(name, urls);
   400         }
   400         }
       
   401 
   401         return urls;
   402         return urls;
   402     }
   403     }
   403 
   404 
   404     /**
   405     /**
   405      * Returns the URL to a resource in a module or {@code null} if not found.
   406      * Returns the URL to a resource in a module or {@code null} if not found.