Qodana 2023.2 Help

Structural search

Structural search is a powerful and flexible mechanism built into JetBrains IDEs. It employs search templates to let you inspect the codebase taking into account its semantics. You can create your own search templates and use them as Qodana inspections.

How it works

As an example, consider this search template:

$Instance$.$MethodCall$($Parameter$)

It lets you detect the System.out.println() Java statement. To make it detect the statements with hardcoded text like System.out.println("Hardcoded text!"), the template should take the modifiers described below:

Variable

Value

Explanation

$Instance$

[0,1]

Accepts zero or one value.

$MethodCall$

text=println

Searching for the println method.

$Parameter$

text=^\".*\"$

Any text enclosed in quote characters.

This procedure explains how to use this search template for inspecting your codebase using Qodana.

  1. Using the Structural Search dialog of IntelliJ IDEA, create the template:

    Search template
  2. Convert the template to inspection as described on the Work with structural search and replace page of the IntelliJ IDEA documentation portal. For example, it can be saved as Hardcoded-print.

  3. Use Settings | Editor | Inspections to enable the inspection in the Structural search group of inspections. For more details, see the Configure profiles page.

    Enabling inspection
  4. In the Inspections section of the Settings dialog, export the updated profile.

  5. In the project root directory, create the qodana.yaml file and specify the path to the profile location in it:

    profile: path: path/to/the/profile/file
  6. Inspect your code using Qodana:

    docker run --rm -p 8080:8080 \ -v <project-directory>:/data/project/ \ -v <report-directory>:/data/results/ \ jetbrains/qodana-jvm --show-report
  7. You can observe the problems detected by the structural search inspection under the Structural search category in the Qodana report.

    Observing the Structural Search inspection results
Last modified: 31 May 2023