Well, I finally managed to find a solution.
Just as @Medo42 said:
Another option is to check for and cast to List as the instanceof error suggests. Then you can iterate over the elements in the list and check if they are actually all instances of MyObject, and copy them to a new List.
Even though I did not went through the process of creating a whole new object in order to make this particular case to work "warning-less" this was the right direction to go.
So I took @lokoko 's idea and use it in a new setItems()
method, with an Object
parameter instead of a List<MyObject>
in order to make sure
The result code is the following:
public void setItems(List<MyObject> var){
this.list = var;
}
public void setItems(Object var){
List<MyObject> result = new ArrayList<MyObject>();
if (var instanceof List){
for(int i = 0; i < ((List<?>)var).size(); i++){
Object item = ((List<?>) var).get(i);
if(item instanceof MyObject){
result.add((MyObject) item);
}
}
}
setItems(result);
}
Thanks everyone for your help!