-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
That's a significant change and changes the API surface. kotlin-inject supports scopes with and without parameters. This change implements a similar API as Anvil for Dagger 2 used to provide, where scope references can be added to annotations, e.g. ```kotlin @ContributesTo(AppScope::class) interface ContributedComponentInterface @component @MergeComponent(AppScope::class) interface MergedComponent ``` The existing mechanism by annotating classes with the scope annotation is still working: ```kotlin @ContributesTo @SingleInAppScope interface ContributedComponentInterface @component @MergeComponent @SingleInAppScope interface MergedComponent ``` Most of the logic changed in the way we resolve the scope where to merge code, which happens during compilation. There is an additional change for generated code, where we no longer add the scope to generated interfaces and instead rely on the `@Origin` annotation to determine the scope. That simplifies code generation and is backwards compatible. Resolves #1.
- Loading branch information
Showing
23 changed files
with
1,057 additions
and
198 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
157 changes: 157 additions & 0 deletions
157
compiler/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/MergeScope.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,157 @@ | ||
package software.amazon.lastmile.kotlin.inject.anvil | ||
|
||
import com.google.devtools.ksp.symbol.KSAnnotation | ||
import com.google.devtools.ksp.symbol.KSType | ||
import com.squareup.kotlinpoet.AnnotationSpec | ||
import com.squareup.kotlinpoet.ksp.toAnnotationSpec | ||
import com.squareup.kotlinpoet.ksp.toClassName | ||
|
||
/** | ||
* Represents the destination of contributed types and which types should be merged during | ||
* the merge phase. There is complexity to this problem, because `kotlin-inject` didn't | ||
* support parameters for scopes initially and our Anvil extensions added support for that. Later, | ||
* we started supporting parameters, which changed the API. E.g. one could use: | ||
* ``` | ||
* @ContributesTo(AppScope::class) | ||
* interface ContributedComponentInterface | ||
* | ||
* @Component | ||
* @MergeComponent(AppScope::class) | ||
* interface MergedComponent | ||
* ``` | ||
* Or the old way: | ||
* ``` | ||
* @ContributesTo | ||
* @SingleInAppScope | ||
* interface ContributedComponentInterface | ||
* | ||
* @Component | ||
* @MergeComponent | ||
* @SingleInAppScope | ||
* interface MergedComponent | ||
* ``` | ||
*/ | ||
internal sealed class MergeScope { | ||
/** | ||
* The fully qualified name of the annotation used as scope, e.g. | ||
* ``` | ||
* @ContributesTo | ||
* @SingleInAppScope | ||
* interface Abc | ||
* ``` | ||
* Note that the annotation itself is annotated with `@Scope`. | ||
* | ||
* The value is `null`, when only a marker is used, e.g. | ||
* ``` | ||
* @ContributesTo(AppScope::class) | ||
* interface Abc | ||
* ``` | ||
* | ||
* If the `scope` parameter is used and the argument is annotated with `@Scope`, then | ||
* this value is non-null, e.g. for this: | ||
* ``` | ||
* @ContributesBinding(scope = SingleInAppScope::class) | ||
* class Binding : SuperType | ||
* ``` | ||
*/ | ||
abstract val annotationFqName: String? | ||
|
||
/** | ||
* A marker for a scope that isn't itself annotated with `@Scope`, e.g. | ||
* ``` | ||
* @ContributesTo(AppScope::class) | ||
* interface Abc | ||
* ``` | ||
* | ||
* The value is null, if no marker is used, e.g. | ||
* ``` | ||
* @ContributesTo | ||
* @SingleInAppScope | ||
* interface Abc | ||
* ``` | ||
* | ||
* The value is also null, when the `scope` parameter is used and the argument is annotated | ||
* with `@Scope`, e.g. | ||
* ``` | ||
* @ContributesBinding(scope = SingleInAppScope::class) | ||
* class Binding : SuperType | ||
* ``` | ||
*/ | ||
abstract val markerFqName: String? | ||
|
||
/** | ||
* A reference to the scope. | ||
* | ||
* [markerFqName] is preferred, because it allows us to decouple contributions from | ||
* kotlin-inject's scoping mechanism. E.g. imagine someone using `@Singleton` as a scope, and | ||
* they'd like to adopt kotlin-inject-anvil with `@ContributesTo(AppScope::class)`. Because we | ||
* prefer the marker, this would be supported. | ||
*/ | ||
val fqName: String get() = requireNotNull(markerFqName ?: annotationFqName) | ||
|
||
abstract fun toAnnotationSpec(): AnnotationSpec | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is MergeScope) return false | ||
|
||
if (fqName != other.fqName) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return fqName.hashCode() | ||
} | ||
|
||
private class MarkerBasedMergeScope( | ||
override val annotationFqName: String, | ||
override val markerFqName: String?, | ||
private val ksAnnotation: KSAnnotation, | ||
) : MergeScope() { | ||
override fun toAnnotationSpec(): AnnotationSpec { | ||
return ksAnnotation.toAnnotationSpec() | ||
} | ||
} | ||
|
||
private class AnnotationBasedMergeScope( | ||
override val annotationFqName: String?, | ||
override val markerFqName: String?, | ||
private val ksType: KSType, | ||
) : MergeScope() { | ||
override fun toAnnotationSpec(): AnnotationSpec { | ||
return AnnotationSpec.builder(ksType.toClassName()).build() | ||
} | ||
} | ||
|
||
companion object { | ||
operator fun invoke( | ||
contextAware: ContextAware, | ||
annotationType: KSType?, | ||
markerType: KSType?, | ||
): MergeScope { | ||
val nonNullType = contextAware.requireNotNull(markerType ?: annotationType, null) { | ||
"Couldn't determine scope. No scope annotation nor marker found." | ||
} | ||
|
||
return AnnotationBasedMergeScope( | ||
annotationFqName = annotationType?.declaration?.requireQualifiedName(contextAware), | ||
markerFqName = markerType?.declaration?.requireQualifiedName(contextAware), | ||
ksType = nonNullType, | ||
) | ||
} | ||
|
||
operator fun invoke( | ||
contextAware: ContextAware, | ||
ksAnnotation: KSAnnotation, | ||
): MergeScope { | ||
return MarkerBasedMergeScope( | ||
annotationFqName = ksAnnotation.annotationType.resolve().declaration | ||
.requireQualifiedName(contextAware), | ||
markerFqName = ksAnnotation.scopeParameter(contextAware)?.declaration | ||
?.requireQualifiedName(contextAware), | ||
ksAnnotation = ksAnnotation, | ||
) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.