Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scan Kotlin constants for JavaPsiFacade
so that constants in light classes can be resolved by JavaPsiFacade.
- Loading branch information
Showing
11 changed files
with
203 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
integration-tests/src/test/kotlin/com/google/devtools/ksp/test/KotlinConstsInJavaIT.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,42 @@ | ||
package com.google.devtools.ksp.test | ||
|
||
import org.gradle.testkit.runner.BuildResult | ||
import org.gradle.testkit.runner.GradleRunner | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import org.junit.Assert | ||
import org.junit.Assume | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
class KotlinConstsInJavaIT { | ||
@Rule | ||
@JvmField | ||
val project: TemporaryTestProject = TemporaryTestProject("kotlin-consts-in-java") | ||
|
||
private fun GradleRunner.buildAndCheck(vararg args: String, extraCheck: (BuildResult) -> Unit = {}) = | ||
buildAndCheckOutcome(*args, outcome = TaskOutcome.SUCCESS, extraCheck = extraCheck) | ||
|
||
private fun GradleRunner.buildAndCheckOutcome( | ||
vararg args: String, | ||
outcome: TaskOutcome, | ||
extraCheck: (BuildResult) -> Unit = {} | ||
) { | ||
val result = this.withArguments(*args).build() | ||
|
||
Assert.assertEquals(outcome, result.task(":workload:kspKotlin")?.outcome) | ||
|
||
extraCheck(result) | ||
} | ||
|
||
@Test | ||
fun testKotlinConstsInJava() { | ||
// FIXME: `clean` fails to delete files on windows. | ||
Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows", ignoreCase = true)) | ||
val gradleRunner = GradleRunner.create().withProjectDir(project.root).withDebug(true) | ||
gradleRunner.buildAndCheck(":workload:kspKotlin") | ||
|
||
File(project.root, "workload/src/main/java/com/example/JavaClass.java").appendText("\n") | ||
gradleRunner.buildAndCheck(":workload:kspKotlin") | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-tests/src/test/resources/kotlin-consts-in-java/build.gradle.kts
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 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} |
19 changes: 19 additions & 0 deletions
19
integration-tests/src/test/resources/kotlin-consts-in-java/settings.gradle.kts
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,19 @@ | ||
pluginManagement { | ||
val kotlinVersion: String by settings | ||
val kspVersion: String by settings | ||
val testRepo: String by settings | ||
plugins { | ||
id("com.google.devtools.ksp") version kspVersion | ||
kotlin("jvm") version kotlinVersion | ||
} | ||
repositories { | ||
maven(testRepo) | ||
gradlePluginPortal() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
} | ||
|
||
rootProject.name = "playground" | ||
|
||
include(":workload") | ||
include(":test-processor") |
24 changes: 24 additions & 0 deletions
24
integration-tests/src/test/resources/kotlin-consts-in-java/test-processor/build.gradle.kts
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,24 @@ | ||
val kspVersion: String by project | ||
val testRepo: String by project | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
group = "com.example" | ||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
maven(testRepo) | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib")) | ||
implementation("com.google.devtools.ksp:symbol-processing-api:$kspVersion") | ||
} | ||
|
||
sourceSets.main { | ||
java.srcDirs("src/main/kotlin") | ||
} |
27 changes: 27 additions & 0 deletions
27
.../src/test/resources/kotlin-consts-in-java/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,27 @@ | ||
import com.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSFunctionDeclaration | ||
|
||
class TestProcessor( | ||
private val codeGenerator: CodeGenerator, | ||
private val options: Map<String, String>, | ||
private val logger: KSPLogger | ||
) : SymbolProcessor { | ||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
resolver | ||
.getSymbolsWithAnnotation("com.example.ann.MyAnn") | ||
.filterIsInstance<KSFunctionDeclaration>() | ||
.forEach { func -> | ||
val arg = func.annotations.first().arguments.first().value.toString() | ||
if (!arg.startsWith("REPLACE")) | ||
throw IllegalStateException(arg) | ||
} | ||
|
||
return emptyList() | ||
} | ||
} | ||
|
||
class TestProcessorProvider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = | ||
TestProcessor(environment.codeGenerator, environment.options, environment.logger) | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
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 @@ | ||
TestProcessorProvider |
20 changes: 20 additions & 0 deletions
20
integration-tests/src/test/resources/kotlin-consts-in-java/workload/build.gradle.kts
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,20 @@ | ||
val testRepo: String by project | ||
|
||
plugins { | ||
id("com.google.devtools.ksp") | ||
kotlin("jvm") | ||
} | ||
|
||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
maven(testRepo) | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib")) | ||
implementation(project(":test-processor")) | ||
ksp(project(":test-processor")) | ||
} |
9 changes: 9 additions & 0 deletions
9
...rc/test/resources/kotlin-consts-in-java/workload/src/main/java/com/example/JavaClass.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,9 @@ | ||
package com.example; | ||
|
||
import com.example.ann.MyAnn; | ||
|
||
public class JavaClass { | ||
@MyAnn(KotlinConsts.ACTION) | ||
public void f() { | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...c/test/resources/kotlin-consts-in-java/workload/src/main/java/com/example/KotlinConsts.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,8 @@ | ||
package com.example | ||
|
||
class KotlinConsts { | ||
companion object { | ||
const val ACTION = "REPLACE" | ||
const val ACTION2 = "REPLACE" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
.../src/test/resources/kotlin-consts-in-java/workload/src/main/java/com/example/ann/MyAnn.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,3 @@ | ||
package com.example.ann | ||
|
||
annotation class MyAnn(val value: String) |