Skip to content

Commit 1afb280

Browse files
ting-yuanKSP Auto Pick
authored and
KSP Auto Pick
committed
KSP2: Fix KSPropertyDeclaration.isMutable for Java libs
(cherry picked from commit a7611f2)
1 parent 40e5350 commit 1afb280

File tree

5 files changed

+443
-1
lines changed

5 files changed

+443
-1
lines changed

compiler-plugin/src/test/kotlin/com/google/devtools/ksp/test/KSPCompilerPluginTest.kt

+6
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ class KSPCompilerPluginTest : AbstractKSPCompilerPluginTest() {
321321
runTest("../test-utils/testData/api/interfaceWithDefault.kt")
322322
}
323323

324+
@TestMetadata("isMutable.kt")
325+
@Test
326+
fun testIsMutable() {
327+
runTest("../test-utils/testData/api/isMutable.kt")
328+
}
329+
324330
@TestMetadata("javaModifiers.kt")
325331
@Test
326332
fun testJavaModifiers() {

kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationJavaImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class KSPropertyDeclarationJavaImpl private constructor(val ktJavaFieldSymbol: K
3131
}
3232

3333
override val isMutable: Boolean
34-
get() = !modifiers.contains(Modifier.FINAL)
34+
get() = !ktJavaFieldSymbol.isVal
3535

3636
override val hasBackingField: Boolean
3737
get() = true

kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt

+6
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ class KSPAATest : AbstractKSPAATest() {
358358
runTest("../test-utils/testData/api/interfaceWithDefault.kt")
359359
}
360360

361+
@TestMetadata("isMutable.kt")
362+
@Test
363+
fun testIsMutable() {
364+
runTest("../test-utils/testData/api/isMutable.kt")
365+
}
366+
361367
@TestMetadata("javaModifiers.kt")
362368
@Test
363369
fun testJavaModifiers() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.google.devtools.ksp.processor
19+
20+
import com.google.devtools.ksp.KspExperimental
21+
import com.google.devtools.ksp.processing.Resolver
22+
import com.google.devtools.ksp.symbol.KSAnnotated
23+
import com.google.devtools.ksp.symbol.KSNode
24+
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
25+
import com.google.devtools.ksp.visitor.KSTopDownVisitor
26+
27+
@Suppress("unused") // used in tests
28+
@OptIn(KspExperimental::class)
29+
open class IsMutableProcessor : AbstractTestProcessor() {
30+
lateinit var results: List<String>
31+
32+
override fun process(resolver: Resolver): List<KSAnnotated> {
33+
results = listOf("lib", "main").flatMap { pkg ->
34+
resolver.getDeclarationsFromPackage(pkg)
35+
.flatMap { declaration ->
36+
val properties = mutableListOf<KSPropertyDeclaration>()
37+
declaration.accept(AllMembersVisitor(), properties)
38+
properties
39+
}.map {
40+
"${it.qualifiedName?.asString()}: ${it.isMutable}"
41+
}.sorted()
42+
}
43+
return emptyList()
44+
}
45+
46+
private class AllMembersVisitor : KSTopDownVisitor<MutableList<KSPropertyDeclaration>, Unit>() {
47+
override fun defaultHandler(node: KSNode, data: MutableList<KSPropertyDeclaration>) {
48+
}
49+
50+
override fun visitPropertyDeclaration(
51+
property: KSPropertyDeclaration,
52+
data: MutableList<KSPropertyDeclaration>
53+
) {
54+
data.add(property)
55+
super.visitPropertyDeclaration(property, data)
56+
}
57+
}
58+
59+
override fun toResult(): List<String> {
60+
return results
61+
}
62+
}

0 commit comments

Comments
 (0)