Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Please explain JDK 1.5 generic type warning

42 views
Skip to first unread message

Don Leckie

unread,
Feb 28, 2006, 4:13:09 PM2/28/06
to
Hello,

Could someone please explain the JDK 1.5 warning below? It appears every
readObject( ) for a list. I've tried several things, but cannot correct the
code to eliminate the warning.

Sample code:

private List<TLoopingData> m_loopingData;
. . .
m_loopingData = (ArrayList<TLoopingData>) reader.readObject( );

The warning:
Type safety: The cast from Object to ArrayList<TLoopData> is actually
checking against the erased type ArrayList.

Thank you,
Don


Thomas Hawtin

unread,
Feb 28, 2006, 5:55:06 PM2/28/06
to

On Sun's compiler you should get a warning like:

Test.java:9: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.List<java.lang.String>
data = (List<String>)in.readObject();
^
1 warning

which should give you enough to google with.

The cast cannot be checked a runtime, therefore a warning is given. With
an up to date compiler, you can suppress the warning using
@SuppressWarnings("unchecked").

The problem is unavoidable with readObject, so I suggest writing a small
method to wrap up the nastiness in. Just so long as you know it is there.

import static com.mycompany.myproject.serial.Serial.readObject;
...
data = readObject(in);
...

package com.mycompany.myproject.serial;

/** blah */
public class Serial {
/** blah */
@SuppressWarnings("unchecked")
public static <T> T readObject(
java.io.ObjectInputStream in
) throws java.io.IOException, java.lang.ClassNotFoundException {
return (T)in.readObject();
}
}

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

Roedy Green

unread,
Mar 1, 2006, 12:31:53 AM3/1/06
to
On Tue, 28 Feb 2006 16:13:09 -0500, "Don Leckie" <d...@leckieinc.com>
wrote, quoted or indirectly quoted someone who said :

>Type safety: The cast from Object to ArrayList<TLoopData> is actually
>checking against the erased type ArrayList.

see
http://mindprod.com/jgloss/compileerrormessages.html#TYPESAFETYERASED
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

sep

unread,
Mar 1, 2006, 7:15:17 AM3/1/06
to
try

m_loopingData = ArrayList<TLoopingData>() reader.readObject( );

Don Leckie

unread,
Mar 1, 2006, 10:39:47 AM3/1/06
to
Hi Tom,

Thanks for the info!

Don

"Thomas Hawtin" <use...@tackline.plus.com> wrote in message
news:4404ce9d$0$9241$ed26...@ptn-nntp-reader01.plus.net...

Don Leckie

unread,
Mar 1, 2006, 10:42:36 AM3/1/06
to
Hi Roedy,

Thank you for the info!

Don

"Roedy Green" <my_email_is_post...@munged.invalid> wrote in
message news:ibca02dg9vruc0ri8...@4ax.com...

0 new messages