-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37f24a7
commit 9d0c92a
Showing
15 changed files
with
214 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
plugins { | ||
java | ||
`maven-publish` | ||
} | ||
|
||
publishing { | ||
publications { | ||
create<MavenPublication>("maven") { | ||
artifactId = "lavalink-annotation-processor" | ||
from(components["java"]) | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...n-processor/src/main/java/dev/arbjerg/lavalink/processor/LavalinkAnnotationProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package dev.arbjerg.lavalink.processor; | ||
|
||
import javax.annotation.processing.*; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.ElementKind; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.tools.Diagnostic.Kind; | ||
import javax.tools.StandardLocation; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@SupportedSourceVersion(SourceVersion.RELEASE_17) | ||
@SupportedAnnotationTypes(LavalinkAnnotationProcessor.SPRING_CONFIGURATION) | ||
public class LavalinkAnnotationProcessor extends AbstractProcessor { | ||
|
||
public static final String SPRING_CONFIGURATION = "org.springframework.context.annotation.Configuration"; | ||
private TypeElement springConfigurationElement; | ||
private final List<CharSequence> configurationClasses = new ArrayList<>(); | ||
|
||
@Override | ||
public synchronized void init(final ProcessingEnvironment processingEnv) { | ||
springConfigurationElement = processingEnv.getElementUtils().getTypeElement(SPRING_CONFIGURATION); | ||
super.init(processingEnv); | ||
} | ||
|
||
@Override | ||
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) { | ||
if (roundEnv.processingOver()) { | ||
writeStorage(); | ||
return false; | ||
} | ||
var newConfigurations = roundEnv.getElementsAnnotatedWith(springConfigurationElement) | ||
.stream() | ||
.filter(element -> element.getKind() == ElementKind.CLASS) | ||
.map(clazz -> processingEnv.getElementUtils().getBinaryName((TypeElement) clazz)) | ||
.toList(); | ||
processingEnv.getMessager().printMessage(Kind.NOTE, "Found the following new configurations: " + newConfigurations); | ||
configurationClasses.addAll(newConfigurations); | ||
return false; | ||
} | ||
|
||
private void writeStorage() { | ||
try (var resource = new BufferedWriter(processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/configurations.idx").openWriter())) { | ||
for (CharSequence className : configurationClasses) { | ||
resource.write(className.toString()); | ||
resource.newLine(); | ||
} | ||
} catch (IOException e) { | ||
processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage()); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...tion-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dev.arbjerg.lavalink.processor.LavalinkAnnotationProcessor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") | ||
includeBuild("..") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
[versions] | ||
ksp = "2.0.21-1.0.27" | ||
|
||
[libraries] | ||
ksp-api = { group = "com.google.devtools.ksp", name = "symbol-processing-api", version.ref = "ksp" } | ||
|
||
[plugins] | ||
gradle-publish = { id = "com.gradle.plugin-publish", version = "1.3.0" } | ||
buildconfig = { id = "com.github.gmazzo.buildconfig", version = "5.5.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(17) | ||
} | ||
|
||
dependencies { | ||
implementation(libs.ksp.api) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package dev.arbjerg.lavalink.processor | ||
|
||
const val SPRING_CONFIGURATION = "org.springframework.context.annotation.Configuration" | ||
const val PF4J_EXTENSION = "org.pf4j.Extension" |
39 changes: 39 additions & 0 deletions
39
kotlin-symbol-processor/src/main/kotlin/ConfigurationProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dev.arbjerg.lavalink.processor | ||
|
||
import com.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
|
||
class ConfigurationProcessor(private val environment: SymbolProcessorEnvironment) : SymbolProcessor { | ||
private val configurationClasses = mutableListOf<String>() | ||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
configurationClasses += resolver.getSymbolsWithAnnotation(SPRING_CONFIGURATION) | ||
.filterIsInstance<KSClassDeclaration>() | ||
.mapNotNull { it.qualifiedName?.toString() } | ||
.onEach { | ||
environment.logger.logging("Found configuration class: $it") | ||
} | ||
|
||
return emptyList() | ||
} | ||
|
||
override fun finish() { | ||
environment.logger.logging("Received finish signal, writing ${configurationClasses.size} to fs") | ||
environment.codeGenerator.createNewFile( | ||
dependencies = Dependencies.ALL_FILES, | ||
packageName = "META-INF", | ||
fileName = "configuration-classes", | ||
extensionName = "idx" | ||
).bufferedWriter().use { writer -> | ||
configurationClasses.forEach { | ||
writer.write(it) | ||
writer.newLine() | ||
} | ||
} | ||
} | ||
|
||
companion object Provider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = | ||
ConfigurationProcessor(environment) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
kotlin-symbol-processor/src/main/kotlin/ExtensionProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dev.arbjerg.lavalink.processor | ||
|
||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.processing.SymbolProcessor | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
import com.google.devtools.ksp.symbol.ClassKind | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
|
||
class ExtensionProcessor(private val environment: SymbolProcessorEnvironment) : SymbolProcessor { | ||
|
||
private val metaAnnotations = mutableListOf<String>() | ||
private val extensions = mutableListOf<String>() | ||
|
||
companion object Provider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = ExtensionProcessor(environment) | ||
} | ||
|
||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
metaAnnotations += resolver.getSymbolsWithAnnotation(PF4J_EXTENSION) | ||
.filterIsInstance<KSClassDeclaration>() | ||
.filter { it.classKind == ClassKind.ANNOTATION_CLASS } | ||
.onEach { environment.logger.logging("Found meta annotation: ${it.qualifiedName?.asString()}") } | ||
.map { it.qualifiedName.toString() } | ||
|
||
return resolver.getNewFiles() | ||
.flatMap { it.declarations } | ||
.filterIsInstance<KSClassDeclaration>() | ||
.filter { | ||
if (it.qualifiedName != null && it.annotations.any { annotation -> annotation.shortName.asString() in metaAnnotations }) { | ||
extensions.add(it.qualifiedName!!.toString()) | ||
environment.logger.logging("Found extension: ${it.qualifiedName!!.asString()}") | ||
false | ||
} else { | ||
true | ||
} | ||
} | ||
.toList() | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dev.arbjerg.lavalink.processor.ConfigurationProcessor$Provider | ||
dev.arbjerg.lavalink.processor.ExtensionProcessor$Provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
rootProject.name = "lavalink-gradle-plugin" | ||
|
||
include( | ||
"kotlin-symbol-processor", | ||
"annotation-processor" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters