Skip to content

Commit 752bf87

Browse files
committed
#22 - ENH: Support Kotlin non-nullable properties and constructor params
1 parent 0634bac commit 752bf87

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/main/java/io/dinject/javalin/generator/ElementReader.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class ElementReader {
3232
private boolean impliedParamType;
3333
private String paramDefault;
3434

35+
private boolean notNullKotlin;
36+
private boolean notNullJavax;
37+
3538
ElementReader(Element element, ProcessingContext ctx, ParamType defaultType, boolean formMarker) {
3639
this(element, typeDef(element.asType()), ctx, defaultType, formMarker);
3740
}
@@ -51,6 +54,10 @@ public class ElementReader {
5154
}
5255

5356
private void readAnnotations(Element element, ParamType defaultType) {
57+
58+
notNullKotlin = (element.getAnnotation(org.jetbrains.annotations.NotNull.class) != null);
59+
notNullJavax = (element.getAnnotation(javax.validation.constraints.NotNull.class) != null);
60+
5461
Default defaultVal = element.getAnnotation(Default.class);
5562
if (defaultVal != null) {
5663
this.paramDefault = defaultVal.value();
@@ -234,7 +241,14 @@ private boolean setValue(Append writer, PathSegments segments, String shortType)
234241
if (hasParamDefault()) {
235242
writer.append("ctx.%s(\"%s\",\"%s\")", paramType, paramName, paramDefault);
236243
} else {
244+
boolean checkNull = notNullKotlin || (paramType == ParamType.FORMPARAM && typeHandler.isPrimitive());
245+
if (checkNull) {
246+
writer.append("checkNull(");
247+
}
237248
writer.append("ctx.%s(\"%s\")", paramType, paramName);
249+
if (checkNull) {
250+
writer.append(", \"%s\")", paramName);
251+
}
238252
}
239253
}
240254

src/main/java/io/dinject/javalin/generator/TypeHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ interface TypeHandler {
2424
* The short name.
2525
*/
2626
String shortName();
27+
28+
/**
29+
* Return true if this is a primitive type.
30+
*/
31+
boolean isPrimitive();
2732
}

src/main/java/io/dinject/javalin/generator/TypeMap.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ static abstract class JavaLangType implements TypeHandler {
129129
this.shortName = shortName;
130130
}
131131

132+
@Override
133+
public boolean isPrimitive() {
134+
return false;
135+
}
136+
132137
@Override
133138
public String shortName() {
134139
return shortName;
@@ -156,6 +161,11 @@ static abstract class Primitive implements TypeHandler {
156161
this.type = type;
157162
}
158163

164+
@Override
165+
public boolean isPrimitive() {
166+
return true;
167+
}
168+
159169
@Override
160170
public String shortName() {
161171
return type;
@@ -234,6 +244,11 @@ static abstract class ObjectHandler implements TypeHandler {
234244
this.toMethod = "to" + shortName + "(";
235245
}
236246

247+
@Override
248+
public boolean isPrimitive() {
249+
return false;
250+
}
251+
237252
@Override
238253
public String getImportType() {
239254
return importType;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.jetbrains.annotations;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Documented
10+
@Retention(RetentionPolicy.CLASS)
11+
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
12+
public @interface NotNull {
13+
String value() default "";
14+
}

0 commit comments

Comments
 (0)