From ad3ee80c88e45e137ac579e0bb2e210e325aba9f Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Sat, 8 Jun 2024 18:32:06 +1000 Subject: Backend: Make build fail on wrong annotation (#2024) --- .../skyhanni/skyhannimodule/ModuleProcessor.kt | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'annotation-processors/src/main') 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() + override fun process(resolver: Resolver): List { + 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) { + + 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) -- cgit