Revision 1112 org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/python/jsr223/MyPyScriptEngine.java
MyPyScriptEngine.java | ||
---|---|---|
5 | 5 |
import java.io.Reader; |
6 | 6 |
import java.lang.reflect.InvocationHandler; |
7 | 7 |
import java.lang.reflect.Proxy; |
8 |
import java.util.List; |
|
8 | 9 |
import javax.script.AbstractScriptEngine; |
9 | 10 |
import javax.script.Bindings; |
10 | 11 |
import javax.script.Compilable; |
... | ... | |
32 | 33 |
return this.interp; |
33 | 34 |
} |
34 | 35 |
|
36 |
@Override |
|
35 | 37 |
public Object eval(String script, ScriptContext context) throws ScriptException { |
36 | 38 |
return eval(compileScript(script, context), context); |
37 | 39 |
} |
... | ... | |
48 | 50 |
} |
49 | 51 |
} |
50 | 52 |
|
53 |
@Override |
|
51 | 54 |
public Object eval(Reader reader, ScriptContext context) throws ScriptException { |
52 | 55 |
return eval(compileScript(reader, context), context); |
53 | 56 |
} |
54 | 57 |
|
58 |
@Override |
|
55 | 59 |
public Bindings createBindings() { |
56 | 60 |
return new SimpleBindings(); |
57 | 61 |
} |
58 | 62 |
|
63 |
@Override |
|
59 | 64 |
public ScriptEngineFactory getFactory() { |
60 | 65 |
return factory; |
61 | 66 |
} |
62 | 67 |
|
68 |
@Override |
|
63 | 69 |
public CompiledScript compile(String script) throws ScriptException { |
64 | 70 |
return new PyCompiledScript(compileScript(script, context)); |
65 | 71 |
} |
66 | 72 |
|
73 |
@Override |
|
67 | 74 |
public CompiledScript compile(Reader reader) throws ScriptException { |
68 | 75 |
return new PyCompiledScript(compileScript(reader, context)); |
69 | 76 |
} |
... | ... | |
94 | 101 |
} |
95 | 102 |
} |
96 | 103 |
|
104 |
@Override |
|
97 | 105 |
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, |
98 | 106 |
NoSuchMethodException { |
99 | 107 |
try { |
... | ... | |
143 | 151 |
} |
144 | 152 |
} |
145 | 153 |
|
154 |
@Override |
|
146 | 155 |
public <T> T getInterface(Class<T> clazz) { |
147 | 156 |
return getInterface(new PyModule("__jsr223__", interp.getLocals()), clazz); |
148 | 157 |
} |
149 | 158 |
|
159 |
@Override |
|
150 | 160 |
public <T> T getInterface(Object obj, Class<T> clazz) { |
151 | 161 |
if (obj == null) { |
152 | 162 |
throw new IllegalArgumentException("object expected"); |
... | ... | |
161 | 171 |
clazz.getClassLoader(), |
162 | 172 |
new Class[] { clazz }, |
163 | 173 |
new InvocationHandler() { |
174 |
@Override |
|
164 | 175 |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
165 | 176 |
try { |
166 | 177 |
interp.setLocals(new PyScriptEngineScope(MyPyScriptEngine.this, context)); |
... | ... | |
183 | 194 |
} |
184 | 195 |
|
185 | 196 |
private static ScriptException scriptException(PyException pye) { |
186 |
ScriptException se = null;
|
|
197 |
ScriptException se; |
|
187 | 198 |
try { |
188 | 199 |
pye.normalize(); |
189 | 200 |
|
... | ... | |
225 | 236 |
} |
226 | 237 |
|
227 | 238 |
private class PyCompiledScript extends CompiledScript { |
228 |
private PyCode code; |
|
239 |
private final PyCode code;
|
|
229 | 240 |
|
230 | 241 |
PyCompiledScript(PyCode code) { |
231 | 242 |
this.code = code; |
... | ... | |
240 | 251 |
public Object eval(ScriptContext ctx) throws ScriptException { |
241 | 252 |
return MyPyScriptEngine.this.eval(code, ctx); |
242 | 253 |
} |
254 |
|
|
243 | 255 |
} |
244 | 256 |
|
257 |
@Override |
|
245 | 258 |
public void close() { |
246 | 259 |
interp.close(); |
247 | 260 |
} |
261 |
|
|
262 |
public List<String> getLocalNames() { |
|
263 |
PyScriptEngineScope locals = (PyScriptEngineScope) interp.getLocals(); |
|
264 |
PyList names = (PyList) locals.scope_keys(); |
|
265 |
return names; |
|
266 |
} |
|
267 |
|
|
248 | 268 |
} |
Also available in: Unified diff