All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ybo.trackingplugin.tasks.utils.impl.patterns.producers.KotlinMethodPatternProducer.kt Maven / Gradle / Ivy

Go to download

gradle plugin allowing to add automatic logs (or other process) at the start of each traced method

There is a newer version: 0.6.2
Show newest version
package com.ybo.trackingplugin.tasks.utils.impl.patterns.producers

import com.ybo.trackingplugin.TrackingPlugin
import com.ybo.trackingplugin.tasks.data.PatternToSearch
import com.ybo.trackingplugin.tasks.data.TraceAnnotationMark
import com.ybo.trackingplugin.tasks.utils.PatternProducer
import com.ybo.trackingplugin.tasks.utils.impl.patterns.KotlinMethodExtensionFunctionPattern
import com.ybo.trackingplugin.tasks.utils.impl.patterns.KotlinMethodHigherOrderFunctionNoParamsPattern
import com.ybo.trackingplugin.tasks.utils.impl.patterns.KotlinMethodHigherOrderFunctionPattern
import com.ybo.trackingplugin.tasks.utils.impl.patterns.KotlinMethodPattern
import com.ybo.trackingplugin.tasks.utils.impl.patterns.KotlinMethodPatternDecorator
import com.ybo.trackingplugin.tasks.utils.impl.patterns.KotlinMethodPatternName
import java.lang.IllegalStateException

class KotlinMethodPatternProducer(
    private val markToLookFor: TraceAnnotationMark,
) : PatternProducer {
    override fun produce(): List> {
        return listOf(
            KotlinMethodPattern(markToLookFor),
            KotlinMethodExtensionFunctionPattern(markToLookFor),
            KotlinMethodHigherOrderFunctionPattern(markToLookFor),
            KotlinMethodHigherOrderFunctionNoParamsPattern(markToLookFor),
        )
            .orThrowIfIncoherent()
            .also {
                if (TrackingPlugin.DEBUG) println("produced : $it")
            }
    }

    private fun kotlinMethodFixForComplexParams(extensionFunction: Boolean): KotlinMethodPattern {
        return KotlinMethodPatternDecorator(
            substrate = if (extensionFunction) {
                KotlinMethodPattern(markToLookFor)
            } else {
                KotlinMethodExtensionFunctionPattern(markToLookFor)
            },
            paramsWithCaptureReplacement = "\\((\\s*(?:\\w*\\s*:\\s*[\\w\\.]*(?:\\(.*\\)\\s*->\\s*[\\w\\.]*)?(?:\\s*=\\s*.*)?,?\\s*)*\\s*)\\)",
        )
    }

    private fun List>.orThrowIfIncoherent(): List> {
        val mapOfPatternName = mutableMapOf()
        for (pattern in this) {
            if (mapOfPatternName.containsKey(pattern.name)) {
                throw IllegalStateException("produced pattern list should not contain two element of same name")
            }
            mapOfPatternName[pattern.name] = true
        }
        return this
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy