From f96404125323e6997965384eab99c84882db6fa8 Mon Sep 17 00:00:00 2001 From: h908714124 Date: Fri, 8 Dec 2023 10:48:57 +0100 Subject: [PATCH] Update README.md --- README.md | 77 +++---------------------------------------------------- 1 file changed, 4 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 1e84bc5..6ec28c1 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,8 @@ If you inject `Provider`, rather than `TheBean` directly, calling `prov ### Mocking -If you want create a component where some means are swapped for mock instance, use `@Component(mockBuilder = true)`. -The mocks can then be injected in to the component using the `mockBuilder` method. +If you want create a component where some beans are swapped for mock instances, use `@Component(mockBuilder = true)`. +The mocks can then be injected into the component using the `mockBuilder` method. For [example](https://github.com/jbock-java/modular-thermosiphon): ```java @@ -63,80 +63,11 @@ There is no `@AssistedInject`, it's a can of worms. There is no `@IntoList` or `@IntoSet`, you can return these collections from a `@Provides` method. -There is no `Lazy`, please be sure `Provider` does not cover your case before opening an issue. - -### Do more with less - -* Works with both `javax.inject.Inject` or `jakarta.inject.Inject`. -* Includes its own copy of the JSR-330 annotations, excluding `@Scope` and `@Singleton`, so you don't *have* to depend on one of these. -* Allows injection into static method. -* No typecasts in generated code. Yes, dagger does this, if some of your beans are package-private. Ew, gross! -* Generates only the component implementation and nothing else, so it doesn't bloat your jar as much. - -The new feature, "injection into static method" is only allowed if the method's return value matches the type of the enclosing class. -The following is allowed because the method returns `Heater`: - -```java -public interface Heater { - @Inject - static Heater getInstance() { - return new ElectricHeater(); - } - - //... -} -``` - -In the next example, the method `setHeaterFactory` *could* be used to sneak in a mock `Heater` for testing purpose, if invoked early in the test, before the component is created: - -```java -public interface Heater { - - private static Supplier heaterFactory; - - @Inject - static Heater getInstance() { - if (heaterFactory == null) { - heaterFactory = ElectricHeater::new; - } - return heaterFactory.get(); - } - - // only for testing - static void setHeaterFactory(Supplier mockHeaterFactory) { - heaterFactory = mockHeaterFactory; - } - - //... -} -``` - -By the way you can have more than one "static inject" method in the same class, if you add a qualifier: - -```java -public interface Heater { - - @Named("electric") - @Inject - static Heater createElectricHeater() { - return ElectricHeater.getInstance(); - } - - @Named("diesel") - @Inject - static Heater createDieselHeater() { - return DieselHeater.getInstance(); - } - - //... -} -``` - - +There is no `Lazy`, please check if `Provider` covers your use case. ### Samples -* [coffee example from dagger](https://github.com/jbock-java/modular-thermosiphon) +* [modular-thermosiphon (dagger's "coffee machine" demo)](https://github.com/jbock-java/modular-thermosiphon) * [jbock](https://github.com/jbock-java/jbock) uses it, see for example [ValidateComponent](https://github.com/jbock-java/jbock/blob/master/compiler/src/main/java/net/jbock/validate/ValidateComponent.java) ### Alternatives