Writing proper code in your IDE is made easier when using IntelliJ, due to its’ code analysis features. One option to perform a comprehensive coding issue search is to use the Inspect Code feature IntelliJ provides. From the class you want to check for issues, right click (inside the class, in the editor) and choose Analyze –> Inspect Code. Here you have the option to define the inspection scope (where to search for potentially wrong code). You can either choose the file from where you opened the menu, the whole project, or you can choose from a list of predefined scopes (which might not be very useful for you, as they comprise of items like – production or test files). You can also define your own custom scope (where you can add packages or classes you need to be inspected). To do that, in the menu that appears, choose ‘Custom scope’ and click on the ‘…’ button you see next to this option.
In the popup that appears, you can either select an already defined scope, or you can create a new one by clicking the ‘+’ icon at the top.
You can then choose the option ‘Local’, and you will be prompted for a name for your scope. After providing it, in the same window you will be able to add or remove files and packages from your scope (by using the self-describing buttons on the right side).
After you are done with the scope editing, just click it to finish selecting it. Then, in the initial Inspect Code popup, you must choose the profile for running the inspections. A default profile is selected at this time, which is a very comprehensive one.
You can always add some of the items you are interested in, that are not already checked, or remove some that you might not find useful. Once you are finished setting up the scope and profile, just run the inspection.
If you went through the profile configuration step, you probably noticed that some of the inspection categories had also a colored square attached to them. They represent the severity attached to each issue. The benefit of setting up the severity for a particular issue is given by the fast detection of the issue – these are highlighted on the fly, while you are writing the code. Depending on the severity, you either get a colored hint that something is incorrect directly on the code (for example errors have a wave like line displayed in red beneath the text, and also a red square on the right margin of the class editor) or just on the right margin of the class editor (a yellow square, for warnings).
When the search is done, in the lower section of the editor, an ‘Inspection‘ tab is displayed. Within it, on the left side you will see a list of potential issues IntelliJ is suggesting for you to fix.
You can apply the suggested fix for an item, by selecting the item in the left side area, then switching to the right side and choosing the solution you find most suitable in the ‘Problem resolution‘ section. You can fix several issues of the same type with just one move, by performing a multiple selection of the issues in the left hand side (by holding the Ctrl button while selecting the items with your mouse), then right clicking on the selection and choosing the desired fix from the suggestions.
In case you don’t want to change anything regarding the code IntelliJ considers broke, you can choose to ‘Suppress’ the warning. This option will add an annotation to the method or attribute you chose to ignore, meaning that the next time an inspection is run, the annotated component will be excluded from the inspection and will not be suggested as incorrect. Some of the long list of potential issues are described below (currently the tools supports over 600 types of inspections):
– unused code
– unused imports
– overly complicated expressions (like multi-nested if’s)
– improper use of static members
– members that can be declared final
– unused parameters
For the full list of inspections, apart from exploring them within the IDE, you can visit the developer’s page.