Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generics in 1.5, nonsense warnings?

Reply
Thread Tools

Generics in 1.5, nonsense warnings?

 
 
changzhouwang@gmail.com
Guest
Posts: n/a
 
      11-17-2005
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

 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      11-17-2005
On Thu, 17 Nov 2005 19:26:20 -0000, <(E-Mail Removed)> wrote:

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


--
Daniel Dyer
http://www.dandyer.co.uk
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      11-17-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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<String>(a1).

> -------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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
changzhouwang@gmail.com
Guest
Posts: n/a
 
      11-17-2005
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

 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      11-17-2005
(E-Mail Removed) wrote:
>
> 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
Class<ActualClass>, which is not assignment compatible with, say,
Class<Object>.

> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
Tired of Nonsense Sasha Y. Gupta MCSE 26 07-15-2004 07:53 PM
virus nonsense David, the fen-sucked, staring flyswatter Computer Support 16 07-05-2004 11:04 PM
Why the nonsense in spam emails? Dan Welch Computer Support 3 10-08-2003 06:52 AM



Advertisments