Please explain JDK 1.5 generic type warning

Discussion in 'Java' started by Don Leckie, Feb 28, 2006.

  1. Don Leckie

    Don Leckie Guest

    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
    Don Leckie, Feb 28, 2006
    #1
    1. Advertising

  2. Don Leckie wrote:
    >
    > 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.


    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/
    Thomas Hawtin, Feb 28, 2006
    #2
    1. Advertising

  3. Don Leckie

    Roedy Green Guest

    Roedy Green, Mar 1, 2006
    #3
  4. Don Leckie

    sep Guest

    try

    m_loopingData = ArrayList<TLoopingData>() reader.readObject( );
    sep, Mar 1, 2006
    #4
  5. Don Leckie

    Don Leckie Guest

    Hi Tom,

    Thanks for the info!

    Don



    "Thomas Hawtin" <> wrote in message
    news:4404ce9d$0$9241$...
    > Don Leckie wrote:
    >>
    >> 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.

    >
    > 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/
    Don Leckie, Mar 1, 2006
    #5
  6. Don Leckie

    Don Leckie Guest

    Hi Roedy,

    Thank you for the info!

    Don



    "Roedy Green" <> wrote in
    message news:...
    > On Tue, 28 Feb 2006 16:13:09 -0500, "Don Leckie" <>
    > 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.
    Don Leckie, Mar 1, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Babar
    Replies:
    1
    Views:
    416
    Chris Smith
    May 20, 2004
  2. Thomas G. Marshall
    Replies:
    5
    Views:
    787
    Thomas G. Marshall
    Aug 6, 2004
  3. Ulf Meinhardt
    Replies:
    0
    Views:
    6,400
    Ulf Meinhardt
    Aug 10, 2006
  4. Sébastien de Mapias
    Replies:
    7
    Views:
    847
    Daniel Pitts
    May 29, 2008
  5. minlearn
    Replies:
    2
    Views:
    445
    red floyd
    Mar 13, 2009
Loading...

Share This Page