Bug 450978 - [refactor] Add "extract static method" refactoring
Summary: [refactor] Add "extract static method" refactoring
Status: CLOSED DUPLICATE of bug 318560
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.5   Edit
Hardware: PC Windows NT
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-11-11 10:04 EST by Lukas Eder CLA
Modified: 2014-11-12 05:12 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 Lukas Eder CLA 2014-11-11 10:04:30 EST
In some cases, when I'm in a non-static context, I would love to be able to refactor / extract a static method. Example:

Situation
----------------------------------------

private int memberField;

public void memberMethod(int a) {
    int b;

    b = a + memberField; // Call extract method for this line
}


Current output
----------------------------------------

private int memberField;

public void memberMethod(int a) {
    int b;

    extracted(a);
}

private void extracted(int a) {
    int b;
    b = a + memberField;
}


Desired output
----------------------------------------

private int memberField;

public void memberMethod(int a) {
    int b;

    extracted(a, memberField);
}

private static void extracted(int a, int memberField) {
    int b;
    b = a + memberField;
}


Alternative desired output
----------------------------------------

private int memberField;

public void memberMethod(int a) {
    int b;

    extracted(a, this);
}

private static void extracted(int a, ScopeClass scopeClass) {
    int b;
    b = a + scopeClass.memberField;
}

----------------------------------------

Of course, the refactoring should also be made available in the quick fix selection.
Comment 1 Stephan Herrmann CLA 2014-11-11 10:27:44 EST
Passing to JDT/UI.

I understand this is besides the point, but I found your example confusing at first, because you're starting from a statement with no observable effect. Shouldn't we start by adding some statements that read b after the assignment? Or extract only the RHS of the assignment?
Comment 2 Lukas Eder CLA 2014-11-11 10:44:00 EST
Yes it's nonsense, you're right. It should be more like this:

Current output
----------------------------------------

private int memberField;

public void memberMethod(int a) {
    int b;

    b = extracted(a);
}

private int extracted(int a) {
    return a + memberField;
}


Desired output
----------------------------------------

private int memberField;

public void memberMethod(int a) {
    int b;

    b = extracted(a, memberField);
}

private static int extracted(int a, int memberField) {
    return a + memberField;
}


Alternative desired output
----------------------------------------

private int memberField;

public void memberMethod(int a) {
    int b;

    b = extracted(a, this);
}

private static int extracted(int a, ScopeClass scopeClass) {
    return a + scopeClass.memberField;
}
Comment 3 Noopur Gupta CLA 2014-11-12 05:12:28 EST

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