44import core .annotation .web .Controller ;
55import core .annotation .web .RequestMapping ;
66import core .annotation .web .RequestMethod ;
7- import org .reflections .Reflections ;
7+ import exception .NotFoundException ;
8+ import org .reflections .ReflectionUtils ;
89import org .slf4j .Logger ;
910import org .slf4j .LoggerFactory ;
11+ import org .springframework .http .HttpStatus ;
1012
1113import javax .servlet .http .HttpServletRequest ;
1214import java .lang .reflect .Method ;
13- import java .util .List ;
15+ import java .util .HashSet ;
1416import java .util .Map ;
1517import java .util .Set ;
1618
@@ -24,44 +26,50 @@ public AnnotationHandlerMapping(Object... basePackage) {
2426 this .basePackage = basePackage ;
2527 }
2628
27- public void initialize () {
28- Reflections reflections = new Reflections ( basePackage );
29- Set < Class <?>> controllers = reflections . getTypesAnnotatedWith ( Controller . class );
30- for ( Class <?> controller : controllers ) {
31- String path = controller . getAnnotation ( Controller . class ). value ();
32- detectHandlerExecution ( path , controller );
29+ @ Override
30+ public void initMapping () {
31+ ControllerScanner controllerScanner = new ControllerScanner ( basePackage );
32+ Map < Class <?>, Object > controllers = controllerScanner . getControllers ();
33+ if ( controllers . isEmpty ()) {
34+ throw new NotFoundException ( HttpStatus . NOT_FOUND );
3335 }
36+ initHandlerExecution (controllers );
3437 }
3538
36- @ Override
37- public HandlerExecution getHandler (HttpServletRequest request ) {
38- String requestURI = request .getRequestURI ();
39- RequestMethod requestMethod = RequestMethod .valueOf (request .getMethod ().toUpperCase ());
40- return handlerExecutions .get (new HandlerKey (requestURI , requestMethod ));
39+ private void initHandlerExecution (Map <Class <?>, Object > controllers ) {
40+ Set <Method > methods = getMethods (controllers );
41+ for (Method method : methods ) {
42+ Class <?> declaringClass = method .getDeclaringClass ();
43+ Controller annotation = declaringClass .getAnnotation (Controller .class );
44+ addHandlerExecution (controllers .get (declaringClass ), annotation .value (), method );
45+ }
4146 }
4247
43- private void detectHandlerExecution (String path , Class <?> controller ) {
44- Object controllerInstance = null ;
45- try {
46- controllerInstance = controller .getConstructor ().newInstance ();
47- } catch (Exception e ) {
48- e .printStackTrace ();
49- logger .error ("detectHandlerExecution Exception : {}" , e .getMessage ());
50- }
51- List <Method > methods = List .of (controller .getDeclaredMethods ());
52- for (Method method : methods ) {
53- addHandlerExecution (controllerInstance , path , method );
48+ private Set <Method > getMethods (Map <Class <?>, Object > controllers ) {
49+ Set <Method > methods = new HashSet <>();
50+ for (Class <?> clazz : controllers .keySet ()) {
51+ methods .addAll (ReflectionUtils .getAllMethods (clazz , ReflectionUtils .withAnnotation (RequestMapping .class )));
5452 }
53+ return methods ;
5554 }
5655
5756 private void addHandlerExecution (Object controllerInstance , String path , Method method ) {
58- if (method .isAnnotationPresent (RequestMapping .class )) {
59- String uriPath = method .getAnnotation (RequestMapping .class ).value ();
60- RequestMethod requestMethod = method .getAnnotation (RequestMapping .class ).method ();
61- HandlerKey handlerKey = new HandlerKey (path + uriPath , requestMethod );
57+ RequestMapping requestMapping = method .getAnnotation (RequestMapping .class );
58+ HandlerKey handlerKey = createHandlerKey (requestMapping , path );
59+ logger .debug ("Add RequestMapping. URI: {}, requestMethod: {}" , handlerKey , method );
60+ handlerExecutions .put (handlerKey , new HandlerExecution (controllerInstance , method ));
61+ }
6262
63- logger .debug ("Add RequestMapping. URI: {}, requestMethod: {}" , handlerKey , method );
64- handlerExecutions .put (handlerKey , new HandlerExecution (controllerInstance , method ));
65- }
63+ private HandlerKey createHandlerKey (RequestMapping requestMapping , String path ) {
64+ String uriPath = requestMapping .value ();
65+ RequestMethod requestMethod = requestMapping .method ();
66+ return new HandlerKey (path + uriPath , requestMethod );
67+ }
68+
69+ @ Override
70+ public HandlerExecution getHandler (HttpServletRequest request ) {
71+ String requestURI = request .getRequestURI ();
72+ RequestMethod requestMethod = RequestMethod .valueOf (request .getMethod ().toUpperCase ());
73+ return handlerExecutions .get (new HandlerKey (requestURI , requestMethod ));
6674 }
6775}
0 commit comments