fix: don't try to recreate the routemap file

This commit is contained in:
zaaarf 2024-01-24 16:32:10 +01:00
parent f5bff65ba8
commit 324d7e24e5
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B

View file

@ -29,6 +29,11 @@ import java.util.stream.Collectors;
@SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedSourceVersion(SourceVersion.RELEASE_8)
public class RouteCompass extends AbstractProcessor { public class RouteCompass extends AbstractProcessor {
/**
* The filename of the output.
*/
private static final String ROUTE_MAP_FILENAME = "route_map";
/** /**
* A {@link Map} tying each component class to the routes it contains. * A {@link Map} tying each component class to the routes it contains.
*/ */
@ -38,7 +43,7 @@ public class RouteCompass extends AbstractProcessor {
* A {@link Set} containing all the supported annotation classes. * A {@link Set} containing all the supported annotation classes.
*/ */
private final Set<Class<? extends Annotation>> annotationClasses = new HashSet<>(); private final Set<Class<? extends Annotation>> annotationClasses = new HashSet<>();
/** /**
* Default constructor, it only initialises {@link #annotationClasses}. * Default constructor, it only initialises {@link #annotationClasses}.
*/ */
@ -85,11 +90,23 @@ public class RouteCompass extends AbstractProcessor {
} }
try { try {
FileObject serviceProvider = this.processingEnv.getFiler().createResource( CharSequence startingContents;
StandardLocation.SOURCE_OUTPUT, "", "route_map" try {
FileObject existingRouteMap = this.processingEnv.getFiler().getResource(
StandardLocation.SOURCE_OUTPUT, "", ROUTE_MAP_FILENAME
);
startingContents = existingRouteMap.getCharContent(true);
existingRouteMap.delete();
} catch (IOException ex) {
startingContents = "";
}
FileObject routeMap = this.processingEnv.getFiler().createResource(
StandardLocation.SOURCE_OUTPUT, "", ROUTE_MAP_FILENAME
); );
PrintWriter out = new PrintWriter(serviceProvider.openWriter()); PrintWriter out = new PrintWriter(routeMap.openWriter());
out.println(startingContents); //print with an extra newline
for(String componentClass : this.foundRoutes.keySet()) { for(String componentClass : this.foundRoutes.keySet()) {
out.println(componentClass + ":"); out.println(componentClass + ":");
@ -152,7 +169,10 @@ public class RouteCompass extends AbstractProcessor {
String parent = this.getFullRoute(a, e)[0]; String parent = this.getFullRoute(a, e)[0];
for(int i = 0; i < routes.length; i++) { for(int i = 0; i < routes.length; i++) {
StringBuilder sb = new StringBuilder(parent); StringBuilder sb = new StringBuilder(parent);
if(!parent.endsWith("/")) sb.append("/"); if(!parent.endsWith("/") && !routes[i].startsWith("/"))
sb.append("/");
if(parent.endsWith("/") && routes[i].startsWith("/"))
sb.deleteCharAt(sb.length() - 1);
sb.append(routes[i]); sb.append(routes[i]);
routes[i] = sb.toString(); routes[i] = sb.toString();
} }