Different GWT compilation from Eclipse and command line

69 views
Skip to first unread message

jogo

unread,
Nov 16, 2011, 2:48:11 PM11/16/11
to Google Web Toolkit
Hi,

I'm getting really frustrated from this, so any advice is highly
appreciated.

In my GWT project, there is a java native method that calls eval
function inside. All worked fine till I updated from 2.3.0 to 2.4.0
(on Windows XP in Eclipse). After that, when I compiled the project in
Eclipse (GWT 2.4.0) the native method stopped working and I spent
quite a lot of time figuring out what was the cause of this problem.

It turned out, that the update form 2.3.0 to 2.4.0 caused it. Then I
downgraded back to GWT SDK 2.3.0, re-configured Eclipse and then again
all worked fine.

Today I created a simple ant build script (generated from
webAppCreator app using SDK 2.3.0) and used it. And again, the problem
re-surfaced, the native method doesn't work! The ant script uses SDK
2.3.0.

The surprising thing is, that when I compile the project from Eclipse
with SDK 2.3.0 all works fine. When I compile the project from command
line using the same SDK and JRE compiled code is different and doesn't
work.

Any idea where the problem might be? Thanks.

Gal Dolber

unread,
Nov 16, 2011, 10:08:04 PM11/16/11
to google-we...@googlegroups.com
I'm compiling projects using gwt trunk from ant without problem

this is my base ant script http://pastebin.com/prRQEETz

and this is how I extend it for each project http://pastebin.com/t7RLmd29

the script asume you are using lombok and it support annotation processors, but you should be able to extract the gwt compilation part.

Hope it helps


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/




jogo

unread,
Nov 18, 2011, 5:50:03 AM11/18/11
to Google Web Toolkit
Hi Gal, thank you for sharing the gwt ant build script.

The script wasn't the problem, I was able to compile the source with
the default ant build script. The problem was that compiled code from
Eclipse worked but from command line didn't.

Today I made some other tests and I finally I identified the problem.

I compiled the source code in Eclipse using DETAILED, PRETTY and
OBFUSCATED output. And I did the same with the ant script. Then I
compared them.

I was hoping to see some differences in Eclipse generated code and in
command line, but to my surprise all generated files were binary
identical! => it was not problem of compiling.

Then I deployed to the test environment each version. OBFUSCATED
version failed with error. But, when I deployed PRETTY and DETAILED
versions the code worked!

So it also explains, why I wasn't getting errors from Eclipse build,
because output might have been set to PRETTY / DETAILED. Ant script by
default produces obfuscated code.


Here is a code snippet from the native method, that fails:

public static native String parseJSON(String json) /*-{
var retval = [];

try {
var x = 0;

if (json != null) {
var obj = eval( '(' + json + ')' ); <---- HERE IT
FAILS
var entries = obj.feed.entry;

I was getting error 'Can't find variable obj'.

I've replaced the eval function with JSON.parse method (http://
www.json.org/js.html) and included json2.js file in the entry html
file, but it is still the same. Obfuscated version doesn't work and
I'm getting the same error 'Can't find variable obj' while detailed
version works.

Any ideas?


On Nov 17, 4:08 am, Gal Dolber <gal.dol...@gmail.com> wrote:
> I'm compiling projects using gwt trunk from ant without problem
>
> this is my base ant scripthttp://pastebin.com/prRQEETz
>
> and this is how I extend it for each projecthttp://pastebin.com/t7RLmd29

jogo

unread,
Nov 20, 2011, 6:14:46 AM11/20/11
to Google Web Toolkit
Ok. Finally I've found the sneaky bug.

The code was failing actually later in the code where was another eval
function referencing the obj variable eval('var title =
obj.feed.entry[' + i + '].title');

Then I checked the obfuscated code and I realized that the obj
variable gets obfuscated and renamed!

Here is how to fix it:
http://code.google.com/webtoolkit/doc/1.6/FAQ_Client.html#Help!_I'm_having_problems_with_eval()_in_my_JSNI_method!

Eval function is really evil, so be careful using it in the JSNI code.
I've spent a lot of time with figuring out what caused the problem,
but lesson learned ;o)

> I've replaced the eval function with JSON.parse method (http://www.json.org/js.html) and included json2.js file in the entry html

Thomas Broyer

unread,
Nov 20, 2011, 9:46:09 AM11/20/11
to google-we...@googlegroups.com


On Sunday, November 20, 2011 12:14:46 PM UTC+1, jogo wrote:
Ok. Finally I've found the sneaky bug.

The code was failing actually later in the code where was another eval
function referencing the obj variable eval('var title =
obj.feed.entry[' + i + '].title');


Any reason you're not coding this directly in JSNI, without the eval()?

    var title = obj.feed.entry[i].title;

jogo

unread,
Nov 20, 2011, 11:39:04 AM11/20/11
to Google Web Toolkit
Hi Thomas,

> Any reason you're not coding this directly in JSNI, without the eval()?

Great point. I was able to fix one JSNI function (mentioned above)
that just gets the title & id with the solution you suggested. Thank
you.

But unfortunately, there is one even more complex method, that gets
data from Google Spreadsheet feed and here I have to use the "evil"
function. These feeds are quite complex and keys in these feeds are
dynamically generated based on the names of columns in a spreadsheet.

Here is an example JSON feed:
https://spreadsheets.google.com/feeds/list/o03712292828507838454.6243940683656382600/od6/public/values?alt=json

If you check that feed you will notice, that keys where are stored
spreadsheet values are prefixed with 'gsx' + name of the column. There
are three columns in the spreadsheet - year, revenue and profit and
three corresponding JSON keys gsx$year, gsx$revenue and gsx$profit.

My code looks like this:
public static native String parseSpreadsheetFeed(String json) /*-{
if (json != null) {
var obj = JSON.parse(json);
var entries = obj.feed.entry;

// iterate over entries
for (var i in entries) {

// iterate over keys
for (var j in entries[i]) {
var prefix = j.split('$')[0];

// process only keys prefixed with 'gsx'
if (prefix == 'gsx') {
eval('var data = obj.feed.entry[' + i + '].' +
j + '.$t');
...

And here I can't just replace the eval method with:
var data = obj.feed.entry[i].j.$t;

That wouldn't work.

I could probably rewrite that method using GWT, but in Javacript it is
much more elegant code and easy to understand.

The quick & dirty solution would be to check the obfuscated code and
use the renamed variable name in eval function, e.g.:
eval('var data = f.feed.entry[' + i + '].' + j + '.$t');

And pray that the name won't change in future :o) For now it is quite
acceptable solution, because this the only place in my code where I
use this and I can put there some checks, so in future builds I will
get immediate error saying the variable name has change during
compilation.

If anyone else has a better solution, please let me know.

The ideal solution would be, if we could annotate some JSNI variables
so the GWT compiler will not rename them during the obfuscation
process. The we could easily reference them in the code, typically
from eval function. Is there anything like this?

Thomas Broyer

unread,
Nov 20, 2011, 12:49:10 PM11/20/11
to google-we...@googlegroups.com
var data = obj.feed.entry[i][j].$t;

jogo

unread,
Nov 20, 2011, 2:36:53 PM11/20/11
to Google Web Toolkit
Man, you made my day :o) Thanks!

On Nov 20, 6:49 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> On Sunday, November 20, 2011 5:39:04 PM UTC+1, jogo wrote:
>
> > Hi Thomas,
>
> > > Any reason you're not coding this directly in JSNI, without the eval()?
> > Great point. I was able to fix one JSNI function (mentioned above)
> > that just gets the title & id with the solution you suggested. Thank
> > you.
>
> > But unfortunately, there is one even more complex method, that gets
> > data from Google Spreadsheet feed and here I have to use the "evil"
> > function. These feeds are quite complex and keys in these feeds are
> > dynamically generated based on the names of columns in a spreadsheet.
>
> > Here is an example JSON feed:
>

> >https://spreadsheets.google.com/feeds/list/o03712292828507838454.6243...

Reply all
Reply to author
Forward
0 new messages