src/java.scripting/share/classes/javax/script/CompiledScript.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.scripting/share/classes/javax/script/CompiledScript.java@3317bb8137f4
child 49858 56923ee4f07e
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.script;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Extended by classes that store results of compilations.  State
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * might be stored in the form of Java classes, Java class files or scripting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * language opcodes.  The script may be executed repeatedly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * without reparsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <br><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Each <code>CompiledScript</code> is associated with a <code>ScriptEngine</code> -- A call to an  <code>eval</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * method of the <code>CompiledScript</code> causes the execution of the script by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>ScriptEngine</code>.  Changes in the state of the <code>ScriptEngine</code> caused by execution
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 9035
diff changeset
    39
 * of the <code>CompiledScript</code>  may visible during subsequent executions of scripts by the engine.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Mike Grogan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public abstract class CompiledScript {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Executes the program stored in this <code>CompiledScript</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param context A <code>ScriptContext</code> that is used in the same way as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * the <code>ScriptContext</code> passed to the <code>eval</code> methods of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @return The value returned by the script execution, if any.  Should return <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * if no value is returned by the script execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @throws ScriptException if an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @throws NullPointerException if context is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public abstract Object eval(ScriptContext context) throws ScriptException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Executes the program stored in the <code>CompiledScript</code> object using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * the supplied <code>Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * associated <code>ScriptEngine</code> during script execution.  If bindings is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * then the effect of calling this method is same as that of eval(getEngine().getContext()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <p>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * associated with the default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @return The return value from the script execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @throws ScriptException if an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public Object eval(Bindings bindings) throws ScriptException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ScriptContext ctxt = getEngine().getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (bindings != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            SimpleScriptContext tempctxt = new SimpleScriptContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            tempctxt.setBindings(ctxt.getBindings(ScriptContext.GLOBAL_SCOPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            tempctxt.setWriter(ctxt.getWriter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            tempctxt.setReader(ctxt.getReader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            tempctxt.setErrorWriter(ctxt.getErrorWriter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ctxt = tempctxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return eval(ctxt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Executes the program stored in the <code>CompiledScript</code> object.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The effect of calling this method is same as that of eval(getEngine().getContext()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return The return value from the script execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws ScriptException if an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public Object eval() throws ScriptException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return eval(getEngine().getContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
7991
96cdf8806d96 6508943: small typo in the documentation
sundar
parents: 5506
diff changeset
   110
     * Returns the <code>ScriptEngine</code> whose <code>compile</code> method created this <code>CompiledScript</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The <code>CompiledScript</code> will execute in this engine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return The <code>ScriptEngine</code> that created this <code>CompiledScript</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public abstract ScriptEngine getEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}