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 245439 - Introduce variable might change code behavior
Summary: Introduce variable might change code behavior
Status: RESOLVED WORKSFORME
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-07 08:34 UTC by Jachym_Vojtek
Modified: 2016-06-22 12:37 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 Jachym_Vojtek 2014-07-07 08:34:18 UTC
Lets have method:
Collection foo(int i) {
  if (i++ > 0)
    return new HashSet<Integer>(Arrays.asList(i));
  else {
    return Collections.EMPTY_LIST;
  }
}

If you apply introduce variable on 'Arrays.asList(i)' the converted code will be:
Collection foo(int i) {
  final List<Integer> asList = Arrays.asList(i);
  if (i++ > 0)
    return new HashSet<Integer>(asList);
  else {
    return Collections.EMPTY_LIST;
  }
}
which changes behaviour.

I think that curly brackets should be added to properly introduce variable:
Collection foo(int i) {  
  if (i++ > 0) {
    final List<Integer> asList = Arrays.asList(i);
    return new HashSet<Integer>(asList);
  }
  else {
    return Collections.EMPTY_LIST;
  }
}
Comment 1 Jachym_Vojtek 2014-07-09 09:48:49 UTC
One additional example of strange introduce variable behaviour:

Collection<String> getCountries(boolean convertToSet) {
  if (convertToSet) {
     Arrays.asList(Locale.getISOCountries()); // Cursor at position of asList
     return new HashSet<String>();
  } else 
     return Arrays.asList(Locale.getISOCountries());
}

If I apply Introduce variable on expression Arrays.asList(Locale.getISOCountries()) with Replace All Occurences(2) checked, the result is:
Collection<String> getCountries(boolean convertToSet) {
  List<String> asList = Arrays.asList(Locale.getISOCountries()); // Cursor on this line
  if (convertToSet) {
     Arrays.asList(Locale.getISOCountries()); // Cursor on this line        
     return new HashSet<String>();
  } else 
      return asList; 
}

Only one replacement has been performed, I would expect 2.
Comment 2 Svata Dedic 2016-06-22 12:37:41 UTC
Cannot reproduce any of the examples on the current dev builds.