Fix J2KT build error for @Memoized methods returning reference types.#2045
Merged
copybara-service[bot] merged 1 commit intomainfrom Apr 27, 2026
Merged
Fix J2KT build error for @Memoized methods returning reference types.#2045copybara-service[bot] merged 1 commit intomainfrom
copybara-service[bot] merged 1 commit intomainfrom
Conversation
J2KT requires non-nullable Kotlin properties to be initialized. For @memoized methods returning reference types, AutoValue generates fields without initialization, relying on Java's default null initialization. If these fields are not marked @nullable in Java, J2KT generates non-nullable Kotlin properties, leading to compilation errors like "property must be initialized or be abstract". This change ensures that the generated cache fields in Java are marked with @nullable if they are non-primitive types, prompting J2KT to generate nullable Kotlin properties which do not require immediate initialization. We avoid adding @nullable to primitive types (like the boolean flag used in CheckBooleanField) because primitives cannot be annotated with @nullable in Java, and doing so would cause compilation errors. RELNOTES=n/a PiperOrigin-RevId: 906324290
99d1f9a to
2f36604
Compare
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.
Fix J2KT build error for @memoized methods returning reference types.
J2KT requires non-nullable Kotlin properties to be initialized. For @memoized methods returning reference types, AutoValue generates fields without initialization, relying on Java's default null initialization. If these fields are not marked @nullable in Java, J2KT generates non-nullable Kotlin properties, leading to compilation errors like "property must be initialized or be abstract".
This change ensures that the generated cache fields in Java are marked with @nullable if they are non-primitive types, prompting J2KT to generate nullable Kotlin properties which do not require immediate initialization.
We avoid adding @nullable to primitive types (like the boolean flag used in CheckBooleanField) because primitives cannot be annotated with @nullable in Java, and doing so would cause compilation errors.
RELNOTES=n/a