Git Product home page Git Product logo

auto-dagger2's Introduction

Auto Dagger2

Auto Dagger2 is an annotation processor built on top of the Dagger2 annotation processor.
It basically generates the component for you.

The goal is to reduce the boilerplate code required by Dagger2 when you have "empty" or simple components. It is usually the case in Android development.

You can also mix manually written components with the ones generated by Auto Dagger2. Auto Dagger2 produces the human-readable code you would (hopefully) write yourself.

Getting started

@AutoComponent
@Singleton
public class ExampleApplication extends Application { 
}

It generates ExampleApplicationComponent

@Component
@Singleton
public interface ExampleApplicationComponent { 
}

As you can see, the @Singleton annotation is applied to the generated component as well.

API

@AutoComponent

Annotate a class with @AutoComponent to generated an associated Component. On the component, you can add dependencies, modules and superinterfaces.

@AutoComponent(
    dependencies = ExampleApplication.class,
    modules = MainActivity.Module.class,
    superinterfaces = {ExampleApplication.class, GlobalComponent.class})
@Singleton
public class MainActivity extends Activity {
}

It generates MainActivityComponent

@Component(
    dependencies = ExampleApplicationComponent.class,
    modules = MainActivity.Module.class
)
@Singleton
public interface MainActivityComponent extends ExampleApplicationComponent, GlobalComponent {
}

@AutoInjector

@AutoInjector allows to add injector methods into a generated component.

@AutoInjector(MainActivity.class)
public class ObjectA {
}

It updates the MainActivityComponent by adding the following method:

@Component(
    dependencies = ExampleApplicationComponent.class,
    modules = MainActivity.Module.class
)
@Singleton
public interface MainActivityComponent extends ExampleApplicationComponent, GlobalComponent {
  void inject(ObjectA objectA);
}

If you apply the @AutoInjector on the same class that has the @AutoComponent annotation, you can skip the value member:

@AutoComponent(
    dependencies = ExampleApplication.class,
    modules = MainActivity.Module.class,
    superinterfaces = {ExampleApplication.class, GlobalComponent.class})
@AutoInjector
@Singleton
public class MainActivity extends Activity {
}

If your class have parameterized type, you can also specify it:

@AutoInjector(value = MainActivity.class, parameterizedTypes = {String.class, String.class})
public class MyObject3<T, E> {
    private T t;
    private E e;
}

@AutoExpose

@AutoExpose allows to expose a dependency within a generated component.

@AutoExpose(MainActivity.class)
@Singleton
public class SomeObject {

    @Inject
    public SomeObject() {
    }
}

It updates the MainActivityComponent by adding the following method:

@Component(
    dependencies = ExampleApplicationComponent.class,
    modules = MainActivity.Module.class
)
@Singleton
public interface MainActivityComponent extends ExampleApplicationComponent, GlobalComponent {
  SomeObject someObject();
}

If you apply the @AutoExpose on the same class that has the @AutoComponent annotation, you can skip the value member:

@AutoComponent(
    dependencies = ExampleApplication.class,
    modules = MainActivity.Module.class,
    superinterfaces = {ExampleApplication.class, GlobalComponent.class})
@AutoExpose
@Singleton
public class MainActivity extends Activity {
}

@AutoExpose can also expose dependency from a module's provider method:

@dagger.Module
public class Module {
    @Provides
    @Singleton
    @AutoExpose(MainActivity.class)
    public SomeOtherObject providesSomeOtherObject() {
        return new SomeOtherObject();
    }
}

If your class have parameterized type, you can also specify it:

@AutoExpose(value = MainActivity.class, parameterizedTypes = {String.class, String.class})
@Singleton
public class MyObject3<T, E> {
    private T t;
    private E e;

    @Inject
    public MyObject3() {
    }
}

Reuse @AutoComponent

You can reuse @AutoComponent by creating an annotation that is itself annotated with @AutoComponent.

@AutoComponent(
        dependencies = MyApp.class,
        superinterfaces = {HasDependenciesOne.class, HasDependenciesTwo.class},
        modules = StandardModule.class
)
public @interface StandardActivityComponent { }

You can then create an auto component that reuse directly that annotation.
It will adds to the already defined dependencies, modules and superinterfaces.

@AutoComponent(
        modules = SixthActivity.Module.class,
        includes = StandardActivityComponent.class)
@Singleton
public class SixthActivity extends Activity { }

You can also directly annotate the class:

@StandardActivityComponent
@Singleton
public class SixthActivity extends Activity { }

Scope

Whenever you use @AutoComponent, you also need to annotate the class with a dagger scope annotation (an annotation that is itself annotated with @Scope). Auto Dagger2 will detect this annotation, and will apply it on the generated component.

If you don't provide scope annotation, the generated component will be unscoped.

Installation

Beware that the groupId changed to com.github.lukaspili.autodagger2

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
		classpath 'com.android.tools.build:gradle:1.1.3'
		classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    apt 'com.github.lukaspili.autodagger2:autodagger2-compiler:1.1'
    compile 'com.github.lukaspili.autodagger2:autodagger2:1.1'

    apt 'com.google.dagger:dagger-compiler:2.0.1'
    compile 'com.google.dagger:dagger:2.0.1'
    provided 'javax.annotation:jsr250-api:1.0' // Android only
}

Status

Stable API.

Auto Dagger2 was extracted from Auto Mortar to work as a standalone library.
You can find more about Auto Mortar here: https://github.com/lukaspili/Auto-Mortar

Author

License

Auto Dagger2 is released under the MIT license. See the LICENSE file for details.

auto-dagger2's People

Contributors

lukaspili avatar onigirisan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

auto-dagger2's Issues

@Named annotations in constructor

Hi!
I'm using Architect-Robot and it seems that Auto-Dagger2 doesn't handle @Named arguments. E.g. I have the following class (with multiple Scheduler arguments):

@AutoStackable(
        component = @AutoComponent(includes = FromActivityAutoComponent.class),
        pathWithView = SignUpView.class
)
@AutoExpose
@DaggerScope(SignUpPresenter.class)
public class SignUpPresenter extends ViewPresenter<SignUpView> {

    private AccountManager accountManager;
    private Scheduler ioScheduler;
    private Scheduler mainScheduler;

    public SignUpPresenter(AccountManager accountManager, @Named("scheduler.io") Scheduler ioScheduler, @Named("scheduler.main") Scheduler mainScheduler) {
        this.accountManager = accountManager;
        this.ioScheduler = ioScheduler;
        this.mainScheduler = mainScheduler;
    }
}

During the build I get the following error:

Error:(19, 8) error: rx.Scheduler cannot be provided without an @Provides-annotated method.
architect.commons.view.PresentedLinearLayout.presenter
[injected field of type: ui.signup.SignUpPresenter presenter]
ui.signup.stackable.SignUpStackable.Module.providesPresenter(services.AccountManager accountManager, rx.Scheduler ioScheduler, rx.Scheduler mainScheduler)
[parameter: rx.Scheduler ioScheduler]

And I found that generated module looks as follows (without @Named annotations)

@dagger.Module
public class Module {
  @Provides
  @DaggerScope(SignUpPresenter.class)
  public SignUpPresenter providesPresenter(AccountManager accountManager, Scheduler ioScheduler, Scheduler mainScheduler) {
    return new SignUpPresenter(accountManager, ioScheduler, mainScheduler);
  }
}

Inheritance issue

I was implementing inheritance hierarchy with my screens and faced an issue with @AutoComponent.

I have next hierarchy of components:
-- MainActivityComponent
---- DrawerKeyComponent
------ ListKeyComponent

I got manually defined MainActivityComponent. DrawerKeyComponent and ListKeyComponent was generated with auto-dagger:

@ScopeSingleton(MainActivityComponent::class)
@Component(
    dependencies = arrayOf(AppComponent::class),
    modules = arrayOf(MainActivityModule::class)
)
interface MainActivityComponent : AppComponent
@AutoComponent(
    dependencies = arrayOf(MainActivityComponent::class),
    superinterfaces = arrayOf(MainActivityComponent::class)
)
@ScopeSingleton(DrawerKey::class)
class DrawerKey
@AutoComponent(
    dependencies = arrayOf(DrawerKeyComponent::class),
    superinterfaces = arrayOf(DrawerKeyComponent::class),
    modules = arrayOf(ListModule::class)
)
@ScopeSingleton(ListKey::class)
class ListKey

Auto-generated components:

@Generated("autodagger.compiler.AnnotationProcessor")
@Component(
    dependencies = MainActivityComponent.class
)
@ScopeSingleton(DrawerKey.class)
public interface DrawerKeyComponent extends MainActivityComponent
@Generated("autodagger.compiler.AnnotationProcessor")
@Component(
    modules = ListModule.class
)
@ScopeSingleton(ListKey.class)
public interface ListKeyComponent

For some reason ListKeyComponent was generated without inheritance from DrawerKeyComponent. So I tried to define DrawerKeyComponent manually and use @AutoComponent only for ListKey. Here what I get in a result:

@Component(dependencies = arrayOf(MainActivityComponent::class))
@ScopeSingleton(DrawerKey::class)
interface DrawerKeyComponent : MainActivityComponent
@Generated("autodagger.compiler.AnnotationProcessor")
@Component(
    dependencies = DrawerKeyComponent.class,
    modules = ListKeyModule.class
)
@ScopeSingleton(ListKey.class)
public interface ListKeyComponent extends DrawerKeyComponent

In this case inheritance was set properly.

I was also testing example-kotlin project and it seems that auto-dagger can't setup inheritance from auto-generated components at all. To reproduce this issue you can set in example-kotlin next declaration for Container1:

@AutoComponent(
        dependencies = arrayOf(KotlinMainActivityComponent::class), 
        superinterfaces = arrayOf(KotlinMainActivityComponent::class)
)
@ScopeSingleton(Container1::class)
class Container1 {}

Generated component will look like this:

@Generated("autodagger.compiler.AnnotationProcessor")
@Component
@ScopeSingleton(Container1.class)
public interface Container1Component {
}

In this case Container1Component is not extended from KotlinMainActivityComponent as expected.

Would be appreciated for any help.

@AutoSubcomponent annotation

I tried to extend the object graph to the subcomponent from a component, I have the component extend via the superinterface = SubComponentWorkAroundSuperinterface.class to get the method in the generated AppComponent

@AppScope(App.class) @AutoComponent(modules = App.AppModule.class, superinterfaces = SubComponentsWorkAround.class)
public class App extends Application{...}

@AppScope(App.class)  @Component(modules = User.UserModule.class)
public interface SubComponentsWorkAroundSuperinterface{
    ObjectType plus(ObjectModule objectModule);
}

but get the error "Members injection methods may only return the injected type or void."

Would an @UserScope @AutoSubcomponent(modules = User.UserModule.class) class User{...} solve this, allowing us to use the plus(), perhaps in conjunction with another new annotation that is a combination of the the two @AutoExpose + @AutoInjector = @AutoPlus?

@AutoInjector into multiple components

Hi,

Is there any reason why we cannot auto-inject a class from multiple components? Would that be some clue of bad design or would that be something which could be useful?

Using superinterfaces generates invalid component.

If I have

@AutoComponent(
        dependencies = ApplicationComponent.class,
        modules = MainModule.class)
@AutoInjector
@PerActivity
public class MainActivity extends Activity {
}

@AutoComponent(
        superinterfaces = MainActivity.class,
        modules = {MainModule.class},
        dependencies = { ApplicationComponent.class }
)
@AutoInjector
@PerActivity
public class MyFragment extends Fragment {
}

This generates the following component:

@Generated("autodagger.compiler.AnnotationProcessor")
@Component(
    dependencies = ApplicationComponent.class,
    modules = {
        MainModule.class
    }
)
public interface MyFragmentComponent extends MainActivity {
  void inject(MyFragment myFragment);
}

Notice it extends MainActivity instead of MainActivityComponent

If i try to do superinterfaces = MainActivityComponent.class
I'm getting an error:
Error:(37, 8) error: Invalid value: superinterfaces cannot reference generated class. Use the class that applies the @AutoComponent annotation.

Qualifiers do not get exposed when using @AutoExpose in module

Hi,

Thanks for the library, looks good. While migrating a current project to it, I got stuck with qualifiers: I have an EvenBus instance at the application level and an EvenBus instance at each activity level.

With basic Dagger 2, I deal with that with qualifier annotations such as:

/**
 * Qualifier for the module methods to annotate dependencies targeting the activities
 */
@Qualifier
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface ForActivity {
}

My (manually written) components then have methods like:

    @ForActivity
    Context activityContext();

    @ForActivity
    EventBus activityEventBus();

When I write the modules using Auto-Dagger2, I specify to auto-expose the class but the qualifier annotation does not get carried over.

        @Provides
        @ForApplication
        @AutoExpose(MyApp.class)
        public EventBus provideEventBus() {
            return EventBus.builder().build();
        }

That generates a method EventBus eventBus(); which does not have a qualifier and hence creates a compilation error later on when Dagger2 kicks in.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.