I have Objectone that sometimes contains List<Object>. I want to test it with instanceof, and if so, add some elements to it.
void add(Object toAdd) {
Object obj = getValue();
if (obj instanceof List<?>) {
List<?> list = obj;
if (list instanceof List<Object>) {
((List<Object>) list).add(toAdd);
} else {
List<Object> newList = new ArrayList<Object>(list);
newList.add(toAdd);
setValue(newList);
}
return;
}
throw new SomeException();
}
And he says that I cannot check if it instanceof List<Object>is because java does not care and erases the type in <>. Does this mean that I have to create a new ArrayList every time? Or is there a way to check this, for example. with reflection?
source
share