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 243581 - Integer unboxing hint gives bad advice
Summary: Integer unboxing hint gives bad advice
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: Macintosh (x86) Mac OS X
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks: 250842
  Show dependency tree
 
Reported: 2014-04-04 17:05 UTC by brucewee
Modified: 2015-04-12 04:25 UTC (History)
1 user (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 brucewee 2014-04-04 17:05:00 UTC
This is a horrible "bug" in Java that the editor tries to save you from,
but fails.

Integer x = new Integer(4);
Integer y = new Integer(4);

if( x == y ) {  // GOOD hint here: "Integer values compared using == or !="
    System.out.println(x + " == " + y);
} else {
    System.out.println(x + " != " + y);  // prints:  "4 != 4"
}

if( x.intValue() == y.intValue() ) { // BAD hint here: "Unnecessary unboxing"
    System.out.println(x + " == " + y); // prints "4 == 4 "
} else {
    System.out.println(x + " != " + y);
}

The first hint is GOOD
The second hint is BAD!!! You do need to unbox.
Comment 1 lolo_101 2014-06-11 15:35:26 UTC
Another exemple of erroneous "unnecessary unboxing" hint,
In java 8, writing these line :

String values = "some text";
Map<Character, Long> freqV = values.chars().boxed().collect(Collectors.groupingBy(integ -> (char) integ.intValue(), Collectors.counting())); // "unnecessary unboxing" hint

If I remove the unboxing, the code does not compile...
Comment 2 vicricker 2014-08-29 17:53:21 UTC
I ran into this one, today, with AbstractAction: putValue(Action.MNEMONIC_KEY, Integer.valueOf('E'));

Netbeans suggests that you remove Integer.valueOf(); however, doing so changes the semantics of the code.  By removing the valueOf() call, the 'E' is boxed to Character rather than Integer.  The code compiles, because the second method parameter takes an Object, but it crashes at runtime:

java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Integer
javax.swing.AbstractButton.setMnemonicFromAction(AbstractButton.java:1256)
Comment 3 Svata Dedic 2015-04-07 13:19:14 UTC
Defect, not an enhancement.
Comment 4 Svata Dedic 2015-04-08 14:13:33 UTC
(In reply to brucewee from comment #0)
> if( x.intValue() == y.intValue() ) { // BAD hint here: "Unnecessary unboxing"
> 
> The first hint is GOOD
> The second hint is BAD!!! You do need to unbox.

Actually you don't need to unbox ONE of the operands. Once one of the operands is a primitive, the other one will according to JLS 15.21.1 undergo numeric promotion (JLS 5.6.2), which implies unboxing conversion. 

We may argue whether the result is readable and I would say it's not - I'll suppress the hint for this special case (= binary op or conditional alternative where the other argument is also being unboxed)


(In reply to lolo_101 from comment #1)
> String values = "some text";
> Map<Character, Long> freqV =
> values.chars().boxed().collect(Collectors.groupingBy(integ -> (char)
> integ.intValue(), Collectors.counting())); // "unnecessary unboxing" hint
> 
OK, excluding a case when the auto-unboxed type is not assignable to casted-to type

(In reply to vicricker from comment #2)
> I ran into this one, today, with AbstractAction:
> putValue(Action.MNEMONIC_KEY, Integer.valueOf('E'));
> 
Hint suppressed if the target type is a reference & the original passed type and the autoboxed type differs == instance of a different type would be passed after hint is applied.
Comment 5 Svata Dedic 2015-04-09 10:36:54 UTC
Fixed in jet-main#47be3a84e239
Comment 6 Quality Engineering 2015-04-12 04:25:01 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/47be3a84e239
User: Svata Dedic <sdedic@netbeans.org>
Log: #243581: improved boxing/unboxing suggestions