Sep 29, 2009

Inside the Google Web Toolkit (GWT)

What is the Google Web Toolkit (GWT)
  • A tool for creating AJAX sites using Java
    * Take advantage of all of the great Java tools to create AJAX applications
    * Have the maintenance advantages of Java with the deployability of AJAX
    * Build great AJAX sites without having to fill your code with special-cases to handle the different browsers
  • A debugging environment for AJAX sites
    Debug your AJAX site on your desk using the Eclipse debugging environment, no more messagebox- debugging!

What Does GWT Do
  • Compile Java into HTML, CSS, and Javascript
  • Marshal and unmarshal RPC calls
    * For this you do have to provide a server-side servlet to accept the call, but even there GWT provides some assistance.
  • Abstract the details of asynchronous calls
  • Handle browser quirks so your code stays clean
  • Handle I18n using translated message files
  • ImageBundle!
  • Handle browser history support
The GWT Java To Javascript Compiler
  • Parses the original Java code
    * Full parser, almost all Java constructs will be parsed correctly.
  • Generates a full Abstract Syntax Tree (AST) (see AST Article)
    * GWT's compiler isn't a parlor trick, it's a real code parser and cross-language compiler
  • Performs optimization, dead-code elimination, dynamic analysis, and other code fixups
    * The generated JS is fast and efficient, sometimes faster than what you might write yourself
  • Does not (yet) handle generics and erasure
The GWT AST Model
  • The AST represents each syntactic element of the Java code in a Java object
  • Implemented in package
  • Each item is a JNode, the root is a JProgram
    * Much like DOM and Node/Document.
  • The AST also supports JSON objects
    * JSON is a first-class entity in GWT, not an afterthought
  • The AST makes extensive use of Visitor and
  • Decorator patterns
GWT Javascript Code Generation
  • Optimization
    * All code is optimized, including some unusual tricks (like turning some methods into wrapped statics)
  • Dead-Code Elimination
    * Remove any code that cannot be reached, or always returns the same value(s)
  • Javascript Generation
    * Generate Javascript, including all necessary browser-specific checks and workarounds
  • Javascript obfuscation and size reduction
    * Javascript is (optionally) obfuscated to protect your trade-secrets, and to reduce it's size
How Does GWT Native Javascript (JSNI) Work
  • The native methods are objects in the AST much as other methods are
  • When the code generation occurs, the content of the method is just copied into the Javascript
  • From there the process is no different from your Java methods
  • Much of the JDK and UI code is implemented using JSNI
How Does GWT Translate JDK Classes
  • Small subset of the Java library
    * This is growing, but not very quickly at the current time
  • Combination of Java and native Javascript
    * Some methods are regular Java code, some are native methods that just contain the Javascript in a special comment
    * It's easy to follow and understand, and it's relatively easy to add new classes
What are GWT widgets
  • A few commonly needed UI elements
    * Panels, Buttons, Dialogs, FileUpload, Tables, Forms, Listboxes, Popups, Scrollbars, etc...
    * There's also a History mechanism and a JSON client implementation
  • Created using the JSNI mechanism, just like the JDK classes
    * Feel free to create your own You can even contribute them back to the community if you want to.
How Does the GWT RPC Mechanism Work
  • You write a client class that implements RemoteService and AsyncCallback
    * RemoteService is a Marker interface (like Serializable) used as an instruction to the compiler
    * AsyncCallback enables the class to receive the response from the server
  • Write a servlet that extends RemoteServiceServlet
    * This receives the RPC and sends back the response
    * RemoveServiceServlet handles the RPC part, you handle the processing part
  • The Google Web Toolkit can make it much easier to create interactive web applications
  • GWT works by actually compiling your Java code into Javascript, with optimization and obfuscation.
  • There are facilities for History, RPC, UI widgets, and native Javascript methods
  • The code is available under an Open Source (Apache 2.0) license, you're welcome to explore.
  • The team would love to hear what you think of GWT on the GWT forum
List of Applications using GWT
Third party widgets & widget libraries


Text Widget

Copyright © Vinay's Blog | Powered by Blogger

Design by | Blogger Theme by