liveBook · Manning

Static analysis

As explained in 100 Java Mistakes and How to Avoid Them

A static analysis tool is software that examines code without executing it to detect defects, policy violations, or other properties.

Static analysis examines code without running it and flags suspicious, confusing, or likely erroneous patterns. It acts like automated code review for recurring mistakes, but it does not replace human review: pattern-based checks miss many defects that a person can recognize. section 1.3

Use editor analysis for immediate feedback and whole-project or CI analysis for broader reporting. In-editor checks are convenient but typically perform limited interprocedural analysis, so changes can affect call sites that are not currently visible. section 1.3.3

Treat findings as evidence to investigate, not commands to obey:

  • Inspect the language behavior and intent before accepting a quick fix; added parentheses can preserve a wrong shift expression rather than correct it. section 2.1.1
  • Configure rules for the project rather than accepting irrelevant advice, such as a logging recommendation for a small research program. section 1.3.3
  • Suppress a justified finding at the narrowest possible scope and explain why; broad suppression can hide later, useful warnings. section 1.3.4
  • Combine analysis with code review and automated tests, because each technique catches different bug classes. section 1.8

Incorrect shift amount

In Java, shifting an int by 32 bits has no effect and is likely erroneous. IDE plugins can underline the expression, list it as a problem, and offer the rule description or controls to deactivate it. section 1.3.2

Java tooling ranges from IDE inspections and plugins such as IntelliJ IDEA and SonarLint to build-integrated Error Prone. Other options include PMD, SpotBugs, PVS-Studio, and CodeQL; their analysis depth, language coverage, and integration differ. section 1.3.1 section 1.3.2

Figure 1.5 A static analysis additional warning menu that allows you to suppress the warning, edit inspection options, or disable the inspection
figure

Use the warning menu to examine an inspection before choosing a local suppression, changing its options, or disabling it. section 1.3.2

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains tool integration, online versus offline scope, configuration, and suppression, while cautioning that pattern checks miss defects.
Examples
Uses a 32-bit int shift warning, misleading shift-parenthesis quick fix, unused debug collection, and assert-side-effect detection.
What you can do after
Apply it via IDE plugins or build integration, then configure and narrowly suppress findings with judgment.
How it connects
Connects static analysis with code review, automated testing, assertions, continuous integration, and interprocedural analysis.

Also explained in

Compare how 6 books explain static analysis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.