As developers, we all want to refactor our codes or make them follow a certain coding standard or pattern. You can have your own personal preference for naming methods, classes, instance fields etc or follow your organisation’s own standard.
In this post, I will be taking about an interesting feature in Android Studio, which is Structural Search and Replace(SSR). I will share what I learnt from Philippe Breault in “Android Studio Like a Boss” talk.
So what is Structural Search and Replace(SSR)?
Structural Search and Replace (SSR) performs search and replace in your code across the whole project, it takes awareness of the syntax and code structure of the Java code. It searches for a particular pattern or dramatical constructs in your code such as an array assignment, a loop, a method with a particular signature and replaces that with some other pattern. It does not just search and replace, it can inspect your code and find patterns in them by highlighting the ones that match the specified pattern. SSR is very vital for you when you want to modify or refactor a large code base.
An example can be variables with Hungarian Notation(HN). Some people are in support of while others are not. Reasons, why someone might not support it can be that it makes code hard to read, makes it hard to change the name of the variable etc while those who support it says it gives information about a variable when a prefix is added to its name. According to Robert C. Martin in Clean Code(I will recommend any developer to read this if you haven’t), he is against HN due to reasons such that the more we read the code, the less we see the prefixes which eventually the prefixes become unseen. The debate about using HN or not can go on and on. Do you agree with him or are you a pro-HN or anti-HN? 🙂
Example of a class with HN instance fields
Example of a class without HN instance fields
Android Studio Structural Search
You might think you can use CTRL + SHIFT + F shortcut to search the whole project but there is a better way to do such by using Structural Search. So let me introduce you to this cool feature called Structural Search. Structural Search is a different kind of search in Android Studio because it is smarter, it understands the structure of Java, it understands what an anonymous class is, fields of a class, inner classes and an if-else statement, enums etc.
Steps in using Structural Search in Android Studio:
- Fire up your Android Studio and on the main menu, choose Edit -> Find -> Search Structurally or just simply use CTRL + SHIFT + A to open action panel and type “Search Structurally” and click it.
- Click on Copy existing template… button, you will see a lot of search templates. It covers a lot of use cases, such as in for-loop, class fields etc. You might not really see what you want but you can find the one that is closely related to what you want to do and tweak it. In our example, we want to find the fields that start with letter m. So under class-based, select all fields of the class and click OK.
- if you click Ok, it will copy the selected template and paste it on to the Search Template field in step 1. In the Scope drop-down menu, select Project Files to search the whole project files and select File Type as Java(you can search for other file types such HTML, Groovy etc ), then click Find to search for the files in your project with the given template. But that is not what we want to do, instead we want to search for variable names with Hungarian Notation(HN). Let’s see how to do that on the next step.
- Now click on Edit Variables… button and select the FieldName, enter a constraint in the Text/regexp field but in our case, we gonna use “m[A-Z].*” (you can use this cool online tool to learn or practice your regex).Click Ok, it will take you back to the previous dialog then click on Find to begin the search. On your find panel below, it will display only the fields that begin with “m” and highlight the matched ones, this makes Structural Search very useful than the usual find feature we all know.
ANDROID STUDIO STRUCTURAL SEARCH inspection
Let say you have removed(if you are anti-HN) the HN in the fields of the classes, you can prevent new developers from creating such again by using the Structural Search inspection in the settings of Android Studio. Let’s see how that is done.
- Go to File -> Settings -> Editor -> Inspections -> General -> Structural Search Inspection or quickly just type the CTRL + SHIFT + A to fire up the action panel and type settings and click on it. Check the check box beside Structural Search Inspection to make sure it is activated.
- You can change the severity if you want from Warning(yellow) by default to either Server problem(orange), info(green), Error(red) by selecting it from the Severity drop-down menu and also change the scope.
- Click the green “+” and select Add Search template…, it opens up the last Structural Search Template you configured. Click OK and enter the template name and click OK to save the template name.
- Then click OK or Apply in the settings panel. Once activated, it will be running an inspection on your code all the time and flag your code if something matches.
When you have a variable with the Hungarian Notation(HN), it gets highlighted by Android Studio. If you mouse over it, you will see the instruction or template name. Note that If you are using version control, you have a new or edited file – profiles_settings.xml , so any developer that checks out the code will also have it.
ANDROID STUDIO STRUCTURAL replace
Now let’s look at Structural Replace, It finds and replaces fragments of your source code, using the search templates you specified. Let say instead of using the default Log class, you want to use Timber.
- First thing, let’s create a template to structurally search for statements that use the default Log. So open up Structural Search and insert this template. Make sure the methods in Log class are replaced with $MethodName$ and the message parameter is $variable$ in the template. You can view templates by clicking on Copy existing template... and see how others are and modify if you want. Click on Find to search, you can search all files in your project, the current file etc in the Scope drop-down list. If you want to use this template in the future, you can save the template by clicking on the Save template… button.
- So now let us replace our Log statements with Timber instead. After step 1, you can fire up Replace Structurally either by visiting Edit -> Find -> Replace Structurally or CTRL + SHIFT + A and just type “Replace Structurally”. Android Studio knows the last Structural Search so it will insert it for you already or if you have saved the template, you can get it by clicking on Copy existing template… -> user defined then select your saved template.
- Replace the template in the Replacement template field with our Timber log template instead. Make sure you uncheck Use static import if possible so that this will not remove the class Timber and just insert the method names only. Click Find.
- You can now click Preview Replacement to view how it will be when replaced, Replace Selected to replace the currently selected statement or just click Replace All to replace everything.
ANDROID STUDIO STRUCTURAL REPLACE INSPECTION
Another cool thing you can do is structural replace inspection. Just like what we did for structural search inspection. So let’s do that now
- Go to File Menu -> Settings -> Editor -> General -> Structural Search Inspection and click on the green “+” button, select Add Replace template…
- Enter the template name which can be “Use Timber instead of Log” and click OK.
- Your new template name will be displayed. If you are satisfied will all the given settings you have specified, click the final OK or Apply for it to take effect.
When you mouse over any Log statement which is highlighted in your project, it will show a popup with the reason for the warning which you stated already in Step 2.
Another awesome thing again is that if you press ALT + Enter on a highlighted statement, we get the option to replace the statement with our Timber template we defined.
Hope you learnt something from this lesson about SSR in Android Studio, The best way to really know more about this awesome feature is to play around with them especially with the existing templates or create your own.
Thanks for reading. If you like this post, please feel free to recommend this to other people. kindly share this or share your thoughts in the comment section below.
References and further reading