Skip to content

Conversation

@oldwomanjosiah
Copy link
Contributor

@oldwomanjosiah oldwomanjosiah commented Jul 30, 2022

Currently, qualifier annotations on injected constructor params are not propagated to generated code at all.

Proposed Changes

  • Use the qualifier annotations during codegen to annotate injected parameters in modules
    • ViewModels have this done in ViewModelTangleScopeModuleGenerator
    • Fragments have this done in ContributesFragmentGenerator and FragmentInject_ModuleGenerator
    • Code for this has also been removed from Fragment_Factory_Generator as it was being applied in a non-Inject class
    • Workers have this done in AssistedWorkerFactoryGenerator
  • In qualifierAnnotationSpecs use the annotation reference itself to generate the annotation spec, instead of the @Qualifier it is annotated with.
  • In qualifierAnnotationSpec use resolvedName instead of name for argument names, as name is null when the param is passed positionally.
  • In qualifierAnnotationSpec wrap string and char values, so that they are correctly propogated
  • In qualifierAnnotationSpec check whether an argument is an interpolated string, if so kill the compilation nicely.
    This has to be done for now due to an oversight made in Anvil's AnnotationArgumentReference.Psi. I left a longer
    comment inline, but it should be technically feasible to do the constant folding required to make templates work on
    (at least) the happy path if that would be desirable. I am personally of the opinion that that kind of change should be
    upstreamed to anvil though.

@oldwomanjosiah oldwomanjosiah requested a review from RBusarow as a code owner July 30, 2022 21:21
@oldwomanjosiah oldwomanjosiah changed the title VMInject Qualifier Annotation Fix Qualifier Annotation Fix Jul 31, 2022
}

when (val value = arg.value<Any>()) {
is KClassValue -> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason that we are matchin on KClassValue and EnumValue here, it seems like arg.value<Any>() won't ever return them, instead opting for giving us ClassReference and FqName.

@oldwomanjosiah
Copy link
Contributor Author

oldwomanjosiah commented Jul 31, 2022

I've also opened an issue for the string templates here

Update

Was fixed in square/anvil#632, so the workaround can be removed after the next Anvil release.

oldwomanjosiah and others added 17 commits August 23, 2022 19:16
- Parameterized qualifiers keep arguments
- Fails to compile if no qualified value is bound.
- Use Resolved names for parameters, instead of `name`
  The `name` field is null if the use site did not use named arguements,
  causing code to be generated like
  ```kotlin
  @provides
  fun provides_Target(
    @SomeQualifier(null = "A") qualified: Provider<String>
  ): Target
  ```
- Re-escape strings and chars
  Literal values are given to you as the runtime value, causing the
  following
  ```kotlin
  class Target @VMInject(@SomeQualifier("A") qualified: String) : ViewModel()
  ```
  to have the following generated
  ```kotlin
  @provides
  fun provides_Target(
    @SomeQualifier(value = A) qualified: Provider<String>
  ): Target
  ```
  without any `A` in scope.
Anvil's PSI Parsing for AnnotationArgumentReference.Psi currently does
not fold constants within string templates correctly, instead always
taking only the first leaf node. This causes an annotation like
`@MyQualifier("Hello, $world")` to generate `@MyQualifier("Hello, ")`,
which is obviously incorrect. This could be resolved here (in a separate
PR) if desired, but it would be better to upstream the change.

See: https://github.com/square/anvil/blob/main/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/reference/AnnotationArgumentReference.kt#L267
@RBusarow RBusarow force-pushed the josiah/vmInject_qualifiers branch from f5617d7 to 938fd99 Compare August 24, 2022 02:58
@kodiakhq kodiakhq bot merged commit 7dd36c7 into RBusarow:main Aug 24, 2022
@oldwomanjosiah oldwomanjosiah deleted the josiah/vmInject_qualifiers branch August 29, 2022 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants