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
Scan Kotlin constants for JavaPsiFacade #1138
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the case for exception here and why it is simply ignored for the general
Exception
rather than more specific exception types?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's similar to the initialization blocks protected by
handleException
in the above. Exceptions can arise from, e.g., broken inputs. This is the best effort to make constants in Kotlin available to JavaPsiFacade and try not to error out if possible. It's up to the processors to decide whether to error out or not.