Add nullAwayOptions to configure arbitrary NullAway options - #62
Merged
Conversation
The items of maven-compiler-plugin's compilerArgs are a plain list, so any element name is accepted and <compilerArg> is commonly used instead of <arg>. The plugin only inspected the children named "arg", so an ErrorProne argument declared as <compilerArg> was not found and a second -Xplugin:ErrorProne argument was added, which makes javac fail with "plug-in not found: ErrorProne". Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
NullAway has many options that are not exposed as dedicated parameters, and
adding -XepOpt:NullAway:... to maven-compiler-plugin by hand does not work:
javac rejects it as an invalid flag unless it is part of the -Xplugin:ErrorProne
argument.
Options can now be set by name, without the -XepOpt:NullAway: prefix:
<configuration>
<nullAwayOptions>
<KnownInitializers>com.example.SomeClass.init</KnownInitializers>
</nullAwayOptions>
</configuration>
or as nullability.nullAwayOptions.<name> Maven properties. An entry overrides
the option of the same name derived from the other parameters, so no duplicated
option is emitted. Option names and values must not contain whitespace because
they are appended to a single -Xplugin:ErrorProne argument.
Closes gh-55
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes gh-55
Why
NullAway has many options that this plugin does not expose as dedicated parameters, and adding them by hand does not work:
<arg>-XepOpt:NullAway:KnownInitializers=...</arg>inmaven-compiler-plugin→error: invalid flag, because the option is only valid inside the-Xplugin:ErrorProneargument.<compilerArg>-Xplugin:ErrorProne -XepOpt:NullAway:KnownInitializers=...</compilerArg>→plug-in not found: ErrorProne(see the second commit).Exposing every NullAway option as a parameter is not realistic, so an escape hatch is added.
What
1. Detect existing compiler arguments regardless of their element name
The items of
compilerArgsare a plain list, somaven-compiler-pluginaccepts any element name and<compilerArg>is commonly used instead of<arg>. The plugin only inspected children namedarg, so an existing-Xplugin:ErrorProneargument declared with another element name was not found and a second-Xplugin:ErrorProneargument was added, which makes javac fail withplug-in not found: ErrorProne. This is the error reported in the issue, and it is reproduced by the newerrorprone-arg-element-nameIT.2.
nullAwayOptionsis passed to the compiler as
-XepOpt:NullAway:KnownInitializers=com.example.api.SomeClass.init -XepOpt:NullAway:TreatGeneratedAsUnannotated=true, for both main and test compilation.The map form is used instead of the
<nullAwayOption>Key=Value</nullAwayOption>list proposed in the issue: the option name is the key, so no string parsing is needed, the values may contain commas (KnownInitializerstakes a comma-separated list), and an entry can override the option of the same name that the plugin derives from the other parameters instead of emitting the option twice.The options can also be set as
nullability.nullAwayOptions.<name>Maven properties, keeping the parity with the other parameters. A<configuration>entry wins over the property of the same option name.Option names and values must not contain whitespace, because all options are appended to a single
-Xplugin:ErrorProneargument; the build fails with an explicit message otherwise.Tests
NullabilityLifecycleParticipantTest(new): parsing from<configuration>and from properties, precedence, trimming, validation errors.NullAwayArgsBuilderTest,NullabilityConfigurationTest,CompilerConfigurerTest: option emission, override of derived options, defensive copy, and the duplicated-Xplugin:ErrorProneregression.nullaway-options(the build only succeeds becauseKnownInitializersreaches NullAway -- without it the compilation fails with@NonNull field 'greeting' not initialized) anderrorprone-arg-element-name../mvnw verifypasses (15 ITs).README is updated with a "Setting arbitrary NullAway options" section.
🤖 Generated with Claude Code