lukaspili / auto-dagger2 Goto Github PK
View Code? Open in Web Editor NEWAnnotation processor on top of Dagger 2 that generates components for you.
License: MIT License
Annotation processor on top of Dagger 2 that generates components for you.
License: MIT License
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.
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.
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
?
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.
Produces this kind of inject statement:
void inject(BusViewPresenter<V, S> busViewPresenter);
Skipping <V, S> should work?
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);
}
}
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.