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.
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; } }
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.
Implemented in jet-main#35d4582e8d96
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