0

I'm using GWT.

When it is compiling the Java code to Javascript code, sometimes it changes some of the members' names. for example: private int height; becomes in the JS: height_0;

Example:

public class TestClass {

    private int a;

public TestClass()
{
    a = 0;
}

public TestClass(int a)
{
    this.a = a;
}

public int getMember()
{
    return a;
}

public void advance()
{
    a++;
}

}
Will result in output like so:

function TestClass_0(){

 this.a_0 = 0;

}



function TestClass_1(a){

 this.a_0 = a;

}



function TestClass(){

}



_ = TestClass_1.prototype = TestClass_0.prototype = TestClass.prototype = new Object_0;

_.advance_0 = function advance(){

++this.a_0;

}

;

_.getClass$ = function getClass_1(){

return Lcom_autodesk_autocadws_gwt_platform_testExporter_TestClass_2_classLit;

}

;

_.getMember_0 = function getMember(){

return this.a_0;

}

;

_.a_0 = 0;

}

The name of the member a was changed to a_0. In some cases, when using web workers, the code will be compiled differently on the web worker script, and the member's name will not be changed (or will be cjanged to a_1, for example). My problem with that is when I transfer objects in messages to web workers. When coding the web worker side in Java, I'm expecting to access the member a, and not a_0. Using getters does not solve the issue, since they are simply replaced with direct access when compiled to JS.

My question: Why are these naming changes occur? In which cases? Is it possible to avoid them?

Thanks a lot.

Rajesh Pitty
  • 2,823
  • 2
  • 18
  • 28
  • If everything is going through GWT, the naming should be consistent, and you should be fine. – Louis Wasserman Jun 07 '12 at 12:33
  • They are not consistent between main thread context and web worker context, since the two contexts are compiled to 2 independent scripts. – user1442141 Jun 10 '12 at 07:14
  • I think that members will be renamed when their names collide with variables of the global space, or members of Javascript structures. In my case problems occurred with names like width or height (which are, of course, changed in OBF mode, thus solving this problem). Can anyone confirm me on this issue? – user1442141 Jun 11 '12 at 09:18

1 Answers1

1

In most cases, the fields will actually be renamed to something lik a, b, etc. What you're seeing here is due to compiling with -style PRETTY which does not obfuscate the code.

The reason GWT obfuscates the code is to make it smaller.

If you need to pass objects to the outside world, you have to serialize them (or map them to JavaScript objects) to a stable structure.

The easiest way to do it is to use a JavaScriptObject. You can also have a look at gwt-exporter.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • Thanks Thomas, for the quick response. – user1442141 Jun 11 '12 at 06:23
  • Unfortunately, OBF mode is not an option for currently,since we are at development phase and need the pretty mode in order to be able to debug. Extending JavaScriptObject is also problematic due to it's restrictions (our types are pretty complexed). Regarding gwt-exporter - my problem with it is that i need to pass types that contain members which are complex types, etc. That means that I need to export just about the entire system. I guess what I'm trying to find out is what causes these name changes, so I can try to avoid them. Are they created due to names collision in the script's global? – user1442141 Jun 26 '12 at 07:16
  • I have no problem with the renaming, I just want it to be consistent between the main thread context, and the web worker context. which are compiled to 3 different scripts. – user1442141 Jun 26 '12 at 07:16