Skip to content

Commit d6e3483

Browse files
committed
Guice @Inject doesn't create default constructor for MVC routes
- fix #3567
1 parent 5a5dd30 commit d6e3483

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

modules/jooby-apt/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
<scope>test</scope>
3333
</dependency>
3434

35+
<dependency>
36+
<groupId>com.google.inject</groupId>
37+
<artifactId>guice</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
3541
<!-- Test dependencies -->
3642
<dependency>
3743
<groupId>com.google.testing.compile</groupId>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ private StringBuilder constructors(String generatedName, boolean kt) {
298298
}
299299

300300
private static Predicate<Element> hasInjectAnnotation() {
301-
var injectAnnotations = Set.of("javax.inject.Inject", "jakarta.inject.Inject");
301+
var injectAnnotations =
302+
Set.of("javax.inject.Inject", "jakarta.inject.Inject", "com.google.inject.Inject");
302303
return it ->
303304
injectAnnotations.stream()
304305
.anyMatch(annotation -> findAnnotationByName(it, annotation) != null);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.i3567;
7+
8+
import com.google.inject.Inject;
9+
import com.typesafe.config.Config;
10+
import io.jooby.annotation.GET;
11+
import io.jooby.annotation.QueryParam;
12+
13+
public class C3567 {
14+
15+
private final Config config;
16+
17+
@Inject
18+
public C3567(Config config) {
19+
this.config = config;
20+
}
21+
22+
@GET("/3567")
23+
public String webMethod(@QueryParam String property) {
24+
return config.getString(property);
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.i3567;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import com.typesafe.config.ConfigFactory;
13+
import com.typesafe.config.ConfigValueFactory;
14+
import io.jooby.apt.ProcessorRunner;
15+
16+
public class Issue3567 {
17+
18+
@Test
19+
public void shouldSupportGoogleInjectAnnotation() throws Exception {
20+
var value = "bar";
21+
var conf = ConfigFactory.empty().withValue("foo", ConfigValueFactory.fromAnyRef(value));
22+
new ProcessorRunner(new C3567(conf))
23+
.withSourceCode(
24+
source -> {
25+
assertTrue(
26+
source.contains(
27+
"public C3567_() {\n"
28+
+ " this(C3567.class);\n"
29+
+ " }\n"
30+
+ "\n"
31+
+ " public C3567_(Class<C3567> type) {\n"
32+
+ " this(ctx -> ctx.require(type));\n"
33+
+ " }"));
34+
});
35+
}
36+
}

0 commit comments

Comments
 (0)