Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rhino将Java类传递给Javascript函数_Javascript_Exception_Scripting_Rhino - Fatal编程技术网

Rhino将Java类传递给Javascript函数

Rhino将Java类传递给Javascript函数,javascript,exception,scripting,rhino,Javascript,Exception,Scripting,Rhino,我将Rhino嵌入到Java应用程序中,并试图从Java代码中获取JavaAdapter。 如果我这样做,效果很好 (function() { return new JavaAdapter(packageName.InterfaceName, { methodA: function() {}, methodB: function() {} }); })(); 但是当我创建这个函数时 var getJavaAdapter = function(type, obj) {

我将Rhino嵌入到Java应用程序中,并试图从Java代码中获取JavaAdapter。 如果我这样做,效果很好

(function() {
  return new JavaAdapter(packageName.InterfaceName, {
    methodA: function() {},
    methodB: function() {}
  });
})();
但是当我创建这个函数时

var getJavaAdapter = function(type, obj) {
  return new JavaAdapter(type, obj);
};
像这样修改我的test.js文件

(function() {
  return {
    methodA: function() {},
    methodB: function() {}
  };
})();
并从我的Java代码中调用

private static Object invokeFunction(String functionName, Object... args) {
  Object obj = scope.get(functionName, scope);
  if (obj instanceof Function) {
    Function function = (Function) obj;
    Object result = function.call(context, scope, scope, args);
    return result;
  }
  return null;
}

private static <T> T getImplementation(Class<T> type, Object obj) {
  Object implementation = invokeFunction("getJavaAdapter", type, obj);
  return (T) JavaAdapter.convertResult(implementation, type);
}

...
Object obj = evalResource("/test.js");
getImplementation(InterfaceName.class, obj);
我试过键入.class,
我试过打字,
我尝试只传递类名,然后传递java.lang.class.forName(className)
但仍然得到一些类似的异常“参数0不是Java类”

那么如何传递我的类呢?

我不确定JavaAdapter JavaScript类型是否真的需要java.lang.class类型的对象,尽管错误消息表明不是这样

Context cx = Context.enter();
try {
  Scriptable scope = cx.initStandardObjects();
  String script = "java.lang.Runnable";
  Object result = cx.evaluateString(scope, script, "<cmd>", 1, null);
  System.out.println(result.getClass());
} finally {
  Context.exit();
}
Context cx=Context.enter();
试一试{
可编写脚本的范围=cx.initStandardObjects();
String script=“java.lang.Runnable”;
Object result=cx.evaluateString(作用域,脚本,“”,1,null);
System.out.println(result.getClass());
}最后{
Context.exit();
}
上面的代码打印org.mozilla.javascript.NativeJavaClass类

这项工作:

Context cx = Context.enter();
try {
  Scriptable scope = cx.initStandardObjects();
  NativeJavaClass rType = new NativeJavaClass(scope, Runnable.class);
  scope.put("rType", scope, rType);
  String script = "new JavaAdapter(rType," +
        "{ run : function() { java.lang.System.out.println('hi'); }" +
        "});";
  Object r = cx.evaluateString(scope, script, "<cmd>", 1, null);
  Runnable runnable = (Runnable) JavaAdapter.convertResult(r, Runnable.class);
  runnable.run();
} finally {
  Context.exit();
}
Context cx=Context.enter();
试一试{
可编写脚本的范围=cx.initStandardObjects();
NativeJavaClass rType=新的NativeJavaClass(作用域,Runnable.class);
scope.put(“rType”,scope,rType);
String script=“新建JavaAdapter(rType,”+
“{run:function(){java.lang.System.out.println('hi');}”+
"});";
对象r=cx.evaluateString(作用域,脚本,“”,1,null);
Runnable Runnable=(Runnable)JavaAdapter.convertResult(r,Runnable.class);
runnable.run();
}最后{
Context.exit();
}
测试版本为Rhino 1.7R4

Context cx = Context.enter();
try {
  Scriptable scope = cx.initStandardObjects();
  NativeJavaClass rType = new NativeJavaClass(scope, Runnable.class);
  scope.put("rType", scope, rType);
  String script = "new JavaAdapter(rType," +
        "{ run : function() { java.lang.System.out.println('hi'); }" +
        "});";
  Object r = cx.evaluateString(scope, script, "<cmd>", 1, null);
  Runnable runnable = (Runnable) JavaAdapter.convertResult(r, Runnable.class);
  runnable.run();
} finally {
  Context.exit();
}