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