1 /* 2 * Copyright (c) 2015, 2016, 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.ModuleDescriptor; 29 import java.lang.reflect.Layer; 30 import java.lang.reflect.Module; 31 import java.net.URI; 32 import java.security.AccessController; 33 import java.security.PrivilegedAction; 34 import java.util.Set; 35 36 import jdk.internal.loader.BootLoader; 37 import jdk.internal.loader.ClassLoaders; 38 import jdk.internal.misc.JavaLangReflectModuleAccess; 39 import jdk.internal.misc.SharedSecrets; 40 41 /** 42 * A helper class to allow JDK classes create dynamic modules and to update 43 * modules, exports and the readability graph. It is also invoked by the VM 44 * to add read edges when agents are instrumenting code that need to link 45 * to supporting classes. 46 * 47 * The parameters that are package names in this API are the fully-qualified 48 * names of the packages as defined in section 6.5.3 of <cite>The Java™ 49 * Language Specification </cite>, for example, {@code "java.lang"}. 50 */ 51 52 public class Modules { 53 private Modules() { } 54 55 private static final JavaLangReflectModuleAccess JLRMA 56 = SharedSecrets.getJavaLangReflectModuleAccess(); 57 58 /** 59 * Creates a new Module. The module has the given ModuleDescriptor and 60 * is defined to the given class loader. 61 * 62 * The resulting Module is in a larval state in that it does not not read 63 * any other module and does not have any exports. 64 * 65 * The URI is for information purposes only. 66 */ 67 public static Module defineModule(ClassLoader loader, 68 ModuleDescriptor descriptor, 69 URI uri) 70 { 71 return JLRMA.defineModule(loader, descriptor, uri); 72 } 73 74 /** 75 * Define a new module to the VM. The module has the given set of 76 * packages and is defined to the given class loader. 77 * 78 * The resulting Module is in a larval state in that it does not not read 79 * any other module and does not have any exports. 80 */ 81 public static Module defineModule(ClassLoader loader, 82 String name, 83 Set<String> packages) 84 { 85 ModuleDescriptor descriptor = ModuleDescriptor.newModule(name) 86 .packages(packages) 87 .build(); 88 89 return JLRMA.defineModule(loader, descriptor, null); 90 } 91 92 /** 93 * Adds a read-edge so that module {@code m1} reads module {@code m1}. 94 * Same as m1.addReads(m2) but without a caller check. 95 */ 96 public static void addReads(Module m1, Module m2) { 97 JLRMA.addReads(m1, m2); 98 } 99 100 /** 101 * Update module {@code m} to read all unnamed modules. 102 */ 103 public static void addReadsAllUnnamed(Module m) { 104 JLRMA.addReadsAllUnnamed(m); 105 } 106 107 /** 108 * Updates module m1 to export a package to module m2. 109 * Same as m1.addExports(pn, m2) but without a caller check. 110 */ 111 public static void addExports(Module m1, String pn, Module m2) { 112 JLRMA.addExports(m1, pn, m2); 113 } 114 115 /** 116 * Updates module m1 to open a package to module m2. 117 * Same as m1.addOpens(pn, m2) but without a caller check. 118 */ 119 public static void addOpens(Module m1, String pn, Module m2) { 120 JLRMA.addOpens(m1, pn, m2); 121 } 122 123 /** 124 * Updates a module m to export a package to all modules. 125 */ 126 public static void addExportsToAll(Module m, String pn) { 127 JLRMA.addExportsToAll(m, pn); 128 } 129 130 /** 131 * Updates a module m to open a package to all modules. 132 */ 133 public static void addOpensToAll(Module m, String pn) { 134 JLRMA.addOpensToAll(m, pn); 135 } 136 137 /** 138 * Updates module m to export a package to all unnamed modules. 139 */ 140 public static void addExportsToAllUnnamed(Module m, String pn) { 141 JLRMA.addExportsToAllUnnamed(m, pn); 142 } 143 144 /** 145 * Updates module m to open a package to all unnamed modules. 146 */ 147 public static void addOpensToAllUnnamed(Module m, String pn) { 148 JLRMA.addOpensToAllUnnamed(m, pn); 149 } 150 151 /** 152 * Updates module m to use a service 153 */ 154 public static void addUses(Module m, Class<?> service) { 155 JLRMA.addUses(m, service); 156 } 157 158 /** 159 * Updates module m to provide a service 160 */ 161 public static void addProvides(Module m, Class<?> service, Class<?> impl) { 162 Layer layer = m.getLayer(); 163 164 if (layer == null || layer == Layer.boot()) { 165 // update ClassLoader catalog 166 PrivilegedAction<ClassLoader> pa = m::getClassLoader; 167 ClassLoader loader = AccessController.doPrivileged(pa); 168 ServicesCatalog catalog; 169 if (loader == null) { 170 catalog = BootLoader.getServicesCatalog(); 171 } else { 172 catalog = ServicesCatalog.getServicesCatalog(loader); 173 } 174 catalog.addProvider(m, service, impl); 175 } 176 177 if (layer != null) { 178 // update Layer catalog 179 SharedSecrets.getJavaLangReflectModuleAccess() 180 .getServicesCatalog(layer) 181 .addProvider(m, service, impl); 182 } 183 } 184 185 /** 186 * Adds a package to a module's content. 187 * 188 * This method is a no-op if the module already contains the package or the 189 * module is an unnamed module. 190 */ 191 public static void addPackage(Module m, String pn) { 192 JLRMA.addPackage(m, pn); 193 } 194 195 /** 196 * Called by the VM when code in the given Module has been transformed by 197 * an agent and so may have been instrumented to call into supporting 198 * classes on the boot class path or application class path. 199 */ 200 public static void transformedByAgent(Module m) { 201 addReads(m, BootLoader.getUnnamedModule()); 202 addReads(m, ClassLoaders.appClassLoader().getUnnamedModule()); 203 } 204 }