How do you prevent unchecked cast warning when reading from ObjectInputStream?

Discussion in 'Java' started by Knute Johnson, Apr 15, 2007.

  1. How do you prevent unchecked cast warning when you read a parameterized
    object from an ObjectInputStream? If TreeMap isn't parameterized then
    this doesn't happen but that isn't really a good solution. Or should I
    just ignore this?

    The TreeMap in this case is TreeMap<String,Boolean>.

    C:\>javac -Xlint:unchecked
    com/knutejohnson/smallbook/sportsnetwork/TickerClient.java
    com\knutejohnson\smallbook\sportsnetwork\TickerClient.java:390: warning:
    [unchecked] unchecked cast found : java.lang.Object
    required: java.util.TreeMap<java.lang.String,java.lang.Boolean>
    map = (TreeMap<String,Boolean>)ois.readObject();
    ^
    1 warning

    Thanks,

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 15, 2007
    #1
    1. Advertising

  2. Knute Johnson

    Tom Hawtin Guest

    Knute Johnson wrote:
    > How do you prevent unchecked cast warning when you read a parameterized
    > object from an ObjectInputStream? If TreeMap isn't parameterized then
    > this doesn't happen but that isn't really a good solution. Or should I
    > just ignore this?


    A common problem.

    Write yourself a little utility method to read an object and cast. That
    method can suppress the warnings "safely".

    @SuppressWarnings("unchecked")
    static <T> T readObject(
    java.io_ObjectInputStream in
    ) throws java.io.IOException, java.lang.ClassNotFoundException {
    return (T)in.readObject();
    }

    http://groups.google.com/group/comp.../thread/3c05ddbbe4eaaba4#doc_585daaa682e4e563

    Tom Hawtin
    Tom Hawtin, Apr 15, 2007
    #2
    1. Advertising

  3. Tom Hawtin wrote:
    > Knute Johnson wrote:
    >> How do you prevent unchecked cast warning when you read a
    >> parameterized object from an ObjectInputStream? If TreeMap isn't
    >> parameterized then this doesn't happen but that isn't really a good
    >> solution. Or should I just ignore this?

    >
    > A common problem.
    >
    > Write yourself a little utility method to read an object and cast. That
    > method can suppress the warnings "safely".
    >
    > @SuppressWarnings("unchecked")
    > static <T> T readObject(
    > java.io_ObjectInputStream in
    > ) throws java.io.IOException, java.lang.ClassNotFoundException {
    > return (T)in.readObject();
    > }
    >
    > http://groups.google.com/group/comp.../thread/3c05ddbbe4eaaba4#doc_585daaa682e4e563
    >
    >
    > Tom Hawtin


    Thanks Tom, that worked perfectly!

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 15, 2007
    #3
    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. Andrew Lee
    Replies:
    2
    Views:
    3,120
    Andrew Lee
    Nov 18, 2004
  2. Ian Pilcher

    Avoid unchecked cast warning

    Ian Pilcher, Aug 20, 2005, in forum: Java
    Replies:
    0
    Views:
    3,529
    Ian Pilcher
    Aug 20, 2005
  3. Knute Johnson

    Unchecked cast warning?

    Knute Johnson, Nov 1, 2006, in forum: Java
    Replies:
    4
    Views:
    992
    Knute Johnson
    Nov 1, 2006
  4. kathychow
    Replies:
    0
    Views:
    2,112
    kathychow
    Mar 31, 2009
  5. RVic
    Replies:
    19
    Views:
    1,383
Loading...

Share This Page