Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior caused by extraction methods #1357

Closed
DongChunHao opened this issue Apr 23, 2024 · 0 comments · Fixed by #1399
Closed

Inconsistent behavior caused by extraction methods #1357

DongChunHao opened this issue Apr 23, 2024 · 0 comments · Fixed by #1399
Assignees
Labels
bug Something isn't working
Milestone

Comments

@DongChunHao
Copy link
Contributor

Eclipse version:2023-9

Repeat the steps:

  1. select “switch (value) {....return result;”
  2. click “Refactor-extract method”

class ExtractMethodTest {
private final int value;

public ExtractMethodTest(int value) {
    this.value = value;
}

public synchronized int calculate() {
    int result;
    switch (value) {
        case 1:
            result = value * 2;
            break;
        case 2:
            result = value * 3;
            break;
        default:
            result = value * 4;
            break;
    }
    return result;
}

}

refactoring result:
image

After extracting the method, if inline refactoring is performed, the code will be inconsistent with the original code and an additional local variable will be declared

@jjohnstn jjohnstn self-assigned this May 9, 2024
@jjohnstn jjohnstn added the bug Something isn't working label May 9, 2024
@jjohnstn jjohnstn added this to the 4.32 M3 milestone May 9, 2024
jjohnstn added a commit to jjohnstn/eclipse.jdt.ui-1 that referenced this issue May 9, 2024
- modify ExtractMethodRefactoring.createMethodBody() to check all
  locals that needed to be created in new method to see if the old
  declaration is still needed (i.e. referenced outside of the
  selected statements) and if not, remove the original declaration
- modify all ExtractMethodTests where the locals are left behind
  and not used elsewhere
- add new tests to ExtractMethodTests
- fixes eclipse-jdt#1357
jjohnstn added a commit that referenced this issue May 10, 2024
…1399)

- modify ExtractMethodRefactoring.createMethodBody() to check all
  locals that needed to be created in new method to see if the old
  declaration is still needed (i.e. referenced outside of the
  selected statements) and if not, remove the original declaration
- modify all ExtractMethodTests where the locals are left behind
  and not used elsewhere
- add new tests to ExtractMethodTests
- fixes #1357
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants