How to get a Class literal from a generically specific Class

Posted by h2g2java on Stack Overflow See other posts from Stack Overflow or by h2g2java
Published on 2010-05-19T09:10:01Z Indexed on 2010/05/19 9:30 UTC
Read the original article Hit count: 302

Filed under:
|
|
|

There are methods like these which require Class literals as argument.

Collection<EmpInfo> emps =  
  SomeSqlUtil.select(  
  EmpInfo.class,  
  "select * from emps");

or

GWT.create(Razmataz.class);

The problem presents itself when I need to supply generic specific classes like

EmpInfo<String>
Razmataz<Integer>

The following would be wrong syntax

Collection<EmpInfo<String>> emps =  
  SomeSqlUtil.select(  
  EmpInfo<String>.class,  
  "select * from emps");

or

GWT.create(Razmataz<Integer>.class);

Because you cannot do syntax like

Razmataz<Integer>.class

So, how would I be able to squeeze a class literal out of

EmpInfo<String>
Razmataz<Integer>

so that I could feed them as arguments to methods requiring Class literals?

Further info

Okay, I confess that I am asking this primarily for GWT.

I have a pair of GWT RPC interface Razmataz. (FYI, GWT RPC interface has to be defined in server-client pairs). I plan to use the same interface pair for communicating whether it be String, Integer, Boolean, etc.

GWT.create(Razmataz) for Razmataz<T> complains that, since I did not specify T, GWT compiler treated it as Object. Then GWT compiler would not accept Object class. It needs to be more specific than being an Object.

So, it seems there is no way for me to tell GWT.create what T is because a Class literal is a runtime concept while generics is a compile time concept, Right?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics