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 / March 2006

Tip: Looking for answers? Try searching our database.

Please explain JDK 1.5 generic type warning

Thread view: 
Don Leckie - 28 Feb 2006 21:13 GMT
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 - 28 Feb 2006 22:34 GMT
> 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
[quoted text clipped - 9 lines]
> Type safety: The cast from Object to ArrayList<TLoopData> is actually
> checking against the erased type ArrayList.

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
Signature

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

Don Leckie - 01 Mar 2006 15:39 GMT
Hi Tom,

Thanks for the info!

Don

>> 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
[quoted text clipped - 48 lines]
>
> Tom Hawtin
Roedy Green - 01 Mar 2006 05:31 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Don Leckie - 01 Mar 2006 15:42 GMT
Hi Roedy,

Thank you for the info!

Don

>>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
sep - 01 Mar 2006 12:15 GMT
try

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


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.