4..基于Dagger2.38.1版本源码理解hilt注解-其他注解和测试注解

4..基于Dagger2.38.1版本源码理解hilt注解-其他注解和测试注解,第1张

前言 测试注解 bindValue

@BindValue、@BindValueIntoSet、@BindElementsIntoSet或@BindValueIntoMap用于测试环境。

@BindValue、@BindValueIntoSet、@BindElementsIntoSet或@BindValueIntoMap修饰的bindValue节点变量校验规则如下:

  1. @BindValue、@BindValueIntoSet、@BindElementsIntoSet或@BindValueIntoMap同一个变量上只能被其中一个修饰;

  2. bindValue节点只能是变量;

  3. bindValue节点所在父节点如果是类或接口,并且是kotlin文件,那么bindValue节点的getter方法不能使用private修饰;表示kotlin文件,那么当前bindValue变量不能使用private修饰;

  4. bindValue变量不能使用@Inject注解修饰;

  5. bindValue变量上最多只能使用一个@Qualifier修饰的注解修饰;

  6. bindValue变量如果使用了@BindValueIntoMap修饰,那么必须和@MapKey修饰的注解必须同时使用,并且@MapKey修饰的注解只允许出现一次;

  7. bindValue变量不允许使用@Scope修饰的注解修饰;

  8. bindValue变量所在类必须使用@HiltAndroidTest注解修饰;

  9. bindValue变量如果使用@BindElementsIntoSet修饰则当前变量必须是Set< T> - 源码中没有体现,这条是根据@ElementsIntoSet规则推理出来的。

demo:

@HiltAndroidTest
public class FooTest{
	
	@BindValue
	Bar bar;

	@BindValueIntoSet
	BarIntoSet barIntoSet;

	@BindElementsIntoSet
	Set barElementsIntoSet;

	@BindValueIntoMap
	@ClassKey(Foo.class)
	BarIntoMap barIntoMap;
}

生成的代码:

  @Module
  @OriginatingElement(topLevelClass = FooTest.class)
  @InstallIn(SingletonComponent.class)
  @Generated("BindValueGenerator")
  public final class FooTest_BindValueModule {
     
 	 @Provides
     static FooTest providesFooTest(@ApplicationContext Context context) {
       return (FooTest)
           ((TestApplicationComponentManager)
               ((TestApplicationComponentManagerHolder) context).componentManager())
                   .getTestInstance();
     }
	
	 @Provides
     static Bar providesBar(FooTest test) {
       return test.bar;
     }

	 @IntoSet
	 @Provides
     static BarIntoSet providesBarIntoSet(FooTest test) {
       return test.barIntoSet;
     }

	 @ElementsIntoSet
	 @Provides
     static Set providesSet(FooTest test) {
       return test.barElementsIntoSet;
     }

	 @IntoMap
	 @ClassKey(Foo.class)
	 @Provides
     static BarIntoMap providesBarIntoMap(FooTest test) {
       return test.barIntoMap;
     }
  }
@CustomTestApplication注解

规则如下:

  1. @CustomTestApplication修饰节点必须是类或接口;

  2. @CustomTestApplication注解中的value值类型(及其深层次遍历到的类型)不能使用@HiltAndroidApp修饰;

  3. @CustomTestApplication注解中的value值类型(及其深层次遍历到的类型)不能存在@Inject修饰的变量或普通方法或构造函数;

  4. @CustomTestApplication注解中的value值类型必须存在并且是Application或其子类。

demo:

@CustomTestApplication(BaseApplication.class)
class CustomTestApplication{}

生成代码如下:

@Generated("CustomTestApplicationProcessor")
public final class CustomTestApplication_Application extends BaseApplication interface GeneratedComponentManager,TestApplicationComponentManagerHolder{
	 private TestApplicationComponentManager componentManager;

	 protected final void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        componentManager = new TestApplicationComponentManager(this);
      }

	 @Override
     public final GeneratedComponentManager componentManager(){
         return componentManager;
     }

	@Override
    public final Object generatedComponent(){
        return componentManager.generatedComponent();
    }
}
 
@UninstallModules注解 

处理@UninstallModules注解。当前注解用于测试。

校验规则:

  1. @UninstallModules修饰的节点必须是类或接口,还必须同时使用@HiltAndroidTest修饰;

  2. @UninstallModules#value里面的节点必须使用@Module 和 @InstallIn 同时修饰;

  3. @UninstallModules#value中的节点所在顶级节点(当前节点如果在上面是包,那么当前节点就是顶级节点)不允许使用@HiltAndroidTest修饰;

其他注解 @AliasOf

规则如下:

  1. @AliasOf必须和@Scope放在一起使用;
  • @Scope也是用于修饰注解的注解,表示范围;
@DisableInstallInCheck

@DisableInstallInCheck注解的节点必须使用@Module注解。

@OriginatingElement

该注解表示当前节点所在的顶级类名。规则如下:

  1. @OriginatingElement修饰的节点必须是顶级类或接口,顶级-表示再上一级就是包了;

  2. OriginatingElement#topLevelClass中的节点也必须是顶级类或接口。

总结

hilt测试这个环境我也没用认真去查看,抱歉!!!但是我感觉hilt的核心部分我讲解的应该很清楚了,起码我自己感觉到了收获。至此2022年4月25日在家办公的14:55,对Dagger的理解告一段落,感谢家人和自己!!!

并不是为了给别人看,主要对象还是自己学习和后面的复习提供资料。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/733804.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)