简体   繁体   English

Eclipse如何重构变量名?

[英]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. 在我15岁的Java项目中,不遵守标准Java约定,并且变量名的首字母通常用大写字母表示。

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 . 如果您在Windows / ubuntu平台上,请使用alt+shift+R或在mac上,请使用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. 要获取Eclipse中可用的快捷方式列表,请在Windows / ubuntu中使用ctrl+shift+L ,在Mac中使用command+shift+l

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 Checkstyle命名约定

Checkstyle will give you list of all the places code convention is not followed and then you can refactor. Checkstyle将为您提供所有未遵循代码约定的场所的列表,然后您可以进行重构。

In Eclipse Neon, I do the following to rename a variable across the project: 在Eclipse Neon中,我执行以下操作来在项目中重命名变量:

  1. Go to Search->File... 转到Search->File...
  2. Enter the name of the variable in Containing text: Containing text:输入变量的名称Containing text:
  3. Click Replace... which is located at the bottom of the dialog box. 单击位于对话框底部的“ Replace... ”。
  4. A new dialog box opens. 将打开一个新对话框。 There, put the variable name you want to replace with in With... labelled text box. 在此处,将要替换为的变量名称放在With...文本框中。
  5. To preview, where the changes will be reflected, click on Preview > 要预览更改的位置,请点击Preview >
  6. Once, you are sure, click OK . 确定之后,单击OK

Hope this helps! 希望这可以帮助!

Quoting from eclipse docs for Rename refactoring:- 引用Eclipse文档中的重命名重构:-

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 快捷键: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 快捷键:Alt + Shift + C

Refactor within a file: Use Alt+Shift+R 在文件中重构:使用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). 在多个文件中重构:使用Alt + Shift + R两次(为简单起见,请按住Alt + Shift并按R两次)。

This behaviour works with Eclipse Oxygen, Windows. 此行为适用于Windows的Eclipse Oxygen。
Version: Oxygen.3a Release (4.7.3a) 版本:Oxygen.3a版本(4.7.3a)
Build id: 20180405-1200 Build ID:20180405-1200

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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