简体   繁体   English

如何将生成的实现添加到Dagger依赖图?

[英]How to add generated implementation to Dagger Dependency Graph?

I have an interface 我有一个界面

public interface SomeInterface { 
  void test(); 
}

and an annotation processor which generates an implementation of SomeInterface called SomeInterfaceImpl . 和产生的实现注释处理器SomeInterface称为SomeInterfaceImpl

To make this type available with Dagger dependency injection I would create the following: 为了通过Dagger依赖注入使这种类型可用,我将创建以下内容:

@Component(modules = {ApplicationModule.class})
@Singleton
public interface ApplicationComponent {    
    SomeInterface getSomeInterface();       
}

and

@Module
public class ApplicationModule {

    @Provides
    @Singleton
    SomeInterface provideSomeInterface() {
        return new SomeInterfaceImpl();
    }
}

The problem is that I cannot use use SomeInterfaceImpl in my ApplicationModule because it is not yet available and will be generated by an annotation processor. 问题是我无法在ApplicationModule中使用SomeInterfaceImpl ,因为它尚不可用,并且将由注释处理器生成。

How can I extend my annotation processor such that I can use SomeInterface is available for Dagger dependency injection and that the generated implementation SomeInterfaceImpl will be resolved correctly? 如何扩展注释处理器,以便可以将SomeInterface用于Dagger依赖项注入,并且生成的实现SomeInterfaceImpl将得到正确解析?

Edit: 编辑:

The example works, but I want to create the ApplicationModule with another annotation processor and let the processor integrate the ApplicationModule in the dagger graph somehow. 该示例有效,但我想使用另一个注释处理器创建ApplicationModule,并让该处理器以某种方式将ApplicationModule集成到匕首图中。 @Component(modules={ApplicationModule.class}) Will not exist because I do not know in code that ApplicationModule will be generated. @Component(modules = {ApplicationModule.class})将不存在,因为我不知道在代码中将生成ApplicationModule。 Is there a way to integrate a generated a @Module class into the Dagger Graph? 有没有办法将生成的@Module类集成到Dagger Graph中? Note that I do not want to guess that ABCModule will be generated and add it to the @Component. 请注意,我不想猜测将生成ABCModule并将其添加到@Component。 I want that this happens automatically somehow. 我希望这会以某种方式自动发生。

As long as the annotations are both in the same javac invocation, and as long as you eventually do generate your class within one of the processor rounds, Dagger should defer trying to use the symbol until a later round. 只要注释都在同一个javac调用中,并且只要最终在一个处理器回合中最终生成您的类,Dagger就应将尝试使用该符号的时间推迟到下一个回合。

However, in your specific situation cited above, Dagger's processor won't even try to access SomeInterfaceImpl directly, because that's within the body of a @Provides method, and annotation processors cannot (through public APIs) access method body content. 但是,在上述特定情况下,Dagger的处理器甚至不会尝试直接访问SomeInterfaceImpl ,因为它位于@Provides方法的主体内,并且注释处理器不能(通过公共API)访问方法主体的内容。 So Dagger shouldn't even care if SomeInterfaceImpl is generated in time or not - but the code Dagger generates may not compile (nor will the module itself) if you don't generate before the last round. 因此Dagger甚至都不在乎是否SomeInterfaceImpl生成SomeInterfaceImpl但是,如果您没有在上一轮之前生成Dagger生成的代码可能不会编译(模块本身也不会编译)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM