Is there a way to get have GWT not compile a method into JS?

233 views
Skip to first unread message

kevin

unread,
Dec 21, 2010, 11:42:04 AM12/21/10
to Google Web Toolkit

I need to write a method on a share class that only exists on the
server. Any change GWT has some way to annotate a method to be server-
side only?


Mauro Bertapelle

unread,
Dec 21, 2010, 12:41:46 PM12/21/10
to Google Web Toolkit
In the <source> tag of the module XML file you can specify the
subpackage associated with the classes you want GWT to compile.
You can even exclude some particular classes using a pattern based
filter:
http://code.google.com/intl/it-IT/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuidePathFiltering

A. Stevko

unread,
Dec 21, 2010, 1:06:14 PM12/21/10
to google-we...@googlegroups.com
Are you referring to the  <super-source> tag for overriding one implementation with another?



--
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.




--
-- A. Stevko
===========
"If everything seems under control, you're just not going fast enough." M. Andretti





kevin

unread,
Dec 21, 2010, 1:36:23 PM12/21/10
to Google Web Toolkit
I was hoping to do it within a class.

For instance, say have method a() only available on the server.


On Dec 21, 12:41 pm, Mauro Bertapelle <mauro.bertape...@gmail.com>
wrote:
> In the <source> tag of the module XML file you can specify the
> subpackage associated with the classes you want GWT to compile.
> You can even exclude some particular classes using a pattern based
> filter:http://code.google.com/intl/it-IT/webtoolkit/doc/latest/DevGuideOrgan...

al...@mechnicality.com

unread,
Dec 21, 2010, 1:43:12 PM12/21/10
to google-we...@googlegroups.com
Sorry for jumping in, but you could use:

public void yourMethod(...) {
if (!GWT.isClient()) {
everything in your method.
}

}

The complier should then ignore everything inside the conditional.

However, it seems a bit of a kludge to exclude the whole method. Another option is sub-classing and
have a server-side version which is a sub-class of your shared class - I've done that before and
IMHO its a bit more elegant.

HTH

Alan


--
Alan Chaney
CTO and Founder, Mechnicality, Inc.
www.mechnicality.com

John LaBanca

unread,
Dec 21, 2010, 1:57:14 PM12/21/10
to google-we...@googlegroups.com
The GWT compiler performs dead code elimination, so if you never call the code from the client, it will not be compiled into javascript.  You can create your own annotation if you want to annotate the method so other people on your team know not to use it.

If you want the API to be technically correct, you could create a common base class in your shared package and subclass it in your client and/or server packages.
com.myapp.shared.CommonUtils
com.myapp.client.CommonClientUtils extends CommonUtils
com.myapp.server.CommonServerUtils extends CommonUtils


Thanks,
John LaBanca
jlab...@google.com


A. Stevko

unread,
Dec 21, 2010, 2:00:00 PM12/21/10
to google-we...@googlegroups.com
I agree- if (!GWT.isClient()) {} is definitely going to be optimized out of the js.

Even if you could mask out an entire method, how would you handle the server side only includes needed to make it interesting? 


--
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.

kevin

unread,
Dec 21, 2010, 2:05:47 PM12/21/10
to Google Web Toolkit

Yes. I agree it will be optimized out but the problem is I need to use
a class thats only on the server. So the compiler says the class is
not in a module.



On Dec 21, 1:43 pm, "a...@mechnicality.com" <a...@mechnicality.com>
wrote:

al...@mechnicality.com

unread,
Dec 21, 2010, 2:48:27 PM12/21/10
to google-we...@googlegroups.com
Then you have to go the 'subclass' route - create a common (shared) interface and have
implementations for server and client
You can superclass the common parts into a 'shared' (probably abstract) class. Solves the service
side import issues.

I've done that several times and it works just fine. It may even simplify your unit tests.

Alan

Magno Machado

unread,
Apr 9, 2011, 12:14:43 PM4/9/11
to google-we...@googlegroups.com
Isn't it a design issue?

Maybe this class have too much responsabilities

--
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.

Reply all
Reply to author
Forward
0 new messages