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