Improve annotation processing (#2422)

This commit is contained in:
David Choo 2021-07-26 13:30:48 -04:00 committed by GitHub
parent 46cd26ffae
commit e0e6605fb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 14 deletions

View File

@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion; import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@SupportedAnnotationTypes("org.geysermc.connector.network.translators.world.block.entity.BlockEntity") @SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedSourceVersion(SourceVersion.RELEASE_8)
public class BlockEntityProcessor extends ClassProcessor { public class BlockEntityProcessor extends ClassProcessor {
public BlockEntityProcessor() { public BlockEntityProcessor() {

View File

@ -35,13 +35,14 @@ import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.FileObject; import javax.tools.FileObject;
import javax.tools.StandardLocation; import javax.tools.StandardLocation;
import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -50,7 +51,7 @@ public class ClassProcessor extends AbstractProcessor {
private Path outputPath; private Path outputPath;
private final List<String> locations = new ArrayList<>(); private final Set<String> locations = new HashSet<>();
public ClassProcessor(String annotationClassName) { public ClassProcessor(String annotationClassName) {
this.annotationClassName = annotationClassName; this.annotationClassName = annotationClassName;
@ -94,7 +95,7 @@ public class ClassProcessor extends AbstractProcessor {
TypeElement typeElement = (TypeElement) element; TypeElement typeElement = (TypeElement) element;
this.locations.add(typeElement.getQualifiedName().toString()); this.locations.add(typeElement.getQualifiedName().toString());
} }
return true; return false;
} }
public boolean contains(Collection<? extends TypeElement> elements, String className) { public boolean contains(Collection<? extends TypeElement> elements, String className) {
@ -126,18 +127,50 @@ public class ClassProcessor extends AbstractProcessor {
} }
public void complete() { public void complete() {
try (BufferedWriter writer = this.createWriter()) { // Read existing annotation list and verify each class still has this annotation
for (String location : this.locations) { try (BufferedReader reader = this.createReader()) {
writer.write(location); if (reader != null) {
writer.newLine(); 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) { } catch (IOException e) {
ex.printStackTrace(); 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); 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 { private BufferedWriter createWriter() throws IOException {
if (this.outputPath != null) { if (this.outputPath != null) {
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing " + this.annotationClassName + " to " + this.outputPath); this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing " + this.annotationClassName + " to " + this.outputPath);

View File

@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion; import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@SupportedAnnotationTypes("org.geysermc.connector.network.translators.collision.CollisionRemapper") @SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedSourceVersion(SourceVersion.RELEASE_8)
public class CollisionRemapperProcessor extends ClassProcessor { public class CollisionRemapperProcessor extends ClassProcessor {
public CollisionRemapperProcessor() { public CollisionRemapperProcessor() {

View File

@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion; import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@SupportedAnnotationTypes("org.geysermc.connector.network.translators.ItemRemapper") @SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedSourceVersion(SourceVersion.RELEASE_8)
public class ItemRemapperProcessor extends ClassProcessor { public class ItemRemapperProcessor extends ClassProcessor {
public ItemRemapperProcessor() { public ItemRemapperProcessor() {

View File

@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion; import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@SupportedAnnotationTypes("org.geysermc.connector.network.translators.Translator") @SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedSourceVersion(SourceVersion.RELEASE_8)
public class PacketTranslatorProcessor extends ClassProcessor { public class PacketTranslatorProcessor extends ClassProcessor {
public PacketTranslatorProcessor() { public PacketTranslatorProcessor() {

View File

@ -29,7 +29,7 @@ import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion; import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@SupportedAnnotationTypes("org.geysermc.connector.network.translators.sound.SoundHandler") @SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedSourceVersion(SourceVersion.RELEASE_8)
public class SoundHandlerProcessor extends ClassProcessor { public class SoundHandlerProcessor extends ClassProcessor {
public SoundHandlerProcessor() { public SoundHandlerProcessor() {