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