aboutsummaryrefslogtreecommitdiff
path: root/annotation-processors/src/main
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2024-06-08 18:32:06 +1000
committerGitHub <noreply@github.com>2024-06-08 10:32:06 +0200
commitad3ee80c88e45e137ac579e0bb2e210e325aba9f (patch)
treeaeb76a7212fa328f21806fe7e086bc653c413051 /annotation-processors/src/main
parentb649c903a5feab9fd92c5a9119514e2a4f7508c1 (diff)
downloadskyhanni-ad3ee80c88e45e137ac579e0bb2e210e325aba9f.tar.gz
skyhanni-ad3ee80c88e45e137ac579e0bb2e210e325aba9f.tar.bz2
skyhanni-ad3ee80c88e45e137ac579e0bb2e210e325aba9f.zip
Backend: Make build fail on wrong annotation (#2024)
Diffstat (limited to 'annotation-processors/src/main')
-rw-r--r--annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt39
1 files changed, 39 insertions, 0 deletions
diff --git a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt
index bc7d262b4..568164133 100644
--- a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt
+++ b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt
@@ -1,5 +1,7 @@
package at.hannibal2.skyhanni.skyhannimodule
+import com.google.devtools.ksp.getClassDeclarationByName
+import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.KSPLogger
@@ -8,13 +10,24 @@ import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
+import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.validate
import java.io.OutputStreamWriter
class ModuleProcessor(private val codeGenerator: CodeGenerator, private val logger: KSPLogger) : SymbolProcessor {
+ // TODO remove once all events are migrated to SkyHanniEvent
+ private var skyHanniEvent: KSType? = null
+ private var minecraftForgeEvent: KSType? = null
+ private val warnings = mutableListOf<String>()
+
override fun process(resolver: Resolver): List<KSAnnotated> {
+ skyHanniEvent =
+ resolver.getClassDeclarationByName("at.hannibal2.skyhanni.api.event.SkyHanniEvent")?.asStarProjectedType()
+ minecraftForgeEvent = resolver.getClassDeclarationByName("net.minecraftforge.fml.common.eventhandler.Event")
+ ?.asStarProjectedType()
+
val symbols = resolver.getSymbolsWithAnnotation(SkyHanniModule::class.qualifiedName!!).toList()
val validSymbols = symbols.mapNotNull { validateSymbol(it) }
@@ -41,10 +54,36 @@ class ModuleProcessor(private val codeGenerator: CodeGenerator, private val logg
return null
}
+ // TODO remove once all events are migrated to SkyHanniEvent
+ val className = symbol.qualifiedName?.asString() ?: "unknown"
+
+ for (function in symbol.getDeclaredFunctions()) {
+ if (function.annotations.any { it.shortName.asString() == "SubscribeEvent" }) {
+ val firstParameter = function.parameters.firstOrNull()?.type?.resolve()!!
+ if (!minecraftForgeEvent!!.isAssignableFrom(firstParameter)) {
+ warnings.add("Function in $className must have an event assignable from $minecraftForgeEvent because it is annotated with @SubscribeEvent")
+ }
+ }
+
+ if (function.annotations.any { it.shortName.asString() == "HandleEvent" }) {
+ val firstParameter = function.parameters.firstOrNull()?.type?.resolve()!!
+ if (!skyHanniEvent!!.isAssignableFrom(firstParameter)) {
+ warnings.add("Function in $className must have an event assignable from $skyHanniEvent because it is annotated with @HandleEvent")
+ }
+ }
+ }
+
return symbol
}
+ // TODO use Kotlin Poet once KMixins is merged
private fun generateFile(symbols: List<KSClassDeclaration>) {
+
+ if (warnings.isNotEmpty()) {
+ warnings.forEach { logger.warn(it) }
+ error("${warnings.size} errors related to event annotations found, please fix them before continuing. Click on the kspKotlin build log for more information.")
+ }
+
val dependencies = symbols.mapNotNull { it.containingFile }.toTypedArray()
val deps = Dependencies(true, *dependencies)