-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ksp.map.annotation.arguments.in.java
which defaults to false. When enabled, annotation arguments are mapped to Kotlin types in Java source files. (cherry picked from commit a9f2244)
- Loading branch information
Showing
8 changed files
with
139 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
40 changes: 40 additions & 0 deletions
40
integration-tests/src/test/kotlin/com/google/devtools/ksp/test/MapAnnotationArgumentsIT.kt
This file contains 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,40 @@ | ||
package com.google.devtools.ksp.test | ||
|
||
import org.gradle.testkit.runner.GradleRunner | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import org.junit.Assert | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class MapAnnotationArgumentsIT { | ||
@Rule | ||
@JvmField | ||
val project: TemporaryTestProject = TemporaryTestProject("map-annotation-arguments", "test-processor") | ||
|
||
val expectedErrors = listOf( | ||
"e: [ksp] unboxedChar: Char != Character\n", | ||
"e: [ksp] boxedChar: (Char..Char?) != Character\n", | ||
"e: Error occurred in KSP, check log for detail\n", | ||
) | ||
|
||
@Test | ||
fun testMapAnnotationArguments() { | ||
val gradleRunner = GradleRunner.create().withProjectDir(project.root) | ||
|
||
gradleRunner.withArguments("assemble", "-Pksp.map.annotation.arguments.in.java=true").build().let { result -> | ||
Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":workload:kspKotlin")?.outcome) | ||
Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":workload:assemble")?.outcome) | ||
} | ||
|
||
gradleRunner.withArguments("clean", "assemble", "--rerun-tasks").buildAndFail().let { result -> | ||
Assert.assertEquals(TaskOutcome.FAILED, result.task(":workload:kspKotlin")?.outcome) | ||
Assert.assertTrue(expectedErrors.all { it in result.output }) | ||
} | ||
|
||
gradleRunner.withArguments("clean", "assemble", "-Pksp.map.annotation.arguments.in.java=false", "--rerun-tasks") | ||
.buildAndFail().let { result -> | ||
Assert.assertEquals(TaskOutcome.FAILED, result.task(":workload:kspKotlin")?.outcome) | ||
Assert.assertTrue(expectedErrors.all { it in result.output }) | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...c/test/resources/map-annotation-arguments/test-processor/src/main/kotlin/TestProcessor.kt
This file contains 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,36 @@ | ||
import com.google.devtools.ksp.getClassDeclarationByName | ||
import com.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.* | ||
|
||
class TestProcessor( | ||
val codeGenerator: CodeGenerator, | ||
val logger: KSPLogger | ||
) : SymbolProcessor { | ||
val expected = mapOf( | ||
"unboxedChar" to "Char", | ||
"boxedChar" to "(Char..Char?)", | ||
) | ||
|
||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
val j = resolver.getClassDeclarationByName("com.example.AnnotationTest")!! | ||
j.annotations.forEach { annotation -> | ||
annotation.arguments.forEach { | ||
val key = it.name?.asString() | ||
val value = it.value.toString() | ||
if (expected[key] != value) { | ||
logger.error("$key: ${expected[key]} != $value") | ||
} | ||
} | ||
} | ||
|
||
return emptyList() | ||
} | ||
} | ||
|
||
class TestProcessorProvider : SymbolProcessorProvider { | ||
override fun create( | ||
environment: SymbolProcessorEnvironment | ||
): SymbolProcessor { | ||
return TestProcessor(environment.codeGenerator, environment.logger) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...resources/map-annotation-arguments/workload/src/main/java/com/example/AnnotationTest.java
This file contains 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,8 @@ | ||
package com.example; | ||
|
||
@JavaAnnotation( | ||
unboxedChar = char.class, | ||
boxedChar = Character.class | ||
) | ||
public class AnnotationTest { | ||
} |
6 changes: 6 additions & 0 deletions
6
...resources/map-annotation-arguments/workload/src/main/java/com/example/JavaAnnotation.java
This file contains 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,6 @@ | ||
package com.example; | ||
|
||
public @interface JavaAnnotation { | ||
Class unboxedChar(); | ||
Class boxedChar(); | ||
} |