Skip to content

Commit

Permalink
jooby-apt: kotlin: should generates a code with !! null operator for …
Browse files Browse the repository at this point in the history
…nullable types

- fix #3507
  • Loading branch information
jknack committed Aug 22, 2024
1 parent c4744a0 commit 9366f99
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import javax.lang.model.element.VariableElement;

public interface AnnotationSupport {
Predicate<String> NULLABLE = name -> name.toLowerCase().endsWith(".nullable");
Predicate<String> NON_NULL =
name -> name.toLowerCase().endsWith(".nonnull") || name.toLowerCase().endsWith(".notnull");
Predicate<String> VALUE = "value"::equals;
Predicate<String> NAME = "name"::equals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
package io.jooby.internal.apt;

import static io.jooby.internal.apt.AnnotationSupport.NON_NULL;
import static io.jooby.internal.apt.AnnotationSupport.NULLABLE;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand All @@ -15,10 +18,6 @@
import javax.lang.model.element.VariableElement;

public class MvcParameter {
private static final Predicate<String> NULLABLE =
name -> name.toLowerCase().endsWith(".nullable");
private static final Predicate<String> NON_NULL =
name -> name.toLowerCase().endsWith(".nonnull") || name.toLowerCase().endsWith(".notnull");
private final MvcRoute route;
private final VariableElement parameter;
private final Map<String, AnnotationMirror> annotations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,13 @@ public List<String> generateHandlerCall(boolean kt) {
var returnTypeGenerics =
getReturnType().getArgumentsString(kt, false, Set.of(TypeKind.TYPEVAR));
var returnTypeString = type(kt, getReturnType().toString());
boolean nullable = false;
if (kt) {
nullable =
method.getAnnotationMirrors().stream()
.map(AnnotationMirror::getAnnotationType)
.map(Objects::toString)
.anyMatch(NULLABLE);
if (throwsException) {
buffer.add(statement("@Throws(Exception::class)"));
}
Expand Down Expand Up @@ -312,7 +318,7 @@ public List<String> generateHandlerCall(boolean kt) {
setUncheckedCast(true);
call = kt ? call + " as " + returnTypeString : "(" + returnTypeString + ") " + call;
}
buffer.add(statement(indent(2), "return ", call, semicolon(kt)));
buffer.add(statement(indent(2), "return ", call, kt && nullable ? "!!" : "", semicolon(kt)));
}
buffer.add(statement("}", System.lineSeparator()));
if (uncheckedCast) {
Expand Down
17 changes: 17 additions & 0 deletions modules/jooby-apt/src/test/java/tests/i3507/C3507.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package tests.i3507;

import edu.umd.cs.findbugs.annotations.Nullable;
import io.jooby.annotation.GET;
import io.jooby.annotation.QueryParam;

public class C3507 {
@GET("/3507")
@Nullable public String get(@QueryParam String query) {
return null;
}
}
27 changes: 27 additions & 0 deletions modules/jooby-apt/src/test/java/tests/i3507/Issue3507.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package tests.i3507;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import io.jooby.apt.ProcessorRunner;

public class Issue3507 {

@Test
public void shouldGenerateNullSafeKtReturnType() throws IOException {
new ProcessorRunner(new C3507())
.withSource(
true,
source -> {
assertTrue(source.contains("return c.get(ctx.query(\"query\").value())!!"));
});
}
}

0 comments on commit 9366f99

Please sign in to comment.