aboutsummaryrefslogtreecommitdiff
path: root/annotation-processors/src/main
diff options
context:
space:
mode:
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)