This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 229951 - incorrect "Unnecessary cast" hint
Summary: incorrect "Unnecessary cast" hint
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.4
Hardware: PC All
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
: 237054 (view as bug list)
Depends on:
Blocks: 235668 237553
  Show dependency tree
 
Reported: 2013-05-18 19:25 UTC by athompson
Modified: 2013-11-06 03:18 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description athompson 2013-05-18 19:25:38 UTC
Assume you manage a collection of objects. Consider the methods:

  Object findByPropertyValue(String name, Object value);
  Object findByPropertyValue(String name, String value, boolean ignoreCase);

  Object findByPropertyValue(String name, Object value boolean ignoreCase);

The first two are public methods which simply call the private third method to avoid having to have an 'ignoreCase' parameter for anything but Strings.

In the second (public) method, in order to call the private method with the same name, you need to tell the compiler you want the private method instead of recursively calling the original method by explicitly casting the "value" parameter to Object:

  return findByPropertyValue(name, (Object)value, ignoreCase);
                                    ------

The above line incorrectly shows the hint "Unnecessary cast to Object", since it is needed in this case to select the correct method.
Comment 1 athompson 2013-05-24 21:12:15 UTC
Additional problems:

First, it offers to disable/configure the "type cast is too strong" hint instead of itself.

Also, consider this case:


  Map<String, Object> Map;
  Object foo;
  ...
  if (map.containsKey((String)foo)) { ... }
                       ------

The "unnecessary cast" hint is incorrectly displayed, since without the cast the "suspicious call" hint is displayed.

And this case:


  Collection c = (Collection)accessor.invoke(entity, (Object[])null);
                                                      --------

Without the cast the "non-varargs call of varargs method with inexact argument type" hint is shown.
Comment 2 Jan Lahoda 2013-07-09 15:00:53 UTC
Svata, please contact me offline, I have a few comments. Thanks.
Comment 3 lolo_101 2013-09-02 15:44:47 UTC
This also happens with simple types.
Consider the following simple exemple:

int a=2, b=5;
double x = a/b;            // x = 0.0
double y = ((double) a)/b; // y = 0.4

The editor displays the hint "unnecessary cast to double" for the calculation of 'y' whereas the cast obviously changes the result.
Comment 4 fsttesla 2013-10-22 10:03:55 UTC
Another case of the same problem.

Let c a char variable. Consider an expression like
"Character " + c + " has ASCII code " + (int)c
                                         ---

Generally speaking, the "unnecessary cast" hint does not take into account if the (up)cast is relevant in the expression where it occurs, either to select the right method/operator, or to suppress a warning, etc.
Comment 5 Svata Dedic 2013-11-04 12:56:03 UTC
*** Bug 235668 has been marked as a duplicate of this bug. ***
Comment 6 Svata Dedic 2013-11-04 13:02:55 UTC
I would split the issue to method resolution and the arithmetic expression issues. I copied arithmetic stuff in comment #3 and comment #4 to issue #237553.

Please track only method resolution-related issues here.
Comment 7 Svata Dedic 2013-11-04 13:08:17 UTC
varargs and overloaded methods handling fixed in http://hg.netbeans.org/jet-main/rev/2b7422a28f83
Comment 8 Svata Dedic 2013-11-05 17:09:50 UTC
*** Bug 237054 has been marked as a duplicate of this bug. ***
Comment 9 Quality Engineering 2013-11-06 03:18:39 UTC
Integrated into 'main-silver', will be available in build *201311060001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/2b7422a28f83
User: Svata Dedic <sdedic@netbeans.org>
Log: #229951: avoided hints for calls to overloaded methods which may become ambiguous. Improved varargs handling