Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics & explicit casting  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a framework which has not been generic-fied.

So I have to use this constructor:


I want to add a converter method to my EventNG class which will return an Event.


The cast is being flagged by eclipse with the message:

Dictionary is a raw type. References to generic type Dictionary<K,V> should be parameterized

If I use this cast:


eclipse still complains with
Type safety: unchecked cast from Map<String, Object> to Dictionary<String, Object>

Is there a alternative code snippet that will remove this warning message?

Thanks

pho
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, you are mixing generic and non-generic code. This will almost always lead to at least one warning about unchecked casts or raw references. Ideally you would try to change either the generic or the non-generic code, but sometimes that just isn't possible.

Fortunately, annotations can help you out. Add the following line directly above the toEvent method:

This will cause the compiler to ignore the warning. You can add this annotation to an entire class, a method, a field or a local variable.
Please note that you should not just start using it everywhere - the warning is there for a reason. Use it only if you really have no choice, like when forced to mixing generics and non-generic code.
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
What are generic and non generic fields?
Thanks
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

you'll find the long answer here:
http://java.sun.com/docs/books/tutorial/java/generics/index.html


Yours,
Bu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!