From e0e6605fb630595762cdb79f1d5d64c6ab7f7050 Mon Sep 17 00:00:00 2001 From: David Choo Date: Mon, 26 Jul 2021 13:30:48 -0400 Subject: [PATCH] Improve annotation processing (#2422) --- .../processor/BlockEntityProcessor.java | 2 +- .../geysermc/processor/ClassProcessor.java | 51 +++++++++++++++---- .../processor/CollisionRemapperProcessor.java | 2 +- .../processor/ItemRemapperProcessor.java | 2 +- .../processor/PacketTranslatorProcessor.java | 2 +- .../processor/SoundHandlerProcessor.java | 2 +- 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/ap/src/main/java/org/geysermc/processor/BlockEntityProcessor.java b/ap/src/main/java/org/geysermc/processor/BlockEntityProcessor.java index 52a6e3aae..ecb69b604 100644 --- a/ap/src/main/java/org/geysermc/processor/BlockEntityProcessor.java +++ b/ap/src/main/java/org/geysermc/processor/BlockEntityProcessor.java @@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; -@SupportedAnnotationTypes("org.geysermc.connector.network.translators.world.block.entity.BlockEntity") +@SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class BlockEntityProcessor extends ClassProcessor { public BlockEntityProcessor() { diff --git a/ap/src/main/java/org/geysermc/processor/ClassProcessor.java b/ap/src/main/java/org/geysermc/processor/ClassProcessor.java index 3531d8794..409306f1f 100644 --- a/ap/src/main/java/org/geysermc/processor/ClassProcessor.java +++ b/ap/src/main/java/org/geysermc/processor/ClassProcessor.java @@ -35,13 +35,14 @@ import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic; import javax.tools.FileObject; import javax.tools.StandardLocation; +import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -50,7 +51,7 @@ public class ClassProcessor extends AbstractProcessor { private Path outputPath; - private final List locations = new ArrayList<>(); + private final Set locations = new HashSet<>(); public ClassProcessor(String annotationClassName) { this.annotationClassName = annotationClassName; @@ -94,7 +95,7 @@ public class ClassProcessor extends AbstractProcessor { TypeElement typeElement = (TypeElement) element; this.locations.add(typeElement.getQualifiedName().toString()); } - return true; + return false; } public boolean contains(Collection elements, String className) { @@ -126,18 +127,50 @@ public class ClassProcessor extends AbstractProcessor { } public void complete() { - try (BufferedWriter writer = this.createWriter()) { - for (String location : this.locations) { - writer.write(location); - writer.newLine(); + // Read existing annotation list and verify each class still has this annotation + try (BufferedReader reader = this.createReader()) { + if (reader != null) { + reader.lines().forEach(canonicalName -> { + if (!locations.contains(canonicalName)) { + TypeElement element = this.processingEnv.getElementUtils().getTypeElement(canonicalName); + if (element != null && element.getKind() == ElementKind.CLASS && contains(element.getAnnotationMirrors(), this.annotationClassName)) { + locations.add(canonicalName); + } + } + }); } - } catch (IOException ex) { - ex.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } + if (!locations.isEmpty()) { + try (BufferedWriter writer = this.createWriter()) { + for (String location : this.locations) { + writer.write(location); + writer.newLine(); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Did not find any classes annotated with " + this.annotationClassName); + } this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Completed processing for " + this.annotationClassName); } + private BufferedReader createReader() throws IOException { + if (this.outputPath != null) { + this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Reading existing " + this.annotationClassName + " list from " + this.outputPath); + return Files.newBufferedReader(this.outputPath); + } + FileObject obj = this.processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", this.annotationClassName); + if (obj != null) { + this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Reading existing " + this.annotationClassName + " list from " + obj.toUri()); + return new BufferedReader(obj.openReader(false)); + } + return null; + } + private BufferedWriter createWriter() throws IOException { if (this.outputPath != null) { this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing " + this.annotationClassName + " to " + this.outputPath); diff --git a/ap/src/main/java/org/geysermc/processor/CollisionRemapperProcessor.java b/ap/src/main/java/org/geysermc/processor/CollisionRemapperProcessor.java index 3f9111155..dafed179d 100644 --- a/ap/src/main/java/org/geysermc/processor/CollisionRemapperProcessor.java +++ b/ap/src/main/java/org/geysermc/processor/CollisionRemapperProcessor.java @@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; -@SupportedAnnotationTypes("org.geysermc.connector.network.translators.collision.CollisionRemapper") +@SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class CollisionRemapperProcessor extends ClassProcessor { public CollisionRemapperProcessor() { diff --git a/ap/src/main/java/org/geysermc/processor/ItemRemapperProcessor.java b/ap/src/main/java/org/geysermc/processor/ItemRemapperProcessor.java index cbfd939c4..6f146e720 100644 --- a/ap/src/main/java/org/geysermc/processor/ItemRemapperProcessor.java +++ b/ap/src/main/java/org/geysermc/processor/ItemRemapperProcessor.java @@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; -@SupportedAnnotationTypes("org.geysermc.connector.network.translators.ItemRemapper") +@SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class ItemRemapperProcessor extends ClassProcessor { public ItemRemapperProcessor() { diff --git a/ap/src/main/java/org/geysermc/processor/PacketTranslatorProcessor.java b/ap/src/main/java/org/geysermc/processor/PacketTranslatorProcessor.java index cdfbcbe4a..5225c4839 100644 --- a/ap/src/main/java/org/geysermc/processor/PacketTranslatorProcessor.java +++ b/ap/src/main/java/org/geysermc/processor/PacketTranslatorProcessor.java @@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; -@SupportedAnnotationTypes("org.geysermc.connector.network.translators.Translator") +@SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class PacketTranslatorProcessor extends ClassProcessor { public PacketTranslatorProcessor() { diff --git a/ap/src/main/java/org/geysermc/processor/SoundHandlerProcessor.java b/ap/src/main/java/org/geysermc/processor/SoundHandlerProcessor.java index ee4b2ef45..cd0b4a437 100644 --- a/ap/src/main/java/org/geysermc/processor/SoundHandlerProcessor.java +++ b/ap/src/main/java/org/geysermc/processor/SoundHandlerProcessor.java @@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; -@SupportedAnnotationTypes("org.geysermc.connector.network.translators.sound.SoundHandler") +@SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class SoundHandlerProcessor extends ClassProcessor { public SoundHandlerProcessor() {