Possible inspection? Ambiguous AutoBoxing?
Josh Bloch had a puzzler at JavaOne where he was removing a value from an list of Integers. The code looked similar to this:
int x = 2;
listOfInts.remove(x);
Since there is already a remove method that takes an int (primitive), that implementation would be called rather than the remove(Object) method. It seems like this kind of thing would be easy for an inspection to catch.
Am I off base here? Should I submit it to the JIRA?
Please sign in to leave a comment.
Bryan Young wrote:
That would definitely be easy to detect. But I am a little bit concerned
about false positives. How would the inspection detect that a developer
actually intended to remove the x-th element?
Bas
Perhaps the inspection should only trigger for List (or List)? Or would that mean that you miss a lot of cases (do you often want to autowrap integers into List]]>)?
I agree that false positives would make the inspection much less useful. Triggering only against type-safe methods would at least catch a large number of these situations.
Bryan Young wrote:
Well, it would, but as far as I know List.remove() is not a type safe
method.
Bas
Good catch. I hadn't noticed. That seems a little inconsistant to me. Strange.
Well, I'm stumped. I don't see a good way of creating this inspection at this point.