Skip to content

Commit c26a169

Browse files
committed
MVC Code generation fails to declare exception when constructor throws a checked exception
- fix #3737
1 parent 9b83366 commit c26a169

File tree

4 files changed

+86
-7
lines changed

4 files changed

+86
-7
lines changed

jooby/src/main/java/io/jooby/SneakyThrows.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,28 @@ public static <V> Supplier<V> throwingSupplier(Supplier<V> fn) {
899899
return fn;
900900
}
901901

902+
/**
903+
* Singleton supplier from existing instance.
904+
*
905+
* @param instance Singleton.
906+
* @return Single supplier.
907+
* @param <V> Resulting value.
908+
*/
909+
public static <V> Supplier<V> singleton(V instance) {
910+
return () -> instance;
911+
}
912+
913+
/**
914+
* Singleton supplier from another supplier.
915+
*
916+
* @param fn Supplier.
917+
* @return Single supplier.
918+
* @param <V> Resulting value.
919+
*/
920+
public static <V> Supplier<V> singleton(Supplier<V> fn) {
921+
return singleton(fn.get());
922+
}
923+
902924
/**
903925
* Factory method for {@link Function} and {@link java.util.function.Function}.
904926
*

modules/jooby-apt/src/main/java/io/jooby/internal/apt/MvcRouter.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,22 @@ private StringBuilder constructors(String generatedName, boolean kt) {
224224
buffer,
225225
List.of(),
226226
(output, params) -> {
227-
output
228-
.append("this(")
229-
.append(kt ? "" : "new ")
230-
.append(targetType)
231-
.append("())")
232-
.append(semicolon(kt))
233-
.append(System.lineSeparator());
227+
if (kt) {
228+
output
229+
.append("this(")
230+
.append(targetType)
231+
.append("())")
232+
.append(semicolon(true))
233+
.append(System.lineSeparator());
234+
} else {
235+
output
236+
.append("this(")
237+
.append("io.jooby.SneakyThrows.singleton(")
238+
.append(targetType)
239+
.append("::new))")
240+
.append(semicolon(false))
241+
.append(System.lineSeparator());
242+
}
234243
});
235244
}
236245
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3737;
7+
8+
import io.jooby.annotation.GET;
9+
import io.jooby.annotation.Path;
10+
11+
@Path("/3737")
12+
public class C3737 {
13+
14+
public C3737() throws Exception {}
15+
16+
@GET("/hello")
17+
public String hello() {
18+
return "hello world";
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3737;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import io.jooby.apt.ProcessorRunner;
13+
14+
public class Issue3737 {
15+
@Test
16+
public void shouldNotFail() throws Exception {
17+
// must compile
18+
new ProcessorRunner(new C3737())
19+
.withRouter(
20+
(app, source) -> {
21+
var routes = app.getRoutes();
22+
assertNotNull(routes);
23+
assertFalse(routes.isEmpty());
24+
var route = app.getRoutes().get(0);
25+
assertNotNull(route);
26+
});
27+
}
28+
}

0 commit comments

Comments
 (0)