Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
Java 如何在Rhino JS中实现通用接口?_Java_Oop_Generics_Interface_Rhino - Fatal编程技术网

Java 如何在Rhino JS中实现通用接口?

Java 如何在Rhino JS中实现通用接口?,java,oop,generics,interface,rhino,Java,Oop,Generics,Interface,Rhino,我有一个包含通用接口的应用程序: public interface IMyInterface<T> { public int calcStuff(T input); } 公共接口IMyInterface{ 公共int calcStuff(T输入); } 我可以用Java清楚地实现这一点: public class Implementor implements IMyInterface<FooObject>{ public int calcStuff(

我有一个包含通用接口的应用程序:

public interface IMyInterface<T> {
    public int calcStuff(T input); 
}
公共接口IMyInterface{
公共int calcStuff(T输入);
}
我可以用Java清楚地实现这一点:

public class Implementor implements IMyInterface<FooObject>{
    public int calcStuff(FooObject input){ ... }
}
公共类实现器实现IMyInterface{
公共int calcStuff(对象输入){…}
}
我找到了一个关于在Rhino中实现Java非通用接口的教程,可以验证它在我的上下文中是否有效


据我所知,由于动态类型系统和其他因素,Javascript没有泛型,因此Rhino在其JS解析器中没有提供这样的让步。任何进行研究的尝试都会让我得到大量关于Rhino Mock通用接口的结果,而不是Rhino JS通用接口实现的结果。

从Javascript的角度来看,没有泛型,没有接口,甚至没有类。在Javascript中,您可以使用

用Javascript“实现”Java接口只意味着提供一些Javascript对象,这些对象具有与接口方法名称相同的函数名称,并且这些函数具有与相应接口方法相同数量的参数

因此,要实现您提供的通用示例接口,您可以编写如下内容:

myGenericInterfaceImpl = new Object();
// Generic type is supposed to be <String> int calcStuff(String)
myGenericInterfaceImpl.calcStuff = function(input) { 
        println("--- calcStuff called ---");
        println("input" + input);
        println("typeof(input):" + typeof(input));

        // do something with the String input
        println(input.charAt(0));
        return input.length();
    }
然后可以从Javascript调用此方法,如下所示:

// do some Java thing with the generic String type interface
Packages.myPackage.MyClass.callMyInterface(new Packages.myPackage.IMyInterface(myInterfaceImpl)));
关于这一专题的一些背景资料 如果您对Rhino的幕后工作感兴趣,那么在Javascript中实现Java接口时,我建议您看看以下Rhino类:

本质上,静态方法
interfacedapter#create()
将调用
VMBridge#newInterfaceProxy()
,它为接口返回一个Java
代理,该代理使用
interfacedapter
的实例来处理接口上的方法调用。此代理将接口上的任何Java方法调用映射到相应的Javascript函数

 **
 * Make glue object implementing interface cl that will
 * call the supplied JS function when called.
 * Only interfaces were all methods have the same signature is supported.
 *
 * @return The glue object or null if <tt>cl</tt> is not interface or
 *         has methods with different signatures.
 */
static Object create(Context cx, Class<?> cl, ScriptableObject object)
**
*使glue对象实现将
*调用时调用提供的JS函数。
*仅支持具有相同签名的所有方法的接口。
*
*@返回粘合对象,如果cl不是接口或
*具有具有不同签名的方法。
*/
静态对象创建(上下文cx、类cl、ScriptableObject)
当我第一次在Java和Javascript中使用通用接口时,它还帮助我了解了正在发生的事情,通过在创建的Rhino代理上逐步调试我的调用(但是您当然需要Rhino源代码来完成这项工作,设置它可能有点麻烦)

还请注意,使用的默认Rhino实现不允许实现多个Java接口或扩展Java类。从:

Rhino的JavaAdapter已被覆盖。JavaAdapter是 哪个Java类可以通过JavaScript扩展,Java接口可以 可以通过JavaScript实现。我们已经更换了Rhino的JavaAdapter 使用我们自己的JavaAdapter实现。在执行中,, JavaScript对象只能实现单个Java接口


因此,如果您需要这些功能,您仍然需要安装原始Rhino实现(这样可以更容易地设置源代码)。

您能否澄清它在什么意义上不起作用?rhino在提供实现时会抱怨吗?据我所知,JS中的任何接口都是泛型的(或键入到
对象
),因此我想知道您试图实现什么。@oberhamsi它不起作用,因为我找不到任何类型的语法引用来引用Rhino JavaScript中的泛型类。这是否意味着使用
calcStuff(FooObject obj)
创建接口会隐式返回
IMyInterface
实现?无论
calcStuff()
看起来如何,它的行为都类似于
IMyInterface
。我不知道幕后是否真的发生了这种情况,但如果我在rhino中实现java.util.Map,它会接受任何东西,例如,像
java.util.Map
。谢谢。我使用的是非JDK(原创)Rhino。我是否正确理解,由于Javascript有一个动态类型系统,我只会在第一次调用时得到一个类型错误(例如,如果我试图将
HashMap
传递给接口实现)当我将接口传递给某个库函数,该库函数可能会在运行时的某段时间内存储它时,就不会发生任何类型错误了?如果将错误的类型传递给接口,则不会出现任何类型错误。如果试图访问调用接口时不存在的函数(表示Java方法),则会在Javascript中出现错误,。但实际上,如果用Java实现接口,而忽略泛型,情况也是如此。如果您将参数强制转换为错误的类型,那么您只会得到一个类型错误——泛型类型在运行时被擦除。因此,您可以将Javascript接口看作一个简单的Java接口,它忽略了泛型类型。这非常有用,非常感谢。我认为我可以找到一种应用程序级的解决方法(坦率地说,在这种情况下,动态类型使实现者更加灵活)
 **
 * Make glue object implementing interface cl that will
 * call the supplied JS function when called.
 * Only interfaces were all methods have the same signature is supported.
 *
 * @return The glue object or null if <tt>cl</tt> is not interface or
 *         has methods with different signatures.
 */
static Object create(Context cx, Class<?> cl, ScriptableObject object)