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 267242

Summary: Introduce local variable refactoring yields bad results
Product: java Reporter: mclaborn <mclaborn>
Component: RefactoringAssignee: Svata Dedic <sdedic>
Status: RESOLVED INCOMPLETE    
Severity: normal CC: puce
Priority: P2    
Version: 8.2   
Hardware: PC   
OS: Linux   
See Also: https://netbeans.org/bugzilla/show_bug.cgi?id=256528
https://netbeans.org/bugzilla/show_bug.cgi?id=257103
Issue Type: DEFECT Exception Reporter:
Attachments: IDE log
before
after

Description mclaborn 2016-07-21 19:47:32 UTC
Product Version = NetBeans IDE Dev (Build 201607210002)
Operating System = Linux version 4.4.0-31-generic running on amd64
Java; VM; Vendor = 1.8.0_102
Runtime = Java HotSpot(TM) 64-Bit Server VM 25.102-b14

Reproducibility: Happens every time

A simple "Introduce local variable" refactoring introduces bad, uncompilable results.  The results in this set case are not always the same, but they are always wrong. 

Code before:
  private void stuff() {
    Double l_amount = 2.0;
    System.out.println("l_amount=[" + l_amount + "]");
    if (l_amount != null) {
      String l_dollars = new DecimalFormat("0").format(Math.floor(l_amount));
      long l_cents = 0;
    }
  }

refactor Math.floor(l_amount) into a local variable


Code after:
  private void stuff() {
    Double l_amount = 2.0;
    System.out.println("l_amount=[" + l_amount + "]");
    if (l_amount != null) {
      double floor = new .floor(l_amount));
      long l_cents = 0;
    }
 }

      String l_dollars = new DecimalFormat("0").format(floor);

Screen shots coming shortly
Comment 1 mclaborn 2016-07-21 19:47:35 UTC
Created attachment 161370 [details]
IDE log
Comment 2 mclaborn 2016-07-21 19:48:26 UTC
Created attachment 161371 [details]
before
Comment 3 mclaborn 2016-07-21 19:48:27 UTC
Created attachment 161372 [details]
after
Comment 4 Svata Dedic 2016-07-28 09:39:27 UTC
I'll need very precise steps to reproduce it; I've tried on current trunk and on the exact build (201607210002), but when I select "Math.floor(...)" subexpression, different thing happens.

I use Refactor > Introduce > Variable action. Is that the action you used ?

The initial (complete) source code is:

package test;
import java.text.DecimalFormat;
public class T {
  private void stuff() {
    Double l_amount = 2.0;
    System.out.println("l_amount=[" + l_amount + "]");
    if (l_amount != null) {
      String l_dollars = new DecimalFormat("0").format(Math.floor(l_amount));
      long l_cents = 0;
    }
  }
}

My result was:

package test;
import java.text.DecimalFormat;
public class T {
  private void stuff() {
    Double l_amount = 2.0;
    System.out.println("l_amount=[" + l_amount + "]");
    if (l_amount != null) {
          final double floor = Math.floor(l_amount);
      String l_dollars = new DecimalFormat("0").format(floor);
      long l_cents = 0;
    }
  }
}


Please try to isolate the problem into its own file/class. Attach that exact file and export your IDE options - some of the prettyprinter or formatting options may screw up offsets. You have some extra clusters (java2, javatextcopypaste) installed - I don't know whether they interfere. If possible, try to reproduce on a pristine IDE (whatever version from NB.org). Thanks !
Comment 5 mclaborn 2016-07-28 16:14:06 UTC
I can no longer reproduce this on 201607260002.
Closing for now.
Comment 6 mclaborn 2016-09-21 15:28:36 UTC
This problem is still present in 8.2 RC.  Yesterday this refactoring worked maybe 1 out of 5 times I tried it.  I'm working to get a repeatable test case, and disabling some of the plug ins.
Comment 7 mclaborn 2017-02-07 18:03:23 UTC
I'm fairly certain that 256526, 257103 and 267242 all have the same root cause. There is some action I do regularly that messes up some internal table of line numbers in the Java source file with what is actually present in the editor. (My hypothesis.) Once in this state, Fix Imports, refactoring and error/hint icons are all messed up.  Closing the file and reopening it usually fixes the problem. 

How can I go about capturing what is needed to catch this?
Comment 8 mclaborn 2017-02-12 17:03:11 UTC
I suspect that this, and several other of my bugs, may be caused by this: I had mapped Ctrl + V to "Paste Formatted". As an experiment, I put Ctrl + V back to a simple "Paste" and I have not seen these symptoms since.  I'll continue to watch. 

HTH
Comment 9 Svata Dedic 2017-06-30 15:38:34 UTC
Sorry, still no luck. I tried the remapping but still I can't reproduce. 

Do you use shortcut (Alt-Shift-V), Alt-Enter + select the appropriate fix, or do you invoke the action by mouse only ?
Comment 10 mclaborn 2017-06-30 15:49:33 UTC
See my recent notes in https://netbeans.org/bugzilla/show_bug.cgi?id=257103.

I think these are all related. 

The "paste formatted" was not the cause.  I turned it off and still have problems.