Skip to content

Commit 51a40d3

Browse files
committed
Fix jooby-project#1843 by generating valid type name for arrays.
1 parent 0a16808 commit 51a40d3

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,22 @@ private org.objectweb.asm.Type asmType(String type) {
163163
return CHAR_TYPE;
164164
case "char[]":
165165
return org.objectweb.asm.Type.getType(char[].class);
166-
case "String":
166+
case "java.lang.String":
167167
return org.objectweb.asm.Type.getType(String.class);
168-
case "String[]":
168+
case "java.lang.String[]":
169169
return org.objectweb.asm.Type.getType(String[].class);
170170
default:
171-
String prefix = "";
172-
if (type.endsWith("[]")) {
173-
prefix = "[";
171+
StringBuilder prefix = new StringBuilder();
172+
String postfix = "";
173+
while (type.endsWith("[]")) {
174+
prefix.append("[");
175+
type = type.substring(0, type.length() - 2);
174176
}
175-
return getObjectType(prefix + type.replace(".", "/"));
177+
if (prefix.length() > 0) {
178+
prefix.append("L");
179+
postfix = ";";
180+
}
181+
return getObjectType(prefix + type.replace(".", "/") + postfix);
176182
}
177183
}
178184

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package source;
2+
3+
import io.jooby.annotations.GET;
4+
5+
public class ArrayRoute {
6+
7+
@GET("/mypath")
8+
public String[] controllerMethodStrings() {
9+
return new String[] { "/mypath1", "/mypath2" };
10+
}
11+
12+
@GET("/mybeans")
13+
public Bean[] controllerMethodBeans() {
14+
return new Bean[] { new Bean(1), new Bean(2) };
15+
}
16+
17+
@GET("/mymultibeans")
18+
public Bean[][] controllerMethodMultiBeans() {
19+
return new Bean[][] { { new Bean(1) }, { new Bean(2) } };
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package source;
2+
3+
import java.util.Objects;
4+
5+
public class Bean {
6+
private final int id;
7+
8+
public Bean(int id) {
9+
this.id = id;
10+
}
11+
12+
@Override
13+
public boolean equals(Object o) {
14+
if (this == o) return true;
15+
if (o == null || getClass() != o.getClass()) return false;
16+
Bean bean = (Bean) o;
17+
return id == bean.id;
18+
}
19+
20+
@Override
21+
public int hashCode() {
22+
return Objects.hash(id);
23+
}
24+
}

modules/jooby-apt/src/test/java/tests/ModuleCompilerTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import io.jooby.StatusCode;
77
import io.jooby.apt.MvcModuleCompilerRunner;
88
import org.junit.jupiter.api.Test;
9+
import source.ArrayRoute;
10+
import source.Bean;
911
import source.GetPostRoute;
1012
import source.JavaBeanParam;
1113
import source.MinRoute;
@@ -21,6 +23,7 @@
2123
import java.util.List;
2224
import java.util.Map;
2325

26+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2427
import static org.junit.jupiter.api.Assertions.assertEquals;
2528
import static org.junit.jupiter.api.Assertions.assertNotNull;
2629
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -35,6 +38,26 @@ public void minRoute() throws Exception {
3538
});
3639
}
3740

41+
@Test
42+
public void arrayRoute() throws Exception {
43+
new MvcModuleCompilerRunner(new ArrayRoute())
44+
.module(app -> {
45+
MockRouter router = new MockRouter(app);
46+
47+
Object value = router.get("/mypath").value();
48+
assertTrue(value instanceof String[]);
49+
assertArrayEquals(new String[] { "/mypath1", "/mypath2" }, (String[]) value);
50+
51+
value = router.get("/mybeans").value();
52+
assertTrue(value instanceof Bean[]);
53+
assertArrayEquals(new Bean[] { new Bean(1), new Bean(2) }, (Bean[]) value);
54+
55+
value = router.get("/mymultibeans").value();
56+
assertTrue(value instanceof Bean[][]);
57+
assertArrayEquals(new Bean[][] { { new Bean(1) }, { new Bean(2) } }, (Bean[][]) value);
58+
});
59+
}
60+
3861
@Test
3962
public void routes() throws Exception {
4063
new MvcModuleCompilerRunner(new Routes())

0 commit comments

Comments
 (0)