Generics in 1.5, nonsense warnings?

C

changzhouwang

Isn't it strange to see a compilation warning about the following code

-------code-----
ArrayList<String> a1 = new ArrayList<String>(); ...
ArrayList<String> a2 = (ArrayList<String>)a1.clone();
-------warning (in eclipse) -----
Type safety: The cast from Object to ArrayList<String> is actually
checking against the erased type ArrayList
-------end-------

And

-------code-----
EventListener l=...;
Class<EventListener> class1 = (Class<EventListener>)l.getClass();
-------warning (in eclipse) -----
Type safety: The cast from Class<capture-of ? extends EventListener> to
Class<EventListener> is actually checking against the erased type Class
-------end-------

Without the explicit conversion, there will be compilation error, due
to the way JDK 1.5 implements generics. That's understandable.

However, I don't see why we need the warning message if there is not
possible way to get rid of it. Any comments?

-czwang
 
D

Daniel Dyer

Isn't it strange to see a compilation warning about the following code

-------code-----
ArrayList<String> a1 = new ArrayList<String>(); ...
ArrayList<String> a2 = (ArrayList<String>)a1.clone();
-------warning (in eclipse) -----
Type safety: The cast from Object to ArrayList<String> is actually
checking against the erased type ArrayList
-------end-------
....

However, I don't see why we need the warning message if there is not
possible way to get rid of it. Any comments?

Read up on the @SuppressWarnings annotation (and then wait for them to
actually implement it :)).

Dan.
 
T

Thomas Hawtin

Isn't it strange to see a compilation warning about the following code

-------code-----
ArrayList<String> a1 = new ArrayList<String>(); ...
ArrayList<String> a2 = (ArrayList<String>)a1.clone();
-------warning (in eclipse) -----
Type safety: The cast from Object to ArrayList<String> is actually
checking against the erased type ArrayList
-------end-------

You can replace the problem code with new ArrayList said:
-------code-----
EventListener l=...;
Class<EventListener> class1 = (Class<EventListener>)l.getClass();
-------warning (in eclipse) -----
Type safety: The cast from Class<capture-of ? extends EventListener> to
Class<EventListener> is actually checking against the erased type Class
-------end-------

The cast is incorrect.
Without the explicit conversion, there will be compilation error, due
to the way JDK 1.5 implements generics. That's understandable.

However, I don't see why we need the warning message if there is not
possible way to get rid of it. Any comments?

If your code generates no mandatory warnings, then the static type
checking will be correct. You will not get ClassCastExceptions at
unlikely places. I think that is important.

Tom Hawtin
 
C

changzhouwang

Tom:

Thanks for your quick reply.

I agree your suggestion in the first works for ArrayList. But for other
generic class, there may not be such a constructor. My issue is that
clone() method for a generic class will always generate compilation
warning. I tried:
----
ArrayList<String> a2 = (ArrayList)a1.clone();
---
and get different warning
---
Type safety: The expression of type ArrayList needs unchecked
conversion to conform to ArrayList<String>
---

For the second example, why the cast is not correct? Indeed, this is
only way I can use EventListenerList.add(Class<T extends
EventListener> c, T l) and remove(Class<T extends EventListener> c, T
l) methods. Can you suggest a correct way to call these methods when
the listener l is often an object of a subclass of EventListener?

Thanks,
--czwang
 
T

Thomas Hawtin

For the second example, why the cast is not correct?

For any particular class (within a class loader context) there is
exactly one Class object. The type of the object will be
which is not assignment compatible with said:
Indeed, this is
only way I can use EventListenerList.add(Class<T extends
EventListener> c, T l) and remove(Class<T extends EventListener> c, T
l) methods. Can you suggest a correct way to call these methods when
the listener l is often an object of a subclass of EventListener?

Something along the lines of:

listeners.add(ActionListener.class, listener);

Using getClass on the listener is the last thing you want to do. You
want to keep together, say, all ActionListeners. getClass will never
return ActionListener.class, as it is an interface and so cannot be the
runtime class of any object.

Even if you did something odd by retrieving implemented interfaces,
event listeners often implement a number of listener interfaces for
different purposes.

Tom Hawtin
 

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

Similar Threads

Annoying 1.5 warnings 21
Generics 12
Please explain JDK 1.5 generic type warning 5
More Generics warnings. 5
generics puzzle 57
generics and type checking 9
Generics Warnings yet again 3
subclassing and generics 19

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top