Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type safety: Unchecked cast from Object to ArrayList<MyVariable>

Here is a part of a program that sends an ArrayList from a server to a client. I want to remove the warning about the last line in this code:

Client code:

Socket s;
(...)
// A server is sending a list from the other side of the link.
ois = new ObjectInputStream(s.getInputStream());
MyList = (ArrayList<MyVariable>) ois.readObject();

MyVariable is a Java class with some attributes. The server is creating an ArrayList and filling it with MyVariable variables as items. Then it sends the complete list to the client.

I would like to know why do I have a warning there and how to code perfectly in order to have 0 warnings. If it is possible I would like to avoid using "@SuppressWarnings("unchecked")". ;)

Thank you,

Luis

like image 610
luisgomezcaballero Avatar asked Nov 28 '13 22:11

luisgomezcaballero


3 Answers

Try this

Object obj = ois.readObject();
// Check it's an ArrayList
if (obj instanceof ArrayList<?>) {
  // Get the List.
  ArrayList<?> al = (ArrayList<?>) obj;
  if (al.size() > 0) {
    // Iterate.
    for (int i = 0; i < al.size(); i++) {
      // Still not enough for a type.
      Object o = al.get(i);
      if (o instanceof MyVariable) {
        // Here we go!
        MyVariable v = (MyVariable) o;
        // use v.
      }
    }
  }
}
like image 60
Elliott Frisch Avatar answered Oct 08 '22 11:10

Elliott Frisch


It's impossible to avoid this warning. readObject() returns an Object. You need to cast it. And casting to a generic type will always generate such a warning.

If you want to make your code as clean as possible, which is a good idea, you should respect the Java naming conventions though, and make variable names start with a lowercase letter.

like image 16
JB Nizet Avatar answered Oct 08 '22 10:10

JB Nizet


I don't like that, but you can have a container (sort of an alias or typedef):

// add "implements Serializable" in your case
private static class MyVariableList {
    public List<MyVariable> value;
}

And work with MyVariableList instead. That way you explicitly provide enough information to the compiler to do type checking in runtime.

like image 3
wonder.mice Avatar answered Oct 08 '22 09:10

wonder.mice