Skip to content

Commit 9366f99

Browse files
committed
jooby-apt: kotlin: should generates a code with !! null operator for nullable types
- fix #3507
1 parent c4744a0 commit 9366f99

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import javax.lang.model.element.VariableElement;
1818

1919
public interface AnnotationSupport {
20+
Predicate<String> NULLABLE = name -> name.toLowerCase().endsWith(".nullable");
21+
Predicate<String> NON_NULL =
22+
name -> name.toLowerCase().endsWith(".nonnull") || name.toLowerCase().endsWith(".notnull");
2023
Predicate<String> VALUE = "value"::equals;
2124
Predicate<String> NAME = "name"::equals;
2225

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
package io.jooby.internal.apt;
77

8+
import static io.jooby.internal.apt.AnnotationSupport.NON_NULL;
9+
import static io.jooby.internal.apt.AnnotationSupport.NULLABLE;
10+
811
import java.util.*;
912
import java.util.function.Predicate;
1013
import java.util.stream.Collectors;
@@ -15,10 +18,6 @@
1518
import javax.lang.model.element.VariableElement;
1619

1720
public class MvcParameter {
18-
private static final Predicate<String> NULLABLE =
19-
name -> name.toLowerCase().endsWith(".nullable");
20-
private static final Predicate<String> NON_NULL =
21-
name -> name.toLowerCase().endsWith(".nonnull") || name.toLowerCase().endsWith(".notnull");
2221
private final MvcRoute route;
2322
private final VariableElement parameter;
2423
private final Map<String, AnnotationMirror> annotations;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,13 @@ public List<String> generateHandlerCall(boolean kt) {
209209
var returnTypeGenerics =
210210
getReturnType().getArgumentsString(kt, false, Set.of(TypeKind.TYPEVAR));
211211
var returnTypeString = type(kt, getReturnType().toString());
212+
boolean nullable = false;
212213
if (kt) {
214+
nullable =
215+
method.getAnnotationMirrors().stream()
216+
.map(AnnotationMirror::getAnnotationType)
217+
.map(Objects::toString)
218+
.anyMatch(NULLABLE);
213219
if (throwsException) {
214220
buffer.add(statement("@Throws(Exception::class)"));
215221
}
@@ -312,7 +318,7 @@ public List<String> generateHandlerCall(boolean kt) {
312318
setUncheckedCast(true);
313319
call = kt ? call + " as " + returnTypeString : "(" + returnTypeString + ") " + call;
314320
}
315-
buffer.add(statement(indent(2), "return ", call, semicolon(kt)));
321+
buffer.add(statement(indent(2), "return ", call, kt && nullable ? "!!" : "", semicolon(kt)));
316322
}
317323
buffer.add(statement("}", System.lineSeparator()));
318324
if (uncheckedCast) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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.i3507;
7+
8+
import edu.umd.cs.findbugs.annotations.Nullable;
9+
import io.jooby.annotation.GET;
10+
import io.jooby.annotation.QueryParam;
11+
12+
public class C3507 {
13+
@GET("/3507")
14+
@Nullable public String get(@QueryParam String query) {
15+
return null;
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.i3507;
7+
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
import java.io.IOException;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
import io.jooby.apt.ProcessorRunner;
15+
16+
public class Issue3507 {
17+
18+
@Test
19+
public void shouldGenerateNullSafeKtReturnType() throws IOException {
20+
new ProcessorRunner(new C3507())
21+
.withSource(
22+
true,
23+
source -> {
24+
assertTrue(source.contains("return c.get(ctx.query(\"query\").value())!!"));
25+
});
26+
}
27+
}

0 commit comments

Comments
 (0)