Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeReference.asTypeName() does not copy type annotations #647

Open
efemoney opened this issue Oct 18, 2022 · 4 comments
Open

TypeReference.asTypeName() does not copy type annotations #647

efemoney opened this issue Oct 18, 2022 · 4 comments
Labels
custom code generator Related to the compiler-api module enhancement New feature or request

Comments

@efemoney
Copy link

efemoney commented Oct 18, 2022

It seems type annotations are not copied into the TypeName constructed from a TypeReference

@vRallev
Copy link
Collaborator

vRallev commented Oct 18, 2022

Can you please share a concrete example?

@efemoney
Copy link
Author

efemoney commented Oct 18, 2022

We have this top level function:

@ContributesJsonAdapter
internal fun Moshi.Builder.componentModelAdapter(
  types: @JvmSuppressWildcards Set<ComponentTypeMapping<*>>
) {
  add(ComponentModelAdapterFactory(types))
}

The issue here is that when you call ParameterReference.type().asTypeName() the resulting TypeName does not include the type annotations ie @JvmSuppressWildcards in the example above.

@vRallev vRallev added enhancement New feature or request custom code generator Related to the compiler-api module labels Jan 10, 2023
@efemoney efemoney changed the title TypeReference.requireTypeName() does not copy type annotations TypeReference.asTypeName() does not copy type annotations Jan 18, 2023
@efemoney
Copy link
Author

efemoney commented Jan 18, 2023

A fix/workaround is to manually copy the type annotations (for Psi, havent tried with Descriptor APIs)

internal fun KtAnnotated.annotationSpecs(module: ModuleDescriptor) =
  annotationEntries.map { it.toAnnotationReference(null, module).toAnnotationSpec() }
...

typeName = (it.type() as TypeReference.Psi).run {
  asTypeName()
    .copy(annotations = type.annotationSpecs(module)) // manually copy type annotations
},

@vRallev
Copy link
Collaborator

vRallev commented Jan 18, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
custom code generator Related to the compiler-api module enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants