Bug 334558 - [Extract Method] semantically incorrect, return statement missing, boolean value not set
Summary: [Extract Method] semantically incorrect, return statement missing, boolean va...
Status: CLOSED DUPLICATE of bug 213519
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.5.2   Edit
Hardware: PC Windows 7
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-17 13:18 EST by Andreas Ottiger CLA
Modified: 2011-01-18 06:47 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Ottiger CLA 2011-01-17 13:18:21 EST
Build Identifier: M20100211-1343

The following code is to be refactored:

public class TestRefactoring {
  public void test() {
    boolean head = false;
    for (int i = 0; i < 40; i++) {
    }
    for (int k = 0; k < 10; k++) {
      // Extract Method from here
      if (!head) {
        head = true;
      }
      // to here
    }
  }
}

Extract a method in the marked part. Extracted is:

public class TestRefactoring {
  public void test() {
    boolean head = false;
    for (int i = 0; i < 40; i++) {
    }
    for (int k = 0; k < 10; k++) {
      extractedMethod(head);
    }
  }

  private void extractedMethod(boolean head) {
    // Extract Method from here
    if (!head) {
      head = true;
    }
    // to here
  }
}

This is obviously wrong, as the statement "head = true;" is now called multiple times instead of once. It does not matter, if more statements (like generating output etc. are added).

In my opinion this is quite serious, as refactored code might just not work as expected without the user knowing.

Reproducible: Always

Steps to Reproduce:
1. write the above code
2. refactor it with "extract method"
3. verify it produced an error
Comment 1 Andreas Ottiger CLA 2011-01-17 13:21:05 EST
Forgot to mention: this only happends with a first for loop (even if it does not do anything). Without the first for loop the behaviour is correct.

Correct refactoring:
public class TestRefactoring {
  public void test() {
    boolean head = false;
    for (int i = 0; i < 40; i++) {
    }
    for (int k = 0; k < 10; k++) {
      head = extractedMethod(head);
    }
  }

  private boolean extractedMethod(boolean head) {
    // Extract Method from here
    if (!head) {
      head = true;
    }
    // to here
    return head;
  }
}
Comment 2 Deepak Azad CLA 2011-01-17 22:56:51 EST
This works with 3.6, can you please try with that.

*** This bug has been marked as a duplicate of bug 213519 ***