Skip to content

Commit 879677f

Browse files
committed
remove nullable marker on KSValueParameter.type, add logic to retrieve type information from property for property setters' value parameter
1 parent 6cbe464 commit 879677f

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

api/src/main/kotlin/com/google/devtools/ksp/symbol/KSValueParameter.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ interface KSValueParameter : KSAnnotated {
3030
/**
3131
* The reference to the type of the parameter.
3232
*/
33-
// TODO: a setter doesn't have a type; However, it can be learned from the corresponding property declaration.
34-
val type: KSTypeReference?
33+
val type: KSTypeReference
3534

3635
/**
3736
* True if it is a vararg.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.google.devtools.ksp.processor
2+
3+
import com.google.devtools.ksp.processing.Resolver
4+
import com.google.devtools.ksp.symbol.KSNode
5+
import com.google.devtools.ksp.symbol.KSValueParameter
6+
import com.google.devtools.ksp.visitor.KSTopDownVisitor
7+
8+
class ParameterTypeProcessor : AbstractTestProcessor() {
9+
val result = mutableListOf<String>()
10+
11+
override fun toResult(): List<String> {
12+
return result
13+
}
14+
15+
override fun process(resolver: Resolver) {
16+
resolver.getAllFiles().map { it.accept(object : KSTopDownVisitor<Unit, Unit>() {
17+
override fun defaultHandler(node: KSNode, data: Unit) {
18+
}
19+
20+
override fun visitValueParameter(valueParameter: KSValueParameter, data: Unit) {
21+
result.add(valueParameter.type.resolve().toString())
22+
}
23+
}, Unit) }
24+
}
25+
}

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSValueParameterImpl.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ package com.google.devtools.ksp.symbol.impl.kotlin
2020

2121
import com.google.devtools.ksp.symbol.*
2222
import com.google.devtools.ksp.symbol.impl.KSObjectCache
23+
import com.google.devtools.ksp.symbol.impl.findParentDeclaration
24+
import com.google.devtools.ksp.symbol.impl.synthetic.KSTypeReferenceSyntheticImpl
2325
import com.google.devtools.ksp.symbol.impl.toLocation
2426
import org.jetbrains.kotlin.lexer.KtTokens.CROSSINLINE_KEYWORD
2527
import org.jetbrains.kotlin.lexer.KtTokens.NOINLINE_KEYWORD
2628
import org.jetbrains.kotlin.psi.KtParameter
29+
import org.jetbrains.kotlin.psi.KtProperty
2730

2831
class KSValueParameterImpl private constructor(val ktParameter: KtParameter) : KSValueParameter {
2932
companion object : KSObjectCache<KtParameter, KSValueParameterImpl>() {
@@ -58,8 +61,8 @@ class KSValueParameterImpl private constructor(val ktParameter: KtParameter) : K
5861
}
5962
}
6063

61-
override val type: KSTypeReference? by lazy {
62-
ktParameter.typeReference?.let { KSTypeReferenceImpl.getCached(it) }
64+
override val type: KSTypeReference by lazy {
65+
ktParameter.typeReference?.let { KSTypeReferenceImpl.getCached(it) } ?: findPropertyForAccessor()?.type ?: KSTypeReferenceSyntheticImpl.getCached(KSErrorType)
6366
}
6467

6568
override val hasDefault: Boolean = ktParameter.hasDefaultValue()
@@ -71,4 +74,8 @@ class KSValueParameterImpl private constructor(val ktParameter: KtParameter) : K
7174
override fun toString(): String {
7275
return name?.asString() ?: "_"
7376
}
77+
78+
private fun findPropertyForAccessor(): KSPropertyDeclaration? {
79+
return (ktParameter.parent?.parent?.parent as? KtProperty)?.let { KSPropertyDeclarationImpl.getCached(it) }
80+
}
7481
}

compiler-plugin/src/test/java/com/google/devtools/ksp/test/KotlinKSPTestGenerated.java

+5
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public void testMultipleModules() throws Exception {
142142
runTest("testData/api/multipleModules.kt");
143143
}
144144

145+
@TestMetadata("parameterTypes.kt")
146+
public void testParameterTypes() throws Exception {
147+
runTest("testData/api/parameterTypes.kt");
148+
}
149+
145150
@TestMetadata("platformDeclaration.kt")
146151
public void testPlatformDeclaration() throws Exception {
147152
runTest("testData/api/platformDeclaration.kt");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// TEST PROCESSOR: ParameterTypeProcessor
2+
// EXPECTED:
3+
// <ERROR TYPE>
4+
// String
5+
// Int
6+
// Int
7+
// <ERROR TYPE>
8+
// <ERROR TYPE>
9+
// END
10+
11+
class Foo {
12+
var a: ErrorType
13+
set(value) {
14+
a = value
15+
}
16+
var x
17+
get() = "OK"
18+
set(v) = Unit
19+
var a:Int
20+
get() = a
21+
set(value) { a = value }
22+
23+
fun foo(a: Int, b: NonExist, c)
24+
}

0 commit comments

Comments
 (0)