Skip to content

Commit a6d670e

Browse files
committed
apt: bug fixes + get back some tests
1 parent 9bd4c97 commit a6d670e

File tree

15 files changed

+145
-53
lines changed

15 files changed

+145
-53
lines changed

modules/jooby-apt/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@
134134
<groupId>org.apache.maven.plugins</groupId>
135135
<artifactId>maven-compiler-plugin</artifactId>
136136
<configuration>
137-
<compilerArgs>
138-
<arg>-proc:none</arg>
139-
</compilerArgs>
137+
<proc>none</proc>
140138
</configuration>
141139
<executions>
142140
<execution>

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@ public boolean isNamed() {
8686
return isSimpleType();
8787
}
8888

89+
public boolean isNullable() {
90+
if (hasAnnotation("org.jetbrains.annotations.Nullable")
91+
|| hasAnnotation("javax.annotation.Nullable")) {
92+
return true;
93+
}
94+
boolean nonnull = hasAnnotation("org.jetbrains.annotations.NotNull")
95+
|| hasAnnotation("javax.annotation.Nonnull");
96+
if (nonnull) {
97+
return false;
98+
}
99+
return !getType().isPrimitive();
100+
}
101+
102+
private boolean hasAnnotation(String type) {
103+
for (AnnotationMirror annotation : parameter.getAnnotationMirrors()) {
104+
if (annotation.getAnnotationType().equals(type)) {
105+
return true;
106+
}
107+
}
108+
return false;
109+
}
110+
89111
public Method getObjectValue() throws NoSuchMethodException {
90112
return getKind().valueObject(this);
91113
}
@@ -94,7 +116,7 @@ public Method getSingleValue() throws NoSuchMethodException {
94116
return getKind().singleValue(this);
95117
}
96118

97-
private boolean isSimpleType() {
119+
public boolean isSimpleType() {
98120
for (Class builtinType : builtinTypes()) {
99121
if (is(builtinType) || is(Optional.class, builtinType) || is(List.class, builtinType) || is(
100122
Set.class, builtinType)) {

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
import org.objectweb.asm.Type;
99

10+
import javax.lang.model.element.Element;
11+
import javax.lang.model.element.ElementKind;
1012
import javax.lang.model.type.DeclaredType;
13+
import javax.lang.model.type.TypeKind;
1114
import javax.lang.model.type.TypeMirror;
1215
import javax.lang.model.util.Types;
1316

@@ -104,7 +107,7 @@ public List<TypeDefinition> getArguments() {
104107
}
105108

106109
public Type toJvmType() {
107-
return asmType(getRawType().toString());
110+
return asmType(getName(type));
108111
}
109112

110113
public boolean isRawType() {
@@ -167,4 +170,17 @@ private org.objectweb.asm.Type asmType(String type) {
167170
private String typeName(Class type) {
168171
return type.isArray() ? type.getComponentType().getName() + "[]" : type.getName();
169172
}
173+
174+
private String getName(TypeMirror type) {
175+
Element element = typeUtils.asElement(type);
176+
return element == null ? type.toString() : getName(element);
177+
}
178+
179+
private String getName(Element type) {
180+
Element parent = type.getEnclosingElement();
181+
if (parent != null && parent.getKind() == ElementKind.CLASS) {
182+
return getName(parent) + "$" + type.getSimpleName();
183+
}
184+
return type.toString();
185+
}
170186
}

modules/jooby-apt/src/main/java/io/jooby/internal/compiler/ValueWriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
abstract class ValueWriter implements ParamWriter {
2222
@Override
2323
public void accept(ClassWriter writer, String handlerInternalName, MethodVisitor visitor,
24-
ParamDefinition parameter)
25-
throws Exception {
24+
ParamDefinition parameter) throws Exception {
2625
Method convertMethod = parameter.getMethod();
2726
// to(Class)
2827
boolean toClass = is(convertMethod, 0, Class.class);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import examples.CoroutineApp;
44
import org.junit.jupiter.api.Test;
55
import org.objectweb.asm.util.ASMifier;
6+
import source.NullRoutes;
67

78
public class ASMPrinter {
89

@@ -25,5 +26,10 @@ public void mvcExtension() throws Exception {
2526
public void myController() throws Exception {
2627
ASMifier.main(new String[] {MyControllerHandler.class.getName() });
2728
}
29+
30+
@Test
31+
public void nullRoutes() throws Exception {
32+
ASMifier.main(new String[] {NullRoutes.class.getName() });
33+
}
2834
}
2935

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import io.jooby.annotations.GET;
44
import io.jooby.annotations.Path;
5+
import io.jooby.annotations.QueryParam;
56

67
import java.util.Map;
78

89
@Path("/")
910
public class MyController {
1011

1112
@GET
12-
public String doIt(Map<String, Object> mybean) {
13-
return mybean.toString();
13+
public Integer doIt(@QueryParam Integer x) {
14+
return x;
1415
}
1516
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package output;
22

33
import io.jooby.Context;
4-
import io.jooby.Reified;
54
import io.jooby.Route;
5+
import io.jooby.Value;
66

77
import javax.annotation.Nonnull;
88
import javax.inject.Provider;
@@ -16,6 +16,10 @@ public MyControllerHandler(Provider<MyController> provider) {
1616
}
1717

1818
@Nonnull @Override public Object apply(@Nonnull Context ctx) throws Exception {
19-
return provider.get().doIt(ctx.body(Reified.map(String.class, Object.class)));
19+
return provider.get().doIt(x(ctx.query("x")));
20+
}
21+
22+
private static Integer x(Value x) {
23+
return x.to(Integer.class);
2024
}
2125
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package source;
2+
3+
import io.jooby.annotations.GET;
4+
import io.jooby.annotations.QueryParam;
5+
6+
public class NullRoutes {
7+
public static class QParam {
8+
int foo;
9+
10+
Integer bar;
11+
12+
public QParam(int foo, Integer bar) {
13+
this.foo = foo;
14+
this.bar = bar;
15+
}
16+
17+
public void setBaz(int baz) {
18+
19+
}
20+
21+
@Override public String toString() {
22+
return foo + ":" + bar;
23+
}
24+
}
25+
26+
@GET("/nonnull")
27+
public Object nonnulArg(@QueryParam int v) {
28+
return v;
29+
}
30+
31+
@GET("/nullok")
32+
public Object nonnulArg(@QueryParam Integer v) {
33+
return String.valueOf(v);
34+
}
35+
36+
@GET("/nullbean")
37+
public Object nonnulArg(@QueryParam QParam bean) {
38+
return bean;
39+
}
40+
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import io.jooby.Body;
44
import io.jooby.Context;
55
import io.jooby.FileUpload;
6+
import io.jooby.MissingValueException;
67
import io.jooby.MockContext;
78
import io.jooby.Multipart;
9+
import io.jooby.ProvisioningException;
810
import io.jooby.Reified;
911
import io.jooby.StatusCode;
1012
import io.jooby.apt.MvcHandlerCompilerRunner;
@@ -13,6 +15,7 @@
1315
import source.JavaBeanParam;
1416
import source.JaxrsController;
1517
import source.NoPathRoute;
18+
import source.NullRoutes;
1619
import source.Provisioning;
1720

1821
import java.io.InputStream;
@@ -30,6 +33,7 @@
3033
import java.util.UUID;
3134

3235
import static org.junit.Assert.assertEquals;
36+
import static org.junit.jupiter.api.Assertions.assertThrows;
3337
import static org.mockito.Mockito.mock;
3438
import static org.mockito.Mockito.when;
3539

@@ -79,7 +83,7 @@ public void queryParam() throws Exception {
7983
@Test
8084
public void cookieParam() throws Exception {
8185
new MvcHandlerCompilerRunner(new Provisioning())
82-
.compile("/p/cookieParam", handler -> {
86+
.compile("/p/cookieParam", true, handler -> {
8387
assertEquals("cookie",
8488
handler.apply(new MockContext().setCookieMap(mapOf("c", "cookie"))));
8589
})
@@ -402,6 +406,22 @@ public void noTopLevel() throws Exception {
402406
;
403407
}
404408

409+
@Test
410+
public void nullRoutes() throws Exception {
411+
new MvcHandlerCompilerRunner(new NullRoutes())
412+
.compile("/nullok", true, handler -> {
413+
assertEquals("null", handler.apply(new MockContext()));
414+
})
415+
.compile("/nonnull", handler -> {
416+
assertThrows(MissingValueException.class, () -> handler.apply(new MockContext()));
417+
})
418+
419+
.compile("/nullbean", handler -> {
420+
assertThrows(ProvisioningException.class, () -> handler.apply(new MockContext()));
421+
})
422+
;
423+
}
424+
405425
private Context mockContext(String method, String path) {
406426
Context ctx = mock(Context.class);
407427
when(ctx.getMethod()).thenReturn(method);

modules/jooby-apt/src/test/kotlin/ktsource/KtRoutes.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)