Generators and Generics

144 views
Skip to first unread message

Magno Machado

unread,
Dec 14, 2008, 1:31:47 PM12/14/08
to Google-We...@googlegroups.com
I have to write a generator whose marker interface has a generic type
My marker interface will be like this:

public interface MyInterface<T> {

}

the subinterfaces will provide a type for that parameter, like:
public interface MySubInterface extends MyInterface<Customer> {
}

and then I will call:
MySubInterface = GWT.create(MySubInterface.class);

Inside the generator, how can I find out what type was used on T parameter? (Customer, in this case)

Riyaz Mansoor

unread,
Dec 14, 2008, 9:41:14 PM12/14/08
to Google Web Toolkit

I don't know the answer to your question - I'm guessing generic
markers are not possible?

But, I simplified a similar problem by launching the generator on the
"real type" - in your case Customer; you can still extend your
implementation of MySubSubSubInterface in the generator.

:)

darkAngel

unread,
Dec 15, 2008, 4:04:25 AM12/15/08
to Google Web Toolkit
Hi:

public interface MyInterface<T> {
// ....
}

public class Impl implements<String> {
// ....
Type genericInterface = Impl.class.getGenericInterfaces()[0];
if (genericInterface instanceof Class) {
throw new RuntimeException();
}
ParameterizedType pt = (ParameterizedType) genericInterface;
Type typeArg = pt.getActualTypeArguments()[0];
Class<?> cls = (Class<?>) typeArg;
}

darkAngel

unread,
Dec 15, 2008, 4:05:39 AM12/15/08
to Google Web Toolkit
Hi:

public class Impl implements MyInterface<String> {}

Magno Machado

unread,
Dec 15, 2008, 6:10:00 AM12/15/08
to Google-We...@googlegroups.com
Sorry, darkAngel, but I didn't understand. Is this code suposed to go on the Generator?

Riyas, generic markers are possible. I have some remote services defined with parameterized types. But I've always instantiated them using a subinterface which put defined types on the parameterized types... like MySubInterface in my exemple.

Anyway, I've managed to get the parameters... I'm at work now, but I can post the code as soon as arrive at home



2008/12/15 darkAngel <ab.dar...@gmail.com>

Thomas Broyer

unread,
Dec 15, 2008, 10:09:54 AM12/15/08
to Google Web Toolkit
If I understood correctly (both your question and the GWT code), you
should be able to write:

// where 'type' has been returned by the TypeOracle
JParameterizedType parameterizedType = type.isParameterized();
JClassType T = parameterizedType.getTypeArgs()[0];
// now you can check that T is Consumer or something else

Magno Machado

unread,
Dec 15, 2008, 10:51:21 AM12/15/08
to Google-We...@googlegroups.com
Thomas, this solution is better than mine... I'll try it!

2008/12/15 Thomas Broyer <t.br...@gmail.com>
Reply all
Reply to author
Forward
0 new messages