diff options
author | Linnea Gräf <nea@nea.moe> | 2024-04-11 11:25:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-11 11:25:35 +0200 |
commit | d6128f0dac37768bb295efe3f92c55ba890073de (patch) | |
tree | 81063a18b66914b0f31571b395b9158bd9b1eb68 /annotations | |
parent | 4cd130f140655f8741da93b1aa6c7bc7a2ce6d41 (diff) | |
download | NotEnoughUpdates-d6128f0dac37768bb295efe3f92c55ba890073de.tar.gz NotEnoughUpdates-d6128f0dac37768bb295efe3f92c55ba890073de.tar.bz2 NotEnoughUpdates-d6128f0dac37768bb295efe3f92c55ba890073de.zip |
Add hex overpay warning (#1072)
Co-authored-by: Lulonaut <lulonaut@lulonaut.dev>
Diffstat (limited to 'annotations')
-rw-r--r-- | annotations/src/main/kotlin/io/github/moulberry/notenoughupdates/autosubscribe/NEUAutoSymbolProcessor.kt | 83 |
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) |