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-- |
2 | 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 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.script; |
|
27 |
||
28 |
import java.util.Map; |
|
29 |
||
30 |
/** |
|
31 |
* Extended by classes that store results of compilations. State |
|
32 |
* might be stored in the form of Java classes, Java class files or scripting |
|
33 |
* language opcodes. The script may be executed repeatedly |
|
34 |
* without reparsing. |
|
35 |
* <br><br> |
|
36 |
* Each <code>CompiledScript</code> is associated with a <code>ScriptEngine</code> -- A call to an <code>eval</code> |
|
37 |
* method of the <code>CompiledScript</code> causes the execution of the script by the |
|
38 |
* <code>ScriptEngine</code>. Changes in the state of the <code>ScriptEngine</code> caused by execution |
|
21278 | 39 |
* of the <code>CompiledScript</code> may visible during subsequent executions of scripts by the engine. |
2 | 40 |
* |
41 |
* @author Mike Grogan |
|
42 |
* @since 1.6 |
|
43 |
*/ |
|
44 |
public abstract class CompiledScript { |
|
45 |
||
46 |
/** |
|
47 |
* Executes the program stored in this <code>CompiledScript</code> object. |
|
48 |
* |
|
49 |
* @param context A <code>ScriptContext</code> that is used in the same way as |
|
50 |
* the <code>ScriptContext</code> passed to the <code>eval</code> methods of |
|
51 |
* <code>ScriptEngine</code>. |
|
52 |
* |
|
53 |
* @return The value returned by the script execution, if any. Should return <code>null</code> |
|
54 |
* if no value is returned by the script execution. |
|
55 |
* |
|
56 |
* @throws ScriptException if an error occurs. |
|
57 |
* @throws NullPointerException if context is null. |
|
58 |
*/ |
|
59 |
||
60 |
public abstract Object eval(ScriptContext context) throws ScriptException; |
|
61 |
||
62 |
/** |
|
63 |
* Executes the program stored in the <code>CompiledScript</code> object using |
|
64 |
* the supplied <code>Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the |
|
65 |
* associated <code>ScriptEngine</code> during script execution. If bindings is null, |
|
66 |
* then the effect of calling this method is same as that of eval(getEngine().getContext()). |
|
67 |
* <p>. |
|
68 |
* The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer</code> |
|
69 |
* associated with the default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> are used. |
|
70 |
* |
|
71 |
* @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>. |
|
72 |
* |
|
73 |
* @return The return value from the script execution |
|
74 |
* |
|
75 |
* @throws ScriptException if an error occurs. |
|
76 |
*/ |
|
77 |
public Object eval(Bindings bindings) throws ScriptException { |
|
78 |
||
79 |
ScriptContext ctxt = getEngine().getContext(); |
|
80 |
||
81 |
if (bindings != null) { |
|
82 |
SimpleScriptContext tempctxt = new SimpleScriptContext(); |
|
83 |
tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE); |
|
84 |
tempctxt.setBindings(ctxt.getBindings(ScriptContext.GLOBAL_SCOPE), |
|
85 |
ScriptContext.GLOBAL_SCOPE); |
|
86 |
tempctxt.setWriter(ctxt.getWriter()); |
|
87 |
tempctxt.setReader(ctxt.getReader()); |
|
88 |
tempctxt.setErrorWriter(ctxt.getErrorWriter()); |
|
89 |
ctxt = tempctxt; |
|
90 |
} |
|
91 |
||
92 |
return eval(ctxt); |
|
93 |
} |
|
94 |
||
95 |
||
96 |
/** |
|
97 |
* Executes the program stored in the <code>CompiledScript</code> object. The |
|
98 |
* 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 execution |
|
102 |
* |
|
103 |
* @throws ScriptException if an error occurs. |
|
104 |
*/ |
|
105 |
public Object eval() throws ScriptException { |
|
106 |
return eval(getEngine().getContext()); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
7991 | 110 |
* Returns the <code>ScriptEngine</code> whose <code>compile</code> method created this <code>CompiledScript</code>. |
2 | 111 |
* The <code>CompiledScript</code> will execute in this engine. |
112 |
* |
|
113 |
* @return The <code>ScriptEngine</code> that created this <code>CompiledScript</code> |
|
114 |
*/ |
|
115 |
public abstract ScriptEngine getEngine(); |
|
116 |
||
117 |
} |