GWT 2.8.0 it's coming
GWT2.8.0. Beta1 was just released since a month. This main feature release is the Javascript interoperability (EDIT: and its final spec was just released) in this post I will report the beta state of some JsInterop capabilities.
EDIT: before continue reading it's good to know that on the official gwt site is reported a new documented final spec of JsInterop features. I will write a post on it as soon as possible
EDIT: to the offcial repository site it's possible to find last release note of the RC1 release. Major updates are the JDK8 API emulation and major compilation bugs fixed.
The missing piece
If we think that Javascript language provides a good support for functional programming it's easy to think that JSInterop is the missing piece in GWT between Java and new Javascript paradigms!
Let's look more deep into JsInterop:
package it.luigibifulco;
@JsType
public class MyJsType {
public boolean bool = true;
public MyJsType(boolean bool) {
this.bool = bool;
}
public String aPublicMethod() {
return "Hello ";
}
public static String aStaticMethd() {
return "What's the meaning of life?";
}
}
var myType = new it.luigibifulco.MyJsType();
if (myType.bool) {
alert("Ask me what's the meaning of life...");
}
The idea of a structured core written in Java together with a layer for fast functional development it's not innovative, (Groovy is a great example of what i mean) but in my humble opinion it should be a development pattern for many enterprise project. This approach helps developers to detect structural modifications in each development iteration and to maintain the functional part decoupled from the structural part of our application. The good news is that this approach will become more maintainable with GWT JsInterop feature.
We have just seen @JSType annotation that easy enables to compile any Java type in a Javascript Module (or whatelse you want to model from the Java type, remember that Javscript has no type!!) .
There are many others JsInterop annotations that help us to simplify our Java-2-Javascript compilation and export!
@JsType
It can be used on a Java class and exports all public fields and methods, for instance:
package it.luigibifulco;
@JsType
public class Parent {
public void String parentMethod() {
return "parentMethod";
}
public static Child createChild() {
return new Child();
}
}
public class Child extends Parent {
public void String childMethod() {
return "childMethod";
}
}
var child = it.luigibifulco.Parent.createChild();
child.parentMethod() //it works
child.childMethod(); //WARNING breaking code it will not works
@JsMethod, @JsProperty, @JsConstructor
With these annotations we can instruct the GWT compiler to export only some pieces of our Java class such as 'methods', 'fields' and an only and one only constructor.
package it.luigibifulco;
public class Vehicle {
@JsProperty
public int wheels;
@JsConstructor
public Vehicle(int wheels) {
this.wheels = wheels;
}
@JsMethod
public String modelType() {
return "Generic Model";
}
@JsMethod
public static String start() {
return "Vehicle started";
}
public int getWheels() { return this.wheels;}
}
var car = new it.luigibifulco.Vehicle(4);
var wheels = car.wheels; //it will work
wheels = car.getWheels(); //it will NOT work
@JsIgnore
On the other hand we could instruct the GWT compiler to not export some pieces of our class, for instance if we know that only one field has not to be exported this annotation is just what we are looking for
package it.luigibifulco;
@JsType
public class Vehicle {
public int wheels;
public Vehicle(int wheels) {
this.wheels = wheels;
}
public String modelType() {
return "Generic Model";
}
public static String start() {
return "Vehicle started";
}
@JsIgnore
public int getWheels() { return this.wheels;}
}
JsFunction
I really love this annotation. It can be used on a Java interface to make a mapping between a Java capability and a Javascript closure!! This is a great feature because we can finally declare the contract there is between the callee and the caller!!
@JsFunction
interface CommandFunction {
int exec(int requestNumer);
}
@JsType
class Executor {
public static int action(CommandFunction cmd) {
return cmd.exec(40);
}
}
it.luigibifulco.Executor.action(
function(cmdId) {
console.log(cmdId); //it will print 40
return commandExecuted();
}); //
function commandExecuted(){
return "0";
}
Namespace
We can instruct the GWT compiler about the way the Java types will be exported in Javascript. So my 'it.luigibifulco.Blog' type could be easily exported as a 'blog' namespace (not encouraged!! ).
Java:
@JsType(name = "MyType", namespace = "lb")
Javascript:
lb.Mytype
....and don't forget we can separate logic in packages too :)
@jsinterop.annotations.JsPackage(namespace = "lb")
package it.luigibifulco