3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-19 14:30:17 +01:00

Improve annotation processing (#2422)

Dieser Commit ist enthalten in:
David Choo 2021-07-26 13:30:48 -04:00 committet von GitHub
Ursprung 46cd26ffae
Commit e0e6605fb6
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
6 geänderte Dateien mit 47 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -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() {

Datei anzeigen

@ -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);

Datei anzeigen

@ -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() {

Datei anzeigen

@ -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() {

Datei anzeigen

@ -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() {

Datei anzeigen

@ -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() {