jsweet - Convert Java to JavaScript - OSS Daily

Daily Open Source News for IT Engineer and Designer

Breaking

Home Top Ad

Post Top Ad

Monday, October 2, 2017

jsweet - Convert Java to JavaScript

Since JavaScript is the only programming language guaranteed to work on a web browser, it is a necessary element for those who develop web. However, many people still do not want to write.

So I will introduce jsweet that I would like to use for Java programmers this time. Java is JavaScript to compile.

How to use jsweet

An example. The original Java has this form.
  1. package quickstart;
  2. import static def.dom.Globals.alert;
  3. import static def.jquery.Globals.$;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import def.js.Array;
  7. /**
  8. * This class is used within the webapp/index.html file.
  9. */
  10. public class QuickStart {
  11. public static void main(String[] args) {
  12. // you can use regular Java API
  13. List<string> l = new ArrayList<>();
  14. l.add("Hello");
  15. l.add("world");
  16. // and you can also use regular JavaScript APIs
  17. Array</string><string> a = new Array<>();
  18. a.push("Hello", "world");
  19. // use of jQuery with the jQuery candy
  20. $("#target").text(l.toString());
  21. // use of the JavaScript DOM API
  22. alert(a.toString());
  23. }
  24. }
It is after conversion.
  1. /* Generated from Java with JSweet 2.0.0 - http://www.jsweet.org */
  2. var quickstart;
  3. (function (quickstart) {
  4. /**
  5. * This class is used within the webapp/index.html file.
  6. * @class
  7. */
  8. var QuickStart = (function () {
  9. function QuickStart() {
  10. }
  11. QuickStart.main = function (args) {
  12. var l = ([]);
  13. /* add */ (l.push("Hello") > 0);
  14. /* add */ (l.push("world") > 0);
  15. var a = (new Array());
  16. a.push("Hello", "world");
  17. $("#target").text(/* toString */ ('[' + l.join(', ') + ']'));
  18. alert(a.toString());
  19. };
  20. return QuickStart;
  21. }());
  22. quickstart.QuickStart = QuickStart;
  23. QuickStart["__class"] = "quickstart.QuickStart";
  24. })(quickstart || (quickstart = {}));
  25. quickstart.QuickStart.main(null);
Using this library will make the code amazing, but jsweet is proportionally easy to read. Function is returned for each namespace, so is not it easy to understand the converted code? You can also use jQuery in combination.

jsweet is Java open source software (GPL v3).



No comments:

Post a Comment

Post Bottom Ad