Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit a80219a

Browse files
committed
make ensure routes from modules follow the order where they are defined
1 parent 9867c11 commit a80219a

6 files changed

Lines changed: 35 additions & 69 deletions

File tree

examples/src/main/java/jooby/MyApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ public class MyApp extends Jooby {
209209

210210
{
211211

212+
use(new HibernatePersistence(User.class));
213+
212214
use(new Jackson());
213215
use(new Hbs());
214216

@@ -232,8 +234,6 @@ public class MyApp extends Jooby {
232234
resp.send(em.createQuery("from User").getResultList());
233235
});
234236

235-
use(new HibernatePersistence(User.class));
236-
237237
}
238238

239239
public static void main(final String[] args) throws Exception {

jooby-core/src/main/java/jooby/Jooby.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public class Jooby {
287287
/**
288288
* Keep track of routes.
289289
*/
290-
private final Set<Object> routes = new LinkedHashSet<>();
290+
private final Set<Object> bag = new LinkedHashSet<>();
291291

292292
/**
293293
* Keep track of modules.
@@ -487,7 +487,7 @@ public void route(final Class<?> routeType) {
487487
} else {
488488
singletonRoutes.add(routeType);
489489
}
490-
routes.add(routeType);
490+
bag.add(routeType);
491491
}
492492

493493
/**
@@ -497,7 +497,7 @@ public void route(final Class<?> routeType) {
497497
* @return The same route definition.
498498
*/
499499
private RouteDefinition route(final RouteDefinition route) {
500-
routes.add(route);
500+
bag.add(route);
501501
return route;
502502
}
503503

@@ -511,6 +511,7 @@ private RouteDefinition route(final RouteDefinition route) {
511511
public Jooby use(final JoobyModule module) {
512512
requireNonNull(module, "A module is required.");
513513
modules.add(module);
514+
bag.add(module);
514515
return this;
515516
}
516517

@@ -605,21 +606,19 @@ public void configure(final Binder binder) {
605606
binder.bind(File.class).annotatedWith(Names.named("java.io.tmpdir"))
606607
.toInstance(new File(config.getString("java.io.tmpdir")));
607608

608-
// configure module
609-
modules.forEach(m -> {
610-
install(m, mode, config, binder);
611-
});
612-
613-
// Routes
614-
routes.forEach(candidate -> {
615-
if (candidate instanceof RouteDefinition) {
616-
definitions.addBinding().toInstance((RouteDefinition) candidate);
617-
} else {
618-
Class<?> routeClass = (Class<?>) candidate;
619-
Routes.routes(mode, routeClass)
620-
.forEach(route -> definitions.addBinding().toInstance(route));
621-
}
622-
});
609+
// modules and routes
610+
bag.forEach(candidate -> {
611+
if (candidate instanceof JoobyModule) {
612+
install((JoobyModule) candidate, mode, config, binder);
613+
} else
614+
if (candidate instanceof RouteDefinition) {
615+
definitions.addBinding().toInstance((RouteDefinition) candidate);
616+
} else {
617+
Class<?> routeClass = (Class<?>) candidate;
618+
Routes.routes(mode, routeClass)
619+
.forEach(route -> definitions.addBinding().toInstance(route));
620+
}
621+
});
623622

624623
// Singleton routes
625624
singletonRoutes.forEach(routeClass -> binder.bind(routeClass).in(Scopes.SINGLETON));

jooby-core/src/main/java/jooby/Route.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ interface Chain {
5353

5454
String name();
5555

56-
int index();
57-
5856
Map<String, String> vars();
5957

6058
List<MediaType> consume();

jooby-core/src/main/java/jooby/RouteDefinition.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Collections;
77
import java.util.List;
88
import java.util.Optional;
9-
import java.util.concurrent.atomic.AtomicInteger;
109

1110
import javax.annotation.Nonnull;
1211

@@ -75,11 +74,8 @@
7574
public class RouteDefinition {
7675

7776
private static final List<MediaType> ALL = ImmutableList.of(MediaType.all);
78-
private static final AtomicInteger INDEX = new AtomicInteger(-1);
7977

80-
private final int index = INDEX.incrementAndGet();
81-
82-
private String name = "route" + index;
78+
private String name = "anonymous";
8379

8480
/**
8581
* A route pattern.
@@ -148,10 +144,6 @@ public String verb() {
148144
return verb;
149145
}
150146

151-
public int index() {
152-
return index;
153-
}
154-
155147
public String name() {
156148
return name;
157149
}
@@ -235,6 +227,16 @@ public List<MediaType> produces() {
235227
return ImmutableList.copyOf(this.produces);
236228
}
237229

230+
@Override
231+
public String toString() {
232+
StringBuilder buffer = new StringBuilder();
233+
buffer.append(verb()).append(" ").append(pattern()).append("\n");
234+
buffer.append(" name: ").append(name()).append("\n");
235+
buffer.append(" consume: ").append(consumes()).append("\n");
236+
buffer.append(" produces: ").append(produces()).append("\n");
237+
return buffer.toString();
238+
}
239+
238240
public static RouteDefinition newRoute(final String verb, final String path,
239241
final Router router) {
240242
return new RouteDefinition(verb, path, (req, res, chain) -> {
@@ -249,7 +251,7 @@ public static RouteDefinition newRoute(final String verb, final String path,
249251
}
250252

251253
private Route asRoute(final RouteMatcher matcher, final List<MediaType> produces) {
252-
return new RouteImpl(filter, verb, matcher.path(), pattern, name, index, matcher.vars(), consumes,
254+
return new RouteImpl(filter, verb, matcher.path(), pattern, name, matcher.vars(), consumes,
253255
produces);
254256
}
255257

jooby-core/src/main/java/jooby/internal/RouteImpl.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public class RouteImpl implements Route, Filter {
2626

2727
private String name;
2828

29-
private int index;
30-
3129
private Map<String, String> vars;
3230

3331
private List<MediaType> consumes;
@@ -47,19 +45,18 @@ public static RouteImpl notFound(final String verb, final String path,
4745

4846
public static RouteImpl fromStatus(final Filter filter, final String verb, final String path,
4947
final HttpStatus status, final List<MediaType> produces) {
50-
return new RouteImpl(filter, verb, path, path, status.value() + "", -1, Collections.emptyMap(),
48+
return new RouteImpl(filter, verb, path, path, status.value() + "", Collections.emptyMap(),
5149
ALL, produces);
5250
}
5351

5452
public RouteImpl(final Filter filter, final String verb, final String path,
55-
final String pattern, final String name, final int index, final Map<String, String> vars,
53+
final String pattern, final String name, final Map<String, String> vars,
5654
final List<MediaType> consumes, final List<MediaType> produces) {
5755
this.filter = filter;
5856
this.verb = verb;
5957
this.path = path;
6058
this.pattern = pattern;
6159
this.name = name;
62-
this.index = index;
6360
this.vars = vars;
6461
this.consumes = consumes;
6562
this.produces = produces;
@@ -91,11 +88,6 @@ public String name() {
9188
return name;
9289
}
9390

94-
@Override
95-
public int index() {
96-
return index;
97-
}
98-
9991
@Override
10092
public Map<String, String> vars() {
10193
return vars;
@@ -116,7 +108,6 @@ public String toString() {
116108
StringBuilder buffer = new StringBuilder();
117109
buffer.append(verb()).append(" ").append(path()).append("\n");
118110
buffer.append(" pattern: ").append(pattern()).append("\n");
119-
buffer.append(" index: ").append(index()).append("\n");
120111
buffer.append(" name: ").append(name()).append("\n");
121112
buffer.append(" vars: ").append(vars()).append("\n");
122113
buffer.append(" consume: ").append(consume()).append("\n");

jooby-tests/src/test/java/jooby/RouteReferenceFeature.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,17 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
55

6-
import java.lang.reflect.Field;
7-
import java.util.concurrent.atomic.AtomicInteger;
8-
96
import org.apache.http.client.fluent.Request;
107
import org.junit.Test;
118

129
public class RouteReferenceFeature extends ServerFeature {
1310

14-
static {
15-
/**
16-
* reset index when running multiple tests at once, either from IDE or Maven.
17-
* Index is produced by a static var and when running multiples tests the number of routes
18-
* wont matches.
19-
*
20-
* I'm sure something better can be done... later
21-
*/
22-
try {
23-
Field field = RouteDefinition.class.getDeclaredField("INDEX");
24-
field.setAccessible(true);
25-
AtomicInteger index = (AtomicInteger) field.get(null);
26-
index.set(-1);
27-
} catch (Exception ex) {
28-
// ignored
29-
ex.printStackTrace();
30-
}
31-
}
32-
3311
{
3412

3513
get("/", (req, res) -> {
3614
Route route = req.route();
3715
assertNotNull(route);
38-
assertEquals(0, route.index());
39-
assertEquals("route0", route.name());
16+
assertEquals("anonymous", route.name());
4017
assertEquals("GET", route.verb());
4118
assertEquals("/", route.path());
4219
assertEquals("/", route.pattern());
@@ -46,8 +23,7 @@ public class RouteReferenceFeature extends ServerFeature {
4623
get("/:var", (req, res) -> {
4724
Route route = req.route();
4825
assertNotNull(route);
49-
assertEquals(1, route.index());
50-
assertEquals("route1", route.name());
26+
assertEquals("anonymous", route.name());
5127
assertEquals("GET", route.verb());
5228
assertEquals("/" + req.param("var").stringValue(), route.path());
5329
assertEquals("/:var", route.pattern());

0 commit comments

Comments
 (0)