Preserve type annotations better after type rewriting. #1704
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.
Preserve type annotations better after type rewriting.
Sometimes we rewrite types, for example to see that the return type of
T foo()
inBar<T>
is actuallyString
inclass Baz implements Bar<String>
. Unfortunately rewriting types using thejavax.lang.model
API leads to the loss of any type annotations they originally had. If we had@Nullable T foo()
it would become justString foo()
rather than@Nullable String foo()
. So we introduce a new classAnnotatedTypeMirror
that tracks both the original type (with its annotations) and the rewritten type (without them), so we can combine the two when writing types into the generated code.This is not a fully general solution. We will indeed reconstruct
@Nullable String
in the example, but we will still lose the annotation when rewritingList<@Nullable T>
asList<String>
. I think the only way around that is to forgo the use ofTypes.asMemberOf
and instead substitute type variables ourselves. That's similar to but more general than what we already do inTypeVariables.substituteTypeVariables
. It is made more awkward by the lack of a way to synthesize aTypeMirror
with annotations; see https://bugs.openjdk.org/browse/JDK-8174126. (We could implementTypeMirror
and its subinterfaces ourselves, but the resulting objects would not work with thejavax.lang.model
API.)RELNOTES=Handling of type annotations on inherited methods has been improved.