简体   繁体   中英

Eclipse how to refactor variable names?

In my 15 years old java project, Standard Java Conventions are not respected, and first letter of variable names is often capitalized.

I would like to refactor my project to lower case first letter of variables names, especially in method signatures :

public void creerProfil(String CodeProfil, Profil ProfilACreer) throws Exception {
...
}

should be :

public void creerProfil(String codeProfil, Profil profilACreer) throws Exception {
...
}

If you have any idea of plugin which could do that, I would be glad to hear it :)

If you are on windows/ubuntu platform use alt+shift+R or on mac use command+option+r . It will refactor all the occurences where that variable is used.

To get the list of shortcuts available in eclipse, use ctrl+shift+L in windows/ubuntu and command+shift+l in mac.

No plugins needed, rightclick on the variable name, then "refactor" -> "rename". However, never tried before, I don't know how reliable it is among the whole project.

您还可以在Eclipse中按Alt + Shift + R来重构变量。

To answer you simply, right Click variable name > refactor > rename. But you have to do it variable by variable.I case you want to find all the violation of variable names at one go, use checkstyle Checkstyle naming convention

Checkstyle will give you list of all the places code convention is not followed and then you can refactor.

In Eclipse Neon, I do the following to rename a variable across the project:

  1. Go to Search->File...
  2. Enter the name of the variable in Containing text:
  3. Click Replace... which is located at the bottom of the dialog box.
  4. A new dialog box opens. There, put the variable name you want to replace with in With... labelled text box.
  5. To preview, where the changes will be reflected, click on Preview >
  6. Once, you are sure, click OK .

Hope this helps!

Quoting from eclipse docs for Rename refactoring:-

Renames the selected element and (if enabled) corrects all references to the elements (also in other files).

Available: Methods, method parameters, fields, local variables, types, type parameters, enum constants, compilation units, packages, source folders, projects and on a text selection resolving to one of these element types

Shortcut: Alt + Shift + R

For Refactoring a method's signature/parameters:-

Changes parameter names, parameter types, parameter order and updates all references to the corresponding method. Additionally, parameters and thrown exceptions can be removed or added and method return type and method visibility can be changed.

Available: Methods or on text selection resolving to a method

Shortcut: Alt + Shift + C

Refactor within a file: Use Alt+Shift+R

Refactor within multiple files: Use Alt+Shift+R two times (To make it simple, hold Alt+Shift and press R two times).

This behaviour works with Eclipse Oxygen, Windows.
Version: Oxygen.3a Release (4.7.3a)
Build id: 20180405-1200

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