ModuleGraphManipulator.java revision 1894:48d8835e3aaa
1/* 2 * Copyright (c) 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 26package jdk.nashorn.internal.scripts; 27 28import jdk.nashorn.api.scripting.JSObject; 29 30/** 31 * Nashorn's StructureLoader and ScriptLoader instances load 32 * this class in the respective dynamic modules created. This 33 * class is never loaded by Nashorn's own class loader. The 34 * .class bytes of this class are loaded as resource by the 35 * {@link jdk.nashorn.internal.runtime.NashornLoader} class. This class 36 * exists in this package because nashorn structures and scripts 37 * modules use this package name for the only exported package 38 * from those modules. 39 * 40 * Note that this class may be dynamically generated at runtime. 41 * But, this java source is used for ease of reading. 42 */ 43final class ModuleGraphManipulator { 44 private ModuleGraphManipulator() {} 45 46 private static final Module MY_MODULE; 47 private static final String MY_PKG_NAME; 48 49 static { 50 final Class<?> myClass = ModuleGraphManipulator.class; 51 MY_MODULE = myClass.getModule(); 52 final String myName = myClass.getName(); 53 MY_PKG_NAME = myName.substring(0, myName.lastIndexOf('.')); 54 55 // nashorn's module is the module of the class loader of current class 56 final Module nashornModule = myClass.getClassLoader().getClass().getModule(); 57 58 // Make sure this class was not loaded by Nashorn's own loader! 59 if (MY_MODULE == nashornModule) { 60 throw new IllegalStateException(myClass + " loaded by wrong loader!"); 61 } 62 63 // open package to nashorn module 64 MY_MODULE.addOpens(MY_PKG_NAME, nashornModule); 65 } 66 67 // The following method is reflectively invoked from Nashorn 68 // to add required module export edges. Because this package 69 // itself is qualified exported only to nashorn and this 70 // method is private, unsafe calls are not possible. 71 72 private static void addExport(final Module otherMod) { 73 MY_MODULE.addExports(MY_PKG_NAME, otherMod); 74 } 75} 76