concept compiler in category gwt

This is an excerpt from Manning's book GWT in Action.
At the heart of the toolkit is the compiler, which takes your Java code and compiles it into JavaScript. During the process it will analyze your code in order to optimize it and remove any code that isn’t reachable by your application. It then generates JavaScript code that’s minified so that the file size is as small as possible. And this is only part of what the compiler does.
The compiler is a busy bee, and throughout this book we’ll show you some of its secrets. One of these is a code generator, which generates Java source code for you in certain cases. The compiler may kick off one or more code generators based on what it finds in your code. This could be the generator that produces the code to serialize Java objects and send them to the server (chapter 7, “Communicating with GWT-RPC”), or it could be the generator that converts your templates into working code (chapter 6, “Interface design with UiBinder”). It could also be the generator that takes external files like CSS or images and optimizes them in a way that allows the browser to load them more quickly (chapter 5, “Using client bundles”). Or perhaps you’ll create your own custom generator to fill a specific need (chapter 18, “Generators”).
Figure 1.2 shows the work performed by the compiler, where the generators are kicked off near the beginning of the compilation process.
Figure 1.2. This is an artist’s rendering of what the GWT compiler is responsible for. This figure doesn’t cover everything the compiler does, but it does provide a high-level overview of how Java code (on the left) is compiled into JavaScript code.
![]()
Another cool feature of the compiler is the ability to weave your Java code with existing JavaScript code. The compiler does this via the use of the JavaScript Native Interface (JSNI), which allows you to embed JavaScript code within your Java classes in order to facilitate Java-to-JavaScript communication (chapter 11, “Using JSNI—JavaScript Native Interface”). It all ends up as JavaScript when the compiler weaves everything together.
But using JavaScript can be tricky because each browser has its own idiosyncrasies. That’s where deferred binding comes in. Deferred binding allows you to create multiple implementations of the same functionality, perhaps one for Internet Explorer and one for all others. The compiler will generate multiple JavaScript output files, one for each browser (chapter 17, “Deferred binding”).
GWT provides a way of solving both of those problems: deferred binding. It harnesses the power of Java to manage differences in your source code (reduces the maintenance and debugging problem), and the GWT compiler automatically creates several JavaScript versions of your application, called permutations. These permutations each contain only the code for one combination of differences (smaller code download equals an enhanced user experience). The compiler also produces some bootstrapping code that determines which permutation the user requires based on resolution of one or more properties. Differences are identified by the use of a property that can have one of several predeclared values. In the GWT bootstrap code the compiler inserts a property provider—a segment of JavaScript that determines the precise value of the property for this implementation—and downloads the appropriate permutation.