From 4aee9909fcaa777d02bb87e3a03e8cbb1d85f3a2 Mon Sep 17 00:00:00 2001 From: DatLag Date: Sun, 26 May 2024 17:24:51 +0200 Subject: [PATCH] fix for K2 --- .../datlag/sekret/SekretComponentRegistrar.kt | 11 ++--- .../dev/datlag/sekret/common/ExtendIr.kt | 45 ++++++++++++------- .../sekret/transformer/ElementTransformer.kt | 7 ++- .../sekret/transformer/ToStringTransformer.kt | 8 +++- 4 files changed, 46 insertions(+), 25 deletions(-) diff --git a/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/SekretComponentRegistrar.kt b/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/SekretComponentRegistrar.kt index 7d6c400..57a36ea 100644 --- a/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/SekretComponentRegistrar.kt +++ b/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/SekretComponentRegistrar.kt @@ -37,19 +37,14 @@ class SekretComponentRegistrar : CompilerPluginRegistrar() { IrGenerationExtension.registerExtension(object : IrGenerationExtension { override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) { - DeobfuscatorGenerator.createIrClass(pluginContext, logger) + // DeobfuscatorGenerator.createIrClass(pluginContext, logger) - val generatedDeobfuscatorModule = moduleFragment.transform( - transformer = DeobfuscatorTransformer(logger, pluginContext), - data = null - ) - - val addedObfuscatedValues = generatedDeobfuscatorModule.transform( + moduleFragment.transform( transformer = ElementTransformer(config, logger, pluginContext), data = null ) - DeobfuscatorGenerator.generateList(pluginContext, logger) + // DeobfuscatorGenerator.generateList(pluginContext, logger) } }) } diff --git a/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/common/ExtendIr.kt b/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/common/ExtendIr.kt index 121ae2c..2688298 100644 --- a/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/common/ExtendIr.kt +++ b/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/common/ExtendIr.kt @@ -9,7 +9,7 @@ import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol -import org.jetbrains.kotlin.ir.symbols.impl.IrAnonymousInitializerSymbolImpl +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.SpecialNames +@OptIn(UnsafeDuringIrConstructionAPI::class) fun IrProperty.hasMatchingAnnotation( name: FqName, parent: IrClass?, @@ -25,27 +26,32 @@ fun IrProperty.hasMatchingAnnotation( ): Boolean { return this.hasAnnotation(name) || this.originalProperty.hasAnnotation(name) || run { if (checkGetter && parent != null) { - (runCatching { - parent.getPropertyGetter(this.name.asString()) - }.getOrNull() ?: runCatching { - parent.getPropertyGetter(this.name.asStringStripSpecialMarkers()) - }.getOrNull())?.owner?.hasAnnotation(name) == true + runCatching { + (runCatching { + parent.getPropertyGetter(this.name.asString()) + }.getOrNull() ?: runCatching { + parent.getPropertyGetter(this.name.asStringStripSpecialMarkers()) + }.getOrNull())?.owner?.hasAnnotation(name) + }.getOrNull() == true } else { false } } || run { if (checkSetter && parent != null) { - (runCatching { - parent.getPropertySetter(this.name.asString()) - }.getOrNull() ?: runCatching { - parent.getPropertySetter(this.name.asStringStripSpecialMarkers()) - }.getOrNull())?.owner?.hasAnnotation(name) == true + runCatching { + (runCatching { + parent.getPropertySetter(this.name.asString()) + }.getOrNull() ?: runCatching { + parent.getPropertySetter(this.name.asStringStripSpecialMarkers()) + }.getOrNull())?.owner?.hasAnnotation(name) + }.getOrNull() == true } else { false } } } +@OptIn(UnsafeDuringIrConstructionAPI::class) fun IrField.hasMatchingAnnotation( name: FqName, parent: IrClass?, @@ -54,12 +60,17 @@ fun IrField.hasMatchingAnnotation( ): Boolean { return this.hasAnnotation(name) || this.type.hasAnnotation(name) - || this.correspondingPropertySymbol?.owner?.hasMatchingAnnotation(name, parent, checkGetter, checkSetter) ?: false + || runCatching { + this.correspondingPropertySymbol?.owner?.hasMatchingAnnotation(name, parent, checkGetter, checkSetter) + }.getOrNull() ?: false } +@OptIn(UnsafeDuringIrConstructionAPI::class) fun IrField.matchesProperty(property: IrProperty): Boolean { return if (this.isPropertyField) { - this.correspondingPropertySymbol?.owner == property || this.name == property.name + runCatching { + this.correspondingPropertySymbol?.owner == property + }.getOrNull() ?: false || this.name == property.name } else { false } @@ -69,11 +80,13 @@ fun IrField.matchesAnyProperty(properties: Iterable): Boolean { return properties.any { this.matchesProperty(it) } } +@OptIn(UnsafeDuringIrConstructionAPI::class) fun IrType.matches(signature: IdSignature.CommonSignature, nullable: Boolean? = null): Boolean { if (this !is IrSimpleType) return false if (nullable != null && this.isMarkedNullable() != nullable) return false - return signature == classifier.signature || - classifier.owner.let { it is IrClass && it.signatureMatchesFqName(signature) } + return signature == classifier.signature || runCatching { + classifier.owner.let { it is IrClass && it.signatureMatchesFqName(signature) } + }.getOrNull() ?: false } fun IrClass.signatureMatchesFqName(signature: IdSignature.CommonSignature): Boolean = @@ -138,6 +151,7 @@ fun IrClass.declareThisReceiver( } } +@OptIn(UnsafeDuringIrConstructionAPI::class) fun IrClass.declareObjectConstructor( unitType: IrType, irFactory: IrFactory, @@ -168,6 +182,7 @@ fun IrClass.declareObjectConstructor( } } +@OptIn(UnsafeDuringIrConstructionAPI::class) fun IrClass.declareObjectConstructor( pluginContext: IrPluginContext ) = this.declareObjectConstructor( diff --git a/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/transformer/ElementTransformer.kt b/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/transformer/ElementTransformer.kt index 48e64fa..f14a6d6 100644 --- a/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/transformer/ElementTransformer.kt +++ b/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/transformer/ElementTransformer.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.builders.irBlockBody import org.jetbrains.kotlin.ir.builders.irCall import org.jetbrains.kotlin.ir.builders.irReturn import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.name.* @@ -25,11 +26,15 @@ class ElementTransformer( private val obfuscateAnnotation = FqName.fromSegments(listOf("dev.datlag.sekret", "Obfuscate")) + @OptIn(UnsafeDuringIrConstructionAPI::class) override fun visitClassNew(declaration: IrClass): IrStatement { val hasObfuscate = declaration.hasAnnotation(obfuscateAnnotation) val secretAnnotation = FqName.fromSegments(listOf("dev.datlag.sekret", "Secret")) - val secretProperties = declaration.properties.filter { it.hasMatchingAnnotation(secretAnnotation, declaration) } + val secretProperties = runCatching { + declaration.properties.filter { it.hasMatchingAnnotation(secretAnnotation, declaration) } + }.getOrNull().orEmpty() + if (secretProperties.count() > 0) { declaration.getSimpleFunction("toString")?.owner?.transformChildren( ToStringTransformer(secretProperties.toList(), config, logger, pluginContext), diff --git a/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/transformer/ToStringTransformer.kt b/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/transformer/ToStringTransformer.kt index 0edd0d3..c9bfe5d 100644 --- a/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/transformer/ToStringTransformer.kt +++ b/sekret-compiler-plugin/src/main/kotlin/dev/datlag/sekret/transformer/ToStringTransformer.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.ir.declarations.IrProperty import org.jetbrains.kotlin.ir.expressions.IrConst import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrGetField +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI class ToStringTransformer( private val secretProperties: Collection, @@ -19,6 +20,7 @@ class ToStringTransformer( private val logger: Logger, private val pluginContext: IrPluginContext ) : IrElementTransformerVoidWithContext() { + @OptIn(UnsafeDuringIrConstructionAPI::class) override fun visitGetField(expression: IrGetField): IrExpression { val string = expression.type.isAnyString(true) val charSequence = expression.type.isAnyCharSequence(true) @@ -27,7 +29,11 @@ class ToStringTransformer( val stringBuffer = expression.type.isStringBuffer(true) if (string || charSequence || stringBuilder || appendable || stringBuffer) { - if (expression.symbol.owner.matchesAnyProperty(secretProperties)) { + val matches = runCatching { + expression.symbol.owner.matchesAnyProperty(secretProperties) + }.getOrNull() ?: false + + if (matches) { return DeclarationIrBuilder(pluginContext, expression.symbol).irString(config.secretMask) } }