aboutsummaryrefslogtreecommitdiff
path: root/annotations
diff options
context:
space:
mode:
Diffstat (limited to 'annotations')
-rw-r--r--annotations/src/main/kotlin/io/github/moulberry/notenoughupdates/autosubscribe/NEUAutoSymbolProcessor.kt83
1 files changed, 48 insertions, 35 deletions
diff --git a/annotations/src/main/kotlin/io/github/moulberry/notenoughupdates/autosubscribe/NEUAutoSymbolProcessor.kt b/annotations/src/main/kotlin/io/github/moulberry/notenoughupdates/autosubscribe/NEUAutoSymbolProcessor.kt
index 2fae6ceb..d2a25380 100644
--- a/annotations/src/main/kotlin/io/github/moulberry/notenoughupdates/autosubscribe/NEUAutoSymbolProcessor.kt
+++ b/annotations/src/main/kotlin/io/github/moulberry/notenoughupdates/autosubscribe/NEUAutoSymbolProcessor.kt
@@ -29,11 +29,8 @@ 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.validate
-import com.squareup.kotlinpoet.FileSpec
-import com.squareup.kotlinpoet.FunSpec
+import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
-import com.squareup.kotlinpoet.TypeSpec
-import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.writeTo
import java.util.function.Consumer
@@ -65,7 +62,9 @@ internal class NEUAutoSymbolProcessor(val codeGenerator: CodeGenerator, val logg
}
if (instanceGetter != null) {
val returnType = instanceGetter.returnType
- if (returnType == null || !element.asStarProjectedType().isAssignableFrom(returnType.resolve())) {
+ if (returnType == null || !element.asStarProjectedType()
+ .isAssignableFrom(returnType.resolve())
+ ) {
logger.error(
"getInstance() does not have the expected return type ${element.asStarProjectedType()}",
instanceGetter
@@ -118,45 +117,59 @@ internal class NEUAutoSymbolProcessor(val codeGenerator: CodeGenerator, val logg
if (subscribers.isEmpty()) return
val deps = subscribers.mapNotNull { it.declaration.containingFile }
logger.info("Dependencies: $deps")
+ val objectBuilder = TypeSpec.objectBuilder("AutoLoad")
FileSpec.builder("io.github.moulberry.notenoughupdates.autosubscribe", "AutoLoad")
.addFileComment("@generated by ${NEUAutoSymbolProcessor::class.simpleName}")
.addType(
- TypeSpec.objectBuilder("AutoLoad")
- .addFunction(
- FunSpec.builder("provide")
- .addParameter(
- "consumer",
- Consumer::class.asTypeName()
- .parameterizedBy(Supplier::class.parameterizedBy(Any::class))
- )
- .apply {
- subscribers.sortedBy { it.declaration.simpleName.asString() }.forEach { (invocationKind, declaration) ->
+ objectBuilder.addFunction(
+ FunSpec.builder("provide")
+ .addParameter(
+ "consumer",
+ Consumer::class.asTypeName()
+ .parameterizedBy(Supplier::class.parameterizedBy(Any::class))
+ )
+ .apply {
+ var instanceCounter = 0
+ subscribers.sortedBy { it.declaration.simpleName.asString() }
+ .forEach { (invocationKind, declaration) ->
when (invocationKind) {
- InvocationKind.GET_INSTANCE -> addStatement(
- "consumer.accept { %T.getInstance() }",
- declaration.toClassName()
- )
+ InvocationKind.GET_INSTANCE -> {
+ addStatement(
+ "consumer.accept { %T.getInstance() }",
+ declaration.toClassName()
+ )
+ }
- InvocationKind.OBJECT_INSTANCE -> addStatement(
- "consumer.accept { %T }",
- declaration.toClassName()
- )
+ InvocationKind.OBJECT_INSTANCE -> {
+ addStatement(
+ "consumer.accept { %T }",
+ declaration.toClassName()
+ )
+ }
- InvocationKind.DEFAULT_CONSTRUCTOR -> addStatement(
- "consumer.accept { %T() }",
- declaration.toClassName()
- )
+ InvocationKind.DEFAULT_CONSTRUCTOR -> {
+ val name = "instance_${instanceCounter++}"
+ objectBuilder.addProperty(
+ PropertySpec.builder(name, declaration.toClassName())
+ .delegate("lazy { %T() }", declaration.toClassName())
+ .build()
+ )
+ addStatement(
+ "consumer.accept { $name }",
+ )
+ }
- InvocationKind.ACCESS_INSTANCE -> addStatement(
- "consumer.accept { %T.INSTANCE }",
- declaration.toClassName()
- )
+ InvocationKind.ACCESS_INSTANCE -> {
+ addStatement(
+ "consumer.accept { %T.INSTANCE }",
+ declaration.toClassName()
+ )
+ }
}
}
- }
- .build()
- )
- .build()
+ }
+ .build()
+ ).build()
)
.build()
.writeTo(codeGenerator, aggregating = true, originatingKSFiles = deps)