Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KSP] Incorrect generates map binding code for ClassKey when class is generic #4055

Closed
BenSchwab opened this issue Sep 1, 2023 · 1 comment

Comments

@BenSchwab
Copy link

This can be reproduced with the following code, when using the KSP proccessor (dagger version 2.48)

@Component(
  modules = [
    MyModuleA::class
  ]
)
interface ApplicationComponent {

  val map: Map<Class<*>, String>

}

@Module
internal object MyModuleA {

  @Provides
  @IntoMap
  @ClassKey(Thing::class)
  fun provideThingValue(): String {
    return "value for Thing"
  }

  @Provides
  @IntoMap
  @ClassKey(GenericThing::class)
  fun provideAbstractThingValue(): String {
    return "value for Thing"
  }

}

class Thing

class GenericThing<T>

This produces the following incorrect method:

 @Override
    public Map<Class<?>, String> getMap() {
      return MapBuilder.<Class<?>, String>newMapBuilder(2).put(Thing.class, MyModuleA_ProvideThingValueFactory.provideThingValue()).put(GenericThing<?>.class, MyModuleA_ProvideAbstractThingValueFactory.provideAbstractThingValue()).build();
    }

Notably GenericThing<?>.class should be GenericThing.class.

Using kapt will generate the correct code:

 @Override
    public Map<Class<?>, String> getMap() {
      return MapBuilder.<Class<?>, String>newMapBuilder(2).put(Thing.class, MyModuleA_ProvideThingValueFactory.provideThingValue()).put(GenericThing.class, MyModuleA_ProvideAbstractThingValueFactory.provideAbstractThingValue()).build();
    }
@bcorso
Copy link

bcorso commented Sep 1, 2023

@BenSchwab thanks for filing this bug!

Looks like this this is a bug in the code here: https://github.com/google/dagger/blob/master/java/dagger/internal/codegen/binding/AnnotationExpression.java#L115

We should be using the ClassName rather than the TypeName there. I'll send out a fix for that.

copybara-service bot pushed a commit that referenced this issue Sep 1, 2023
The issue here was that `AnnotationExpression` was using `XType#getTypeName()` resulting in `GenericType<?>.class` in generated code. This CL fixes the issue by using `XTypeElement#getClassName()` instead so that the generated code results in `GenericType.class` instead.

See #4055.

Fixes #4055

RELNOTES=Fixes #4055:
PiperOrigin-RevId: 562037303
copybara-service bot pushed a commit that referenced this issue Sep 2, 2023
The issue here was that `AnnotationExpression` was using `XType#getTypeName()` resulting in `GenericType<?>.class` in generated code. This CL fixes the issue by using `XTypeElement#getClassName()` instead so that the generated code results in `GenericType.class` instead.

See #4055.

Fixes #4055

RELNOTES=Fixes #4055:
PiperOrigin-RevId: 562037303
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants