通过JSR-223执行动态Java代码 [英] Executing dynamic Java code over JSR-223

查看:340
本文介绍了通过JSR-223执行动态Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在通过JSR-223在我的应用程序中执行动态代码.基本逻辑是:

I have been executing dynamic code in my application over JSR-223 for a while now. The basics logic is:

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(engineName);
final CompiledScript compiled = ((Compilable) scriptEngine).compile(script);
Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("key", value);
compiled.eval(bindings);

这对于Nashorn和Groovy脚本引擎非常有用.但是,如果扩展应用程序的逻辑而不需要知道另一种语言的语法和技巧,那就太好了-基本代码毕竟是Java.

This works great for Nashorn and Groovy scripting engines. However it would be great if extending the logic of the application would not require knowing the syntax and tricks of another language - the base code is in Java after all.

列出内置引擎时,Java似乎不存在任何东西:

When listing the built-in engines there do not seem to be any present for Java:

ScriptEngineManager manager = new ScriptEngineManager();
List<ScriptEngineFactory> factoryList = manager.getEngineFactories();
for (ScriptEngineFactory factory : factoryList) {
    log.debug(factory.getLanguageName());
}

我发现了一些动态Java库,但似乎不再维护它们了.我想可以用javax.tools.JavaCompiler一起写一些东西,但我希望也许我缺少一些基本的东西,并且已经有针对这种情况的工作选项.我也了解Java 9中有关jshell的基础知识,但是我还没有听说过任何有关允许使用ScriptEngineManager执行代码的信息.

I have found a few dynamic Java libraries but they do not seem to be maintained any more. I guess it is possible to write something together using the javax.tools.JavaCompiler but I am hoping that maybe I am missing something basic and there are working options for such a thing already in existence. I am also aware of the basics about jshell coming in Java 9 but I have not heard anything about it allowing code execution using the ScriptEngineManager.

我应该如何使用Java语言本身来实现脚本编制?除了在javax.tools.JavaCompiler的帮助下开始实施自己的javax.script.ScriptEngine之外,还有其他选择吗?

How should I go about implementing scripting using the Java language itself? Are there any options beyond starting to implement my own javax.script.ScriptEngine with the help of javax.tools.JavaCompiler?

推荐答案

对于该线程我有些迟了,但是我刚刚发布了jshell-scriptengine的1.1.0版本.

I am a bit late for this thread, but I just released version 1.1.0 of the jshell-scriptengine.

看一下: https://github.com/eobermuhlner/jshell-scriptengine

这篇关于通过JSR-223执行动态Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆