2.基于Dagger2.38.1的hilt源码-BindValueProcessor

前言

BindValueProcessor类处理@BindValue、@BindValueIntoSet、@BindElementsIntoSet或@BindValueIntoMap。当前使用该注解的节点校验并且生成BindValueMetadata对象,针对该BindValueMetadata对象再生成类。

@BindValue、@BindValueIntoSet、@BindElementsIntoSet或@BindValueIntoMap修饰变量,这些注解的作用是用于测试。并且这些注解修饰的变量必须存在于@HiltAndroidTest修饰的类中。

校验并且生成BindValueMetadata对象

@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规则推理出来的。

以上可知,bindingValue变量的作用是做测试。

BindValueMetadata对象属性

  1. TypeElement testElement:bindingValue节点所在类;

  2. ImmutableSet bindValueElements:bindingValue节点所在类的所有bindingValue节点,查看BindValueElement对象;

BindValueElement对象属性

bindingValue节点所在类的所有bindingValue节点。具体属性如下:

  1. VariableElement variableElement:bindingValue变量;
  2. ClassName annotationName:bindingValue变量使用的@BindValue、@BindValueIntoSet、@BindElementsIntoSet或@BindValueIntoMap注解类名,
  3. Optional qualifier:bindingValue变量使用的@Qualifier修饰的注解,
  4. Optional mapKeybindingValue变量使用的@MapKey修饰的注解,
  5. Optional getterElement:用于获取当前bindingValue变量的getter方法。

BindValueGenerator生成类

demo:

@HiltAndroidTest
public class FooTest{
	
	@BindValue
	Bar bar;

	@BindValueIntoSet
	BarIntoSet barIntoSet;

	@BindElementsIntoSet
	Set<BarElementsIntoSet> 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<BarElementsIntoSet> providesSet(FooTest test) {
       return test.barElementsIntoSet;
     }

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

总结

bindvalue节点实际起到的是测试的作用,将bindvalue节点转换成一个@Module和@InstallIn(SingletonComponent.class)共同修饰的module节点里面的一个bindingMethod方法,以待做下一步处理。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值