-
Notifications
You must be signed in to change notification settings - Fork 397
Fix type arguments for annotations #2848
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
Open
jonamireh
wants to merge
2
commits into
google:main
Choose a base branch
from
jonamireh:bug/2622
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // WITH_RUNTIME | ||
| // TEST PROCESSOR: AnnotationTypeArgumentsProcessor | ||
| // EXPECTED: | ||
| // ContainerTarget.nested.annotationType: MapConvert<SourceObject, DestinationObject, DestinationConverter> | ||
| // ContainerTarget.nested.typeArgCount: 3 | ||
| // ContainerTarget.nested.typeArgs: SourceObject, DestinationObject, DestinationConverter | ||
| // NestedTarget.annotationType: MapConvert<INVARIANT List<INVARIANT SourceObject>, INVARIANT Map<INVARIANT String, INVARIANT DestinationObject>, INVARIANT NestedDestinationConverter> | ||
| // NestedTarget.typeArgCount: 3 | ||
| // NestedTarget.typeArgs: kotlin.collections.List, kotlin.collections.Map, NestedDestinationConverter | ||
| // Target.annotationType: MapConvert<INVARIANT SourceObject, INVARIANT DestinationObject, INVARIANT DestinationConverter> | ||
| // Target.typeArgCount: 3 | ||
| // Target.typeArgs: SourceObject, DestinationObject, DestinationConverter | ||
| // END | ||
|
|
||
| interface Converter<S : Any, D : Any> | ||
|
|
||
| @Target(AnnotationTarget.CLASS) | ||
| @Retention(AnnotationRetention.SOURCE) | ||
| annotation class MapConvert<S : Any, D : Any, C : Converter<S, D>> | ||
|
|
||
| @Target(AnnotationTarget.CLASS) | ||
| @Retention(AnnotationRetention.SOURCE) | ||
| annotation class Container( | ||
| val nested: MapConvert<out Any, out Any, out Converter<out Any, out Any>>, | ||
| ) | ||
|
|
||
| class SourceObject | ||
| class DestinationObject | ||
| class DestinationConverter : Converter<SourceObject, DestinationObject> | ||
| class NestedDestinationConverter : Converter<List<SourceObject>, Map<String, DestinationObject>> | ||
|
|
||
| @MapConvert<SourceObject, DestinationObject, DestinationConverter> | ||
| class Target | ||
|
|
||
| @MapConvert<List<SourceObject>, Map<String, DestinationObject>, NestedDestinationConverter> | ||
| class NestedTarget | ||
|
|
||
| @Container( | ||
| nested = MapConvert<SourceObject, DestinationObject, DestinationConverter>() | ||
| ) | ||
| class ContainerTarget |
60 changes: 60 additions & 0 deletions
60
kotlin-analysis-api/testData/annotationTypeArgumentsInLibrary.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // WITH_RUNTIME | ||
| // TEST PROCESSOR: AnnotationTypeArgumentsInLibraryProcessor | ||
| // EXPECTED: | ||
| // BinaryRetentionTarget.annotationType: BinaryRetentionAnno | ||
| // BinaryRetentionTarget.typeArgCount: 0 | ||
| // RuntimeRetentionTarget.annotationType: RuntimeRetentionAnno | ||
| // RuntimeRetentionTarget.typeArgCount: 0 | ||
| // END | ||
|
|
||
| // MODULE: lib | ||
| // FILE: LibraryAnnotations.kt | ||
| package com.example | ||
|
|
||
| @Target(AnnotationTarget.CLASS) | ||
| @Retention(AnnotationRetention.SOURCE) | ||
| annotation class SourceRetentionAnno<A : Any> | ||
|
|
||
| @Target(AnnotationTarget.CLASS) | ||
| @Retention(AnnotationRetention.BINARY) | ||
| annotation class BinaryRetentionAnno<A : Any> | ||
|
|
||
| @Target(AnnotationTarget.CLASS) | ||
| @Retention(AnnotationRetention.RUNTIME) | ||
| annotation class RuntimeRetentionAnno<A : Any> | ||
|
|
||
| class SourceRetentionValue | ||
| class BinaryRetentionValue | ||
| class RuntimeRetentionValue | ||
|
|
||
| @SourceRetentionAnno<SourceRetentionValue> | ||
| class SourceRetentionTarget | ||
|
|
||
| @BinaryRetentionAnno<BinaryRetentionValue> | ||
| class BinaryRetentionTarget | ||
|
|
||
| @RuntimeRetentionAnno<RuntimeRetentionValue> | ||
| class RuntimeRetentionTarget | ||
|
|
||
| // MODULE: main(lib) | ||
| // FILE: Main.kt | ||
| package com.example.main | ||
|
|
||
| class Main |
75 changes: 75 additions & 0 deletions
75
...ain/kotlin/com/google/devtools/ksp/processor/AnnotationTypeArgumentsInLibraryProcessor.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.devtools.ksp.processor | ||
|
|
||
| import com.google.devtools.ksp.getClassDeclarationByName | ||
| import com.google.devtools.ksp.processing.Resolver | ||
| import com.google.devtools.ksp.symbol.KSAnnotated | ||
| import com.google.devtools.ksp.symbol.KSAnnotation | ||
|
|
||
| class AnnotationTypeArgumentsInLibraryProcessor : AbstractTestProcessor() { | ||
| private val results = mutableListOf<String>() | ||
|
|
||
| override fun process(resolver: Resolver): List<KSAnnotated> { | ||
| renderAnnotationIfVisible( | ||
| resolver, | ||
| className = "com.example.SourceRetentionTarget", | ||
| annotationName = "SourceRetentionAnno", | ||
| ) | ||
| renderAnnotationIfVisible( | ||
| resolver, | ||
| className = "com.example.BinaryRetentionTarget", | ||
| annotationName = "BinaryRetentionAnno", | ||
| ) | ||
| renderAnnotationIfVisible( | ||
| resolver, | ||
| className = "com.example.RuntimeRetentionTarget", | ||
| annotationName = "RuntimeRetentionAnno", | ||
| ) | ||
| return emptyList() | ||
| } | ||
|
|
||
| private fun renderAnnotationIfVisible( | ||
| resolver: Resolver, | ||
| className: String, | ||
| annotationName: String, | ||
| ) { | ||
| val declaration = resolver.getClassDeclarationByName(className) ?: return | ||
| val annotation = declaration.annotations.singleOrNull { | ||
| it.shortName.asString() == annotationName | ||
| } ?: return | ||
| results.addAll(renderAnnotation(declaration.simpleName.asString(), annotation)) | ||
| } | ||
|
|
||
| private fun renderAnnotation(label: String, annotation: KSAnnotation): List<String> { | ||
| val typeArguments = annotation.annotationType.element!!.typeArguments | ||
| return buildList { | ||
| add("$label.annotationType: ${annotation.annotationType}") | ||
| add("$label.typeArgCount: ${typeArguments.size}") | ||
| if (typeArguments.isNotEmpty()) { | ||
| add("$label.typeArgs: ${ | ||
| typeArguments.joinToString { argument -> | ||
| argument.type!!.resolve().declaration.qualifiedName!!.asString() | ||
| } | ||
| }") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun toResult(): List<String> = results.sorted() | ||
| } |
61 changes: 61 additions & 0 deletions
61
...ils/src/main/kotlin/com/google/devtools/ksp/processor/AnnotationTypeArgumentsProcessor.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.devtools.ksp.processor | ||
|
|
||
| import com.google.devtools.ksp.processing.Resolver | ||
| import com.google.devtools.ksp.symbol.KSAnnotated | ||
| import com.google.devtools.ksp.symbol.KSAnnotation | ||
| import com.google.devtools.ksp.symbol.KSDeclaration | ||
|
|
||
| class AnnotationTypeArgumentsProcessor : AbstractTestProcessor() { | ||
| private val results = mutableListOf<String>() | ||
|
|
||
| override fun process(resolver: Resolver): List<KSAnnotated> { | ||
| resolver.getSymbolsWithAnnotation("MapConvert").forEach { annotated -> | ||
| val name = (annotated as KSDeclaration).simpleName.asString() | ||
| val annotation = annotated.annotations.single { it.shortName.asString() == "MapConvert" } | ||
| results.addAll(renderAnnotation(name, annotation)) | ||
| } | ||
|
|
||
| resolver.getSymbolsWithAnnotation("Container").forEach { annotated -> | ||
| val name = (annotated as KSDeclaration).simpleName.asString() | ||
| val containerAnnotation = annotated.annotations.single { it.shortName.asString() == "Container" } | ||
| val nestedAnnotation = containerAnnotation.arguments.single().value as KSAnnotation | ||
| results.addAll(renderAnnotation("$name.nested", nestedAnnotation)) | ||
| } | ||
|
|
||
| return emptyList() | ||
| } | ||
|
|
||
| private fun renderAnnotation(label: String, annotation: KSAnnotation): List<String> { | ||
| val typeArguments = annotation.annotationType.element!!.typeArguments | ||
| return buildList { | ||
| add("$label.annotationType: ${annotation.annotationType}") | ||
| add("$label.typeArgCount: ${typeArguments.size}") | ||
| if (typeArguments.isNotEmpty()) { | ||
| add("$label.typeArgs: ${ | ||
| typeArguments.joinToString { argument -> | ||
| argument.type!!.resolve().declaration.qualifiedName!!.asString() | ||
| } | ||
| }") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun toResult(): List<String> = results.sorted() | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.