Skip to content

Commit 60367c1

Browse files
committed
jooby-apt generates ambiguous constructors for controllers with a single argument constructor w/ single methods
-ref #3756
1 parent 9add8ee commit 60367c1

File tree

8 files changed

+140
-3
lines changed

8 files changed

+140
-3
lines changed

modules/jooby-apt/src/main/resources/io/jooby/internal/apt/Source.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ${generatedClassName} implements io.jooby.Extension {
99
}
1010

1111
public ${generatedClassName}(io.jooby.SneakyThrows.Supplier<${className}> provider) {
12-
setup(ctx -> provider.get());
12+
setup(ctx -> (${className}) provider.get());
1313
}
1414

1515
public ${generatedClassName}(io.jooby.SneakyThrows.Function<Class<${className}>, ${className}> provider) {

modules/jooby-apt/src/test/java/io/jooby/apt/ProcessorRunner.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ protected void onGeneratedSource(JavaFileObject source) {
9090
}
9191
}
9292

93+
private final Object instance;
9394
private final HookJoobyProcessor processor;
9495

9596
public ProcessorRunner(Object instance) throws IOException {
@@ -106,6 +107,7 @@ public ProcessorRunner(Object instance, Map<String, Object> options) throws IOEx
106107

107108
public ProcessorRunner(Object instance, Consumer<String> stdout, Map<String, Object> options)
108109
throws IOException {
110+
this.instance = instance;
109111
this.processor = new HookJoobyProcessor(stdout::accept);
110112
var optionsArray =
111113
options.entrySet().stream().map(e -> "-A" + e.getKey() + "=" + e.getValue()).toList();
@@ -126,8 +128,14 @@ public ProcessorRunner withRouter(SneakyThrows.Consumer2<Jooby, JavaFileObject>
126128
var classLoader = processor.createClassLoader();
127129
var factoryName = classLoader.getClassName();
128130
var factoryClass = (Class<? extends Extension>) classLoader.loadClass(factoryName);
129-
var constructor = factoryClass.getDeclaredConstructor();
130-
var extension = constructor.newInstance();
131+
Extension extension;
132+
try {
133+
var constructor = factoryClass.getDeclaredConstructor();
134+
extension = constructor.newInstance();
135+
} catch (NoSuchMethodException x) {
136+
extension = factoryClass.getDeclaredConstructor(instance.getClass()).newInstance(instance);
137+
}
138+
131139
var application = new Jooby();
132140
application.install(extension);
133141
consumer.accept(application, processor.getSource());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.i3756;
7+
8+
import io.jooby.annotation.GET;
9+
import io.jooby.annotation.Path;
10+
11+
@Path("/C3756")
12+
public class C3756 {
13+
private final S3756 s3756;
14+
15+
public C3756(S3756 s3756) {
16+
super();
17+
this.s3756 = s3756;
18+
}
19+
20+
@GET
21+
public String handle() {
22+
s3756.accept("hello");
23+
return "hello";
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.i3756;
7+
8+
import static org.junit.jupiter.api.Assertions.assertFalse;
9+
import static org.junit.jupiter.api.Assertions.assertNotNull;
10+
11+
import org.junit.jupiter.api.Test;
12+
13+
import io.jooby.apt.ProcessorRunner;
14+
15+
public class Issue3756 {
16+
@Test
17+
public void shouldCompile() throws Exception {
18+
// must compile
19+
new ProcessorRunner(new C3756((S3756) s -> {}))
20+
.withRouter(
21+
(app, source) -> {
22+
System.out.println(source);
23+
var routes = app.getRoutes();
24+
assertNotNull(routes);
25+
assertFalse(routes.isEmpty());
26+
var route = app.getRoutes().get(0);
27+
assertNotNull(route);
28+
});
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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.i3756;
7+
8+
public interface S3756 {
9+
void accept(String s);
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 io.jooby.i3756;
7+
8+
import io.jooby.annotation.GET;
9+
import io.jooby.annotation.Path;
10+
11+
@Path("/3756")
12+
public class C3756 {
13+
private final S3756 s3756;
14+
15+
public C3756(S3756 s3756) {
16+
this.s3756 = s3756;
17+
}
18+
19+
@GET
20+
public String handle() {
21+
s3756.accept("hello");
22+
return "hello";
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 io.jooby.i3756;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
import io.jooby.junit.ServerTest;
11+
import io.jooby.junit.ServerTestRunner;
12+
13+
public class Issue3756 {
14+
@ServerTest
15+
public void shouldCompileConflictConstructor(ServerTestRunner runner) {
16+
runner
17+
.define(
18+
app -> {
19+
app.mvc(new C3756_(s -> {}));
20+
})
21+
.ready(
22+
http -> {
23+
http.get(
24+
"/3756",
25+
rsp -> {
26+
assertEquals("hello", rsp.body().string());
27+
});
28+
});
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 io.jooby.i3756;
7+
8+
public interface S3756 {
9+
void accept(String s);
10+
}

0 commit comments

Comments
 (0)