This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyDependency {} | |
public class MyClass{ | |
private MyDependency dependency; | |
//Bad! ex 1 | |
public MyClass(){ | |
dependency = new MyDependency(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(modules = MyModule.class) | |
public interface MyComponent { | |
void inject(MyActivity Activity); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Factory<MainActivityPresenter> create(PresenterModule module, Provider<TwitterApi> twitterApiProvider) { | |
return new PresenterModule_ProvidesMainActivityPresenterFactory(module, twitterApiProvider); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Scope | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface ActivityScope { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
public class AppModule { | |
@Provides | |
@Singleton | |
MyClass providesMyClass(){ | |
return new MyClass(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(modules = {AppModule.class, OKModule.class}) | |
@Singleton | |
public interface BaseComponent { | |
MyClass getMyClass(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyClass{ | |
private int type = 0; | |
public MyClass(int type){ | |
this.type = type; | |
} | |
} | |
public class InjectableClass{ | |
MyClass myClass1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void injectMembers(MainActivity instance) { | |
if (instance == null) { | |
throw new NullPointerException("Cannot inject members into a null reference"); | |
} | |
supertypeInjector.injectMembers(instance); | |
instance.tweeterApi = tweeterApiProvider.get(); | |
instance.retrofit = retrofitProvider.get(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("unchecked") // cast only happens when result comes from the factory | |
@Override | |
public T get() { | |
// double-check idiom from EJ2: Item 71 | |
Object result = instance; | |
if (result == UNINITIALIZED) { | |
synchronized (this) { | |
result = instance; | |
if (result == UNINITIALIZED) { | |
instance = result = factory.get(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public String get() { | |
String provided = module.providesString(); | |
if (provided == null) { | |
throw new NullPointerException("Cannot return null from a non-@Nullable @Provides method"); | |
} | |
return provided; | |
} |