@ SuppressWarnings ( "unchecked" ) private List < Route > createControllerRoutes ( Map < Method , Class < ? extends Annotation > > controllerMethods ) { List < Route > routes = new ArrayList <> ( ) ; Class < ? extends Controller > controllerClass = ( Class < ? extends Controller > ) controllerMethods . keySet ( ) . iterator ( ) . next ( ) . getDeclaringClass ( ) ; Set < String > controllerPaths = getControllerPaths ( controllerClass ) ; Collection < Method > methods = sortControllerMethods ( controllerMethods . keySet ( ) ) ; for ( Method method : methods ) { Class < ? extends Annotation > httpMethodAnnotationClass = controllerMethods . get ( method ) ; Annotation httpMethodAnnotation = method . getAnnotation ( httpMethodAnnotationClass ) ; String httpMethod = httpMethodAnnotation . annotationType ( ) . getAnnotation ( HttpMethod . class ) . value ( ) ; String [ ] methodPaths = ClassUtils . executeDeclaredMethod ( httpMethodAnnotation , "value" ) ; if ( controllerPaths . isEmpty ( ) ) { // add an empty string to allow controllerPaths iteration controllerPaths . add ( "" ) ; } for ( String controllerPath : controllerPaths ) { if ( methodPaths . length == 0 ) { // controllerMethod does not specify a path, inherit from controller String fullPath = StringUtils . addStart ( controllerPath , "/" ) ; // create the route handler RouteHandler handler = new ControllerHandler ( application , method ) ; // create the route Route route = new Route ( httpMethod , fullPath , handler ) . bind ( "__controllerClass" , controllerClass ) . bind ( "__controllerMethod" , method ) ; // add the route to the list of routes routes . add ( route ) ; } else { // controllerMethod specifies one or more paths, concatenate with controller paths for ( String methodPath : methodPaths ) { String path = Stream . of ( StringUtils . removeEnd ( controllerPath , "/" ) , StringUtils . removeStart ( methodPath , "/" ) ) . filter ( Objects :: nonNull ) . collect ( Collectors . joining ( "/" ) ) ; String fullPath = StringUtils . addStart ( path , "/" ) ; // create the route handler RouteHandler handler = new ControllerHandler ( application , method ) ; // create the route Route route = new Route ( httpMethod , fullPath , handler ) . bind ( "__controllerClass" , controllerClass ) . bind ( "__controllerMethod" , method ) ; // add the route to the list of routes routes . add ( route ) ; } } } } return routes ; }
Create controller routes from controller methods .