How can I specify the <source path="..."> to be the default package?

465 views
Skip to first unread message

Will

unread,
Nov 15, 2007, 10:53:42 PM11/15/07
to Google Web Toolkit
Hi,

Searching this newsgroup for "default package" and "module default
package" didn't reveal what I was looking for. I'm trying to create my
own module while specifying the Java default package as the source
path. I have successfully created a module when my module's source
path is the default source path ("client" package), as shown below in
my MyModule.gwt.xml file:
<module>
<inherits name="com.google.gwt.user.User"/>
<source path="client"/>
</module>

The corresponding src file is named Test.java, and contains:
//Test.java
package client;
public class Test {
public static String foo() { return "working!"; }
}



So what I'm trying to do in MyModule.gwt.xml file, which doesn't work,
is this:
<module>
<inherits name="com.google.gwt.user.User"/>
<source path=""/>
</module>

...note that I'm supposing that the default Java package is
represented by an empty string (path="").
Below, you see that my Test source file is in the default package,
since no package declaration statement is included:

//Test.java
public class Test {
public static String foo() { return "working!"; }
}

Now, I know it's not the best design to put classes in the default
package. But setting that aside for the moment, is there a way I can
specify the source path to be the default package?

Thank you,
Will

Reinier Zwitserloot

unread,
Nov 16, 2007, 12:37:45 AM11/16/07
to Google Web Toolkit
Try source path=".".

And if that doesn't work, just use a package like the rest of us. Not
using packages in java has been deprecated for a while now.

Will

unread,
Nov 16, 2007, 5:20:28 AM11/16/07
to Google Web Toolkit
Thanks, Reinier, but it didn't work. Based on some reading and
experimenting, I've come up with my understanding of a module's
<source path="path"/> element, which I hope I've nailed-down correctly
(corrections/thoughts welcome):

<source path="pkgPattern"/>
===============================

A module is a ".gwt.xml" file. A module has a package prefix. It may
be funny to think of a module as having a Java package prefix because
after all, a module is a ".gwt.xml" file, not a Java source file.
Nevertheless, a module has a prefix and this prefix is found relative
to the classpath that is passed to the GWTCompiler. So if the
GWTCompiler's classpath is "-cp someDir" and your module is located at
someDir/foo/goo/MyModule.gwt.xml, then your module's logical name is
"foo.goo.MyModule" and its pkgPrefix is "foo.goo". This classpath can
include dirs or JAR files (thus, your module can live inside a JAR, if
you wish, along with other Java source files). It's possible that a
module has no prefix (for example, if the module is located at someDir/
MyModule.gwt.xml instead).

A module associates itself with Java source files that are
translatable (Java->JS) by specifying a pkgPattern for those source
files. The pkgPattern is relative to the module's pkgPrefix (if any)
and is specified in a single <source...> element. When a logical
module name is passed to the GWTCompiler, then all Java source files
found on the GWTCompiler's classpath that match the fully-qualified
"pkgPrefix.pkgPattern" pattern will be translated. For example, if my
module's pkgPrefix is "foo.goo" and I specify the pkgPattern
"hoo" (<source path="hoo">), then all source files found on the
GWTCompiler's classpath that match the "foo.goo.hoo.*" pattern
(including subpackages) are associated with the module, and are thus
translated to JS when I pass "foo.goo.MyModule" as an arg to
GWTCompiler.

There are some restrictions. The module's <source path="pkgPattern"/>
must specify a _non-empty_ pkgPattern that is relative to (ie,
appended to) the module's pkgPrefix. So for example, path="" is not
allowed because it's empty. The "." (curDir) and ".." (parentDir)
markers are also not allowed, so ".", "./hoo" and "../hoo" are not
allowable patterns. This means that a module can never be associated
with a Java source file that is contained within the default package,
so be sure to never place your Java source files in the default
package since they will never be translatable by GWTCompiler in that
case.

Regards,
Will
Reply all
Reply to author
Forward
0 new messages