Как переименовать класс в intellij idea
Перейти к содержимому

Как переименовать класс в intellij idea

  • автор:

Rename dialog for a class or an interface

Use this dialog to rename a class or an interface.

In addition to renaming the class or the interface itself, IntelliJ IDEA can also look for the usages of the class or the interface name. If found, the changes you are making to the class or the interface name can also be applied to these usages.

The usages are assigned to different categories which correspond to the options which you can turn on and off.

You can set up a scope for usages of a class where the renaming method is located. For example, it might be helpful if you want to rename some popular method such as getSomething . Instead of searching through the whole project and deciding what to rename or what to leave unchanged along the way, you can limit your search to just one file, or just open files, or the recently changed files, and so on.

Rename and its usages to

Specify a new name for the class or the interface.

Search for references

If this option is on, IntelliJ IDEA will look for occurrences of the class or the interface name in class or interface references in source code files.

Whether or not a string literal is a reference is defined by the (programming) language used in a source file.

Search in comments and strings

If this checkbox is selected, IntelliJ IDEA will look for occurrences of the class or the interface name within comments and string literals in your source code files.

Search in loaded sources

If this checkbox is selected, IntelliJ IDEA will look for occurrences of the class or the interface name within downloaded object sources.

The statement to be run to rename the table or column. If necessary, you can edit the statement right in this pane.

Execute the statement and make associated changes right away.

Preview potential associated changes prior to executing the statement.

Search for text occurrences

If this option is on, IntelliJ IDEA will look for occurrences of the class or the interface name in the files that don’t contain source code. These may be the text files, properties files, HTML files, documentation files, and so on.

If this option is on, IntelliJ IDEA will look for occurrences of the class or the interface name in the names of variables. Note that only the variables that have the type of the class or the interface you are renaming will be taken into account.

To illustrate, let’s assume that you are renaming the class MyClass and there are the variables MyClass myclass , MyClass myclass20 , MyClass m and YourClass myclass25 . In such a case, IntelliJ IDEA will suggest to rename the first and the second of the variables but won’t suggest to rename the third and the fourth.

If this option is on, IntelliJ IDEA will look for occurrences of the class or the interface name in the names of its inheritors:

  • If you are renaming a class, IntelliJ IDEA will search the hierarchies of the classes that extend this class.
  • If you are renaming an interface, IntelliJ IDEA will search the hierarchies of the interfaces that extend this interface and the hierarchies of the classes that implement this interface.

If this option is on, IntelliJ IDEA will look for occurrences of the class name in the names of the test classes.

Rename bound forms

This option is available if the class you are renaming has an associated GUI form.

Select this checkbox to rename the associated form.

Use this option to set a scope for the Rename refactoring. For example, you can limit the refactoring to only recently changed files, or to only open files, and so on. You can also create a custom scope that you can either share or keep local.

Rename refactorings

Use the Rename refactoring to change names of symbols, files, directories, packages, modules and all the references to them throughout code.

Renaming local variables or private methods can be done easily inline since only the limited scope is affected. Renaming classes or public methods could potentially impact a lot of files. Preview potential changes before you refactor.

When you rename methods that are declared in interfaces, you can also review and rename their implementations as well. The refactoring changes are displayed on the Refactoring Preview tab in the Find tool window.

Rename code in place with gutter icon

The rename process using a gutter icon does not apply to Java files. Renaming in Java files is implemented with an inlay hint.

  1. In the editor start renaming a parameter, a method, or other code element. IntelliJ IDEA will display in the gutter.
  2. Click the gutter icon or press Alt+Enter and apply a suggestion. Rename inlineIntelliJ IDEA renames the code element and updates its usages accordingly. Rename refactoring result

Rename code in place with inlay hint

The inlay hint rename refactoring applies only to Java files. Other files may use a gutter icon.

  1. In the editor, find the code element you want to rename. Ij identify code element
  2. Edit the text of the code element you want to rename, IntelliJ IDEA will display the Rename usages inlay hint. Ij rename usages inlay hint
  3. Select the inlay hint. If IntelliJ IDEA determines that some variables might also be renamed, the Rename Variables dialog opens. To confirm the rename refactoring, select OK . Ij rename variables dialogIf IntelliJ IDEA doesn’t identify any variables that might also be renamed, IntelliJ IDEA immediately renames the code element and updates its usages accordingly. Ij code element renamed

Rename a directory or a module

Select refactoring popup

  1. In the Project tool window right-click a directory or a module that you want to rename.
  2. From the context menu, select Refactor | Rename ( Shift+F6 ).
  3. In the popup that opens, select what you want to rename. If you have a module with one content root, and it has the same name as your module, you can rename them both simultaneously.
  4. In the Rename dialog that opens, type the new name, specify additional options and the scope of the refactoring, and click Refactor . If you rename both module and directory at once, in the Rename dialog add the new name, check where to search for references and then click OK .

Rename a package

  1. In the Project tool window, click Project and select Packages from the list. IntelliJ IDEA lists all the packages in your project.
  2. Right-click the package you want to rename and from the context menu, select Refactor | Rename ( Shift+F6 ). If you have multiple directories that correspond to the package, or the package contains directories in the libraries that cannot be renamed, IntelliJ IDEA will display a warning. However, you can proceed with one of the offered options in the warning. IntelliJ IDEA opens the Rename dialog where you can enter the new package name and click Preview to see what directories and files will be affected before you decide to refactor. If you want to undo the changes you have made, press Ctrl+Z .

Rename a code element

  1. In the editor, select the element you want to rename. If you need to rename a file, select one in the Project tool window.
  2. Press Shift+F6 or go to Refactor | Rename in the main menu. When you invoke the rename Shift+F6 action, IntelliJ IDEA displays and next to the highlighted element. Rename variableYou can press Tab to open the context menu and select the additional rename options. Additional Rename optionsIf you want to see the Rename dialog with more options, click the More options link or press Shift+F6 .
  3. Open ( Shift+F6 ) the Rename dialog. Enter a new name of the element to enable the Preview and Refactor buttons. The Rename dialogYou can specify additional options. For example, specify where to search for element occurrences, or what else to rename. You can also specify a scope for the refactoring.
  4. Click Preview to see the potential changes or click Refactor . When you click Preview , IntelliJ IDEA opens the Find tool window with the results of found usages where you can check the results and confirm the refactoring ( Do Refactor ).

Next time you invoke the Rename refactoring, IntelliJ IDEA remembers the options you have specified inside the Rename dialog.

You cannot rename the .idea project directory since IntelliJ IDEA always reads the project files from the directory with that exact name.

Examples

Let’s rename a class MyClass .

Как в IDEA переименовать пересекающиеся по названию пакеты?

Несмотря на то, что на первый взгляд эти пакеты являются разными (потому что разнесены по разным модулям), по факту они являются одним пакетом. Это создаёт проблемы. Например, если я попытаюсь переименовать пакет ru.site.my.source.super в Module A , то такой же пакет переименуется и в остальных модулях, потому что это и есть тот же самый пакет. Поэтому я сейчас пытаюсь разграничить пакеты в разных модулях, получив на выходе такой результат:

Module A: ru.site.my.source.one.* Module B: ru.site.my.source.two.* Module C: ru.site.my.source.three.* 

Как я могу это сделать? Проблема в том, что это очень сложно: я не могу просто переименовать пакет в одном модуле без того, чтобы он не переименовался бы в другом. По сути приходится вручную пересоздавать структуру пакетов и вручную перетаскивать каждый класс. Есть ли в IDEA какие-либо инструменты, которые позволят мне ускорить данный процесс?

Как переименовать класс в intellij idea

Marit van Dijk

Rename your classes, methods, and variables using Refactor | Rename. Press ⇧F6 (macOS) / Shift+F6 (Windows/Linux), type the new name and press ⏎ (macOS) / Enter (Windows/Linux).

  • (video) IntelliJ IDEA. Everyday Refactorings
  • (documentation) IntelliJ IDEA Help — Rename refactorings
  • (documentation) IntelliJ IDEA Help — Code refactoring

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *