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 242591 - Invalid hints about "Unnecessary cast" and "Unnecessary boxing"(?)
Summary: Invalid hints about "Unnecessary cast" and "Unnecessary boxing"(?)
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: PC Linux
: P4 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-06 00:24 UTC by josephcz
Modified: 2015-04-12 04:25 UTC (History)
0 users

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 josephcz 2014-03-06 00:24:37 UTC
On the top two lines of the getDefaultValue(..) function below there are hints being shown regarding an "Unnecessary cast to byte" and an "Unnecessary boxing to Short". Similar issues are seen in the getZero(..) function below. In each of these situations a failure to cast/box will result in a different (wrong) object type being returned.

In the situations where the boxing is explicit (and possibly taking advantage of the implicit cast to a wider type), accepting the hint (e.g. "Remove Double.valueOf") produces a line with the "Unnecessary cast to ..." hint. Fixing the "Unnecessary cast" hint may make changes to the "Unnecessary boxing" hint unnecessary.

public class Test {
  public static Object getDefaultValue (Class<?> clazz) {
    if (clazz == Byte.TYPE     ) return (byte)0;                   // <-- "Unnecessary cast to byte"
    if (clazz == Short.TYPE    ) return Short.valueOf((short)0);   // <-- "Unnecessary boxing to Short"
    if (clazz == Integer.TYPE  ) return 0;
    if (clazz == Long.TYPE     ) return 0L;
    if (clazz == Float.TYPE    ) return 0f;
    if (clazz == Double.TYPE   ) return 0d;
    if (clazz == Boolean.TYPE  ) return Boolean.FALSE;
    if (clazz == Character.TYPE) return '\0';
    return null;
  }

  public static Number getZero (Class<?> clazz) {
    byte zero = 0;
    if (clazz == Byte.TYPE     ) return zero;
    if (clazz == Short.TYPE    ) return (short)zero;          // <-- "Unnecessary cast to short"
    if (clazz == Integer.TYPE  ) return (int)zero;            // <-- "Unnecessary cast to int"
    if (clazz == Long.TYPE     ) return (float)zero;          // <-- "Unnecessary cast to float"
    if (clazz == Float.TYPE    ) return Long.valueOf(zero);   // <-- "Unnecessary boxing to Long"
    if (clazz == Double.TYPE   ) return Double.valueOf(zero); // <-- "Unnecessary boxing to Double"
    return null;
  }
}
Comment 1 Svata Dedic 2015-04-09 14:36:04 UTC
With recent changes, the second-level hint (unnecessary cast) is not shown, as the IDE recognizes result type change.

For:

if (clazz == Short.TYPE    ) return Short.valueOf((short)0);   // <-- "Unnecessary boxing to Short"

the "unnecessary" hint is IMHO correct; if 0 is typecasted to short, it will be autoboxed to the correct (Short) type. 

The last 2 are actually a matter of coding style. I'll added an option (default: off) to prefer written typecasts over written boxing.
Comment 2 Svata Dedic 2015-04-09 14:50:12 UTC
Implemented in jet-main#35d4582e8d96
Comment 3 Quality Engineering 2015-04-12 04:25:39 UTC
Integrated into 'main-silver', will be available in build *201504120001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/35d4582e8d96
User: Svata Dedic <sdedic@netbeans.org>
Log: #242591: added option to convert to typecasts