javachecker-framework

How to exclude 3rd party classes when using the NullnessChecker from the checker framework?


I am trying to use the NullnessChecker from the checker framework. I have a java project building it with gradle and kotlin.

The idea is to have as little boiler plate coding as I can therefore having everything by default as nonnull and then override where nulls are required (for parameters, return arguments and instance variables).

I have the following config in my build.gradle.kts file:

import org.checkerframework.gradle.plugin.CheckerFrameworkExtension

plugins {
    id("java")
    id("org.checkerframework") version "0.6.35"
}

configure<CheckerFrameworkExtension> {
    excludeTests = false
    checkers = listOf("org.checkerframework.checker.nullness.NullnessChecker")
}

This works well and if I pass a null as a parameter the build fails and where needed I can add the @Nullable annotation to skipp the check.

I however have calls to 3rd party classes like:

// Where I pass a null argument as the 2nd parameter
javax.xml.transform.TransformerFactory.newInstance("...", null); 

It is not my code hence I cannot add the @Nullable annotation to it and the checkerframework fails indicating that I cannot pass a null here.

Is there a way to get around this or apply theNullnessChecker only to my own package?


Solution

  • It is not my code hence I cannot add the @Nullable annotation to it

    You can write annotations for third-party libraries in a stub file. The Checker Framework manual says:

    "Write annotations in a “stub file”, if you do not have access to the source code."

    Is there a way to apply the Nullness Checker only to my own package?

    You can use the -AskipUses command-line argument. The Checker Framework manual says:

    "Set the -AskipUses command-line option to a regular expression that matches fully-qualified class names (not file names) for which warnings and errors should be suppressed. Or, set the -AonlyUses command-line option to a regular expression that matches fully-qualified class names (not file names) for which warnings and errors should be emitted; warnings about uses of all other classes will be suppressed."

    The Checker Framework also supports -AskipDefs and -AonlyDefs.