Setting up Checker Framework in IntelliJ

2019-02-19

Set up Checker Framework in IntelliJ

These are step-by-step instructions for setting up Checker Framework (CF) for a basic Java project in IntelliJ. This is based on my configuration:

In these instructions, I’ll show you how to enable CF’s We’ll enable CF’s nullness checking for a simple test program.

Create IntelliJ project

From the Welcome to IntelliJ IDEA window:

  1. Click Create New Project
  2. Select Java
  3. Select 1.8 under Project SDK
  4. Click Next
  5. Check Create project from template
  6. Select Command Line App
  7. Click Next
  8. Enter a name under Project name etc.
  9. Click Finish

Create test program

Replace the content of Main.java with:

Make sure that this program compiles and runs using Ctrl+R. It should output null to the console.

Download Checker Framework dependencies

From File menu:

  1. Click Project Structure…
  2. Click Libraries
  3. Click + and select From Maven…
  4. Enter org.checkerframework and click the search button
  5. Select org.checkerframework:checker:1.9.10 and click OK twice to add this to the project
  6. Click + and select From Maven…
  7. Enter org.checkerframework and click the search button
  8. Select org.checkerframework:jdk8:1.9.10 and click OK twice to add this to the project
  9. Click + and select From Maven…
  10. Enter org.checkerframework and click the search button
  11. Select org.checkerframework:checker-qual:1.9.10 and click OK twice to add this to the project
  12. Click OK to dismiss the project structure window

Enable Checker Framework as an annotation processor

From the IntelliJ IDEA application menu:

  1. Click Preferences…
  2. Expand Build, Execution, Deployment
  3. Expand Compiler
  4. Click Annotation Processors
  5. Ensure that your module name appears under Default
  6. Select your module
  7. Check Enable annotation processing
  8. Ensure Obtain processors from project classpath radio button is selected
  9. Under Annotation Processors click +
  10. Enter org.checkerframework.checker.nullness.NullnessChecker under Processor FQ Name
  11. Click Apply
  12. From the navigation pane, click Java Compiler
  13. Under Additional command line parameters enter -Xbootclasspath/p:$USER_HOME$/.m2/repository/org/checkerframework/jdk8/1.9.10/jdk8-1.9.10.jar
  14. Click OK

Rebuild your project

  1. Press Cmd-F9 to rebuild

You should see the following error in the build output pane:

Error:(20, 33) java: [argument.type.incompatible] incompatible types in argument.
  found   : null
  required: @Initialized @NonNull String

This tells us that passing null to the constructor of Foo is invalid. Fix this line to pass a non-null string:

final Foo foo = new Foo("Hello world");

Then press Cmd-F9 again and the program should compile without error.

Tags

Java
IntelliJ
Checker Framework

Content © 2024 Richard Cook. All rights reserved.