Bug 33222 - [rename] Refactor rename of class to duplicate name can result in invalid source
Summary: [rename] Refactor rename of class to duplicate name can result in invalid source
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows 2000
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Markus Keller CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 46765 355568 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-02-26 06:01 EST by Mike D Pilsbury CLA
Modified: 2011-10-25 02:14 EDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike D Pilsbury CLA 2003-02-26 06:01:46 EST
[RC1]

Using Refactor Rename to rename a class to have the same name as an existing 
class in a different package can result in uncompilable code.

Consider three classes; a.X, a.Y and b.Z

// a.X
package a;
import b.Z;

public class X {
  private Y y;
  private Z z;
  
  void func() {
    y.getYValue();
    z.getZValue();
  }
}

// a.Y
package a;

public class Y {
    public String getYValue() {
      return "";
    }
}

// b.Z
package b;

public class Z {
  public String getZValue() {
    return "";
  }
}

Now refactor rename b.Z to b.Y, and class a.X will become

// a.X
package a;
import b.Y;

public class X {
  private Y y;
  private Y z;
  
  void func() {
    y.getYValue();
    z.getZValue();
  }
}

There has been no attempt to distinguish between a.Y and b.Y. I think that what 
should probably have happened is that
  private Y z;
should have become
  private b.Y z;

The most frustrating aspect of this bug is that it is not possible to undo the 
refactoring, as the two Y classes are now unseparable in a.X. Not too much of a 
problem in this scenario, but when the refactoring hits 100+ classes it's not 
funny.

[I am not suggesting that two classes of the same name in different packages 
(both under my control) is a good idea. When I encountered the problem, it was 
an intermediate step in some refactoring I was doing.]
Comment 1 Dirk Baeumer CLA 2003-02-26 14:46:06 EST
Refactor->Undo should work even if the refactoring left your code in this 
state. 

Adam, can you please comment on this.
Comment 2 Mike D Pilsbury CLA 2003-02-27 04:36:55 EST
Sorry, you are quite right, Refactor->Undo does indeed undo the damage. I had 
completely overlooked that menu option, and was referring to the fact that 
attempting to rename b.Y back to Z wouldn't work because of the confusion over 
the Y classes.
Comment 3 Markus Keller CLA 2006-06-14 13:25:52 EDT
We already detect this problem and issue an error message. However, we could become smarter and start an (expensive) ImportRewrite for compilation units with conflicts.
Comment 4 Markus Keller CLA 2006-06-14 13:29:54 EDT
*** Bug 46765 has been marked as a duplicate of this bug. ***
Comment 5 Dani Megert CLA 2011-10-25 02:14:46 EDT
*** Bug 355568 has been marked as a duplicate of this bug. ***