feat: implemented parameter printing

This commit is contained in:
zaaarf 2024-01-19 15:40:29 +01:00
parent ef3529336c
commit e858d20172
No known key found for this signature in database
GPG key ID: C91CFF9E2262BBA1
2 changed files with 52 additions and 4 deletions

View file

@ -12,8 +12,9 @@ public class Route {
public final String produces;
public final String consumes;
public final boolean deprecated;
public final Param[] params;
public Route(String route, RequestMethod[] methods, MediaType consumes, MediaType produces, boolean deprecated) {
public Route(String route, RequestMethod[] methods, MediaType consumes, MediaType produces, boolean deprecated, Param... params) {
this.route = route;
StringBuilder methodStringBuilder = new StringBuilder("[");
@ -33,5 +34,19 @@ public class Route {
else this.consumes = null;
this.deprecated = deprecated;
this.params = params;
}
public static class Param {
public final String typeFQN;
public final String name;
public final String defaultValue;
public Param(String typeFQN, String name, String defaultValue) {
this.typeFQN = typeFQN;
this.name = name;
this.defaultValue = defaultValue;
}
}
}

View file

@ -10,6 +10,7 @@ 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.lang.model.element.VariableElement;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
@ -50,12 +51,13 @@ public class RouteCompass extends AbstractProcessor {
this.getRequestMethods(annotationType, elem),
this.getConsumedType(annotationType, elem),
this.getProducedType(annotationType, elem),
this.isDeprecated(elem)
this.isDeprecated(elem),
this.getParams(elem.getParameters())
));
});
}
try { //TODO: support param printing
try {
FileObject serviceProvider = this.processingEnv.getFiler().createResource(
StandardLocation.SOURCE_OUTPUT, "", "routes"
);
@ -66,10 +68,19 @@ public class RouteCompass extends AbstractProcessor {
List<Route> routesInClass = this.foundRoutes.get(componentClass);
for(Route r : routesInClass) {
out.print("\t- " + r.method + r.route);
out.print("\t- ");
if(r.deprecated) out.print("[DEPRECATED] ");
out.print(r.method + " " + r.route);
if(r.consumes != null) out.print("(expects: " + r.consumes + ")");
if(r.produces != null) out.print("(returns: " + r.produces + ")");
out.println();
for(Route.Param p : r.params) {
out.print("\t\t- " + p.typeFQN + " " + p.name);
if(p.defaultValue != null)
out.print(" " + "(default: " + p.defaultValue + ")");
out.println();
}
}
}
@ -132,6 +143,28 @@ public class RouteCompass extends AbstractProcessor {
|| elem.getEnclosingElement().getAnnotation(Deprecated.class) != null;
}
private Route.Param[] getParams(List<? extends VariableElement> params) {
return params.stream()
.map(p -> {
RequestParam ann = p.getAnnotation(RequestParam.class);
if(ann == null) return null;
String name = ann.name(); //first try annotation.name()
name = name.isEmpty()
? ann.value() //then annotation.value()
: name;
name = name.isEmpty()
? p.getSimpleName().toString() //fall back on parameter name
: name;
String defaultValue = ann.defaultValue();
if(defaultValue.equals(ValueConstants.DEFAULT_NONE))
defaultValue = null;
return new Route.Param(name, defaultValue, p.asType().toString());
}).filter(Objects::nonNull).toArray(Route.Param[]::new);
}
@SuppressWarnings({"OptionalGetWithoutIsPresent", "unchecked"})
private <T> T getAnnotationFieldsValue(TypeElement annotationType, Element element, String ... fieldNames)
throws ReflectiveOperationException {