Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top