Java执行Scala脚本

public class Test {

private static Logger log = Logger.getLogger(Test.class);

private static ScriptEngine engine;

@BeforeClass
public static void init() {
engine = ScalaEngine.get();
}

public void test(String input) {
engine.put("input", input);

URL url = getClass().getResource("test.scala");
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
Reader reader = new InputStreamReader(is);
try {
Object result = engine.eval(reader);
System.out.println(result);
} catch (ScriptException e) {
e.printStackTrace();
}
}

@org.junit.Test
public void test() {
test("test");
test("not test");
}
}


test.scala
var _input = "test"
_input == input


public class ScalaEngine {

private static Logger log = Logger.getLogger(ScalaEngine.class);

private static ScriptEngine engine = init();

private static ScriptEngine init() {
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("scala");

if (engine instanceof IMain) {
IMain entry = (IMain) engine;
ISettings isettings = entry.isettings();
Settings settings = entry.settings();

MutableSettings.BooleanSetting setting = null;
try {
setting = (MutableSettings.BooleanSetting) field(settings, "usejavacp");
log.info("usejavacp: " + setting.v());
if (! setting.v()) {
field(setting, "v", true, MutableSettings.BooleanSetting.class);
}
setting = (MutableSettings.BooleanSetting) field(settings, "usejavacp");
log.info("usejavacp: " + setting.v());
} catch (IllegalAccessException e) {
log.warn(e.getMessage(), e);
}
}
return engine;
}

public static ScriptEngine get() {
return engine;
}

public static Object field(Object o, String fieldName, Class<?> klass) throws IllegalAccessException {
Field valueOps = null;
try {
valueOps = klass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
throw new IllegalAccessException(e.getMessage());
}
valueOps.setAccessible(true);
return valueOps.get(o);
}

public static Object field(Object o, String fieldName) throws IllegalAccessException {
Class<?> klass = o.getClass();
while (klass != Object.class) {
Class<?> parent = klass.getSuperclass();
Object value = null;
try {
value = field(o, fieldName, klass);
} catch(IllegalAccessException e) {
log.info("field " + fieldName + " of " + klass.getName() + " not found, would trying " + parent.getName());
}
if (value != null) {
return value;
}
klass = parent;
}
throw new IllegalAccessException("field " + fieldName + " not found");
}

public static void field(Object o, String fieldName, Object value, Class<?> klass) throws IllegalAccessException {
Field valueOps = null;
try {
valueOps = klass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
throw new IllegalAccessException(e.getMessage());
}
valueOps.setAccessible(true);
valueOps.set(o, value);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值