Kevin K
Kevin K

Reputation: 9584

Does GWT remove unused classes during compilation?

Suppose I have a project structure that looks roughly like this:

{module-package}.webapp  
    module.gwt.xml
{module-package}.webapp.client
    Client.java
    UsedByClient.java
    NotUsedByClient.java

And the module.gwt.xml file has:

<source path='client'/>
<entry-point class='{module-package}.webapp.client.Client'/>

When I compile this project using GWT, how much of the Java code will be compiled into Javascript?

The motivation is that unfortunately I'm working with a legacy codebase that has server-side code living alongside client-side code in the same package and it would be some work to separate them. The server-side code isn't used by the client, but I'm concerned that GWT might compile it to Javascript where someone might notice it and try to reverse engineer it.

Upvotes: 0

Views: 422

Answers (2)

Colin Alworth
Colin Alworth

Reputation: 18356

All of the above and more happen:

  • unreferenced classes are removed
  • unreferenced methods and fields are removed
  • constants may be inlined
  • various operations on constants (like !, ==, +, &&, etc) may be simplified (based on some field always being null, or true, etc)
  • un-overridden methods may be made final...
  • ...and final methods may be made static in certain situations (leading to smaller callsites, and no "this" reference inside that method)...
  • and small, frequently called static methods may be inlined

And this process repeats, with even more optimizations that I skipped, to further assist in removing code, both big and small. At the end, all classes, methods, fields, and local variables are renamed in a way to further reduce output size, including reordering methods in the output so that they are ordered by length, letting gzip more efficiently compress your content on the way to the client.

So while some aspects of your code could be reverse engineered (just like any machine code could be reverse engineered), code which isn't referenced won't be available, and code which is may not even be readable.

Upvotes: 5

Kevin K
Kevin K

Reputation: 9584

I somehow managed to stumble upon a 'deep dive' video presentation on the compiler by one of the GWT engineers which has an explanation: https://youtu.be/n-P4RWbXAT8?t=865

Key points:

  • One of the compiler optimizations is called Pruner and it will "Traverse all reachable code from entrypoint, delete everything else (uses ControlFlowAnalyzer)"
  • It is actually an essential optimization because without it, all GWT apps would need to include gwt-user.jar in its entirety, which would greatly increase app sizes.

So it seems the GWT compiler does indeed remove unused code.

Upvotes: 0

Related Questions