66package io .jooby .internal .apt ;
77
88import static io .jooby .internal .apt .AnnotationSupport .findAnnotationValue ;
9+ import static io .jooby .internal .apt .Types .BUILT_IN ;
910import static java .util .stream .Collectors .joining ;
1011
11- import java .net .URI ;
12- import java .net .URL ;
13- import java .time .Duration ;
14- import java .time .Period ;
15- import java .time .ZoneId ;
1612import java .util .*;
1713import java .util .function .Predicate ;
1814import java .util .stream .Stream ;
@@ -53,10 +49,10 @@ public String toSourceCode(
5349 }
5450 }
5551 },
56- CookieParam ("cookie" , "io.jooby.annotation.CookieParam" , "jakarta.ws.rs.CookieParam" ),
57- FlashParam ("flash" , "io.jooby.annotation.FlashParam" ),
52+ CookieParam ("cookie" , BUILT_IN , "io.jooby.annotation.CookieParam" , "jakarta.ws.rs.CookieParam" ),
53+ FlashParam ("flash" , BUILT_IN , "io.jooby.annotation.FlashParam" ),
5854 FormParam ("form" , "io.jooby.annotation.FormParam" , "jakarta.ws.rs.FormParam" ),
59- HeaderParam ("header" , "io.jooby.annotation.HeaderParam" , "jakarta.ws.rs.HeaderParam" ),
55+ HeaderParam ("header" , BUILT_IN , "io.jooby.annotation.HeaderParam" , "jakarta.ws.rs.HeaderParam" ),
6056 Lookup ("lookup" , "io.jooby.annotation.Param" ) {
6157 @ Override
6258 protected Predicate <String > namePredicate () {
@@ -65,7 +61,7 @@ protected Predicate<String> namePredicate() {
6561 },
6662 PathParam ("path" , "io.jooby.annotation.PathParam" , "jakarta.ws.rs.PathParam" ),
6763 QueryParam ("query" , "io.jooby.annotation.QueryParam" , "jakarta.ws.rs.QueryParam" ),
68- SessionParam ("session" , "io.jooby.annotation.SessionParam" ),
64+ SessionParam ("session" , BUILT_IN , "io.jooby.annotation.SessionParam" ),
6965 BodyParam ("body" ) {
7066 @ Override
7167 public String parameterName (AnnotationMirror annotation , String defaultParameterName ) {
@@ -368,6 +364,24 @@ public static ParameterGenerator findByAnnotation(String annotation) {
368364 this .annotations = Set .of (annotations );
369365 }
370366
367+ ParameterGenerator (String method , Set <String > typeRestrictions , String ... annotations ) {
368+ this (method , annotations );
369+ this .typeRestrictions = typeRestrictions ;
370+ }
371+
372+ public void verifyType (String type , String parameterName , MvcRoute route ) {
373+ if (!typeRestrictions .isEmpty ()) {
374+ if (typeRestrictions .stream ().noneMatch (type ::equals )) {
375+ throw new IllegalArgumentException ("""
376+ Illegal argument type at '%s.%s()'.\s
377+ Parameter '%s' annotated as %s cannot be of type '%s'.\s
378+ Supported types are: %s
379+ """ .formatted (route .getRouter ().getTargetType ().toString (),
380+ route .getMethodName (), parameterName , annotations , type , Types .BUILT_IN ));
381+ }
382+ }
383+ }
384+
371385 protected String source (AnnotationMirror annotation ) {
372386 if (ParameterGenerator .Lookup .annotations .contains (annotation .getAnnotationType ().toString ())) {
373387 var sources = findAnnotationValue (annotation , AnnotationSupport .VALUE );
@@ -382,40 +396,6 @@ protected String source(AnnotationMirror annotation) {
382396
383397 protected final String method ;
384398 private final Set <String > annotations ;
399+ private Set <String > typeRestrictions = Set .of (); // empty set means no restrictions by default
385400 private static final Set <Class > CONTAINER = Set .of (List .class , Set .class , Optional .class );
386- private static final Set <String > BUILT_IN =
387- Set .of (
388- String .class .getName (),
389- Boolean .class .getName (),
390- Boolean .TYPE .getName (),
391- Byte .class .getName (),
392- Byte .TYPE .getName (),
393- Character .class .getName (),
394- Character .TYPE .getName (),
395- Short .class .getName (),
396- Short .TYPE .getName (),
397- Integer .class .getName (),
398- Integer .TYPE .getName (),
399- Long .class .getName (),
400- Long .TYPE .getName (),
401- Float .class .getName (),
402- Float .TYPE .getName (),
403- Double .class .getName (),
404- Double .TYPE .getName (),
405- Enum .class .getName (),
406- java .util .UUID .class .getName (),
407- java .time .Instant .class .getName (),
408- java .util .Date .class .getName (),
409- java .time .LocalDate .class .getName (),
410- java .time .LocalDateTime .class .getName (),
411- java .math .BigDecimal .class .getName (),
412- java .math .BigInteger .class .getName (),
413- Duration .class .getName (),
414- Period .class .getName (),
415- java .nio .charset .Charset .class .getName (),
416- "io.jooby.StatusCode" ,
417- TimeZone .class .getName (),
418- ZoneId .class .getName (),
419- URI .class .getName (),
420- URL .class .getName ());
421401}
0 commit comments