Auto Dagger2 Compiler Utils

Auto Dagger2

Лицензия

Лицензия

Категории

Категории

Auto Библиотеки уровня приложения Code Generators Dagger Dependency Injection
Группа

Группа

com.github.lukaspili.autodagger2
Идентификатор

Идентификатор

autodagger2-compiler-utils
Последняя версия

Последняя версия

1.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

Auto Dagger2 Compiler Utils
Auto Dagger2
Ссылка на сайт

Ссылка на сайт

https://github.com/lukaspili/auto-dagger2
Система контроля версий

Система контроля версий

https://github.com/lukaspili/auto-dagger2

Скачать autodagger2-compiler-utils

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.github.lukaspili.autodagger2/autodagger2-compiler-utils/ -->
<dependency>
    <groupId>com.github.lukaspili.autodagger2</groupId>
    <artifactId>autodagger2-compiler-utils</artifactId>
    <version>1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.lukaspili.autodagger2/autodagger2-compiler-utils/
implementation 'com.github.lukaspili.autodagger2:autodagger2-compiler-utils:1.1'
// https://jarcasting.com/artifacts/com.github.lukaspili.autodagger2/autodagger2-compiler-utils/
implementation ("com.github.lukaspili.autodagger2:autodagger2-compiler-utils:1.1")
'com.github.lukaspili.autodagger2:autodagger2-compiler-utils:jar:1.1'
<dependency org="com.github.lukaspili.autodagger2" name="autodagger2-compiler-utils" rev="1.1">
  <artifact name="autodagger2-compiler-utils" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.lukaspili.autodagger2', module='autodagger2-compiler-utils', version='1.1')
)
libraryDependencies += "com.github.lukaspili.autodagger2" % "autodagger2-compiler-utils" % "1.1"
[com.github.lukaspili.autodagger2/autodagger2-compiler-utils "1.1"]

Зависимости

compile (6)

Идентификатор библиотеки Тип Версия
com.squareup : javapoet jar 1.1.0
com.google.guava : guava jar 18.0
com.google.auto : auto-common jar 0.4
com.github.lukaspili.processor-workflow : processor-workflow jar 1.0.1
org.apache.commons : commons-lang3 jar 3.4
com.github.lukaspili.autodagger2 : autodagger2 jar 1.1

Модули Проекта

Данный проект не имеет модулей.

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.

Версии библиотеки

Версия
1.1