Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning public items into private items - Java, IntelliJ

I just wonder if there is a way to make public items (fields, methods) private with a simple button or something

public Group wQ;
public Group wA1;
public Group wA2;
public Group wA3;
public Group wG1;
public Group wG2;
public Group wG3;
public Group wS1;
public Group wS2;
public Group wB1;
public Group wB2;

For example make all these fields private in one step, without replacing public with private. I use IntelliJ IDEA btw.

like image 294
Karatawi Avatar asked Mar 19 '16 13:03

Karatawi


People also ask

How do I refactor a method in IntelliJ?

Select a code fragment you want to extract to a method. Press Ctrl+Alt+M or from the main menu, select Refactor | Extract | Method. In the dialog that opens, configure a method options, such as visibility, parameters, and so on. You can also change a name of the method if you need.


2 Answers

The function you are looking for is called Encapsulate fields.

  1. Right-click inside of the class you want to edit
  2. Select Refactor.
  3. Select Encapsulate Fields... screenshot
  4. Check the checkboxes next to all public fields GIF
  5. You may then configure to provide both set/get access via accessors or just get access with the checkboxes below.
  6. Select Refactor.

Note that this method generates getters (and, if you choose so, setters too) for all fields you select. If you don't want these getters, you can choose one of the other options or simply delete the generated getters. It seems like there is no way to opt out of creating getters.


Alternatively, you can use multi-line selections.

  1. Set your cursor at the end of the first public keyword.
  2. Hold down your middle mouse button.
  3. Drag the mouse down to the end of the public block.
  4. Edit the lines simultaneously by using backspace/typing the new private keyword. GIF

As a third method, you may use Find and replace. It has some nifty options which give you great control over what you are doing.

  1. Select the menu Edit.
  2. Select Find.
  3. Select Replace.

    screenshot

  4. Select the text block with the public fields.

  5. Select Regex, Match Case and In Selection.
  6. In the upper field, enter ^(\s*)public(.*)$.
  7. In the lower field, enter $1private$2.
  8. All replacements will now be highlighted. Review them whether they are correct. You can step through them with the blue arrows next to the upper text box.

    GIF

  9. Select Replace all.

like image 172
randers Avatar answered Nov 15 '22 09:11

randers


In addition to @RAnders00's answer, there's another way that lets IntelliJ do it automatically without generating setters/getters whilst also checking if you would break code:

Rightclick on the class or package → Analyze → Run Inspection by Name → Declaration access can be weaker

IntelliJ will search your class for members that can have a reduced visiblity based on usage. After scanning you can have it updated automatically.

like image 44
Benny Bottema Avatar answered Nov 15 '22 10:11

Benny Bottema