How to convert existing javascript to java to use with GWT?

前端 未结 5 1795
半阙折子戏
半阙折子戏 2021-01-13 12:53

Is there a tool to convert javascript to java, so I can handle the project using GWT?

update

For those who don\'t know, GWT (Google Web Tool

相关标签:
5条回答
  • 2021-01-13 13:33

    Ask Google how they do that... Just joke :)

    If seriously I have not heard about such open common tool for interpreting Javascript to Java...

    0 讨论(0)
  • 2021-01-13 13:37

    As a proof-of-concept, I wrote a translator that converts a small subset of JavaScript into Java. It is based on the transpiler library for SWI-Prolog:

    :- use_module(library(transpiler)).
    :- set_prolog_flag(double_quotes,chars).
    :- initialization(main).
    main :- 
        Input = "function add(a,b){return a+b;} function squared(a){return a*a;} function add_exclamation_point(parameter){return parameter+\"!\";}",
        translate(Input,'javascript','java',X),
        atom_chars(Y,X),
        writeln(Y).
    

    This is the Java source code that is generated by this program:

    public static String add(String a,String b){
            return a+b;
    }
    public static int squared(int a){
            return a*a;
    }
    public static String add_exclamation_point(String parameter){
            return parameter+"!";
    }
    
    0 讨论(0)
  • 2021-01-13 13:43

    What exactly do you have in mind? If you're looking for some sort of automatic tool, that generates GWT Java code from Javascript, then I'm afraid there's no such thing.

    You can (and should) use JavaScript Native Interface (JSNI), probably in combination with JavaScript Overlay Types (JSO) to wrap your existing Javascript code, so that it's possible to interface with it from GWT's Java code. See the Getting to really know GWT, Part 1: JSNI (and Part 2) post on GWT's offcial blog for some pointers and use cases.

    0 讨论(0)
  • 2021-01-13 13:46

    At a theoretical level, you could use Rhino compiler to turn Javascript into Java class files, but the project seems mostly abandoned nowadays, and it would be crazy to use it with GWT to turn the result back to JavaScript.

    For GWT, as you are eventually going back to Javascript anyway, you should use the JavaScript native interface to call existing Javascript code.

    0 讨论(0)
  • 2021-01-13 13:58

    I'm gonna go with a simple "no"

    JavaScript and Java are so completely different that I can't imagine there'd be one.

    0 讨论(0)
提交回复
热议问题