• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics and class literals

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following example:
This fails with two compiler errors:
If I change Class<? extends Base<?>> into Class<? extends Base> then it works, except Eclipse shows a warning:

Base is a raw type. References to generic type Base<T> should be parameterized


Since I want something like this in one of my APIs I want to handle this as cleanly as possible. Right now I have my method return Set<Class<? extends Base<?>>> which causes class literals for all generic sub types to require an explicit cast (which I have to suppress with @SuppressWarnings("unchecked")). The alternative is make the method return Set<Class<? extends Base>> but then my API includes use of a raw type (which I have to suppress with @SuppressWarnings("rawtypes") in both the interface and implementations). Either way I have to suppress warnings.

Does anyone have suggestions on how to fix this in the cleanest way? Or have I already chosen the cleanest way?
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't I think that it's possible to get rid of the compiler warnings because Base.class and GenericSub.class are raw types.

Class<? extends Base<?>> cls2 = NonGenericSub.class;

does not generate a compiler warning because NonGenericSub.class is not a raw type.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And unfortunately there's no way to make a class literal of a parameterized type, because of erasure:

Why is there no class literal for concrete parameterized types?
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already suspected I have to choose one or the other for ignoring warnings, I just hoped there was a better way.
reply
    Bookmark Topic Watch Topic
  • New Topic