Sun's javac doesn't grok generics?

I

Ian Pilcher

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?
 
T

Thomas Hawtin

Ian said:
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?

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
 
M

Mike Schilling

Ian Pilcher said:
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>>

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.
 
I

Ian Pilcher

Thomas said:
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!
 
J

John C. Bollinger

Ian said:
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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top