Is there any functionality in Eclipse to refactor my code such that unqualified field accesses gain the this
qualifier? For example, I had been writing some of my code in static methods but would now like to change them to non-static methods. I prefer styling my code with this
so that it's more clear and readable.
I would like an easy way for the foo
and bar
here:
public class Example
{
private static String foo = "Hovercraft";
private static int bar = 9001;
public static void main(String[] args)
{
System.out.println(foo + " has " + bar + " eels.");
}
}
to turn into this.foo
and this.bar
after I change the code to use non-static methods:
public class Example
{
private String foo;
private int bar;
public class Example(String theFoo, int theBar)
{
this.foo = theFoo;
this.bar = theBar;
}
public void run()
{
System.out.println(this.foo + " has " + this.bar + " eels.");
}
}
Of course it would be easy to add this.
manually if the code is short like this, but suppose I have a rather large piece of code with at least a few dozen unqualified field accesses. How would I go about doing this automatically in Eclipse? I can't use "Refactor > Rename" because that won't let me use this.foo
for the name, and using "Find/Replace" is a pain since it will also change the declarations and possibly other local variables that were named the same.
I found one way to do this after poking around more just before posting this question so I will add it as an answer, but I'm wondering if I'm missing other obvious ways to do this.