Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add this. to member variables/methods in IntelliJ Idea

Tags:

Is there a way in IntelliJ Idea to force this. to be automatically placed into code on a format or save or at the very least show an error if it should be in there?

like image 774
Dan King Avatar asked Nov 08 '13 14:11

Dan King


People also ask

How do I create a method description in IntelliJ?

From the main menu, select Tools | Generate JavaDoc. In the dialog that opens, select a scope — a set of files or directories for which you want to generate the reference, and set the output directory where the generated documentation will be placed.

How do I set variables in IntelliJ?

Click edit configurations on the dropdown box for your runtime configurations. Select the runtime you want to add environment variables to. Click environment variables folder icon to pull up the environment variables screen. From here you can add as many environment variables as you want.

How do I show methods in IntelliJ?

By default, IntelliJ IDEA shows all classes, methods, and other elements of the current file. To toggle the elements you want to show, click the corresponding buttons on the Structure tool window toolbar.


2 Answers

You can turn on the following inspections:

  • File > Settings > [Project Settings] > Inspections > Code Style Issues >
    • "Instance method call not qualified with 'this'"
    • "Instance fields access not qualified with 'this'"

That will highlight any missing 'this.' declarations. With your cursor on the highlight, Alt+Enter will bring up the quick fix to add 'this". If on that initial pop-up you arrow to the right, You can select the "Fix All "Instance Method call not qualified with 'this'" problems" option to fix all issues in the current file. (you'll need to do this for both methods and fields)

If you run the inspection on the project - either by the Analyze > Inspect Code... or by the Analyze > Run Inspection by Name... option you can then apply the fix to all files in the project (or module or package, etc.)

There is no way to get the quick fix to occur on a reformat.

like image 184
Javaru Avatar answered Sep 20 '22 19:09

Javaru


You can add the 'this.' qualifier to fields by using the 'Save Actions' Plugin (which I also recommend to use for other reasons). Just install it via Settings > Plugins > Browse repositories and be sure to check the quick fix 'Qualify field access with this' later.

like image 21
Clemens Liebich Avatar answered Sep 19 '22 19:09

Clemens Liebich