java 动态执行代码,通过JSR-223执行动态Java代码

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);

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.

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

ScriptEngineManager manager = new ScriptEngineManager();

List factoryList = manager.getEngineFactories();

for (ScriptEngineFactory factory : factoryList) {

log.debug(factory.getLanguageName());

}

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.

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?

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值