Skip to content

Commit b82cde4

Browse files
committed
apt: Add parameter nullability support
1 parent 84ee97b commit b82cde4

7 files changed

Lines changed: 60 additions & 42 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ private void processArguments(ClassWriter classWriter, MethodVisitor visitor) th
186186
}
187187
}
188188

189+
190+
189191
private void processReturnType(MethodVisitor visitor) throws Exception {
190192
TypeKind kind = executable.getReturnType().getKind();
191193
if (kind == TypeKind.VOID) {

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

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -160,35 +160,37 @@ private Class[] builtinTypes() {
160160
}
161161

162162
public Method getMethod() throws NoSuchMethodException {
163-
if (is(String.class)) {
164-
return Value.class.getDeclaredMethod("value");
165-
}
166-
if (is(int.class)) {
167-
return Value.class.getDeclaredMethod("intValue");
168-
}
169-
if (is(byte.class)) {
170-
return Value.class.getDeclaredMethod("byteValue");
171-
}
172-
if (is(long.class)) {
173-
return Value.class.getDeclaredMethod("longValue");
174-
}
175-
if (is(float.class)) {
176-
return Value.class.getDeclaredMethod("floatValue");
177-
}
178-
if (is(double.class)) {
179-
return Value.class.getDeclaredMethod("doubleValue");
180-
}
181-
if (is(boolean.class)) {
182-
return Value.class.getDeclaredMethod("booleanValue");
183-
}
184-
if (is(Optional.class, String.class)) {
185-
return Value.class.getDeclaredMethod("toOptional");
186-
}
187-
if (is(List.class, String.class)) {
188-
return Value.class.getDeclaredMethod("toList");
189-
}
190-
if (is(Set.class, String.class)) {
191-
return Value.class.getDeclaredMethod("toSet");
163+
if (!isNullable()) {
164+
if (is(String.class)) {
165+
return Value.class.getDeclaredMethod("value");
166+
}
167+
if (is(int.class)) {
168+
return Value.class.getDeclaredMethod("intValue");
169+
}
170+
if (is(byte.class)) {
171+
return Value.class.getDeclaredMethod("byteValue");
172+
}
173+
if (is(long.class)) {
174+
return Value.class.getDeclaredMethod("longValue");
175+
}
176+
if (is(float.class)) {
177+
return Value.class.getDeclaredMethod("floatValue");
178+
}
179+
if (is(double.class)) {
180+
return Value.class.getDeclaredMethod("doubleValue");
181+
}
182+
if (is(boolean.class)) {
183+
return Value.class.getDeclaredMethod("booleanValue");
184+
}
185+
if (is(Optional.class, String.class)) {
186+
return Value.class.getDeclaredMethod("toOptional");
187+
}
188+
if (is(List.class, String.class)) {
189+
return Value.class.getDeclaredMethod("toList");
190+
}
191+
if (is(Set.class, String.class)) {
192+
return Value.class.getDeclaredMethod("toSet");
193+
}
192194
}
193195
// toOptional(Class)
194196
if (isOptional()) {

modules/jooby-apt/src/main/java/io/jooby/internal/apt/asm/RouteAttributesWriter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
16
package io.jooby.internal.apt.asm;
27

38
import io.jooby.Route;

modules/jooby-apt/src/test/java/output/ASMPrinter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public void mvcExtension() throws Exception {
2222
ASMifier.main(new String[] {MvcExtension.class.getName() });
2323
}
2424

25+
@Test
26+
public void mvcDispatch() throws Exception {
27+
ASMifier.main(new String[] {MvcDispatch.class.getName() });
28+
}
29+
2530
@Test
2631
public void myController() throws Exception {
2732
ASMifier.main(new String[] {MyControllerHandler.class.getName() });
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package output;
2+
3+
import io.jooby.Jooby;
4+
5+
public class MvcDispatch implements Runnable {
6+
private Jooby application;
7+
8+
public MvcDispatch(Jooby application) {
9+
this.application = application;
10+
}
11+
12+
@Override public void run() {
13+
application.get("/", ctx -> "xx");
14+
}
15+
}

modules/jooby-apt/src/test/java/output/MvcExtension.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ public MvcExtension(Provider c) {
1717
}
1818

1919
@Override public void install(@Nonnull Jooby application) throws Exception {
20-
Route route = application.get("/", ctx -> "..");
21-
route.attribute("str", "string");
22-
route.attribute("annotation", newMap());
23-
}
24-
25-
private static Map<String, Object> newMap() {
26-
Map<String, Object> map = new HashMap<>();
27-
map.put("LinkAnnotation", "v");
28-
return map;
20+
application.dispatch(new MvcDispatch(application));
2921
}
3022
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,8 @@ public void body() throws Exception {
319319
assertEquals(map, handler.apply(ctx));
320320
})
321321
.compile("POST", "/p/bodyStringParam", handler -> {
322-
Body body = mock(Body.class);
323-
when(body.value()).thenReturn("...");
324-
325322
Context ctx = mockContext("POST", "/p/bodyStringParam");
326-
when(ctx.body()).thenReturn(body);
323+
when(ctx.body(String.class)).thenReturn("...");
327324

328325
assertEquals("...", handler.apply(ctx));
329326
})

0 commit comments

Comments
 (0)