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?

5 comments
Comment actions Permalink


Bryan Young wrote:

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.


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

0
Comment actions Permalink

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]]>)?

0
Comment actions Permalink

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.

0
Comment actions Permalink

Bryan Young wrote:

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.


Well, it would, but as far as I know List.remove() is not a type safe
method.

Bas

0
Comment actions Permalink

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.

0

Please sign in to leave a comment.