Path: blob/master/src/java.scripting/share/classes/javax/script/CompiledScript.java
41153 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.script;2627import java.util.Map;2829/**30* Extended by classes that store results of compilations. State31* might be stored in the form of Java classes, Java class files or scripting32* language opcodes. The script may be executed repeatedly33* without reparsing.34* <br><br>35* Each <code>CompiledScript</code> is associated with a <code>ScriptEngine</code> -- A call to an <code>eval</code>36* method of the <code>CompiledScript</code> causes the execution of the script by the37* <code>ScriptEngine</code>. Changes in the state of the <code>ScriptEngine</code> caused by execution38* of the <code>CompiledScript</code> may visible during subsequent executions of scripts by the engine.39*40* @author Mike Grogan41* @since 1.642*/43public abstract class CompiledScript {44/**45* Constructor for subclasses to call.46*/47public CompiledScript() {}4849/**50* Executes the program stored in this <code>CompiledScript</code> object.51*52* @param context A <code>ScriptContext</code> that is used in the same way as53* the <code>ScriptContext</code> passed to the <code>eval</code> methods of54* <code>ScriptEngine</code>.55*56* @return The value returned by the script execution, if any. Should return <code>null</code>57* if no value is returned by the script execution.58*59* @throws ScriptException if an error occurs.60* @throws NullPointerException if context is null.61*/6263public abstract Object eval(ScriptContext context) throws ScriptException;6465/**66* Executes the program stored in the <code>CompiledScript</code> object using67* the supplied <code>Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the68* associated <code>ScriptEngine</code> during script execution. If bindings is null,69* then the effect of calling this method is same as that of eval(getEngine().getContext()).70* <p>.71* The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer</code>72* associated with the default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> are used.73*74* @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>.75*76* @return The return value from the script execution77*78* @throws ScriptException if an error occurs.79*/80public Object eval(Bindings bindings) throws ScriptException {8182ScriptContext ctxt = getEngine().getContext();8384if (bindings != null) {85SimpleScriptContext tempctxt = new SimpleScriptContext(ctxt.getReader(), ctxt.getWriter(), ctxt.getErrorWriter());86tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);87tempctxt.setBindings(ctxt.getBindings(ScriptContext.GLOBAL_SCOPE),88ScriptContext.GLOBAL_SCOPE);89ctxt = tempctxt;90}9192return eval(ctxt);93}949596/**97* Executes the program stored in the <code>CompiledScript</code> object. The98* default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> is used.99* The effect of calling this method is same as that of eval(getEngine().getContext()).100*101* @return The return value from the script execution102*103* @throws ScriptException if an error occurs.104*/105public Object eval() throws ScriptException {106return eval(getEngine().getContext());107}108109/**110* Returns the <code>ScriptEngine</code> whose <code>compile</code> method created this <code>CompiledScript</code>.111* The <code>CompiledScript</code> will execute in this engine.112*113* @return The <code>ScriptEngine</code> that created this <code>CompiledScript</code>114*/115public abstract ScriptEngine getEngine();116117}118119120