Java Eclipse 如何重构变量名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29387945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:54:58  来源:igfitidea点击:

Eclipse how to refactor variable names?

javaeclipsepluginsrefactoring

提问by Ben

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 :)

如果您对可以做到这一点的插件有任何想法,我会很高兴听到它:)

回答by Luigi Cortese

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.

不需要插件,右键单击变量名称,然后“重构”->“重命名”。但是,之前没有尝试过,不知道在整个项目中它的可靠性如何。

回答by MrDumb

You can Also Press Alt+Shift+R in Eclipse to refactor on variable.

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

回答by Yantraguru

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 会给你列出所有没有遵循代码约定的地方,然后你可以重构。

回答by Mustafa sabir

Quoting from eclipse docsfor 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

回答by Vivek Singh

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

如果您在 windows/ubuntu 平台上使用alt+shift+R或在 mac 上使用command+option+r。它将重构使用变量的所有事件。

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

要获取 eclipse 中可用的快捷方式列表,请ctrl+shift+L在 windows/ubuntu 和command+shift+lmac 中使用。

回答by kiner_shah

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

在 Eclipse Neon 中,我执行以下操作以在整个项目中重命名变量:

  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.
  1. Search->File...
  2. 输入变量名 Containing text:
  3. 单击Replace...位于对话框底部的 。
  4. 将打开一个新对话框。在那里,将要替换的变量名称放入带With...标签的文本框中。
  5. 要预览更改将反映的位置,请单击 Preview >
  6. 一旦确定,请单击OK

Hope this helps!

希望这可以帮助!

回答by Shashi

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.
Version: Oxygen.3a Release (4.7.3a)
Build id: 20180405-1200

此行为适用于 Eclipse Oxygen、Windows。
版本:Oxygen.3a 发布 (4.7.3a) 版本号
:20180405-1200