-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract assisted factory dependency matching code
- Loading branch information
1 parent
9fa2d34
commit 64f84c2
Showing
4 changed files
with
91 additions
and
73 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...c/jvmMain/kotlin/com/daugeldauge/kinzhal/processor/AssistedFactoryDependenciesResolver.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,58 @@ | ||
package com.daugeldauge.kinzhal.processor | ||
|
||
import com.daugeldauge.kinzhal.annotations.Assisted | ||
import com.daugeldauge.kinzhal.processor.model.AssistedFactoryType | ||
import com.daugeldauge.kinzhal.processor.model.Key | ||
import com.google.devtools.ksp.symbol.KSFunctionDeclaration | ||
import com.google.devtools.ksp.symbol.KSValueParameter | ||
|
||
internal class AssistedFactoryDependency( | ||
val name: String, | ||
val key: Key, | ||
val isAssisted: Boolean, | ||
val parameter: KSValueParameter, | ||
) | ||
|
||
internal object AssistedFactoryDependenciesResolver { | ||
fun match( | ||
sourceDeclaration: KSFunctionDeclaration, | ||
assistedFactoryType: AssistedFactoryType, | ||
): List<AssistedFactoryDependency> { | ||
val dependencies = sourceDeclaration.parameters.map { | ||
val isAssisted = it.isAssisted | ||
AssistedFactoryDependency( | ||
name = if (isAssisted) { | ||
it.name!!.asString() | ||
} else { | ||
"${it.name!!.asString()}Provider" | ||
}, | ||
key = it.type.toKey(it.annotations), | ||
isAssisted = isAssisted, | ||
parameter = it | ||
) | ||
} | ||
|
||
val assistedSourceParameters = dependencies | ||
.asSequence() | ||
.filter(AssistedFactoryDependency::isAssisted) | ||
.map { it.name to it.key.type } | ||
.toList() | ||
|
||
val assistedFactoryParameters = assistedFactoryType.factoryMethod.parameters | ||
.map { it.name?.asString() to it.type.resolve() } | ||
|
||
if (assistedSourceParameters != assistedFactoryParameters) { | ||
throw NonRecoverableProcessorException( | ||
"Factory method in @AssistedFactory must have assisted parameters by same names, types and in the same order as @AssistedInject constructor", | ||
assistedFactoryType.factoryMethod | ||
) | ||
} | ||
|
||
return dependencies | ||
} | ||
} | ||
|
||
private val KSValueParameter.isAssisted: Boolean | ||
get() = annotations.any { | ||
it.annotationType.resolve().declaration.qualifiedName?.asString() == Assisted::class.requireQualifiedName() | ||
} |
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
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
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