Skip to content

Commit

Permalink
Use own processor for @extension (as pf4j does not work properly for us)
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Nov 14, 2024
1 parent a1f52ba commit a21ce78
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 6 deletions.
4 changes: 4 additions & 0 deletions annotation-processor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ plugins {
`maven-publish`
}

dependencies {
// implementation(libs.pf4j)
}

publishing {
publications {
create<MavenPublication>("maven") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import java.util.Set;

@SupportedSourceVersion(SourceVersion.RELEASE_17)
@SupportedAnnotationTypes(LavalinkAnnotationProcessor.SPRING_CONFIGURATION)
public class LavalinkAnnotationProcessor extends AbstractProcessor {
@SupportedAnnotationTypes(ConfigurationAnnotationProcessor.SPRING_CONFIGURATION)
public class ConfigurationAnnotationProcessor extends AbstractProcessor {

public static final String SPRING_CONFIGURATION = "org.springframework.context.annotation.Configuration";
private TypeElement springConfigurationElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dev.arbjerg.lavalink.processor;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.StandardLocation;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@SupportedAnnotationTypes({"org.pf4j.Extension"})
@SupportedSourceVersion(SourceVersion.RELEASE_17)
public class ExtensionAnnotationProcessor extends AbstractProcessor {
private List<CharSequence> metaAnnotations = new ArrayList<>(List.of("org.pf4j.Extension"));
private List<CharSequence> extensions = new ArrayList<>();

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
var extension = processingEnv.getElementUtils().getTypeElement("org.pf4j.Extension");
var metaAnnotations = roundEnv.getElementsAnnotatedWith(extension)
.stream()
.filter(element -> element.getKind() == ElementKind.ANNOTATION_TYPE)
.map(element -> {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Found new extension meta class: " + element.getSimpleName());
return processingEnv.getElementUtils().getBinaryName((TypeElement) element);
})
.toList();

this.metaAnnotations.addAll(metaAnnotations);

var extensions = roundEnv.getRootElements()
.stream()
.filter(this::checkAnnotations)
.map(element -> {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Found new extension: " + element.getSimpleName());
return processingEnv.getElementUtils().getBinaryName((TypeElement) element).toString();
})
.toList();
this.extensions.addAll(extensions);

if (roundEnv.processingOver()) {
writeStorage();
return false;
}
return false;
}

private boolean checkAnnotations(Element element) {
return element.getAnnotationMirrors().stream()
.map(annotation -> processingEnv.getElementUtils().getBinaryName((TypeElement) annotation.getAnnotationType().asElement()))
.anyMatch(annotation -> metaAnnotations.contains(annotation.toString()));
}

private void writeStorage() {
try (var resource = new BufferedWriter(processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/extensions.idx").openWriter())) {
for (CharSequence className : extensions) {
resource.write(className.toString());
resource.newLine();
}
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dev.arbjerg.lavalink.processor.LavalinkAnnotationProcessor
dev.arbjerg.lavalink.processor.ConfigurationAnnotationProcessor
dev.arbjerg.lavalink.processor.ExtensionAnnotationProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

@Configuration
@Service
public class Plugin extends PluginEventHandler {

Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ksp = "2.0.21-1.0.27"

[libraries]
ksp-api = { group = "com.google.devtools.ksp", name = "symbol-processing-api", version.ref = "ksp" }
pf4j = { group = "org.pf4j", name = "pf4j", version = "3.12.1" }

[plugins]
gradle-publish = { id = "com.gradle.plugin-publish", version = "1.3.0" }
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/LavalinkGradlePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ private fun Project.configureDependencies(): Provider<Dependency> {
add("compileOnly", lavalink("plugin-api"))
if (plugins.hasPlugin("org.gradle.java")) {
add("annotationProcessor", "dev.arbjerg.lavalink:annotation-processor:${BuildConfig.VERSION}")
add("annotationProcessor", "org.pf4j:pf4j:3.12.1")
}
if (plugins.hasPlugin("org.jetbrains.kotlin")) {
if (plugins.hasPlugin("com.google.devtools.ksp")) {
Expand Down

0 comments on commit a21ce78

Please sign in to comment.