-
Notifications
You must be signed in to change notification settings - Fork 389
Fix getJvmName for @JvmRecord data class properties #2813
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * Copyright 2010-2022 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.test | ||
|
|
||
| import org.jetbrains.kotlin.config.JvmTarget | ||
| import org.jetbrains.kotlin.test.TestMetadata | ||
| import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder | ||
| import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirectives | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.parallel.Execution | ||
| import org.junit.jupiter.api.parallel.ExecutionMode | ||
|
|
||
| @Execution(ExecutionMode.SAME_THREAD) | ||
| class KSPAA17Test : AbstractKSPAATest() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you try adding the test to the existing test suite? |
||
|
|
||
| override fun configureTest(builder: TestConfigurationBuilder) { | ||
| builder.defaultDirectives { | ||
| -JvmEnvironmentConfigurationDirectives.JVM_TARGET | ||
| JvmEnvironmentConfigurationDirectives.JVM_TARGET with JvmTarget.JVM_17 | ||
| } | ||
| } | ||
|
|
||
| @TestMetadata("jvmNameRecord.kt") | ||
| @Test | ||
| fun testJvmNameRecord() { | ||
| runTest("../kotlin-analysis-api/testData/jvmNameRecord.kt") | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // TEST PROCESSOR: JvmNameRecordProcessor | ||
| // EXPECTED: | ||
| // (x, null), (y, null) | ||
| // (x, null), (y, null) | ||
| // END | ||
| // MODULE: main | ||
| // FILE: TestRecordClass.kt | ||
| @JvmRecord | ||
| data class TestRecordClass(val x: Int, val y: String) | ||
| // FILE: TestLibRecordClass.kt | ||
| @JvmRecord | ||
| data class TestLibRecordClass(val x: Int, val y: String) | ||
|
Comment on lines
+8
to
+12
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How are these classes distinct except for the name? It's better if the classes are a bit more diverse. I'm thinking an approximation of a matrix of the following:
If you have type parameters and or normal parameters and a class that extends another type, then it's also a good idea to override some parameters and add some locally, e.g., interface Example<A> {
val x: A
}
data class Ext<A, B>(override val x: A, val y: B) : Example<A> |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.google.devtools.ksp.processor | ||
|
|
||
| import com.google.devtools.ksp.KspExperimental | ||
| import com.google.devtools.ksp.getClassDeclarationByName | ||
| import com.google.devtools.ksp.processing.Resolver | ||
| import com.google.devtools.ksp.symbol.KSAnnotated | ||
|
|
||
| class JvmNameRecordProcessor : AbstractTestProcessor() { | ||
| val results = mutableListOf<String>() | ||
| override fun toResult(): List<String> { | ||
| return results | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be sorted to ensure results are reproducible. |
||
| } | ||
|
|
||
| @OptIn(KspExperimental::class) | ||
| override fun process(resolver: Resolver): List<KSAnnotated> { | ||
| listOf("TestRecordClass", "TestLibRecordClass").forEach { clsName -> | ||
| resolver.getClassDeclarationByName(clsName)?.let { cls -> | ||
| results.add( | ||
| cls.getAllProperties().map { | ||
| "(${it.getter?.let { resolver.getJvmName(it) }}, " + | ||
| "${it.setter?.let { resolver.getJvmName(it) }})" | ||
| }.toList().joinToString() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more stable if you use getAllProperties().flatMap {
// create a list that contains the jvm getter name if it exists and the setter name if it exists
}The names should probably also be qualified / prefixed with the class names. |
||
| ) | ||
| } | ||
| } | ||
| return emptyList() | ||
| } | ||
| } | ||
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.
I think we can improve the readability of this by extracting some variables so it reads
Additionally, we can shortcircuit the expensive type resolution by checking for the short name first in the lambda:
any { it.shortName.asString() == "JvmRecord" || it.annotationType.resolve().declaration.qualifiedName?.asString() == "kotlin.jvm.Record }Lastly, maybe we can rename
nametopropertyNameso it's clearer what the name. I know you didn't change it, but let's improve it.