The CompiledScript.java Java example source code
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.Map;
/**
* Extended by classes that store results of compilations. State
* might be stored in the form of Java classes, Java class files or scripting
* language opcodes. The script may be executed repeatedly
* without reparsing.
* <br>
* Each <code>CompiledScript is associated with a ScriptEngine
-- A call to an eval
* method of the <code>CompiledScript causes the execution of the script by the
* <code>ScriptEngine. Changes in the state of the ScriptEngine
caused by execution
* of the <code>CompiledScript may visible during subsequent executions of scripts by the engine.
*
* @author Mike Grogan
* @since 1.6
*/
public abstract class CompiledScript {
/**
* Executes the program stored in this <code>CompiledScript object.
*
* @param context A <code>ScriptContext that is used in the same way as
* the <code>ScriptContext passed to the eval
methods of
* <code>ScriptEngine.
*
* @return The value returned by the script execution, if any. Should return <code>null
* if no value is returned by the script execution.
*
* @throws ScriptException if an error occurs.
* @throws NullPointerException if context is null.
*/
public abstract Object eval(ScriptContext context) throws ScriptException;
/**
* Executes the program stored in the <code>CompiledScript object using
* the supplied <code>Bindings of attributes as the ENGINE_SCOPE
of the
* associated <code>ScriptEngine during script execution. If bindings is null,
* then the effect of calling this method is same as that of eval(getEngine().getContext()).
* <p>.
* The <code>GLOBAL_SCOPE Bindings
, Reader
and Writer
* associated with the default <code>ScriptContext of the associated ScriptEngine
are used.
*
* @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE.
*
* @return The return value from the script execution
*
* @throws ScriptException if an error occurs.
*/
public Object eval(Bindings bindings) throws ScriptException {
ScriptContext ctxt = getEngine().getContext();
if (bindings != null) {
SimpleScriptContext tempctxt = new SimpleScriptContext();
tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
tempctxt.setBindings(ctxt.getBindings(ScriptContext.GLOBAL_SCOPE),
ScriptContext.GLOBAL_SCOPE);
tempctxt.setWriter(ctxt.getWriter());
tempctxt.setReader(ctxt.getReader());
tempctxt.setErrorWriter(ctxt.getErrorWriter());
ctxt = tempctxt;
}
return eval(ctxt);
}
/**
* Executes the program stored in the <code>CompiledScript object. The
* default <code>ScriptContext of the associated ScriptEngine
is used.
* The effect of calling this method is same as that of eval(getEngine().getContext()).
*
* @return The return value from the script execution
*
* @throws ScriptException if an error occurs.
*/
public Object eval() throws ScriptException {
return eval(getEngine().getContext());
}
/**
* Returns the <code>ScriptEngine whose compile
method created this CompiledScript
.
* The <code>CompiledScript will execute in this engine.
*
* @return The <code>ScriptEngine that created this CompiledScript
*/
public abstract ScriptEngine getEngine();
}
Other Java examples (source code examples)
Here is a short list of links related to this Java CompiledScript.java source code file: