How can I implement a common interface in Rhino JS?

I have an application that contains a common interface:

public interface IMyInterface<T> {
    public int calcStuff(T input); 
}

I can clearly implement this in Java:

public class Implementor implements IMyInterface<FooObject>{
    public int calcStuff(FooObject input){ ... }
}

I found a tutorial on implementing non-common Java interfaces in Rhino and can verify that it works in my context.

As far as I understand, Javascript has no generics due to the dynamic type system and other factors, so Rhino does not provide such a concession in its JS parser. Any attempts to conduct research lead me to many results related to the Rhino generic interfaces, but not to the implementation of the Rhino JS generic interface.

+3
source share
1 answer

Javascript , , . Javascript , .

"" Java Javascript 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();
    }

, String.

, Java-, String:

public static class MyClass {
    public static void callMyInterface(IMyInterface<String> myInterface){
        System.out.println(myInterface.calcStuff("some Input"));
    }
}

Javascript :

// do some Java thing with the generic String type interface
Packages.myPackage.MyClass.callMyInterface(new Packages.myPackage.IMyInterface(myInterfaceImpl)));

, Rhino, Java Javascript, Rhino:

InterfaceAdapter#create() VMBridge#newInterfaceProxy(), Java Proxy , InterfaceAdapter . - 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)

Java, Javascript, , , - Rhino ( , , Rhino , ).

, Rhino, Java Scripting API, Java Java. Java:

Rhino JavaAdapter . JavaAdapter - Java- JavaScript Java, JavaScript. Rhino JavaAdapter JavaAdapter. , Java- JavaScript.

, , Rhino ( ).

+3

All Articles