nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java
author attila
Thu, 06 Oct 2016 16:27:47 +0200
changeset 41422 97eda72f53b6
parent 38808 102fd16b8798
child 41502 5772044e1a21
permissions -rw-r--r--
8167117: insert missing final keywords Reviewed-by: jlaskey, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    28
import java.lang.module.ModuleDescriptor;
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    29
import java.lang.reflect.Module;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.security.CodeSource;
28785
a503c972d4bd 8072595: nashorn should not use obj.getClass() for null checks
sundar
parents: 25865
diff changeset
    31
import java.util.Objects;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
/**
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16151
diff changeset
    34
 * Responsible for loading script generated classes.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
final class ScriptLoader extends NashornLoader {
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    38
    private static final String NASHORN_PKG_PREFIX = "jdk.nashorn.internal.";
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    39
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    40
    private volatile boolean structureAccessAdded;
38808
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    41
    private final Context context;
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    42
    private final Module scriptModule;
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    43
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    44
    /*package-private*/ Context getContext() {
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    45
        return context;
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    46
    }
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    47
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
     * Constructor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
     */
38808
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    51
    ScriptLoader(final Context context) {
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    52
        super(context.getStructLoader());
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    53
        this.context = context;
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    54
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    55
        // new scripts module, it's specific exports and read-edges
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    56
        scriptModule = createModule("jdk.scripting.nashorn.scripts");
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    57
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    58
        // specific exports from nashorn to new scripts module
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    59
        NASHORN_MODULE.addExports(OBJECTS_PKG, scriptModule);
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    60
        NASHORN_MODULE.addExports(RUNTIME_PKG, scriptModule);
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    61
        NASHORN_MODULE.addExports(RUNTIME_ARRAYS_PKG, scriptModule);
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    62
        NASHORN_MODULE.addExports(RUNTIME_LINKER_PKG, scriptModule);
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    63
        NASHORN_MODULE.addExports(SCRIPTS_PKG, scriptModule);
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    64
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    65
        // nashorn needs to read scripts module methods,fields
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    66
        NASHORN_MODULE.addReads(scriptModule);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    69
    private Module createModule(final String moduleName) {
38808
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    70
        final Module structMod = context.getStructLoader().getModule();
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    71
        final ModuleDescriptor descriptor
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    72
                = new ModuleDescriptor.Builder(moduleName)
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    73
                    .requires(NASHORN_MODULE.getName())
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    74
                    .requires(structMod.getName())
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    75
                    .conceals(SCRIPTS_PKG)
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    76
                    .build();
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    77
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    78
        final Module mod = Context.createModuleTrusted(structMod.getLayer(), descriptor, this);
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    79
        loadModuleManipulator();
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    80
        return mod;
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    81
    }
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    82
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    83
    @Override
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24720
diff changeset
    84
    protected Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    85
        checkPackageAccess(name);
38808
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    86
        final Class<?> cl = super.loadClass(name, resolve);
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    87
        if (!structureAccessAdded) {
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    88
            final StructureLoader structLoader = context.getStructLoader();
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    89
            if (cl.getClassLoader() == structLoader) {
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    90
                structureAccessAdded = true;
38808
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    91
                structLoader.addModuleExport(scriptModule);
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 31094
diff changeset
    92
            }
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 16525
diff changeset
    93
        }
38808
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    94
        return cl;
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    95
    }
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    96
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    97
    @Override
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 38808
diff changeset
    98
    protected Class<?> findClass(final String name) throws ClassNotFoundException {
38808
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
    99
        final ClassLoader appLoader = context.getAppLoader();
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   100
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   101
        /*
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   102
         * If the appLoader is null, don't bother side-delegating to it!
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   103
         * Bootloader has been already attempted via parent loader
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   104
         * delegation from the "loadClass" method.
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   105
         *
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   106
         * Also, make sure that we don't delegate to the app loader
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   107
         * for nashorn's own classes or nashorn generated classes!
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   108
         */
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   109
        if (appLoader == null || name.startsWith(NASHORN_PKG_PREFIX)) {
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   110
            throw new ClassNotFoundException(name);
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   111
        }
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   112
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   113
        /*
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   114
         * This split-delegation is used so that caller loader
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   115
         * based resolutions of classes would work. For example,
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   116
         * java.sql.DriverManager uses caller's class loader to
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   117
         * get Driver instances. Without this split-delegation
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   118
         * a script class evaluating DriverManager.getDrivers()
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   119
         * will not get back any JDBC driver!
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   120
         */
102fd16b8798 8158338: Nashorn's ScriptLoader split delegation has to be adjusted
sundar
parents: 38806
diff changeset
   121
        return appLoader.loadClass(name);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
   122
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
   123
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
    // package-private and private stuff below this point
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
     * Install a class for use by the Nashorn runtime
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
     * @param name Binary name of class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
     * @param data Class data bytes.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
     * @param cs CodeSource code source of the class bytes.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
     * @return Installed class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
    synchronized Class<?> installClass(final String name, final byte[] data, final CodeSource cs) {
31094
7a9fa21da791 8085810: Return value of Objects.requireNonNull call can be used
sundar
parents: 28785
diff changeset
   136
        return defineClass(name, data, 0, data.length, Objects.requireNonNull(cs));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
}