Skip to content

Commit c7261e2

Browse files
committed
Injectable fields on application bootstrap class fix jooby-project#491
1 parent e1b4dc3 commit c7261e2

6 files changed

Lines changed: 54 additions & 18 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.jooby.issues;
2+
3+
import javax.inject.Inject;
4+
import javax.inject.Named;
5+
6+
import org.jooby.test.ServerFeature;
7+
import org.junit.Test;
8+
9+
public class Issue491 extends ServerFeature {
10+
11+
@Inject
12+
@Named("application.port")
13+
private String applicationPort;
14+
15+
{
16+
17+
get("/491", () -> applicationPort);
18+
19+
}
20+
21+
@Test
22+
public void injectableFields() throws Exception {
23+
request()
24+
.get("/491")
25+
.expect("9999");
26+
}
27+
28+
}

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3648,6 +3648,9 @@ private void start(final String[] args, final Consumer<List<Route.Definition>> r
36483648

36493649
Logger log = logger(this);
36503650

3651+
// inject class
3652+
injector.injectMembers(this);
3653+
36513654
// start services
36523655
for (CheckedConsumer<Registry> onStart : this.onStart) {
36533656
onStart.accept(this);

jooby/src/test/java/org/jooby/JoobyTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ public Object m1() {
523523
expect(injector.getInstance(Config.class)).andReturn(config);
524524
expect(injector.getInstance(Route.KEY)).andReturn(Collections.emptySet());
525525
expect(injector.getInstance(WebSocket.KEY)).andReturn(Collections.emptySet());
526+
injector.injectMembers(isA(Jooby.class));
526527

527528
AppPrinter printer = unit.constructor(AppPrinter.class)
528529
.args(Set.class, Set.class, Config.class)
@@ -580,6 +581,7 @@ public void applicationSecret() throws Exception {
580581
expect(injector.getInstance(Config.class)).andReturn(config);
581582
expect(injector.getInstance(Route.KEY)).andReturn(Collections.emptySet());
582583
expect(injector.getInstance(WebSocket.KEY)).andReturn(Collections.emptySet());
584+
injector.injectMembers(isA(Jooby.class));
583585

584586
unit.mockStatic(Guice.class);
585587
expect(Guice.createInjector(eq(Stage.PRODUCTION), unit.capture(Module.class)))
@@ -988,6 +990,7 @@ public void stopOnServerFailure() throws Exception {
988990
expect(injector.getInstance(Config.class)).andReturn(config);
989991
expect(injector.getInstance(Route.KEY)).andReturn(Collections.emptySet());
990992
expect(injector.getInstance(WebSocket.KEY)).andReturn(Collections.emptySet());
993+
injector.injectMembers(isA(Jooby.class));
991994

992995
unit.mockStatic(Guice.class);
993996
expect(Guice.createInjector(eq(Stage.DEVELOPMENT), unit.capture(Module.class)))

jooby/src/test/java/org/jooby/test/JoobyRunner.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.jooby.test;
2020

21+
import java.lang.reflect.Field;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.List;
@@ -31,8 +32,6 @@
3132
import org.junit.runners.model.Statement;
3233

3334
import com.google.inject.Binder;
34-
import com.google.inject.Guice;
35-
import com.google.inject.name.Names;
3635
import com.typesafe.config.Config;
3736
import com.typesafe.config.ConfigFactory;
3837
import com.typesafe.config.ConfigValueFactory;
@@ -146,14 +145,29 @@ public void evaluate() throws Throwable {
146145
@Override
147146
protected Object createTest() throws Exception {
148147
Object test = super.createTest();
149-
Guice.createInjector(binder -> {
150-
binder.bind(Integer.class).annotatedWith(Names.named("port")).toInstance(port);
151-
binder.bind(Integer.class).annotatedWith(Names.named("securePort")).toInstance(securePort);
152-
}).injectMembers(test);
148+
Class<? extends Object> c = test.getClass();
149+
set(test, c, "port", port);
150+
set(test, c, "securePort", securePort);
153151

154152
return test;
155153
}
156154

155+
@SuppressWarnings("rawtypes")
156+
private void set(final Object test, final Class clazz, final String field, final Object value)
157+
throws Exception {
158+
try {
159+
Field f = clazz.getDeclaredField(field);
160+
f.setAccessible(true);
161+
f.set(test, value);
162+
} catch (NoSuchFieldException ex) {
163+
Class superclass = clazz.getSuperclass();
164+
if (superclass != Object.class) {
165+
set(test, superclass, field, value);
166+
}
167+
}
168+
169+
}
170+
157171
@Override
158172
protected Statement withAfterClasses(final Statement statement) {
159173
Statement next = super.withAfterClasses(statement);

jooby/src/test/java/org/jooby/test/ServerFeature.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
import java.io.IOException;
66

7-
import javax.inject.Inject;
8-
import javax.inject.Named;
9-
107
import org.apache.http.client.utils.URIBuilder;
118
import org.jooby.Jooby;
129
import org.junit.Before;
@@ -20,12 +17,8 @@ public abstract class ServerFeature extends Jooby {
2017

2118
public static boolean DEBUG = false;
2219

23-
@Named("port")
24-
@Inject
2520
protected int port;
2621

27-
@Named("securePort")
28-
@Inject
2922
protected int securePort;
3023

3124
public static String protocol = "http";

jooby/src/test/java/org/jooby/test/SseFeature.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import java.nio.charset.StandardCharsets;
66
import java.util.concurrent.CountDownLatch;
77

8-
import javax.inject.Inject;
9-
import javax.inject.Named;
10-
118
import org.jooby.Jooby;
129
import org.jooby.MediaType;
1310
import org.junit.After;
@@ -25,8 +22,6 @@
2522
@RunWith(JoobySuite.class)
2623
public abstract class SseFeature extends Jooby {
2724

28-
@Named("port")
29-
@Inject
3025
private int port;
3126

3227
private AsyncHttpClient client;

0 commit comments

Comments
 (0)