99import java .io .StringWriter ;
1010import java .util .*;
1111import java .util .function .BiConsumer ;
12- import java .util .stream .Collectors ;
1312
1413import javax .annotation .processing .ProcessingEnvironment ;
1514import javax .lang .model .element .*;
16- import javax .lang .model .type .DeclaredType ;
1715import javax .lang .model .type .TypeMirror ;
1816import javax .tools .Diagnostic ;
1917
2422 * processor options.
2523 */
2624public class MvcContext {
27- private record ResultType (String type , String handler , boolean nonBlocking ) {}
28-
2925 private final ProcessingEnvironment processingEnvironment ;
3026 private final boolean debug ;
3127 private final boolean incremental ;
@@ -34,7 +30,7 @@ private record ResultType(String type, String handler, boolean nonBlocking) {}
3430 private final BiConsumer <Diagnostic .Kind , String > output ;
3531 private final List <MvcRouter > routers = new ArrayList <>();
3632 private final boolean mvcMethod ;
37- private final Map <TypeElement , ResultType > handler = new HashMap <>();
33+ private final Map <TypeElement , ReactiveType > reactiveTypeMap = new HashMap <>();
3834
3935 public MvcContext (
4036 ProcessingEnvironment processingEnvironment , BiConsumer <Diagnostic .Kind , String > output ) {
@@ -45,99 +41,28 @@ public MvcContext(
4541 this .mvcMethod = Options .boolOpt (processingEnvironment , Options .MVC_METHOD , false );
4642 this .routerPrefix = Options .string (processingEnvironment , Options .ROUTER_PREFIX , "" );
4743 this .routerSuffix = Options .string (processingEnvironment , Options .ROUTER_SUFFIX , "_" );
48- computeResultTypes (processingEnvironment , handler ::put );
44+ computeReactiveTypes (processingEnvironment , reactiveTypeMap ::put );
4945
5046 debug ("Incremental annotation processing is turned %s." , incremental ? "ON" : "OFF" );
5147 }
5248
53- private void computeResultTypes (
54- ProcessingEnvironment processingEnvironment , BiConsumer <TypeElement , ResultType > consumer ) {
55- var handler =
56- new HashSet <>(
57- Set .of (
58- "io.jooby.ReactiveSupport" ,
59- "io.jooby.mutiny.Mutiny" ,
60- "io.jooby.reactor.Reactor" ,
61- "io.jooby.rxjava3.Reactivex" ));
62- handler .addAll (Options .stringListOpt (processingEnvironment , Options .HANDLER ));
63- handler .stream ()
64- .map (type -> processingEnvironment .getElementUtils ().getTypeElement (type ))
65- .filter (Objects ::nonNull )
49+ private void computeReactiveTypes (
50+ ProcessingEnvironment processingEnvironment , BiConsumer <TypeElement , ReactiveType > consumer ) {
51+ ReactiveType .supportedTypes ()
6652 .forEach (
67- it -> {
68- var annotation =
69- AnnotationSupport .findAnnotationByName (it , "io.jooby.annotation.ResultType" );
70- if (annotation != null ) {
71- var handlerFunction =
72- AnnotationSupport .findAnnotationValue (annotation , "handler" ::equals ).get (0 );
73- boolean nonBlocking =
74- AnnotationSupport .findAnnotationValue (annotation , "nonBlocking" ::equals )
75- .stream ()
76- .findFirst ()
77- .map (Boolean ::valueOf )
78- .orElse (Boolean .FALSE );
79- ResultType entry ;
80- var i = handlerFunction .lastIndexOf ('.' );
81- if (i > 0 ) {
82- var container = handlerFunction .substring (0 , i );
83- var fn = handlerFunction .substring (i + 1 );
84- entry = new ResultType (container , fn , nonBlocking );
85- } else {
86- entry = new ResultType (it .asType ().toString (), handlerFunction , nonBlocking );
87- }
88- var functions =
89- it .getEnclosedElements ().stream ()
90- .filter (ExecutableElement .class ::isInstance )
91- .map (ExecutableElement .class ::cast )
92- .filter (m -> entry .handler .equals (m .getSimpleName ().toString ()))
93- .toList ();
94- if (functions .isEmpty ()) {
95- throw new IllegalArgumentException (
96- "Method not found: " + entry .type + "." + entry .handler );
97- } else {
98- var args =
99- functions .stream ()
100- .filter (
101- m ->
102- !m .getParameters ().isEmpty ()
103- && m .getParameters ()
104- .get (0 )
105- .asType ()
106- .toString ()
107- .equals ("io.jooby.Route.Handler" ))
108- .findFirst ()
109- .orElseThrow (
110- () ->
111- new IllegalArgumentException (
112- "Signature doesn't match: "
113- + functions
114- + " must be: "
115- + functions .stream ()
116- .map (
117- e ->
118- e .getSimpleName ()
119- + "(io.jooby.Route.Handler)" )
120- .collect (Collectors .joining (", " , "[" , "]" ))));
121- if (!args .getReturnType ().toString ().equals ("io.jooby.Route.Handler" )) {
122- throw new IllegalArgumentException (
123- "Method returns type not supported: "
124- + args
125- + ": "
126- + args .getReturnType ()
127- + " must be: "
128- + args
129- + ": io.jooby.Route.Handler" );
130- }
131- if (!args .getModifiers ().contains (Modifier .STATIC )) {
132- throw new IllegalArgumentException ("Method must be static: " + args );
133- }
134- }
135- var types =
136- AnnotationSupport .findAnnotationValue (
137- annotation , "types" ::equals , value -> (DeclaredType ) value .getValue ());
138- for (var type : types ) {
139- superTypes (type .asElement ()).forEach (t -> consumer .accept (t , entry ));
140- }
53+ reactiveType -> {
54+ var handlerType =
55+ processingEnvironment
56+ .getElementUtils ()
57+ .getTypeElement (reactiveType .handlerType ());
58+ if (handlerType != null ) {
59+ // Handler Type is on classpath
60+ reactiveType .reactiveTypes ().stream ()
61+ .map (it -> processingEnvironment .getElementUtils ().getTypeElement (it ))
62+ .forEach (
63+ it -> {
64+ superTypes (it ).forEach (t -> consumer .accept (t , reactiveType ));
65+ });
14166 }
14267 });
14368 }
@@ -199,11 +124,11 @@ public String pipeline(TypeMirror returnType, String handlerReference) {
199124
200125 public boolean nonBlocking (TypeMirror returnType ) {
201126 var entry = findMappingHandler (returnType );
202- return entry != null && entry . nonBlocking ;
127+ return entry != null ;
203128 }
204129
205- private ResultType findMappingHandler (TypeMirror type ) {
206- for (var e : handler .entrySet ()) {
130+ private ReactiveType findMappingHandler (TypeMirror type ) {
131+ for (var e : reactiveTypeMap .entrySet ()) {
207132 var that = e .getKey ();
208133 if (type .toString ().equals (that .toString ())
209134 || processingEnvironment .getTypeUtils ().isAssignable (type , that .asType ())) {
@@ -281,7 +206,7 @@ public void generateStaticImports(MvcRouter mvcRouter, BiConsumer<String, String
281206 if (process .add (returnType .toString ())) {
282207 var fnq = findMappingHandler (returnType );
283208 if (fnq != null ) {
284- consumer .accept (fnq .type , fnq .handler );
209+ consumer .accept (fnq .handlerType () , fnq .handler () );
285210 }
286211 }
287212 }
0 commit comments