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 / General / October 2005

Tip: Looking for answers? Try searching our database.

Sun's javac doesn't grok generics?

Thread view: 
Ian Pilcher - 07 Oct 2005 19:51 GMT
According to (Sun's) javac, a HashSet<Class<? extends Exception>> cannot
be assigned to a Set<Class<? extends Throwable>>.

Reflection.java:75: incompatible types
found   : java.util.HashSet<java.lang.Class<? extends java.lang.Exception>>
required: java.util.Set<java.lang.Class<? extends java.lang.Throwable>>

Should I be using a different syntax, or are generics just hopeless?

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

Thomas Hawtin - 07 Oct 2005 20:57 GMT
> According to (Sun's) javac, a HashSet<Class<? extends Exception>> cannot
> be assigned to a Set<Class<? extends Throwable>>.
[quoted text clipped - 4 lines]
>
> Should I be using a different syntax, or are generics just hopeless?

Generics are hopeless (in some sense).

I can add Throwable.class to your Set<Class<? extends Throwable>>
reference. Now if that had been assigned from HashSet<Class<? extends
Exception>> reference, that HashSet reference now has a very naughty
instance inside.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Ian Pilcher - 08 Oct 2005 02:03 GMT
> I can add Throwable.class to your Set<Class<? extends Throwable>>
> reference. Now if that had been assigned from HashSet<Class<? extends
> Exception>> reference, that HashSet reference now has a very naughty
> instance inside.

Makes sense.

Thanks to all who replied!

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

Roedy Green - 07 Oct 2005 22:06 GMT
>According to (Sun's) javac, a HashSet<Class<? extends Exception>> cannot
>be assigned to a Set<Class<? extends Throwable>>.

I make a stab at explaining this at:
http://mindprod.com/jgloss/generics.html

I point to several places that also try to explain it.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Mike Schilling - 08 Oct 2005 01:48 GMT
> According to (Sun's) javac, a HashSet<Class<? extends Exception>> cannot
> be assigned to a Set<Class<? extends Throwable>>.
[quoted text clipped - 3 lines]
> java.lang.Exception>>
> required: java.util.Set<java.lang.Class<? extends java.lang.Throwable>>

That's correct.  For clarity (if such a thing is possible with generics)
let's simplify it some.

Should a List<? extends Exception?> be assignable to a List<?  extends
Throwable> ?  The answer is no.

Consider

   List<?  extends Exception> le = new ArrayList<?  extends Exception>;
   List<?  extends Throwable> lt = le;    // not allowed, as we'll see
   lt.add(new OutOfMemoryError());
   Exception e = le.get(0);

This would throw a ClassCastException, since the first member of the list
isn't an Exception.

Note that this isn't a problem introduced by generics; arrays have the same
problem, though it's handled differently:

   Exception[] ae = new Exception[1];
   Throwable[] at = ae;    // so far, so good
   at[0] = new OutOfMemoryError();    // this throws an
ArrayElementException

For arrays, which know what type they contain, the error occurs when the bad
assignment is done.  For Collections, which don't really know what type they
contain, the compiler doesn't allow the questionable polymorphism.
John C. Bollinger - 08 Oct 2005 07:05 GMT
> According to (Sun's) javac, a HashSet<Class<? extends Exception>> cannot
> be assigned to a Set<Class<? extends Throwable>>.
[quoted text clipped - 4 lines]
>
> Should I be using a different syntax, or are generics just hopeless?

Inasmuch as I can divine what you are trying to do, generics will not
accomplish it for you.  In the first place, javac is right, as Tom and
Roedy have already written.  More fundamentally, however, I suspect you
are misunderstanding the use of wildcards in type expressions.
"Set<Class<? extends Exception>>" does NOT mean "a set of Class objects
characterized by type parameters that extend Exception".  Rather, it
means "a set of Class objects all characterized by the *same* type
parameter, unspecified except that it must extend Exception".  Since
there is exactly one Class object for any particular class (per
classloader) such a set could only ever have one member.

Do also note that you cannot use type parameter wildcards in
instantiation expressions, only in declarations.  That's relevant in
that it supports the fact that Java doesn't have the type you seem to be
looking for.  Wildcards are for generic handling of multiple distinct
types, not for creating "more generic" types.

Signature

John Bollinger
jobollin@indiana.edu



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.