1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.module;
  27 
  28 import java.lang.module.Configuration;
  29 import java.lang.module.ResolvedModule;
  30 import java.util.HashMap;
  31 import java.util.HashSet;
  32 import java.util.Map;
  33 import java.util.Set;
  34 import java.util.function.Function;
  35 
  36 import jdk.internal.loader.ClassLoaders;
  37 
  38 
  39 /**
  40  * The module to class loader map.  The list of boot modules and platform modules
  41  * are generated at build time.
  42  */
  43 final class ModuleLoaderMap {
  44 
  45     /**
  46      * Returns the function to map modules in the given configuration to the
  47      * built-in class loaders.
  48      */
  49     static Function<String, ClassLoader> mappingFunction(Configuration cf) {
  50 
  51         // The list of boot modules and platform modules are generated at build time.
  52         final String[] BOOT_MODULES = new String[] { "@@BOOT_MODULE_NAMES@@" };
  53         final String[] PLATFORM_MODULES = new String[] { "@@PLATFORM_MODULE_NAMES@@" };
  54 
  55         Set<String> bootModules = new HashSet<>(BOOT_MODULES.length);
  56         for (String mn : BOOT_MODULES) {
  57             bootModules.add(mn);
  58         }
  59 
  60         Set<String> platformModules = new HashSet<>(PLATFORM_MODULES.length);
  61         for (String mn : PLATFORM_MODULES) {
  62             platformModules.add(mn);
  63         }
  64 
  65         ClassLoader platformClassLoader = ClassLoaders.platformClassLoader();
  66         ClassLoader appClassLoader = ClassLoaders.appClassLoader();
  67 
  68         Map<String, ClassLoader> map = new HashMap<>();
  69 
  70         for (ResolvedModule resolvedModule : cf.modules()) {
  71             String mn = resolvedModule.name();
  72             if (!bootModules.contains(mn)) {
  73                 if (platformModules.contains(mn)) {
  74                     map.put(mn, platformClassLoader);
  75                 } else {
  76                     map.put(mn, appClassLoader);
  77                 }
  78             }
  79         }
  80 
  81         return new Function<String, ClassLoader> () {
  82             @Override
  83             public ClassLoader apply(String mn) {
  84                 return map.get(mn);
  85             }
  86         };
  87     }
  88 }