-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
使用测试
public class JSActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_js); JSEngine jsEngine = new JSEngine(); jsEngine.runScript(testjs); } private String testjs ="var val = getValue('testKey');" + "setValue('setKey',val)"; }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
结果输出如下:
System.out: JSEngine output - getValue : testKey System.out: JSEngine output - setValue : setKey ------> 获取到值了
-
1
-
2
* * *
### 方法测试 - 返回本地类
1. 添加本地测试类
class TestObject{ private String name; private String address; TestObject(String name ,String address){ this.name = name; this.address = address; } }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
1. 为JSEngine添加方法 getObjectValue
public Object getObjectValue(Object keyStr){ System.out.println("JSEngine output - getObjectValue : " + keyStr.toString()); return new TestObject("小明","广州"); }
-
1
-
2
-
3
-
4
1. allFunctions 添加 getObjectValue 声明
allFunctions = " var ScriptAPI = java.lang.Class.forName(\"" + JSEngine.class.getName() + "\", true, javaLoader);\n" + " var methodGetValue= ScriptAPI.getMethod(\"getValue\", [java.lang.String]);\n" + " function getValue(key) {\n" + " return methodGetValue.invoke(javaContext,key);\n" + " }\n" + " var methodSetValue=ScriptAPI.getMethod(\"setValue\",[java.lang.Object,java.lang.Object]);\n" + " function setValue(key,value) {\n" + " methodSetValue.invoke(javaContext,key,value);\n" + " }\n"+ " var methodGetObjectValue=ScriptAPI.getMethod(\"getObjectValue\",[java.lang.Object]);\n" + " function getObjectValue(key) {\n" + " return methodGetObjectValue.invoke(javaContext,key);\n" + " }\n";
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
1. 修改执行语句,尝试获取 name属性的值
private String testjs = "var test = getObjectValue('objectKey');" + "setValue('testvalue',test.name);";
-
1
-
2
-
3
1. 运行,发现无法获取到属性name的值
System.out: JSEngine output - setValue : testvalue ------> undefined
- 1
**解决方案**
思路:先将返回的对象转成字符串,再利用 javascript 的 eval 函数将字符串转成符合要求的对象
此时,需要修改 JSEngine 中的getObjectValue方法和 allFunctions 中的 getObjectValue 方法
//修改 JSEngine 中的getObjectValue方法 public String getObjectValue(Object keyStr){ System.out.println("JSEngine output - getObjectValue : " + keyStr.toString()); return new Gson().toJson(new TestObject("小明","广州"));//利用Gson 将 TestObject对象先转成String }
-
1
-
2
-
3
-
4
-
5
//修改 allFunctions 中的getObjectValue方法 " var methodGetObjectValue=ScriptAPI.getMethod(\"getObjectValue\",[java.lang.Object]);\n" + " function getObjectValue(key) {\n" + " var retStr = methodGetObjectValue.invoke(javaContext,key);\n" + " var ret = {};" + " eval('ret='+retStr);" + " return ret;" + " }\n";
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
仍执行以下语句
private String testjs = "var test = getObjectValue('objectKey');" + "setValue('testvalue',test.name);";
-
1
-
2
-
3
输出如下:可以看到能使用 .name 的形式获取到 name属性的值
System.out: JSEngine output - getObjectValue : objectKey System.out: JSEngine output - setValue : testvalue ------> 小明
-
1
-
2
### 反射构建js语句
通过上面可以看到,在JSEngine中每添加一个方法,在JS语句中也要对应多添加一个方法。而在js语句的编写过程中则需要注意多处细节,比较容易书写错误,所以能否自动生成js语句而不用每次都手写呢?
有的,利用注解和反射。
首先观察前面的js语句
每个本地方法在js中的定义主要包括两部分:
1、通过本地方法的方法名来获得该方法
var methodGetValue= ScriptAPI.getMethod(\"getValue\", [java.lang.String]);\n
- 1
2、自定义js方法(可重新命名),在js方法中调用本地方法的引用
function getValue(key) { return methodGetValue.invoke(javaContext,key); }
-
1
-
2
-
3
其他有差异的话则在于返回值类型为本地类对象时候的js方法的不同,如
function getObjectValue(key) { var retStr = methodGetObjectValue.invoke(javaContext,key); var ret = {}; eval('ret='+retStr); return ret; }
-
1
-
2
-
3
-
4
-
5
-
6
下面开始反射构建js语句
1、创建注解 JSAnnotation ,设定参数 returnObject ,用于区分上面所述的方法是否返回本地类对象。
/** * 注解 */ @Target(value = ElementType.METHOD) @Retention(value = RetentionPolicy.RUNTIME) public @interface JSAnnotation { boolean returnObject() default false;//是否返回对象,默认为false 不返回 }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
2、为方法添加注解
//本地java方法,声明注解 @JSAnnotation public void setValue(Object keyStr, Object o) { System.out.println("JSEngine output - setValue : " + keyStr.toString() + " ------> " + o.toString()); } //本地java方法,声明注解 @JSAnnotation public String getValue(String keyStr) { System.out.println("JSEngine output - getValue : " + keyStr.toString() ); return "获取到值了"; } //有返回本地类对象,则returnObject 设置为true @JSAnnotation(returnObject = true) function getObjectValue(key) { var retStr = methodGetObjectValue.invoke(javaContext,key); var ret = {}; eval('ret='+retStr); return ret; }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
3、利用注解生成js语句
/** * 通过注解自动生成js方法语句 */ private String getAllFunctions(){ String funcStr = " var ScriptAPI = java.lang.Class.forName(\"%s\", true, javaLoader);\n" ; Class cls = this.getClass(); for (Method method: cls.getDeclaredMethods()){ JSAnnotation an = method.getAnnotation(JSAnnotation.class); if (an == null ) continue; String functionName = method.getName(); String paramsTypeString ="";//获取function的参数类型 String paramsNameString = "";//获取function的参数名称 String paramsNameInvokeString = ""; Class [] parmTypeArray = method.getParameterTypes(); if (parmTypeArray != null && parmTypeArray.length > 0){ String[] parmStrArray = new String[parmTypeArray.length]; String[] parmNameArray = new String[parmTypeArray.length]; for (int i=0;i < parmTypeArray.length; i++){ parmStrArray[i] = parmTypeArray[i].getName(); parmNameArray[i] = "param" + i ; } paramsTypeString = String.format(",[%s]",TextUtils.join(",",parmStrArray)); paramsNameString = TextUtils.join(",",parmNameArray); paramsNameInvokeString = "," + paramsNameString; } Class returnType = method.getReturnType(); String returnStr = returnType.getSimpleName().equals("void") ? "" : "return";//是否有返回值 String methodStr = String.format(" var method_%s = ScriptAPI.getMethod(\"%s\"%s);\n",functionName,functionName,paramsTypeString); String functionStr = ""; if (an.returnObject()){//返回对象 functionStr = String.format( " function %s(%s){\n" + " var retStr = method_%s.invoke(javaContext%s);\n" + " var ret = {} ;\n" + " eval('ret='+retStr);\n" + " return ret;\n" + " }\n" ,functionName,paramsNameString,functionName,paramsNameInvokeString ); }else {//非返回对象 functionStr = String.format( " function %s(%s){\n" + " %s method_%s.invoke(javaContext%s);\n" + " }\n",functionName,paramsNameString,returnStr,functionName,paramsNameInvokeString ); } funcStr = funcStr + methodStr + functionStr; } return funcStr; }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
* * *
### js自动生成的完整封装
public class JSEngine { private Class clazz; private String allFunctions ="";//js方法语句 public JSEngine(){ this.clazz = JSEngine.class; allFunctions = String.format(getAllFunctions(), clazz.getName());//生成js语法 } class TestObject{ private String name; private String address; TestObject(String name ,String address){ this.name = name; this.address = address; } } /** * 本地方法 - 返回本地类对象 * @param keyStr * @return */ @JSAnnotation(returnObject = true) public String getObjectValue(Object keyStr){ System.out.println("JSEngine output - getObjectValue : " + keyStr.toString()); return new Gson().toJson(new TestObject("小明","广州")); } /** * 本地java方法 * @param keyStr * @param o */ @JSAnnotation public void setValue(Object keyStr, Object o) { System.out.println("JSEngine output - setValue : " + keyStr.toString() + " ------> " + o.toString()); } /** * 本地java * @param keyStr * @return */ @JSAnnotation public String getValue(String keyStr) { System.out.println("JSEngine output - getValue : " + keyStr.toString() ); return "获取到值了"; } /** * 执行JS * @param js js执行代码 eg: "var v1 = getValue('Ta');setValue(‘key’,v1);" */ public void runScript(String js){ String runJSStr = allFunctions + "\n" + js;//运行js = allFunctions + js org.mozilla.javascript.Context rhino = org.mozilla.javascript.Context.enter(); rhino.setOptimizationLevel(-1); try { Scriptable scope = rhino.initStandardObjects(); ScriptableObject.putProperty(scope, "javaContext", org.mozilla.javascript.Context.javaToJS(this, scope));//配置属性 javaContext:当前类JSEngine的上下文 ScriptableObject.putProperty(scope, "javaLoader", org.mozilla.javascript.Context.javaToJS(clazz.getClassLoader(), scope));//配置属性 javaLoader:当前类的JSEngine的类加载器 rhino.evaluateString(scope, runJSStr, clazz.getSimpleName(), 1, null); } finally { org.mozilla.javascript.Context.exit(); } } /** * 通过注解自动生成js方法语句 */ private String getAllFunctions(){ String funcStr = " var ScriptAPI = java.lang.Class.forName(\"%s\", true, javaLoader);\n" ; Class cls = this.getClass(); for (Method method: cls.getDeclaredMethods()){ JSAnnotation an = method.getAnnotation(JSAnnotation.class); if (an == null ) continue; String functionName = method.getName(); String paramsTypeString ="";//获取function的参数类型 String paramsNameString = "";//获取function的参数名称 String paramsNameInvokeString = ""; Class [] parmTypeArray = method.getParameterTypes(); if (parmTypeArray != null && parmTypeArray.length > 0){ String[] parmStrArray = new String[parmTypeArray.length]; String[] parmNameArray = new String[parmTypeArray.length]; for (int i=0;i < parmTypeArray.length; i++){ parmStrArray[i] = parmTypeArray[i].getName(); parmNameArray[i] = "param" + i ; } paramsTypeString = String.format(",[%s]", TextUtils.join(",",parmStrArray)); paramsNameString = TextUtils.join(",",parmNameArray); paramsNameInvokeString = "," + paramsNameString; } Class returnType = method.getReturnType(); String returnStr = returnType.getSimpleName().equals("void") ? "" : "return";//是否有返回值 String methodStr = String.format(" var method_%s = ScriptAPI.getMethod(\"%s\"%s);\n",functionName,functionName,paramsTypeString); String functionStr = ""; if (an.returnObject()){//返回对象 functionStr = String.format( " function %s(%s){\n" + " var retStr = method_%s.invoke(javaContext%s);\n" + " var ret = {} ;\n" + " eval('ret='+retStr);\n" + " return ret;\n" + " }\n" ,functionName,paramsNameString,functionName,paramsNameInvokeString ); }else {//非返回对象 functionStr = String.format( " function %s(%s){\n" + " %s method_%s.invoke(javaContext%s);\n" + " }\n",functionName,paramsNameString,returnStr,functionName,paramsNameInvokeString ); } funcStr = funcStr + methodStr + functionStr; } return funcStr; } /** * 注解 */ @Target(value = ElementType.METHOD) @Retention(value = RetentionPolicy.RUNTIME) public @interface JSAnnotation { boolean returnObject() default false;//是否返回对象,默认为false 不返回 } }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
* * *
* 补充:运行在子线程
AsyncTask task = new AsyncTask() { @Override protected Object doInBackground(Object[] params) { if (ukjsEngine == null) ukjsEngine = new UKJSEngine(ukjsEngineListener); ukjsEngine.runScript(jsStr); return null; } }; task.execute();
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
则引擎初始化与执行 runScript 需要都在子线程中;如果引擎初始化在主线程,而runScript在子线程,则会报错
* ConsString 问题
var data = { "entityId": "2c63b681-1de9-41b7-9f98-4cf26fd37ef1", "recId": id, "needPower": 0 }; var result = app.request('api/dyxxxxxity/dxxxxl', 'post', data, {});
-
1
-
2
-
3
-
4
-
5
-
6
在本地的request() 方法中,对data进行转化处理
mArgs = new Gson().toJson(data );
- 1
如下图,可以看到数据是正常的
![这里写图片描述](https://img-blog.csdn.net/20171121112415270?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFoYV96aGFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
但在下面实例中:
var productseries = app.getValue('xilie'); var data = { "ProductsetId":productseries, "Direction":"DOWNER" }; var result = app.request('api/prxxxxts/gexxxxes', 'post', data, {});