Skip to content

Commit fe46206

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

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-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
@@ -137,6 +137,11 @@ public void testMultipleModules() throws Exception {
137137
runTest("testData/api/multipleModules.kt");
138138
}
139139

140+
@TestMetadata("parameterTypes.kt")
141+
public void testParameterTypes() throws Exception {
142+
runTest("testData/api/parameterTypes.kt");
143+
}
144+
140145
@TestMetadata("platformDeclaration.kt")
141146
public void testPlatformDeclaration() throws Exception {
142147
runTest("testData/api/platformDeclaration.kt");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TEST PROCESSOR: ParameterTypeProcessor
2+
// EXPECTED:
3+
// <ERROR TYPE>
4+
// Int
5+
// Int
6+
// <ERROR TYPE>
7+
// <ERROR TYPE>
8+
// END
9+
10+
class Foo {
11+
var a: ErrorType
12+
set(value) {
13+
a = value
14+
}
15+
var a:Int
16+
get() = a
17+
set(value) { a = value }
18+
19+
fun foo(a: Int, b: NonExist, c)
20+
}

0 commit comments

Comments
 (0)