Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ/Android Studio: remove unnecessary use of 'this.'

I am working on a project where previous developers added lots of this.x code which in my opinion makes it longer and thus less readable. And it was placed all over the project by some Eclipse save action.

What I am looking for is an IntelliJ refactoring method that I can refactor all self-references on the whole project so that this:

this.something = "foo";

becomes this:

something = "foo";

Is there anything like this (I'm using Android Studio 1.2), or do I have to run a regex replace and make sure that nothing breaks?

like image 663
mreichelt Avatar asked May 05 '15 12:05

mreichelt


People also ask

How do I hide usage in IntelliJ?

In the Power Save Mode (File | Power Save Mode), highlighting of usages is disabled. If necessary, you can disable the automatic highlighting. Press Ctrl+Alt+S to open the IDE settings and select Editor | Code Editing. Clear the Usages of element at caret checkbox in the Highlight on Caret Movement section.

What is safe delete Android Studio?

With safe delete Android Studio will search if your "WebViewA" is used in another file or not, so it won't cause any error if you delete it. For example if you are using class WebViewA in MyActivity class and then you delete the WebViewA class without safe delete, there will be a new error in MyActivity class.

Is Android Studio better than IntelliJ?

Android Studio may be a better choice for businesses that develop primarily Android Applications. It is worth noting that Android Studio is based on IntelliJ IDEA, so for businesses that develop for multiple platforms, IntelliJ IDEA still offers some support for Android development in addition to other platforms.

Do I need both IntelliJ and Android Studio?

If I'm already a user of IntelliJ IDEA, do I need to switch to Android Studio for Android development? No. Android Studio is focused specifically on Android development and provides streamlined environment and project setup, but otherwise all of its features are available in IntelliJ IDEA.


1 Answers

One way which you can achieve this is by using Inspections. You can edit the inspections that are run on your code by going to Settings > Editor > Inspections. The one that you want to change is found within Java > Code Style issues and it's called "Unnecessary 'this' qualifier".

After checking and applying this inspection it will highlight areas of code which unnecessarily use the this keyword. At the moment this won't have any immediate affect on your code base and you would have to go through each instance one by one. In order to fix a batch of inspections warnings then go to Analyze > Inspect code...

From this option you can choose to run the inspection on specific files or the whole project. This will highlight any instances of code which conflict with your Inspection rules, for example this newly added rule. To apply the fix all you need to do is right click on the issue or group of issues that you wish to fix and then select Apply fix.

enter image description here

like image 73
Joel Avatar answered Sep 27 '22 19:09

Joel