-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement KSP support for ContributesMultibinding (#740)
* Also add simple symbol processor support * Conditionally run KSP tests to work-around Github-Windows-CI specific errors --------- Co-authored-by: Joel Wilcox <[email protected]>
- Loading branch information
1 parent
1947fe1
commit 0cea83e
Showing
11 changed files
with
426 additions
and
203 deletions.
There are no files selected for viewing
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
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
187 changes: 187 additions & 0 deletions
187
compiler/src/main/java/com/squareup/anvil/compiler/codegen/ContributesMultibindingCodeGen.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,187 @@ | ||
package com.squareup.anvil.compiler.codegen | ||
|
||
import com.google.auto.service.AutoService | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import com.squareup.anvil.compiler.HINT_MULTIBINDING_PACKAGE_PREFIX | ||
import com.squareup.anvil.compiler.REFERENCE_SUFFIX | ||
import com.squareup.anvil.compiler.SCOPE_SUFFIX | ||
import com.squareup.anvil.compiler.api.AnvilApplicabilityChecker | ||
import com.squareup.anvil.compiler.api.AnvilContext | ||
import com.squareup.anvil.compiler.api.CodeGenerator | ||
import com.squareup.anvil.compiler.api.GeneratedFile | ||
import com.squareup.anvil.compiler.api.createGeneratedFile | ||
import com.squareup.anvil.compiler.codegen.ksp.AnvilSymbolProcessor | ||
import com.squareup.anvil.compiler.codegen.ksp.AnvilSymbolProcessorProvider | ||
import com.squareup.anvil.compiler.codegen.ksp.checkClassExtendsBoundType | ||
import com.squareup.anvil.compiler.codegen.ksp.checkClassIsPublic | ||
import com.squareup.anvil.compiler.codegen.ksp.checkNoDuplicateScopeAndBoundType | ||
import com.squareup.anvil.compiler.codegen.ksp.checkNotMoreThanOneMapKey | ||
import com.squareup.anvil.compiler.codegen.ksp.checkNotMoreThanOneQualifier | ||
import com.squareup.anvil.compiler.codegen.ksp.checkSingleSuperType | ||
import com.squareup.anvil.compiler.codegen.ksp.getKSAnnotationsByType | ||
import com.squareup.anvil.compiler.codegen.ksp.scope | ||
import com.squareup.anvil.compiler.contributesMultibindingFqName | ||
import com.squareup.anvil.compiler.internal.createAnvilSpec | ||
import com.squareup.anvil.compiler.internal.reference.asClassName | ||
import com.squareup.anvil.compiler.internal.reference.classAndInnerClassReferences | ||
import com.squareup.anvil.compiler.internal.reference.generateClassName | ||
import com.squareup.anvil.compiler.internal.safePackageString | ||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.KModifier.PUBLIC | ||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.asClassName | ||
import com.squareup.kotlinpoet.ksp.toClassName | ||
import com.squareup.kotlinpoet.ksp.writeTo | ||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import java.io.File | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Generates a hint for each contributed class in the `anvil.hint.multibinding` package. This | ||
* allows the compiler plugin to find all contributed multibindings a lot faster when merging | ||
* modules and component interfaces. | ||
*/ | ||
internal object ContributesMultibindingCodeGen : AnvilApplicabilityChecker { | ||
|
||
fun generate( | ||
className: ClassName, | ||
scopes: List<ClassName> | ||
): FileSpec { | ||
val fileName = className.generateClassName().simpleName | ||
val generatedPackage = HINT_MULTIBINDING_PACKAGE_PREFIX + | ||
className.packageName.safePackageString(dotPrefix = true) | ||
val classFqName = className.canonicalName | ||
val propertyName = classFqName.replace('.', '_') | ||
|
||
return FileSpec.createAnvilSpec(generatedPackage, fileName) { | ||
addProperty( | ||
PropertySpec | ||
.builder( | ||
name = propertyName + REFERENCE_SUFFIX, | ||
type = KClass::class.asClassName().parameterizedBy(className) | ||
) | ||
.initializer("%T::class", className) | ||
.addModifiers(PUBLIC) | ||
.build() | ||
) | ||
|
||
scopes.forEachIndexed { index, scope -> | ||
addProperty( | ||
PropertySpec | ||
.builder( | ||
name = propertyName + SCOPE_SUFFIX + index, | ||
type = KClass::class.asClassName().parameterizedBy(scope) | ||
) | ||
.initializer("%T::class", scope) | ||
.addModifiers(PUBLIC) | ||
.build() | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun isApplicable(context: AnvilContext) = !context.generateFactoriesOnly | ||
|
||
internal class KspGenerator( | ||
override val env: SymbolProcessorEnvironment, | ||
) : AnvilSymbolProcessor() { | ||
|
||
override fun processChecked(resolver: Resolver): List<KSAnnotated> { | ||
resolver.getSymbolsWithAnnotation(ContributesMultibinding::class.java.canonicalName) | ||
.forEach { clazz -> | ||
if (clazz !is KSClassDeclaration) { | ||
env.logger.error( | ||
"@${ContributesMultibinding::class.simpleName} can only be applied to classes", | ||
clazz | ||
) | ||
return@forEach | ||
} | ||
clazz.checkClassIsPublic { | ||
"${clazz.qualifiedName!!.asString()} is binding a type, but the class is not public. " + | ||
"Only public types are supported." | ||
} | ||
clazz.checkNotMoreThanOneQualifier(contributesMultibindingFqName) | ||
clazz.checkNotMoreThanOneMapKey() | ||
clazz.checkSingleSuperType(contributesMultibindingFqName, resolver) | ||
clazz.checkClassExtendsBoundType(contributesMultibindingFqName, resolver) | ||
|
||
// All good, generate away | ||
val className = clazz.toClassName() | ||
val scopes = clazz.getKSAnnotationsByType(ContributesMultibinding::class) | ||
.toList() | ||
.also { it.checkNoDuplicateScopeAndBoundType(clazz) } | ||
.map { it.scope().toClassName() } | ||
.distinct() | ||
// Give it a stable sort. | ||
.sortedBy { it.canonicalName } | ||
|
||
generate(className, scopes) | ||
.writeTo( | ||
codeGenerator = env.codeGenerator, | ||
aggregating = false, | ||
originatingKSFiles = listOf(clazz.containingFile!!) | ||
) | ||
} | ||
|
||
return emptyList() | ||
} | ||
|
||
@AutoService(SymbolProcessorProvider::class) | ||
class Provider : AnvilSymbolProcessorProvider(ContributesMultibindingCodeGen, ::KspGenerator) | ||
} | ||
|
||
@AutoService(CodeGenerator::class) | ||
internal class EmbeddedGenerator : CodeGenerator { | ||
|
||
override fun isApplicable(context: AnvilContext): Boolean = | ||
ContributesMultibindingCodeGen.isApplicable(context) | ||
|
||
override fun generateCode( | ||
codeGenDir: File, | ||
module: ModuleDescriptor, | ||
projectFiles: Collection<KtFile> | ||
): Collection<GeneratedFile> { | ||
return projectFiles | ||
.classAndInnerClassReferences(module) | ||
.filter { it.isAnnotatedWith(contributesMultibindingFqName) } | ||
.onEach { clazz -> | ||
clazz.checkClassIsPublic { | ||
"${clazz.fqName} is binding a type, but the class is not public. " + | ||
"Only public types are supported." | ||
} | ||
clazz.checkNotMoreThanOneQualifier(contributesMultibindingFqName) | ||
clazz.checkNotMoreThanOneMapKey() | ||
clazz.checkSingleSuperType(contributesMultibindingFqName) | ||
clazz.checkClassExtendsBoundType(contributesMultibindingFqName) | ||
} | ||
.map { clazz -> | ||
val className = clazz.asClassName() | ||
val scopes = clazz.annotations | ||
.find(contributesMultibindingFqName) | ||
.also { it.checkNoDuplicateScopeAndBoundType() } | ||
.distinctBy { it.scope() } | ||
// Give it a stable sort. | ||
.sortedBy { it.scope() } | ||
.map { it.scope().asClassName() } | ||
|
||
val spec = generate(className, scopes) | ||
|
||
createGeneratedFile( | ||
codeGenDir = codeGenDir, | ||
packageName = spec.packageName, | ||
fileName = spec.name, | ||
content = spec.toString() | ||
) | ||
} | ||
.toList() | ||
} | ||
} | ||
} |
Oops, something went wrong.