Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2005

Tip: Looking for answers? Try searching our database.

Generics type-safety question

Thread view: 
philly.craigslist@gmail.com - 24 Aug 2005 03:14 GMT
Hi,

I'm trying to understand a warning message that Eclipse is giving me.

I have the following code:

1: public class Test {
2:    class Foo {}
3:    class Bar extends Foo {}
4:    public static Collection<? extends Foo> getStuff() {
5:        return new ArrayList<Bar>();
6:    }
7:    public static void main(String args[]) {
8:        Collection<Bar> stuff = (Collection<Bar>)getStuff();
9:    }
10: }

The compiler has no problem with that but Eclipse thinks the cast on
line 8 might be dangerous.  The IDE gives me the following error:

"Type safety: The cast from Collection<capture-of ? extends Test.Foo>
to Collection<Test.Bar> is actually checking against the erased type
Collection"

Can anyone explain exactly what this means and why the above code may
be dangerous?

This is just example code, of course.  The real code that I ran into
this warning on was on a factory method that creates a collectoin of
objects of a given type that I pass as a parameter to the factory
method. Thus, the client of the factory method knows what type of
objects the returned collection is going to contain but the factory
method itself can't know so it must be declared as returning
Collecton<? extends BaseType>.
Ingo R. Homann - 24 Aug 2005 15:05 GMT
Hi,

> 8:        Collection<Bar> stuff = (Collection<Bar>)getStuff();
>
[quoted text clipped - 4 lines]
> Can anyone explain exactly what this means and why the above code may
> be dangerous?

It means that the cast is only checked for "Collection", not for
"Collection<Bar>".

Ciao,
Ingo
hiwa - 24 Aug 2005 19:33 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> Ciao,
> Ingo

No. It is because wildcard ? for Java generics means 'specific some
class extending Foo' and doesn't mean
'any class extending  Foo' as  the  OP  have had expected. In other
words, the return value from the method
could be other Collection<? extends Foo> than Collection<Bar>.
 


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.