11package io .jooby .compiler ;
22
3+ import com .squareup .javapoet .JavaFile ;
4+ import com .squareup .javapoet .TypeSpec ;
35import io .jooby .Context ;
46import io .jooby .FlashMap ;
57import io .jooby .Formdata ;
68import io .jooby .Multipart ;
9+ import io .jooby .ProvisioningException ;
710import io .jooby .QueryString ;
811import io .jooby .Reified ;
912import io .jooby .Route ;
3437import java .io .PrintWriter ;
3538import java .lang .reflect .Method ;
3639import java .util .Collections ;
40+ import java .util .HashMap ;
3741import java .util .List ;
3842import java .util .Map ;
3943import java .util .Optional ;
@@ -117,6 +121,17 @@ public TypeMirror arg0(TypeMirror mirror) {
117121 }
118122 }
119123
124+ private static final Map <String , Set <String >> PARAMS = new HashMap <>();
125+
126+ static {
127+ PARAMS .put ("path" , Annotations .PATH_PARAMS );
128+ PARAMS .put ("query" , Annotations .QUERY_PARAMS );
129+ PARAMS .put ("cookie" , Annotations .COOKIE_PARAMS );
130+ PARAMS .put ("header" , Annotations .HEADER_PARAMS );
131+ PARAMS .put ("flash" , Annotations .FLASH_PARAMS );
132+ PARAMS .put ("multipart" , Annotations .FORM_PARAMS );
133+ }
134+
120135 private static final Type OBJ = getType (Object .class );
121136 private static final Type HANDLER = getType (Route .Handler .class );
122137
@@ -132,8 +147,11 @@ public TypeMirror arg0(TypeMirror mirror) {
132147 private final TypeElement owner ;
133148 private final ExecutableElement executable ;
134149 private final ProcessingEnvironment environment ;
150+ private final String httpMethod ;
135151
136- public MvcHandlerCompiler (ProcessingEnvironment environment , ExecutableElement executable ) {
152+ public MvcHandlerCompiler (ProcessingEnvironment environment , String httpMethod ,
153+ ExecutableElement executable ) {
154+ this .httpMethod = httpMethod .toLowerCase ();
137155 this .environment = environment ;
138156 this .executable = executable ;
139157 this .owner = (TypeElement ) executable .getEnclosingElement ();
@@ -203,14 +221,16 @@ private void apply(ClassWriter writer) throws Exception {
203221 apply .visitMethodInsn (INVOKESTATIC , "java/util/Optional" , "ofNullable" ,
204222 "(Ljava/lang/Object;)Ljava/util/Optional;" , false );
205223 }
206- } else if (isPathParam (parameter )) {
207- String parameterName = parameterName (parameter , Annotations .PATH_PARAMS );
224+ } else {
225+ Map .Entry <String , Set <String >> strategy = paramStrategy (parameter );
226+
227+ String parameterName = parameterName (parameter , strategy .getValue ());
208228
209229 Method paramValue = paramValue (parameter , paramType );
210230 boolean dynamic =
211231 paramValue .getName ().equals ("to" ) && !isSimpleType (paramType .arg0 (parameter .asType ()));
212232 if (dynamic ) {
213- Method pathParam = Context .class .getDeclaredMethod ("path" );
233+ Method pathParam = Context .class .getDeclaredMethod (strategy . getKey () );
214234 apply .visitMethodInsn (INVOKEINTERFACE , CTX .getInternalName (), pathParam .getName (),
215235 getMethodDescriptor (pathParam ), true );
216236 apply .visitLdcInsn (asmType (rawType ));
@@ -228,7 +248,7 @@ private void apply(ClassWriter writer) throws Exception {
228248 } else {
229249 apply .visitLdcInsn (parameterName );
230250
231- Method pathParam = Context .class .getDeclaredMethod ("path" , String .class );
251+ Method pathParam = Context .class .getDeclaredMethod (strategy . getKey () , String .class );
232252 apply .visitMethodInsn (INVOKEINTERFACE , CTX .getInternalName (), pathParam .getName (),
233253 getMethodDescriptor (pathParam ), true );
234254 // to(Class)
@@ -259,6 +279,15 @@ private void apply(ClassWriter writer) throws Exception {
259279 apply .visitEnd ();
260280 }
261281
282+ private Map .Entry <String , Set <String >> paramStrategy (VariableElement parameter ) {
283+ return PARAMS .entrySet ().stream ()
284+ .filter (it -> isParam (parameter , it .getValue ()))
285+ .findFirst ()
286+ .orElseThrow (() -> new ProvisioningException (
287+ "Unable to provision parameter: '" + parameter + ": " + parameter .asType () + "'" ,
288+ null ));
289+ }
290+
262291 private ParamType paramType (VariableElement parameter ) {
263292 TypeMirror typeMirror = parameter .asType ();
264293 if (isSimpleType (typeMirror )) {
@@ -337,10 +366,10 @@ private Method paramValue(VariableElement param, ParamType paramType)
337366 }
338367
339368 private boolean isSimpleType (TypeMirror mirror ) {
340- return eq (String .class , mirror ) || mirror .getKind ().isPrimitive () || isPrimitiveWrapper (mirror );
369+ return eq (String .class , mirror ) || mirror .getKind ().isPrimitive () || isBasic (mirror );
341370 }
342371
343- private boolean isPrimitiveWrapper (TypeMirror mirror ) {
372+ private boolean isBasic (TypeMirror mirror ) {
344373 switch (rawType (mirror )) {
345374 case "java.lang.Byte" :
346375 case "java.lang.Character" :
@@ -349,6 +378,7 @@ private boolean isPrimitiveWrapper(TypeMirror mirror) {
349378 case "java.lang.Long" :
350379 case "java.lang.Float" :
351380 case "java.lang.Double" :
381+ case "java.time.Instant" :
352382 return true ;
353383 default :
354384 return false ;
@@ -410,12 +440,8 @@ private String cleanString(String value) {
410440 return value ;
411441 }
412442
413- private boolean isPathParam (VariableElement parameter ) {
414- return isPathParam (parameter .getAnnotationMirrors ());
415- }
416-
417- private boolean isPathParam (List <? extends AnnotationMirror > annotationMirrors ) {
418- return annotations (annotationMirrors , Annotations .PATH_PARAMS ).size () > 0 ;
443+ private boolean isParam (VariableElement parameter , Set <String > annotations ) {
444+ return annotations (parameter .getAnnotationMirrors (), annotations ).size () > 0 ;
419445 }
420446
421447 private List <AnnotationMirror > annotations (List <? extends AnnotationMirror > annotationMirrors ,
@@ -439,10 +465,10 @@ private Method typeInjection(String rawType, ParamType paramType) throws Excepti
439465 return Context .class .getDeclaredMethod ("multipart" );
440466 }
441467 if (FlashMap .class .getName ().equals (rawType )) {
442- return Context .class .getDeclaredMethod ("flashMap " );
468+ return Context .class .getDeclaredMethod ("flash " );
443469 }
444470 if (Session .class .getName ().equals (rawType )) {
445- if (paramType == ParamType . OPTIONAL ) {
471+ if (paramType . isOptional () ) {
446472 return Context .class .getDeclaredMethod ("sessionOrNull" );
447473 } else {
448474 return Context .class .getDeclaredMethod ("session" );
@@ -507,7 +533,7 @@ public String getSourceFileName() {
507533 }
508534
509535 public String getHandlerName () {
510- return getOwner () + "$" + executable .getSimpleName ();
536+ return getOwner () + "$" + httpMethod . toUpperCase () + "$" + executable .getSimpleName ();
511537 }
512538
513539 public String getHandlerInternal () {
0 commit comments