|
| 1 | +package dev.arbjerg.lavalink.processor; |
| 2 | + |
| 3 | +import javax.annotation.processing.*; |
| 4 | +import javax.lang.model.SourceVersion; |
| 5 | +import javax.lang.model.element.Element; |
| 6 | +import javax.lang.model.element.ElementKind; |
| 7 | +import javax.lang.model.element.TypeElement; |
| 8 | +import javax.tools.Diagnostic; |
| 9 | +import javax.tools.StandardLocation; |
| 10 | +import java.io.BufferedWriter; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +@SupportedAnnotationTypes({"org.pf4j.Extension"}) |
| 17 | +@SupportedSourceVersion(SourceVersion.RELEASE_17) |
| 18 | +public class ExtensionAnnotationProcessor extends AbstractProcessor { |
| 19 | + private List<CharSequence> metaAnnotations = new ArrayList<>(List.of("org.pf4j.Extension")); |
| 20 | + private List<CharSequence> extensions = new ArrayList<>(); |
| 21 | + |
| 22 | + @Override |
| 23 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 24 | + var extension = processingEnv.getElementUtils().getTypeElement("org.pf4j.Extension"); |
| 25 | + var metaAnnotations = roundEnv.getElementsAnnotatedWith(extension) |
| 26 | + .stream() |
| 27 | + .filter(element -> element.getKind() == ElementKind.ANNOTATION_TYPE) |
| 28 | + .map(element -> { |
| 29 | + processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Found new extension meta class: " + element.getSimpleName()); |
| 30 | + return processingEnv.getElementUtils().getBinaryName((TypeElement) element); |
| 31 | + }) |
| 32 | + .toList(); |
| 33 | + |
| 34 | + this.metaAnnotations.addAll(metaAnnotations); |
| 35 | + |
| 36 | + var extensions = roundEnv.getRootElements() |
| 37 | + .stream() |
| 38 | + .filter(this::checkAnnotations) |
| 39 | + .map(element -> { |
| 40 | + processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Found new extension: " + element.getSimpleName()); |
| 41 | + return processingEnv.getElementUtils().getBinaryName((TypeElement) element).toString(); |
| 42 | + }) |
| 43 | + .toList(); |
| 44 | + this.extensions.addAll(extensions); |
| 45 | + |
| 46 | + if (roundEnv.processingOver()) { |
| 47 | + writeStorage(); |
| 48 | + return false; |
| 49 | + } |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + private boolean checkAnnotations(Element element) { |
| 54 | + return element.getAnnotationMirrors().stream() |
| 55 | + .map(annotation -> processingEnv.getElementUtils().getBinaryName((TypeElement) annotation.getAnnotationType().asElement())) |
| 56 | + .anyMatch(annotation -> metaAnnotations.contains(annotation.toString())); |
| 57 | + } |
| 58 | + |
| 59 | + private void writeStorage() { |
| 60 | + try (var resource = new BufferedWriter(processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/extensions.idx").openWriter())) { |
| 61 | + for (CharSequence className : extensions) { |
| 62 | + resource.write(className.toString()); |
| 63 | + resource.newLine(); |
| 64 | + } |
| 65 | + } catch (IOException e) { |
| 66 | + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage()); |
| 67 | + throw new RuntimeException(e); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments