Fork me on GitHub

logo

This is a major release. JSweet now supports much better the Java language without loosing its original spirit of low-impedance between Java and JavaScript.

Here is a summary of the new features:

  • The transpiler now maps core Java APIs to JavaScript ones through the use of “built-in macros”. For instance, the java.lang.String and java.lang.Math APIs are directly mapped to JavaScript at compile-time. As a result, you can use Java core API but, under the hood, it is still JavaScript that it used (not an emulation on contrary to most approaches).
  • For other Java APIs (such as java.util and java.io), JSweet rely on the J4TS project, which is a fork of GWT’s JRE emulation. J4TS removes the Java emulation part from GWT and compiles it with JSweet to generate the j4ts candy, which can be used like any other JavaScript library (except that it provides an implementation of some Java APIs).
  • We significantly enhanced the transpiler to support more Java constructions and got closer to the Java Language Semantics. For instance, JSweet now supports static and non-static inner-classes and anonymous classes, it fully supports methods and constructors overloading, it now supports the instanceof operator for interfaces, and it mimics the JLS for static initializers.
  • Last but not least, JSweet 1.1 comes with a brand new bundling system that allows the programmers to directly generate working bundles for the browser. Under the hood, JSweet performs static dependencies analysis and uses lazy initialization to remove forward dependencies, so that your bundle can be run as is in a browser.

Here is an example of a valid JSweet program that mixes Java and JavaScript APIs in a very straightforward way (see the “Complementary colors” program in the live sandbox):

public class ComplementaryColors {

	private static Map<String, String> complementaryColors = new HashMap<String, String>();

	static {
		complementaryColors.put("green", "magenta");
		complementaryColors.put("red", "cyan");
		complementaryColors.put("blue", "yellow");
	}

	public static void main(String[] args) {
		console.info("starting Java API example");
		HTMLUListElement ul = document.createElement(StringTypes.ul);
		document.body.appendChild(ul);
		for (Map.Entry<String, String> e : complementaryColors.entrySet()) {
			HTMLLIElement li = document.createElement(StringTypes.li);
			ul.appendChild(li);
			HTMLAnchorElement a = document.createElement(StringTypes.a);
			a.innerHTML = "Complementary color for " + e.getKey();
			a.addEventListener(StringTypes.click, evt -> {
				alert("The complementary color for " + e.getKey() + " is " + e.getValue());
				return evt;
			});
			a.href = "#";
			li.appendChild(a);
			System.out.println(e.getKey() + " -> " + e.getValue());
		}
	}
}

To get a complete list of changes, go to the Gihub release page.
To checkout the Java APIs that are now supported by JSweet, go to the Language Specification (download PDF), and checkout the J4TS project.
If you like it and want to help us, a star on Github would be a good start 😉

Leave a Reply

Your email address will not be published. Required fields are marked *


JSweet version 2 is now available!