|
| 1 | +package com.bumptech.glide.annotation.ksp |
| 2 | + |
| 3 | +import com.bumptech.glide.annotation.Excludes |
| 4 | +import com.google.devtools.ksp.getConstructors |
| 5 | +import com.google.devtools.ksp.processing.Resolver |
| 6 | +import com.google.devtools.ksp.processing.SymbolProcessorEnvironment |
| 7 | +import com.google.devtools.ksp.symbol.KSAnnotation |
| 8 | +import com.google.devtools.ksp.symbol.KSClassDeclaration |
| 9 | +import com.google.devtools.ksp.symbol.KSDeclaration |
| 10 | +import com.google.devtools.ksp.symbol.KSFunctionDeclaration |
| 11 | +import com.squareup.kotlinpoet.AnnotationSpec |
| 12 | +import com.squareup.kotlinpoet.ClassName |
| 13 | +import com.squareup.kotlinpoet.FileSpec |
| 14 | +import com.squareup.kotlinpoet.FunSpec |
| 15 | +import com.squareup.kotlinpoet.KModifier |
| 16 | +import com.squareup.kotlinpoet.ParameterSpec |
| 17 | +import com.squareup.kotlinpoet.TypeSpec |
| 18 | +import kotlin.reflect.KClass |
| 19 | + |
| 20 | +// This class is visible only for testing |
| 21 | +// TODO(b/174783094): Add @VisibleForTesting when internal is supported. |
| 22 | +object AppGlideModuleConstants { |
| 23 | + // This variable is visible only for testing |
| 24 | + // TODO(b/174783094): Add @VisibleForTesting when internal is supported. |
| 25 | + const val INVALID_MODULE_MESSAGE = |
| 26 | + "Your AppGlideModule must have at least one constructor that has either no parameters or " + |
| 27 | + "accepts only a Context." |
| 28 | + |
| 29 | + private const val CONTEXT_NAME = "Context" |
| 30 | + internal const val CONTEXT_PACKAGE = "android.content" |
| 31 | + internal const val GLIDE_PACKAGE_NAME = "com.bumptech.glide" |
| 32 | + internal const val CONTEXT_QUALIFIED_NAME = "$CONTEXT_PACKAGE.$CONTEXT_NAME" |
| 33 | + internal const val GENERATED_ROOT_MODULE_PACKAGE_NAME = GLIDE_PACKAGE_NAME |
| 34 | + |
| 35 | + internal val CONTEXT_CLASS_NAME = ClassName(CONTEXT_PACKAGE, CONTEXT_NAME) |
| 36 | +} |
| 37 | + |
| 38 | +internal data class AppGlideModuleData( |
| 39 | + val name: ClassName, |
| 40 | + val constructor: Constructor, |
| 41 | +) { |
| 42 | + internal data class Constructor(val hasContext: Boolean) |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Given a [com.bumptech.glide.module.AppGlideModule] class declaration provided by the developer, |
| 47 | + * validate the class and produce a fully parsed [AppGlideModuleData] that allows us to generate a |
| 48 | + * valid [com.bumptech.glide.GeneratedAppGlideModule] implementation without further introspection. |
| 49 | + */ |
| 50 | +internal class AppGlideModuleParser( |
| 51 | + private val environment: SymbolProcessorEnvironment, |
| 52 | + private val resolver: Resolver, |
| 53 | + private val appGlideModuleClass: KSClassDeclaration, |
| 54 | +) { |
| 55 | + |
| 56 | + fun parseAppGlideModule(): AppGlideModuleData { |
| 57 | + val constructor = parseAppGlideModuleConstructorOrThrow() |
| 58 | + val name = ClassName.bestGuess(appGlideModuleClass.qualifiedName!!.asString()) |
| 59 | + |
| 60 | + return AppGlideModuleData(name = name, constructor = constructor) |
| 61 | + } |
| 62 | + |
| 63 | + private fun parseAppGlideModuleConstructorOrThrow(): AppGlideModuleData.Constructor { |
| 64 | + val hasEmptyConstructors = appGlideModuleClass.getConstructors().any { it.parameters.isEmpty() } |
| 65 | + val hasContextParamOnlyConstructor = |
| 66 | + appGlideModuleClass.getConstructors().any { it.hasSingleContextParameter() } |
| 67 | + if (!hasEmptyConstructors && !hasContextParamOnlyConstructor) { |
| 68 | + throw InvalidGlideSourceException(AppGlideModuleConstants.INVALID_MODULE_MESSAGE) |
| 69 | + } |
| 70 | + return AppGlideModuleData.Constructor(hasContextParamOnlyConstructor) |
| 71 | + } |
| 72 | + |
| 73 | + private fun KSFunctionDeclaration.hasSingleContextParameter() = |
| 74 | + parameters.size == 1 && |
| 75 | + AppGlideModuleConstants.CONTEXT_QUALIFIED_NAME == |
| 76 | + parameters.single().type.resolve().declaration.qualifiedName?.asString() |
| 77 | + |
| 78 | + private data class IndexFilesAndLibraryModuleNames( |
| 79 | + val indexFiles: List<KSDeclaration>, |
| 80 | + val libraryModuleNames: List<String>, |
| 81 | + ) |
| 82 | + |
| 83 | + private fun extractGlideModulesFromIndexAnnotation( |
| 84 | + index: KSDeclaration, |
| 85 | + ): List<String> { |
| 86 | + val indexAnnotation: KSAnnotation = index.atMostOneIndexAnnotation() ?: return emptyList() |
| 87 | + environment.logger.info("Found index annotation: $indexAnnotation") |
| 88 | + return indexAnnotation.getModuleArgumentValues().toList() |
| 89 | + } |
| 90 | + |
| 91 | + private fun KSAnnotation.getModuleArgumentValues(): List<String> { |
| 92 | + val result = arguments.find { it.name?.getShortName().equals("modules") }?.value |
| 93 | + if (result is List<*> && result.all { it is String }) { |
| 94 | + @Suppress("UNCHECKED_CAST") return result as List<String> |
| 95 | + } |
| 96 | + throw InvalidGlideSourceException("Found an invalid internal Glide index: $this") |
| 97 | + } |
| 98 | + |
| 99 | + private fun KSDeclaration.atMostOneIndexAnnotation() = atMostOneAnnotation(Index::class) |
| 100 | + |
| 101 | + private fun KSDeclaration.atMostOneExcludesAnnotation() = atMostOneAnnotation(Excludes::class) |
| 102 | + |
| 103 | + private fun KSDeclaration.atMostOneAnnotation( |
| 104 | + annotation: KClass<out Annotation>, |
| 105 | + ): KSAnnotation? { |
| 106 | + val matchingAnnotations: List<KSAnnotation> = |
| 107 | + annotations |
| 108 | + .filter { |
| 109 | + annotation.qualifiedName?.equals( |
| 110 | + it.annotationType.resolve().declaration.qualifiedName?.asString() |
| 111 | + ) |
| 112 | + ?: false |
| 113 | + } |
| 114 | + .toList() |
| 115 | + if (matchingAnnotations.size > 1) { |
| 116 | + throw InvalidGlideSourceException( |
| 117 | + """Expected 0 or 1 $annotation annotations on ${this.qualifiedName}, but found: |
| 118 | + ${matchingAnnotations.size}""" |
| 119 | + ) |
| 120 | + } |
| 121 | + return matchingAnnotations.singleOrNull() |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Given valid [AppGlideModuleData], writes a Kotlin implementation of |
| 127 | + * [com.bumptech.glide.GeneratedAppGlideModule]. |
| 128 | + * |
| 129 | + * This class should obtain all of its data from [AppGlideModuleData] and should not interact with |
| 130 | + * any ksp classes. In the long run, the restriction may allow us to share code between the Java and |
| 131 | + * Kotlin processors. |
| 132 | + */ |
| 133 | +internal class AppGlideModuleGenerator(private val appGlideModuleData: AppGlideModuleData) { |
| 134 | + |
| 135 | + fun generateAppGlideModule(): FileSpec { |
| 136 | + val generatedAppGlideModuleClass = generateAppGlideModuleClass(appGlideModuleData) |
| 137 | + return FileSpec.builder( |
| 138 | + AppGlideModuleConstants.GLIDE_PACKAGE_NAME, |
| 139 | + "GeneratedAppGlideModuleImpl" |
| 140 | + ) |
| 141 | + .addType(generatedAppGlideModuleClass) |
| 142 | + .build() |
| 143 | + } |
| 144 | + |
| 145 | + private fun generateAppGlideModuleClass( |
| 146 | + data: AppGlideModuleData, |
| 147 | + ): TypeSpec { |
| 148 | + return TypeSpec.classBuilder("GeneratedAppGlideModuleImpl") |
| 149 | + .superclass( |
| 150 | + ClassName( |
| 151 | + AppGlideModuleConstants.GENERATED_ROOT_MODULE_PACKAGE_NAME, |
| 152 | + "GeneratedAppGlideModule" |
| 153 | + ) |
| 154 | + ) |
| 155 | + .addModifiers(KModifier.INTERNAL) |
| 156 | + .addProperty("appGlideModule", data.name, KModifier.PRIVATE) |
| 157 | + .primaryConstructor(generateConstructor(data)) |
| 158 | + .addFunction(generateRegisterComponents()) |
| 159 | + .addFunction(generateApplyOptions()) |
| 160 | + .addFunction(generateManifestParsingDisabled()) |
| 161 | + .build() |
| 162 | + } |
| 163 | + |
| 164 | + private fun generateConstructor(data: AppGlideModuleData): FunSpec { |
| 165 | + val contextParameterBuilder = |
| 166 | + ParameterSpec.builder("context", AppGlideModuleConstants.CONTEXT_CLASS_NAME) |
| 167 | + if (!data.constructor.hasContext) { |
| 168 | + contextParameterBuilder.addAnnotation( |
| 169 | + AnnotationSpec.builder(ClassName("kotlin", "Suppress")) |
| 170 | + .addMember("%S", "UNUSED_VARIABLE") |
| 171 | + .build() |
| 172 | + ) |
| 173 | + } |
| 174 | + |
| 175 | + return FunSpec.constructorBuilder() |
| 176 | + .addParameter(contextParameterBuilder.build()) |
| 177 | + .addStatement( |
| 178 | + "appGlideModule = %T(${if (data.constructor.hasContext) "context" else ""})", |
| 179 | + data.name |
| 180 | + ) |
| 181 | + .build() |
| 182 | + |
| 183 | + // TODO(judds): Log the discovered modules here. |
| 184 | + } |
| 185 | + |
| 186 | + // TODO(judds): call registerComponents on LibraryGlideModules here. |
| 187 | + private fun generateRegisterComponents() = |
| 188 | + FunSpec.builder("registerComponents") |
| 189 | + .addModifiers(KModifier.PUBLIC, KModifier.OVERRIDE) |
| 190 | + .addParameter("context", AppGlideModuleConstants.CONTEXT_CLASS_NAME) |
| 191 | + .addParameter("glide", ClassName(AppGlideModuleConstants.GLIDE_PACKAGE_NAME, "Glide")) |
| 192 | + .addParameter("registry", ClassName(AppGlideModuleConstants.GLIDE_PACKAGE_NAME, "Registry")) |
| 193 | + .addStatement("appGlideModule.registerComponents(context, glide, registry)") |
| 194 | + .build() |
| 195 | + |
| 196 | + private fun generateApplyOptions() = |
| 197 | + FunSpec.builder("applyOptions") |
| 198 | + .addModifiers(KModifier.PUBLIC, KModifier.OVERRIDE) |
| 199 | + .addParameter("context", AppGlideModuleConstants.CONTEXT_CLASS_NAME) |
| 200 | + .addParameter( |
| 201 | + "builder", |
| 202 | + ClassName(AppGlideModuleConstants.GLIDE_PACKAGE_NAME, "GlideBuilder") |
| 203 | + ) |
| 204 | + .addStatement("appGlideModule.applyOptions(context, builder)") |
| 205 | + .build() |
| 206 | + |
| 207 | + private fun generateManifestParsingDisabled() = |
| 208 | + FunSpec.builder("isManifestParsingEnabled") |
| 209 | + .addModifiers(KModifier.PUBLIC, KModifier.OVERRIDE) |
| 210 | + .returns(Boolean::class) |
| 211 | + .addStatement("return false") |
| 212 | + .build() |
| 213 | +} |
0 commit comments