---
title: "Retrofitting null-safety onto Java at Meta"
description: "Nullsafe is a static analyzer built at Meta to catch NullPointerExceptions in Java, implemented as an extra compiler pass via the JSR-199 compiler API."
url: "https://swe.quest/articles/retrofitting-null-safety-onto-java-at-meta-9ef65f9615ca03e4"
canonical: "https://engineering.fb.com/2022/11/22/developer-tools/meta-java-nullsafe/"
source: "Meta Engineering"
published: "2022-11-22"
difficulty: "medium"
kind: "Case study"
reading_minutes: 15
tags: ["static-analysis","jvm","type-systems","mobile"]
---

# Retrofitting null-safety onto Java at Meta

A post on [Meta Engineering](https://swe.quest/sources/meta.md), published 2022-11-22, 15 min read, Case study. Original: [https://engineering.fb.com/2022/11/22/developer-tools/meta-java-nullsafe/](https://engineering.fb.com/2022/11/22/developer-tools/meta-java-nullsafe/)

## Summary

Nullsafe is a static analyzer built at Meta to catch NullPointerExceptions in Java, implemented as an extra compiler pass via the JSR-199 compiler API. It runs flow-sensitive nullness type inference over a control flow graph, then type checks the AST against those results, with added support for nullness in generics despite complications from Java's target typing. A three-tier model (Nullsafe-compliant, optimistically checked first-party code, pessimistically checked third-party code) allowed gradual adoption across millions of lines of legacy code. On Instagram, production NPE crashes fell 27% over 18 months of code transformation.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Static analysis](https://swe.quest/tags/static-analysis.md) (Engineering practices)
- [Java & JVM](https://swe.quest/tags/jvm.md) (Languages)
- [Type systems](https://swe.quest/tags/type-systems.md) (Compilers & runtimes)
- [Mobile](https://swe.quest/tags/mobile.md) (Web & mobile)

## Prerequisites

- Java generics ([easy posts on Java & JVM](https://swe.quest/tags/jvm.md?difficulty=easy))
- nullable annotations
- control flow graphs
- static type checking

## Keywords

Nullsafe, Java, Infer, Kotlin, JSR-199, NullPointerException, Instagram, Dataflow library

## Related posts

- [Translating Java to Kotlin at Scale](https://swe.quest/articles/translating-java-to-kotlin-at-scale-3648a1737157b36f.md): Meta Engineering · 2024-12-18 · Medium · 13 min read · Java & JVM, Refactoring & migrations, Mobile, Static analysis, Developer tools
  Meta is translating roughly ten million lines of Android Java to Kotlin using the Kotlinator, an automated pipeline built around a headless version of JetBrains' J2K.
- [How we migrated our static analyzer from Java to Rust](https://swe.quest/articles/how-we-migrated-our-static-analyzer-from-java-to-rust-8d2e3080358d7827.md): Datadog Engineering · 2024-05-23 · Medium · 8 min read · Rust, Static analysis, Refactoring & migrations, Performance engineering, Java & JVM
  Datadog rewrote the Codiga static analyzer from Java to Rust after switching its parser from ANTLR to Tree-sitter, whose Java bindings lacked features like pattern matching.
- [Open-sourcing Mariana Trench: Analyzing Android and Java app security in depth](https://swe.quest/articles/open-sourcing-mariana-trench-analyzing-android-and-java-app-security-in-depth-6f84a56369fe533f.md): Meta Engineering · 2021-09-29 · Easy · 6 min read · Static analysis, Application security, Mobile, Java & JVM, Open source
  Mariana Trench is Meta's open-sourced static taint-analysis tool for Android and Java apps that finds security and privacy bugs by tracing data flows from sources (e.g., Intent.getData) to sinks (e.g., Log.w, Runtime.exec).
- [An open source compositional deadlock detector for Android Java](https://swe.quest/articles/an-open-source-compositional-deadlock-detector-for-android-java-baaa0219c7853320.md): Meta Engineering · 2022-03-08 · Hard · 2 min read · Static analysis, Concurrency, Java & JVM, Mobile
  Meta built a compositional static analyzer, now part of the open source Infer framework, that detects deadlocks in Android Java code without running it.
- [Hakana: Taking Hack Seriously](https://swe.quest/articles/hakana-taking-hack-seriously-564898cdb11987b5.md): Slack Engineering · 2023-02-08 · Medium · 5 min read · Static analysis, PHP, Application security, Rust, Open source
  Slack has open-sourced Hakana, a Rust-based type checker and static analyzer for Hack, built on Psalm's design and reusing the Hack parser bundled with HHVM.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
