• 10.Creating Controller Annotation Parser
  • Completing the Class Provider




    Download 1,03 Mb.
    bet4/7
    Sana21.03.2017
    Hajmi1,03 Mb.
    #802
    1   2   3   4   5   6   7

    8.Completing the Class Provider


    In the ClassProvider class, create a constructor that initializes the private fields:

    With this, the class provider is ready.


    9.Creating Annotation Parser Interface


    In the server package, create a new package called parser. Create a new generic interface with two parameters called AnnotationParser:

    It should contain only one method called parse(), which will parse the annotation. It should accept a map with the generics from the interface.


    10.Creating Controller Annotation Parser


    Create a new class called ControllerAnnotationParser. It should implement the AnnotationParser. The parameters for the AnnotationParser should be >:

    Before we implement the parse() method, we want to create a constructor that takes a ClassProvider as a parameter:



    Now, we need to implement another method called createMappingRegex():



    This method will get our routing and create regex that we are going to use for our router. We need the current method, the URL, the different parts of the URL in the mappingTokens collection and the map which will keep the type of our parameters. First, we need to make sure that we are only checking the parts of our routing that are parameters. A parameter is identified by "{" and "}" in the begging and in the end:



    After the if statement, we need to iterate over the parameters for our current method. We only need the parameter if it contains the UriParameter annotation:



    Now we need to get the value of our UriParameter and check if the current URL part is the same as our UriParameter:



    When we find the right UriParameter, we want to save it to our map. Then we need to replace the parameter from the URL with the regex we want to match. We will only have strings and numbers, so we are going to use [a-zA-Z]+ and [0-9]+. After this is done, we can exit the loop:



    Finally, the method will return the mapping string, which corresponds to the URL:



    Now we can finally go back to our parse() method. It should get all the classes with the Controller annotation. We will get them, using the ClassProvider. Then we are going to iterate over all of them and for each one we are going to iterate over its methods that contain the RequestMapping annotation:



    For every method, we need to create a mapping regex, using the method we've created earlier. In order to do that, we need to initialize the parameters that we are going to pass to that method:



    Now, we only need to create new instance of the class, new ControllerActionPair and add it to our routes map:



    This should be our controller annotation parser done. Now we need to put it to good use.



    Download 1,03 Mb.
    1   2   3   4   5   6   7




    Download 1,03 Mb.