Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically rename a Java method in Eclipse?

I'm trying to rename a method in my Eclipse Java project, but it seems to be renaming every method which has the same name. (Perhaps I'm misunderstanding what this feature is for - maybe it's just using sed?)

Here is a simplified example:

    public class C1 {
        interface Listener {
            void f();
        }

        public C2.Listener c2l = new C2.Listener() {
            public void f() {
            }
        };
    }

    public class C2 {
        interface Listener {
            void f();
        }
    }

If I select the f method in C2, and select "rename" from the "refactor" menu to rename it to g, C1.Listener.f is also renamed, resulting with C1 being changed to this:

    public class C1 {
        interface Listener {
            void g();
        }

        public C2.Listener c2l = new C2.Listener() {
            public void g() {
            }
        };
    }

I expected it to be changed to this:

    public class C1 {
        interface Listener {
            void f();
        }

        public C2.Listener c2l = new C2.Listener() {
            public void g() {
            }
        };
    }

Is there a way to make it only rename C2.Listener.f?

I tried in Eclipse Helios and Android Development Tools 22.3.0.

like image 699
Dog Avatar asked Dec 14 '13 23:12

Dog


1 Answers

This seems like a bug in Eclipse and seems to happen on Kepler too. Even so you can work around it by following the steps below:

Goto Preferences > Java. On that page in under the header "Refactor Java Code", uncheck the option "Rename in editor without dialog"

Now when you Refactor it gives you an option to Preview your changes and here you can uncheck the box to apply the changes to C1.Listener()

like image 130
Abhi Avatar answered Nov 09 '22 03:11

Abhi