Catch Problems in Proper Places
Any Java project consists of the different parts that require individual approach in locating potential problems and errors. For example, if your project contains open API, it might be interesting to pinpoint the wrong or missing JavaDoc comments. For the test roots, it would be helpful to look for the potential problems related to JUnit or TestNG. If a project contains modules written with the different JDKs, it is advisable to inspect each module for the version-specific issues.
IntelliJ IDEA suggests a way to break down a project into meaningful parts, or scopes, and apply selected inspections (or inspection profiles) purposefully to these parts.
Scopes are the logical areas, which encompass different parts of a project, from a single file or package to an entire project. There are pre-defined scopes that cover the whole project and all of its production and test sources, but you can also create your custom scopes. (We'll continue with custom scopes in the later posts...)
Combining an inspection profile with a scope helps address the problems that are specific for certain areas: every time you open a file from this scope for editing, it is highlighted according to the respective inspection profile, and you can view the profile name in the Status bar.
Let’s see how it works…
Though IntelliJ IDEA provides a default project profile, we’d rather like to have only specific inspection highlighting for the tests, and for the sources. To do that, we need to associate two pre-defined scopes with our custom profiles.
First, let’s create the two inspection profiles. There are several ways to do it, but we’ll follow the fastest one: press Ctrl+Alt+S to open the Settings dialog, click the Error button,copy an existing profile, enter new name (for example, MyTestingProfile or MySrcProfile), and check the desired inspections:
Now, it’s time to put together inspection profiles and scopes. To do that, click Project Profiles node in the Errors dialog box. This will display the table of profile-scope combinations, which we are going to fill. Click Add. Initially, the default combination is displayed:
This is not our choice – we want to narrow down the scope and make our inspection more targeted. You might already have your custom scopes created, or use the pre-defined scopes. Otherwise, you can create scopes immediately in the Errors dialog.
Click the scope column and select the desired scope from the drop-down list. In this case, it is the pre-defined Tests scope.
Next, click the inspection profile column, and select the corresponding profile:
Repeat the same procedure and combine the Production scope with MySrcProfile.
Now we can enjoy the results of our effort: open a test class in the editor, and see that the Project Default profile changes to MyTestingProfile in the Status bar, and inspection results are highlighted: