Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 424388

Summary: [move method] method invocation introduces class-instance-creation as the parameter incorrectly
Product: [Eclipse Project] JDT Reporter: Jongwook Kim <jongwook.kim>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: jongwook.kim, manju656
Version: 4.2.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard: stalebug

Description Jongwook Kim CLA 2013-12-18 16:52:55 EST
When A.m() is moved to B via A.b, method invocation new A().m() is updated to new A().b.m(new A()). That is wrong (due to side effect).

class A {
    B b;
    A m() { return this; }
}

class B {
}

class C {
    void n() {
        new A().m();
    }
}
Comment 1 Jongwook Kim CLA 2013-12-18 16:58:04 EST
The side effect occurs by creating two instances of class A.
Comment 2 Martin Mathew CLA 2013-12-18 22:11:25 EST

*** This bug has been marked as a duplicate of bug 313918 ***
Comment 3 Jongwook Kim CLA 2013-12-18 22:45:34 EST
This is NOT a duplicate of bug 313918.

The following example prints 2 before moving A.m() to b but 3 after the same refactoring:

class A {
    static int i = 1;
    B b = B.singleton;

    A() {
        A.i++;
    }

    A m(){
        return this;
    }
}

class B {
    static B singleton = new B();
}

class C {
    static void main(String args[]) {
        new A().m();
        System.out.println(A.i);
    }
}
Comment 4 Martin Mathew CLA 2013-12-18 23:33:54 EST
The code snippet provided in the initial bug report had the same problem reported in bug 313918, comment 3.
Comment 5 Jongwook Kim CLA 2013-12-18 23:45:23 EST
OK. The key point is that method invocation new A().m() is updated to new A().b.m(new A()) which has two different instances of class A.
Comment 6 Jongwook Kim CLA 2013-12-31 22:29:54 EST
In general, class-instance-creation can exist inside a method body. From the example above, suppose another method foo returns new A() and the method invocation looks like foo().m(). The following has the same side effect:

foo().m(foo());    //After Refactoring
Comment 7 Eclipse Genie CLA 2018-12-22 12:56:53 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.