Instrument @NotNull assertions into the compiled JVM bytecode #SCL-22085 - #152
Merged
Conversation
Add a NotNull/Nullable bytecode instrumentation feature matching what IntelliJ's JPS build and the IntelliJ Platform Gradle Plugin do: null checks are generated for @NotNull-annotated method parameters (throwing IllegalArgumentException) and return values (IllegalStateException). The instrumenter itself (NotNullVerifyingInstrumenter, AuxiliaryMethodGenerator, FailSafeClassReader, FailSafeMethodVisitor) is copied verbatim from IntelliJ IDEA Community Edition (Apache 2.0, commit 7a35a7d7fe64), with only the package, the ASM package, and Opcodes.API_VERSION -> Opcodes.ASM9 changed. It is wired into the existing manipulateBytecode flow next to the threading-annotations instrumentation and controlled by two new settings: instrumentNotNullAnnotations (default false) and notNullAnnotations (default org.jetbrains.annotations.NotNull). Co-Authored-By: Claude Fable 5 <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.
Supersedes #150.
Summary
This PR adds
@NotNull/@Nullablebytecode instrumentation, expanding the annotationinstrumentation feature introduced with the threading annotations (#SCL-18660). It provides
feature parity with IntelliJ's internal JPS build and the IntelliJ Platform Gradle Plugin's
instrumentCodetask.Two new settings control the feature:
instrumentNotNullAnnotations: SettingKey[Boolean](default:false) — generates nullchecks for annotated method parameters (throwing
IllegalArgumentException) and returnvalues (throwing
IllegalStateException), with the same bytecode and error messages asIntelliJ's own build.
notNullAnnotations: SettingKey[Seq[String]](default:Seq("org.jetbrains.annotations.NotNull")) — the annotation classes to instrument.Implementation
NotNullVerifyingInstrumenter,AuxiliaryMethodGenerator,FailSafeClassReader,FailSafeMethodVisitor) is copied verbatim from IntelliJ IDEACommunity Edition (Apache 2.0, commit
7a35a7d7fe64). The only changes are the packagename, the ASM package (
org.jetbrains.org.objectweb.asm→org.objectweb.asm), andOpcodes.API_VERSION→Opcodes.ASM9, keeping the sources easy to diff against upstream.No Ant machinery is needed — the Gradle plugin's "Ant task" is a wrapper around this
same class, which we call directly.
NotNullInstrumenter) handles what JPS's builder infrastructuredoes there:
COMPUTE_FRAMESwriter flags with common-superclass resolution against theproject classpath (
classDirectory+dependencyClasspath), plus the module-info andclass-file-version gates.
manipulateBytecodeflow next to the threading instrumentation;both instrumenters share the changed-classes detection and zinc re-stamping.
returns are skipped, as upstream does.
Tests
New
NotNullInstrumenterTest(9 tests, modeled on IDEA'sNotNullVerifyingInstrumenterTest):fixtures are compiled with
javax.toolsat test runtime, instrumented, then loaded andinvoked in a throwaway classloader — covering parameter/return checks, parameter names from
debug info, custom messages, custom annotations with custom exception classes, enum and
inner-class constructor parameter offsets, the non-null peephole, and the Kotlin skip.
Housekeeping
Also included as separate commits:
marketplace-zip-signer 0.1.43, asm 9.10.1, commons-io 2.22.0, annotations 26.1.0
actions/checkout@v7