Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Dec 8, 2023
1 parent e88cc21 commit f964041
Showing 1 changed file with 4 additions and 73 deletions.
77 changes: 4 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ If you inject `Provider<TheBean>`, 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
Expand Down Expand Up @@ -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<T>`, please be sure `Provider<T>` 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<Heater> heaterFactory;

@Inject
static Heater getInstance() {
if (heaterFactory == null) {
heaterFactory = ElectricHeater::new;
}
return heaterFactory.get();
}

// only for testing
static void setHeaterFactory(Supplier<Heater> 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<T>`, please check if `Provider<T>` 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
Expand Down

0 comments on commit f964041

Please sign in to comment.