本文系转载
原文地址:http://blog.csdn.net/sdyy321/article/details/6959199
由于要用到该技术,所以写了几个测试,直接上代码。
定义外部资源E:/ScriptEngine/test01.js
if(age>=18){
print('Old enough to vote!');
}else{
print('Back to school!');
}
定义外部资源E:/ScriptEngine/test02.js
if(age<25){
riskFactor = 1.5;
}else if(noClaims){
ristFactor = 0.75;
}else{
ristFactor = 1.0;
}
定义外部资源E:/ScriptEngine/test03.js
function Hello(){}
Hello.prototype.sayHello = function(value){
println('hello!'+value);
}
var hello = new Hello();
hello.sayHello('World1');
//使用脚本引擎运行脚本程序
public static void test01() throws ScriptException{
// 如果调用者可访问线程上下文 ClassLoader,则调用此构造方法的效果与调用 ScriptEngineManager(Thread.currentThread().getContextClassLoader()) 相同
//ScriptEngineManager检索感兴趣的脚本编写的语言的ScriptEngine对象
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");//ScriptEngine用来处理脚本解释和求值
engine.put("age", 26);//赋值脚本环境中所使用的变量
engine.eval("if(age>=18){println('Old enough to vote!');}else{println('Back to school!');}");//解析 JavaScript 脚本,对脚本表达式进行求值
}
//使用脚本引擎执行外部资源中的脚本
public static void test02() throws FileNotFoundException, ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("age", 26);
engine.eval(new FileReader("E:/ScriptEngine/test01.js"));//对文件或其它外部资源中得脚本表达式进行求值
}
//使用脚本引擎检索结果
public static void test03() throws FileNotFoundException, ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("age", 26);
engine.put("noClaims", Boolean.TRUE);
Object result = engine.eval(new FileReader("E:/ScriptEngine/test02.js"));//eval()函数返回执行脚本后所返回的值,默认情况下,将返回上次执行的表达式的值
System.out.println(result.toString());
}
//上下文属性的作用域
public static void test04() throws ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
Bindings globalBindings = manager.getBindings();//属于ScriptEngineManager的全局上下文,每个ScriptEngineManager拥有一个Bindings
globalBindings.put("name", "yangYong");
ScriptEngine engine = manager.getEngineByName("js");
Bindings globalBindings02 = engine.getBindings(ScriptContext.GLOBAL_SCOPE);//每个引擎都共有一个ScriptEngineManager的Bindings
System.out.println("name="+globalBindings02.get("name"));
System.out.println(globalBindings==globalBindings02);
ScriptContext context = engine.getContext();//每个引擎都有自己独立的scriptContext上下文 Bindings engineDefaultBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);//每个引擎的scriptContext中都有个默认属于自己的Bindings
engineDefaultBindings.put("gender", "男");
engine.eval("name2 = name;println(name2);",engineDefaultBindings);
engineDefaultBindings.put("name", "zhangSan");
engine.eval("name2 = name;println(name2);",engineDefaultBindings);
Bindings engineCreatBindings = engine.createBindings();//每个引擎的scriptContext中也可以新建多个属于自己的Bindings
engineCreatBindings.put("gender", "女");
System.out.println(engineDefaultBindings==engineCreatBindings);
System.out.println(engine.getBindings(ScriptContext.ENGINE_SCOPE).get("gender"));
System.out.println(engineCreatBindings.get("gender"));
engine.eval("name2 = name;println(name2);",engineCreatBindings);
}
//java应用程序与脚本交互
public static void test05() throws FileNotFoundException, ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Bindings bindings = engine.createBindings();//Bindings:KV对映射,用来在Java应用程序和javascript脚本之间交换信息,即上下文
bindings.put("age", 26);
bindings.put("noClaims", Boolean.TRUE);
bindings.put("riskFactor", 1.0);
engine.eval("if(age<25){riskFactor = 1.5;}else if(noClaims){ristFactor = 0.75;}else{ristFactor = 1.0;}",bindings);//必须绑定bindings执行脚本,否则报变量未定义异常
double risk = (Double) bindings.get("riskFactor");
System.out.println(risk);
}
//脚本内部访问java资源
public static void test06() throws ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.eval("importPackage(java.util)");//importPackage()函数允许在脚本内部导入java包,即可在脚本中实例化java对象
engine.eval("today = new Date();println(today);");
engine.put("name", "ZhangSan");
engine.eval("name2 = name.toUpperCase();println(name2);");
}
//重定向脚本输出
public static void test07() throws IOException, ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js"); PipedReader pr = new PipedReader();
PipedWriter pw = new PipedWriter(pr);
//用户可以通过与这个 PrintWriter 连通的 PrintReader 读取实际的输出,使 Java 应用程序能获取脚本运行输出
PrintWriter out = new PrintWriter(pw);
engine.getContext().setWriter(out); engine.eval("println('hello from javascript!');"); BufferedReader br = new BufferedReader(pr);
System.out.println(br.readLine());
}
//可编译且可调用的引擎
public static void test08() throws ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
if(engine instanceof Compilable){//判断脚本引擎是否支持编译操作
Compilable compilable = (Compilable)engine;//用来保存编译的结果,从而能重复调用脚本而没有重复解释的开销
//compile()方法将脚本编译成无需重新编译就能反复执行的CompiledScript容器
CompiledScript script = compilable.compile("if(age<25){riskFactor = 1.5;}else if(noClaims){ristFactor = 0.75;}else{ristFactor = 1.0;}"); Bindings bindings = engine.createBindings();
bindings.put("age", 26);
bindings.put("noClaims", Boolean.TRUE);
bindings.put("riskFactor", 1.0); Object result = script.eval(bindings);//使用CompiledScript的eval()方法计算编译后的表达式
System.out.println(result.toString());
}
}
//调用脚本中的函数和方法
public static void test09() throws ScriptException, NoSuchMethodException, FileNotFoundException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.eval("function increment(i){return i+1;}");
if (engine instanceof Invocable) {//判断脚本引擎是否支持编译操作
Invocable invocable = (Invocable)engine;//Invocable允许从java代码调用定义在脚本中的单个函数
Object result = invocable.invokeFunction("increment", new Object[]{10});// 调用脚本中定义的顶层程序和函数
System.out.println("result = "+result);
} ScriptEngine engine2 = manager.getEngineByName("js");
engine2.eval(new FileReader("E:/ScriptEngine/test03.js"));
if(engine2 instanceof Invocable){
Invocable invocable = (Invocable)engine2;
invocable.invokeMethod(engine2.get("hello"), "sayHello", "yangYong");//对以前的脚本执行中已经编译的脚本对象(以 ScriptEngine 的状态保持)调用某一方法
}
}
定义java文件Hello.java
public interface Hello {
public int increment(int value);
public void sayHello(String name);
}
//调用脚本中的函数和方法并将他们作为java程序中的接口实现
public static void test10() throws FileNotFoundException, ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.eval("function increment(i){return i+1;}");
if (engine instanceof Invocable) {
Invocable invocable = (Invocable)engine;
Hello hello = invocable.getInterface(Hello.class);//返回一个接口的实现,该接口使用解释器中编译的函数
Object result = hello.increment(2);
System.out.println("result = "+result);
}
engine.eval(new FileReader("E:/ScriptEngine/test03.js"));
if(engine instanceof Invocable){
Invocable invocable2 = (Invocable)engine;
Hello hello = invocable2.getInterface(engine.get("hello"), Hello.class);// 返回一个接口的实现,该接口使用解释器中已编译脚本对象的成员函数
hello.sayHello("zhangSan");
}
}