@@ -65,13 +65,23 @@ public MvcContext getContext() {
6565 return context ;
6666 }
6767
68+ public String getProjection () {
69+ var project = AnnotationSupport .findAnnotationByName (method , Types .PROJECT );
70+ if (project != null ) {
71+ return AnnotationSupport .findAnnotationValue (project , VALUE ).stream ().findFirst ().orElse ("" );
72+ }
73+ return null ;
74+ }
75+
6876 public TypeDefinition getReturnType () {
6977 var processingEnv = context .getProcessingEnvironment ();
7078 var types = processingEnv .getTypeUtils ();
7179 var elements = processingEnv .getElementUtils ();
72- var project = AnnotationSupport .findAnnotationByName (method , Types .PROJECT );
73- if (project != null ) {
74- return new TypeDefinition (types , elements .getTypeElement (Types .PROJECTED ).asType ());
80+ var isProjection =
81+ !returnType .is (Types .PROJECTED )
82+ && AnnotationSupport .findAnnotationByName (method , Types .PROJECT ) != null ;
83+ if (isProjection ) {
84+ return new TypeDefinition (types , elements .getTypeElement (Types .PROJECTED ).asType (), true );
7585 } else if (returnType .isVoid ()) {
7686 return new TypeDefinition (types , elements .getTypeElement ("io.jooby.StatusCode" ).asType ());
7787 } else if (isSuspendFun ()) {
@@ -223,6 +233,12 @@ public List<String> generateHandlerCall(boolean kt) {
223233 var returnTypeGenerics =
224234 getReturnType ().getArgumentsString (kt , false , Set .of (TypeKind .TYPEVAR ));
225235 var returnTypeString = type (kt , getReturnType ().toString ());
236+ var customReturnType = getReturnType ();
237+ if (customReturnType .isProjection ()) {
238+ // Override for projection
239+ returnTypeGenerics = "" ;
240+ returnTypeString = Types .PROJECTED + "<" + returnType + ">" ;
241+ }
226242
227243 boolean nullable = false ;
228244 if (kt ) {
@@ -321,7 +337,10 @@ public List<String> generateHandlerCall(boolean kt) {
321337 buffer .add (statement (indent (2 ), "return statusCode" , semicolon (kt )));
322338 } else {
323339 controllerVar (kt , buffer );
324- var cast = getReturnType ().getArgumentsString (kt , false , Set .of (TypeKind .TYPEVAR ));
340+ var cast =
341+ customReturnType .isProjection ()
342+ ? ""
343+ : customReturnType .getArgumentsString (kt , false , Set .of (TypeKind .TYPEVAR ));
325344 var kotlinNotEnoughTypeInformation = !cast .isEmpty () && kt ? "<Any>" : "" ;
326345 var call =
327346 of (
@@ -333,7 +352,15 @@ public List<String> generateHandlerCall(boolean kt) {
333352 setUncheckedCast (true );
334353 call = kt ? call + " as " + returnTypeString : "(" + returnTypeString + ") " + call ;
335354 }
336- buffer .add (statement (indent (2 ), "return " , call , kt && nullable ? "!!" : "" , semicolon (kt )));
355+ if (customReturnType .isProjection ()) {
356+ var projected =
357+ of (Types .PROJECTED , ".wrap(" , call , ").include(" , string (getProjection ()), ")" );
358+ buffer .add (
359+ statement (indent (2 ), "return " , projected , kt && nullable ? "!!" : "" , semicolon (kt )));
360+ } else {
361+ buffer .add (
362+ statement (indent (2 ), "return " , call , kt && nullable ? "!!" : "" , semicolon (kt )));
363+ }
337364 }
338365 buffer .add (statement ("}" , System .lineSeparator ()));
339366 if (uncheckedCast ) {
0 commit comments