This discussion is archived
4 Replies Latest reply: May 8, 2013 8:26 AM by ptoye RSS

How to unserialize parametrized types without compile-time warnings

ptoye Newbie
Currently Being Moderated
I'm trying to serialize Hashset <Type> and Arraylist <type>. I can use stream.WriteObject to serialize them OK.

And stream.ReadObject reads the object OK. But I get a complie-time warning that
setmoveList = (ArrayList<Move>) (is.readObject());
has an unchecked cast.

If I replace the code with
Object o= is.readObject();
if (o instanceof ArrayList<Move>) {
  SetMoveList=  (ArrayList<Move>) o
}
I get a compile time error "illegal generic type for instanceof".

Obviously I can live with the warnings, but is there a way of producing clean code which checks the type correctly?

Edited by: ptoye on May 7, 2013 2:51 AM
  • 1. Re: How to unserialize parametrized types without compile-time warnings
    gimbal2 Guru
    Currently Being Moderated
    Your quest to write clean code is commendable, but it is very much subject to personal taste. For example: I find code that has suppress warning annotations on it clean enough, other people will ridicule me for it. What do you think?
    is there a way of producing clean code which checks the type correctly?
    Reason about it for a moment: can you check the type correctly at compile time? readObject() is totally a runtime deal.
  • 2. Re: How to unserialize parametrized types without compile-time warnings
    ptoye Newbie
    Currently Being Moderated
    I take your point about it being taste, and certainly wouldn't think of ridiculing you for your stance. I've been living with this particular issue for some months now.

    But isn't "instanceof" a run-time check? I'd have expected that the runtime system would read in the object and then check its type against the required template. After all, it has to do this for simpler forms like ReadInt (and throw an exception if they're not correct). Or is it just too complex for parameterised types? I must try writing one type of Object and reading in another here to see what happens.
  • 3. Re: How to unserialize parametrized types without compile-time warnings
    jtahlborn Expert
    Currently Being Moderated
    ptoye wrote:
    But isn't "instanceof" a run-time check? I'd have expected that the runtime system would read in the object and then check its type against the required template. After all, it has to do this for simpler forms like ReadInt (and throw an exception if they're not correct). Or is it just too complex for parameterised types? I must try writing one type of Object and reading in another here to see what happens.
    java generics are "erased" at runtime, thus "instanceof" has no meaning when it comes to generic parameters. the only way to fully verify that "List<String>" is valid is to call instanceof on every element of the List.
  • 4. Re: How to unserialize parametrized types without compile-time warnings
    ptoye Newbie
    Currently Being Moderated
    >
    java generics are "erased" at runtime, thus "instanceof" has no meaning when it comes to generic parameters. the only way to fully verify that "List<String>" is valid is to call instanceof on every element of the List.
    OIC. Thanks.

    Of course, this means that instead of serialising the entire structure I'd have to serialise each item separately and rebuild it part by part. Too much like hard work for no gain.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points