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

name.remal.gradle_plugins.dsl.utils.code_quality.FindBugsReport.kt Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package name.remal.gradle_plugins.dsl.utils.code_quality

import name.remal.gradle_plugins.dsl.utils.canonizeText
import name.remal.gradle_plugins.dsl.utils.htmlToText
import name.remal.nullIfEmpty
import java.io.File
import java.time.Instant
import java.util.Comparator.naturalOrder
import java.util.Comparator.nullsLast

@DslMarker
private annotation class FindBugsReportDslMarker

@FindBugsReportDslMarker
data class FindBugsReport(
    var tool: String? = null,
    var version: String? = null,
    var analysisTimestamp: Long? = null,
    var project: FindBugsProject? = null,
    val bugs: MutableList = mutableListOf(),
    val types: MutableMap = sortedMapOf(),
    val categories: MutableMap = sortedMapOf()
) {

    val sortedBugs: List get() = bugs.sorted()


    fun tool(value: String?) {
        tool = value.nullIfEmpty()
    }


    fun version(value: String?) {
        version = value.nullIfEmpty()
    }


    fun analysisTimestamp(value: Long?) {
        analysisTimestamp = value
    }

    fun analysisTimestamp(value: Instant?) = analysisTimestamp(value?.toEpochMilli())


    fun project(init: FindBugsProject.() -> Unit) {
        project = FindBugsProject().apply(init)
    }


    fun bug(init: FindBugsBug.() -> Unit) {
        bugs.add(FindBugsBug().apply(init))
    }


    fun type(type: String, init: FindBugsType.() -> Unit) {
        types[type] = FindBugsType().apply(init)
    }


    fun category(type: String, init: FindBugsCategory.() -> Unit) {
        categories[type] = FindBugsCategory().apply(init)
    }

}

@FindBugsReportDslMarker
data class FindBugsProject(
    var name: String? = null,
    val srcDirs: MutableSet = mutableSetOf()
) {

    fun name(value: String?) {
        name = value.nullIfEmpty()
    }


    fun srcDir(value: File) {
        srcDirs.add(value.absolutePath)
    }

    fun srcDirs(vararg values: File) {
        values.forEach(::srcDir)
    }

    fun srcDir(value: String) {
        srcDirs.add(value)
    }

    fun srcDirs(vararg values: String) {
        srcDirs.addAll(values)
    }

    fun srcDirs(values: Iterable) {
        srcDirs.addAll(values)
    }

}

@FindBugsReportDslMarker
data class FindBugsBug(
    var category: String? = null,
    var type: String? = null,
    var priority: Int? = null,
    var message: String? = null,
    var shortMessage: String? = null,
    var location: FindBugsLocation? = null
) : Comparable {

    fun category(value: String?) {
        category = value.nullIfEmpty()
    }


    fun type(value: String?) {
        type = value.nullIfEmpty()
    }


    fun priority(value: Int?) {
        priority = value
    }


    fun message(value: String?) {
        message = canonizeText(value)
    }


    fun shortMessage(value: String?) {
        shortMessage = canonizeText(value)
    }


    fun location(init: FindBugsLocation.() -> Unit) {
        location = FindBugsLocation().apply(init)
    }


    @Suppress("ComplexMethod")
    override fun compareTo(other: FindBugsBug): Int {
        nullsLast(naturalOrder()).compare(priority, other.priority).let { if (it != 0) return it }
        nullsLast(naturalOrder()).compare(location?.className, other.location?.className).let { if (it != 0) return it }
        nullsLast(naturalOrder()).compare(location?.sourceFile, other.location?.sourceFile).let { if (it != 0) return it }
        nullsLast(naturalOrder()).compare(location?.startLine, other.location?.startLine).let { if (it != 0) return it }
        nullsLast(naturalOrder()).compare(location?.startLineOffset, other.location?.startLineOffset).let { if (it != 0) return it }
        nullsLast(reverseOrder()).compare(location?.endLine, other.location?.endLine).let { if (it != 0) return it }
        nullsLast(reverseOrder()).compare(location?.endLineOffset, other.location?.endLineOffset).let { if (it != 0) return it }
        nullsLast(naturalOrder()).compare(type, other.type).let { if (it != 0) return it }
        return 0
    }

}

@FindBugsReportDslMarker
data class FindBugsLocation(
    var className: String? = null,
    var sourceFile: String? = null,
    var startLine: Int? = null,
    var startLineOffset: Int? = null,
    var endLine: Int? = null,
    var endLineOffset: Int? = null
) {

    fun className(value: String?) {
        className = value.nullIfEmpty()
    }


    fun sourceFile(value: String?) {
        sourceFile = value.nullIfEmpty()
    }

    fun sourceFile(value: File?) {
        sourceFile = value?.path
    }


    fun startLine(value: Int?) {
        startLine = value
    }


    fun startLineOffset(value: Int?) {
        startLineOffset = value
    }


    fun endLine(value: Int?) {
        endLine = value
    }


    fun endLineOffset(value: Int?) {
        endLineOffset = value
    }

}

@FindBugsReportDslMarker
data class FindBugsType(
    override var textDescription: String? = null,
    override var htmlDescription: String? = null
) : FindBugsWithDescription

@FindBugsReportDslMarker
data class FindBugsCategory(
    override var textDescription: String? = null,
    override var htmlDescription: String? = null
) : FindBugsWithDescription

@FindBugsReportDslMarker
interface FindBugsWithDescription {

    var textDescription: String?

    var htmlDescription: String?

    fun textDescription(value: String?) {
        if (value != null) {
            canonizeText(value).let {
                textDescription = it
                htmlDescription = it?.replace("\n", "
\n") } } else { textDescription = null htmlDescription = null } } fun htmlDescription(value: String?) { if (value != null) { canonizeText(value).let { textDescription = it?.let { htmlToText(it) } htmlDescription = it } } else { textDescription = null htmlDescription = null } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy