简体   繁体   中英

How to refactor variable type in Eclipse?

I want to refactor code like this:

int x=4;
int y=x;
System.out.println(y);

How can i do this automatically in Eclipse so x's type promotion to long would cause dependent variables to change their types also:

long x=4;
long y=x;
System.out.println(y);

?

How I usually do is that I change one of the upstream variables to long. This causes Eclipse to give error from each assignment where you offer long instead of int. Then I just select ctrl-1 (Quick-fix) on each of them and pick "change variable x to long".

This works when the new type is not directly assignable to the old one.

I don't think you can do this directly using the refactoring tool in Eclipse. However, what you can do is place your cursor over the variable x and hit CTRL + SHIFT + G to find all references in your workspace. This will then allow you to change any dependent references.

IntelliJ has this feature (refactor->type migration), but unfortunately, Eclipse doesn't have it yet. I hope Eclipse will add it soon.

If your field has setter and getter and you have used them in your project, you can first change type of the field manually then refactor its setter and getter methods. ei you can change return type of the setter and getter by method refactor.
To refactor a method use:
Open context menu on the method > Refactor > Change method signature

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM