Skip to content

Commit

Permalink
fix for K2
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 26, 2024
1 parent 6515c10 commit 4aee990
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ 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.*
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?,
Expand All @@ -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?,
Expand All @@ -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
}
Expand All @@ -69,11 +80,13 @@ fun IrField.matchesAnyProperty(properties: Iterable<IrProperty>): 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 =
Expand Down Expand Up @@ -138,6 +151,7 @@ fun IrClass.declareThisReceiver(
}
}

@OptIn(UnsafeDuringIrConstructionAPI::class)
fun IrClass.declareObjectConstructor(
unitType: IrType,
irFactory: IrFactory,
Expand Down Expand Up @@ -168,6 +182,7 @@ fun IrClass.declareObjectConstructor(
}
}

@OptIn(UnsafeDuringIrConstructionAPI::class)
fun IrClass.declareObjectConstructor(
pluginContext: IrPluginContext
) = this.declareObjectConstructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ 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<IrProperty>,
private val config: Config,
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)
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 4aee990

Please sign in to comment.