Skip to content

Commit

Permalink
guice: upgrade to 7.0.0 fix #2922
Browse files Browse the repository at this point in the history
- Also allow to inject "branch" of configuration
  • Loading branch information
jknack committed May 17, 2023
1 parent bc80114 commit 61dc762
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
21 changes: 17 additions & 4 deletions modules/jooby-guice/src/main/java/io/jooby/guice/JoobyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.inject.name.Names;
import com.google.inject.util.Types;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.ConfigValue;
import edu.umd.cs.findbugs.annotations.NonNull;
import io.jooby.Environment;
Expand Down Expand Up @@ -62,21 +63,21 @@ private void configureResources(ServiceRegistry registry) {
} else {
binding = bind(key.getType());
}
// Guice does not support jakarta inject yet
// https://github.com/google/guice/issues/1383
javax.inject.Provider legacyProvider = () -> provider.get();
binding.toProvider(legacyProvider);
binding.toProvider(provider);
}
}

/*package*/ void configureEnv(Environment env) {
Config config = env.getConfig();
// root nodes
traverse("", config.root());

// configuration properties
for (Map.Entry<String, ConfigValue> entry : config.entrySet()) {
String name = entry.getKey();
Named named = Names.named(name);
Object value = entry.getValue().unwrapped();

if (value instanceof List) {
List values = (List) value;
componentType(values)
Expand All @@ -92,6 +93,18 @@ private void configureResources(ServiceRegistry registry) {
}
}

/*package*/ void traverse(String p, ConfigObject root) {
root.forEach(
(n, v) -> {
if (v instanceof ConfigObject child) {
var path = p + n;
var named = Names.named(path);
bind(Config.class).annotatedWith(named).toProvider(child::toConfig);
traverse(path + ".", child);
}
});
}

private Stream<Class> componentType(List values) {
if (values.isEmpty()) {
// For empty list we generates a binding for primitive wrappers.
Expand Down
1 change: 0 additions & 1 deletion modules/jooby-guice/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
requires typesafe.config;
requires com.google.guice;
requires jakarta.inject;
requires javax.inject;
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<config.version>1.4.2</config.version>

<!-- dependency injection -->
<guice.version>5.1.0</guice.version>
<guice.version>7.0.0</guice.version>

<!-- logging -->
<logback-classic.version>1.4.7</logback-classic.version>
Expand Down
2 changes: 1 addition & 1 deletion tests/src/test/java/io/jooby/i2457/ControllerV12457.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ControllerV12457 {

private WelcomeService2457 welcomeService;

@javax.inject.Inject // Guice does not support jakarta annotations
@jakarta.inject.Inject
public ControllerV12457(WelcomeService2457 welcomeService) {
super();
this.welcomeService = welcomeService;
Expand Down
3 changes: 1 addition & 2 deletions tests/src/test/java/io/jooby/i2457/ControllerV22457.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
@Path("/")
public class ControllerV22457 {

@javax.inject.Inject // Guice does not support jakarta inject yet.
private WelcomeService2457 welcomeService;
@jakarta.inject.Inject private WelcomeService2457 welcomeService;

@GET("/welcome")
public String sayHi() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HealthController2457 {

private WelcomeService2457 welcomeService;

@javax.inject.Inject // Guice does not support jakarta annotations
@jakarta.inject.Inject
public HealthController2457(WelcomeService2457 welcomeService) {
super();
this.welcomeService = welcomeService;
Expand Down

0 comments on commit 61dc762

Please sign in to comment.