Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Fix boxing and unboxing of annotated primitives #986

Merged
merged 4 commits into from
Oct 30, 2023

Conversation

invliD
Copy link
Contributor

@invliD invliD commented Oct 5, 2023

TypeName#box currently relies on identity comparisons between this and one of the primitive TypeName constants. However, it is possible to have an instance of a primitive that isn't any of those constants by calling TypeName#annotated or TypeName#withoutAnnotations. The returned TypeName instance can no longer be boxed.

TypeName#unbox and TypeName#isBoxedPrimitive have a similar issue. this is compared using equals instead of identity, but annotations are part of the compared string, so a boxed primitive with annotations is not found to be any of the existing constants. Annotated boxed TypeName instances can no longer be unboxed.

This PR changes behavior of all 3 methods to do a more appropriate comparison when trying to find the (un)boxed version of this. After (un)boxing, the annotations from this are copied onto the returned value. That way, boxing and unboxing works on both unannotated and annotated TypeNames, preserving any annotations. When no annotations are present, identity is preserved.
I added unit tests to test both boxing and unboxing. Those tests fail without the code changes in TypeName.

@invliD
Copy link
Contributor Author

invliD commented Oct 10, 2023

@JakeWharton Would you be able to take a look at this?

src/main/java/com/squareup/javapoet/TypeName.java Outdated Show resolved Hide resolved
src/main/java/com/squareup/javapoet/TypeName.java Outdated Show resolved Hide resolved
@JakeWharton JakeWharton enabled auto-merge (squash) October 30, 2023 16:40
@JakeWharton JakeWharton merged commit d682f20 into square:master Oct 30, 2023
1 check passed
@invliD invliD deleted the annotated-primitives branch October 30, 2023 18:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants