Android

注解自动生成dagger2 Component组件的工具

2017-07-05  本文已影响179人  Lightofrain

自己通过apt写的一个自动生成Component组件的注解工具,主要具有以下功能:

注解种类:

@GenRootComponent(scope = Singleton.class, modules = {AppModule.class}, tag = 10, childTag = 11)
public class App extends Application {
}

生成的代码

@Component(
    modules = AppModule.class
)
@Singleton
public abstract interface AppComponent extends MembersInjector<App> {
  BaseActivityComponent.Builder providerBaseActivityComponent();

  @Component.Builder
  abstract interface Builder {
    AppComponent build();

    Builder setModule(AppModule module);
  }
}
@GenSubComponent(modules = ActivityModule.class, tag = 12)
public class MainActivity extends BaseActivity {
}

生成的代码

@Subcomponent(
    modules = ActivityModule.class
)
public abstract interface MainActivityComponent extends 
//提供子组件
MembersInjector<MainActivity> {
  @Subcomponent.Builder
  abstract interface Builder {
    MainActivityComponent build();

    Builder setModule(ActivityModule module);
  }
}

参数含义:
-tag 是自己的标识,tag一致的Component为同一组(主要用于父组件查找子组件,不填默认为0)
-childTag 是子组件标识,工具会通过该标识来查找所有tag和此标识相等的SubComponent,并在本Component中提供出来
-modules 该组件对应的Module(必填,一个或多个)
-scope 该组件对应的scope(选填)

@GenInheritedSubComponent(tag = 11, childTag = 12, modules = BaseModule.class, scope = BaseScope.class,shouldInject = false)
public class BaseActivity extends AppCompatActivity

生成的代码

@Subcomponent(
    modules = BaseModule.class
)
@BaseScope
public abstract interface BaseActivityComponent {
//该Activity没有自己的Component,则提供注入方法
  void injectMembers(AppActivity target);

  SplashActivityComponent.Builder providerSplashActivityComponent();

  MainActivityComponent.Builder providerMainActivityComponent();

  @Subcomponent.Builder
  abstract interface Builder {
    BaseActivityComponent build();

    Builder setModule(BaseModule module);
  }
}

具体来看生产代码

public class DaggerHelper {
  public AppComponent appcomponent;

  private BaseActivityComponent baseactivitycomponent;

  private DaggerHelper() {
  }

  public static DaggerHelper getInstance() {
    return Holder.INSTANCE;
  }

  public AppComponent getAppComponent(App target, Application param0) {
    appcomponent = DaggerAppComponent.builder().setModule(new AppModule(param0)).build();
    appcomponent.injectMembers(target);
    return appcomponent;
  }

//因BaseModule()有参数,所以这里也需要传参(param0)
  public BaseActivityComponent getBaseActivityComponent(Object target, Activity param0) {
    baseactivitycomponent = appcomponent.providerBaseActivityComponent().setModule(new BaseModule(param0)).build();
    if(target instanceof AppActivity) {
      AppActivity appactivity = (AppActivity)target;
      baseactivitycomponent.injectMembers(appactivity);
    }
    return baseactivitycomponent;
  }

  public MainActivityComponent getMainActivityComponent(MainActivity target, Activity param0) {
    MainActivityComponent mainactivitycomponent = baseactivitycomponent.providerMainActivityComponent().setModule(new ActivityModule(param0)).build();
    mainactivitycomponent.injectMembers(target);
    return mainactivitycomponent;
  }

  public SplashActivityComponent getSplashActivityComponent(SplashActivity target) {
    SplashActivityComponent splashactivitycomponent = baseactivitycomponent.providerSplashActivityComponent().setModule(new SpashModule()).build();
    splashactivitycomponent.injectMembers(target);
    return splashactivitycomponent;
  }

  private static class Holder {
    private static final DaggerHelper INSTANCE = new DaggerHelper();
  }
}

库的介绍就到这里了,希望对大家有所帮助!!!

下面是项目地址:
注:项目中还有两个其他的注解工具,以后有时间再介绍

gradle:

 compile "com.suhang.lazy:lazyannotation_annotation:1.1.8"
 compile "com.suhang.lazy:lazyannotation:1.1.8"
 annotationProcessor "com.suhang.lazy:lazyannotation_compiler:1.1.8"

https://github.com/lightofrain/LazyAnnotation.git

上一篇下一篇

猜你喜欢

热点阅读