summaryrefslogtreecommitdiff
path: root/src/main/java/foo/zaaarf/routecompass/RouteCompass.java
blob: 6b3ab764b97cbce5aeb9ae04571fdffdcd30cf4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package foo.zaaarf.routecompass;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class RouteCompass extends AbstractProcessor {

   private final HashMap<String, List<Route>> foundRoutes = new HashMap<>();
   private final HashSet<Class<? extends Annotation>> annotationClasses = new HashSet<>();

   public RouteCompass() {
      annotationClasses.add(RequestMapping.class);
      annotationClasses.add(GetMapping.class);
      annotationClasses.add(PostMapping.class);
      annotationClasses.add(PutMapping.class);
      annotationClasses.add(DeleteMapping.class);
      annotationClasses.add(PatchMapping.class);
   }

   @Override
   public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
      for(TypeElement annotationType : annotations) {
         env.getElementsAnnotatedWith(annotationType)
            .stream()
            .filter(elem -> elem instanceof ExecutableElement)
            .map(elem -> (ExecutableElement) elem)
            .forEach(elem -> {
               String classFQN = elem.getEnclosingElement().asType().toString();
               List<Route> routesInClass = foundRoutes.computeIfAbsent(classFQN, k -> new ArrayList<>());
               routesInClass.add(new Route(
                  this.getFullRoute(annotationType, elem),
                  this.getRequestMethods(annotationType, elem),
                  this.getConsumedType(annotationType, elem),
                  this.getProducedType(annotationType, elem),
                  this.isDeprecated(elem)
               ));
            });
      }

      try { //TODO: support param printing
         FileObject serviceProvider = this.processingEnv.getFiler().createResource(
            StandardLocation.SOURCE_OUTPUT, "", "routes"
         );

         PrintWriter out = new PrintWriter(serviceProvider.openWriter());
         for(String componentClass : this.foundRoutes.keySet()) {
            out.println(componentClass + ":");

            List<Route> routesInClass = this.foundRoutes.get(componentClass);
            for(Route r : routesInClass) {
               out.print("\t- " + r.method + r.route);
               if(r.consumes != null) out.print("(expects: " + r.consumes + ")");
               if(r.produces != null) out.print("(returns: " + r.produces + ")");
               out.println();
            }
         }

         out.close();
      } catch(IOException e) {
         throw new RuntimeException(e);
      }

      return false; //don't claim them, let spring do its job
   }

   private String getFullRoute(TypeElement annotationType, Element element) {
      try {
         String route = this.getAnnotationFieldsValue(annotationType, element, "path", "value");
         return this.getParentOrFallback(element, route, (a, e) -> {
            String parent = this.getFullRoute(a, e);
            StringBuilder sb = new StringBuilder(parent);
            if(!parent.endsWith("/")) sb.append("/");
            sb.append(route);
            return sb.toString();
         });
      } catch (ReflectiveOperationException ex) {
         throw new RuntimeException(ex); //if it fails something went very wrong
      }
   }

   private MediaType getConsumedType(TypeElement annotationType, Element element) {
      try {
         MediaType res = this.getAnnotationFieldsValue(annotationType, element, "consumes");
         return res == null
            ? this.getParentOrFallback(element, res, this::getConsumedType)
            : res;
      } catch(ReflectiveOperationException ex) {
         throw new RuntimeException(ex);
      }
   }

   private MediaType getProducedType(TypeElement annotationType, Element element) {
      try {
         MediaType res = this.getAnnotationFieldsValue(annotationType, element, "produces");
         return res == null
            ? this.getParentOrFallback(element, res, this::getProducedType)
            : res;
      } catch(ReflectiveOperationException ex) {
         throw new RuntimeException(ex);
      }
   }

   private RequestMethod[] getRequestMethods(TypeElement annotationType, Element element) {
      RequestMethod[] methods = annotationType.getQualifiedName().contentEquals(RequestMapping.class.getName())
         ? element.getAnnotation(RequestMapping.class).method()
         : annotationType.getAnnotation(RequestMapping.class).method();
      return methods.length == 0
         ? this.getParentOrFallback(element, methods, this::getRequestMethods)
         : methods;
   }

   private boolean isDeprecated(Element elem) {
      return elem.getAnnotation(Deprecated.class) != null
         || elem.getEnclosingElement().getAnnotation(Deprecated.class) != null;
   }

   @SuppressWarnings({"OptionalGetWithoutIsPresent", "unchecked"})
   private <T> T getAnnotationFieldsValue(TypeElement annotationType, Element element, String ... fieldNames)
      throws ReflectiveOperationException {

      Class<? extends Annotation> annClass = this.annotationClasses.stream()
         .filter(c -> annotationType.getQualifiedName().contentEquals(c.getName()))
         .findFirst()
         .get(); //should never fail

      T result = null;
      for(String fieldName : fieldNames) {
         result = (T) annClass.getField(fieldName).get(element.getAnnotation(annClass));
         if(result != null) return result;
      }

      return result;
   }

   private <T> T getParentOrFallback(Element element, T fallback, BiFunction<TypeElement, Element, T> fun) {
      List<Class<? extends Annotation>> found = this.annotationClasses.stream()
         .filter(annClass -> element.getEnclosingElement().getAnnotation(annClass) != null)
         .collect(Collectors.toList());

      if(found.isEmpty()) return fallback;

      if(found.size() > 1) this.processingEnv.getMessager().printMessage(
         Diagnostic.Kind.WARNING,
         "Found multiple mapping annotations on "
            + element.getSimpleName().toString()
            + ", only one of the will be considered!"
      );

      return fun.apply(
         this.processingEnv.getElementUtils()
            .getTypeElement(found.get(0).getName()),
         element
      );
   }

   @Override
   public Set<String> getSupportedAnnotationTypes() {
      return annotationClasses.stream().map(Class::getCanonicalName).collect(Collectors.toSet());
   }
}