Deferred binding on generic types (use case: simulate JAXB under GWT)

166 views
Skip to first unread message

Olivier Monaco

unread,
Apr 29, 2008, 12:28:24 PM4/29/08
to Google Web Toolkit, mon...@antidot.net
Hi all,

My problem is to write something like :

GWT.class(Parser<FileSystem>.class)

but it's not a legal statement.

Explanation :
I'm planning to use JAXB on a RestLet server to exchange XML messages
between server and client. One of the client is GWT-based and I'm
trying to use the same way as JAXB to read/write XML.
1. I use XJC to generate Java classes (eg. FileSystem) from XSD.
2. I use generated classes on my server.
3. I use generated classes with GWT (works fine, just ignoring missing
annotations from XJC).
4. I create a generic parser interface (works fine) :

interface Parser<T> {
public T parse(Element element) ;
}

5. I write a generator (ParserGen) associated with my Parser
interface than generate on-fly the parser code using the annotations
associated with my XJC-generated classes (eg. FileSystem) (in
progress).
6. I use GWT.create to instanciate a parser. But how to do ? I can't
write :

GWT.create(Parser<FileSystem>.class)

Any idea ? Is it the right way ?

Olivier.

Olivier Monaco

unread,
May 2, 2008, 11:08:01 AM5/2/08
to Google Web Toolkit
To anyone interested.

The only way I've found to write "GWT.create(A<T>.class) is to define
an intermediary class/interface:
interface FileSystemParser extends Parser<FileSystem> {}

So I wrote:
GWT.create(FileSystemParser.class) ;

And in my generator i walk through the implemented interfaces to find
my marker (Parser<T>) and the enclosed type (ie. FileSystem):
JClassType markerClass =
((JClassType) context.getTypeOracle().parse(
Parser.class.getName())).getErasedType();

JClassType dataClass = null;

JClassType typeClass =
(JClassType) context.getTypeOracle().parse(typeName);
JClassType interfs[] = typeClass.getImplementedInterfaces();

for (int i = 0; i < interfs.length; ++i) {
JClassType interf = interfs[i];

if ((interf instanceof JParameterizedType)
&& (((JParameterizedType) interf).getErasedType() ==
markerClass)) {
dataClass = ((JParameterizedType) interf).getTypeArgs()[0];
}
}

// dataClass contains FileSystem

This works but need more code (an intermediary interface) that can
become heavy if you have many generics that use deferred binding. For
my purpose it's the problem. JAXB can generate one class per XML
element. And each class need its parser. So I have to write an
interface per class (this can be automatic generated for child element
in father element's parser...).

The only other way is to add a GWT.create method that takes more than
one argument; the second and so on being the enclosed types. But it
need change in many compiler classes.

If this can help someone...

Olivier.
Reply all
Reply to author
Forward
0 new messages