David
David

Reputation: 8196

Is spotbugs compatible with Kotlin?

When running spotbugs on a Kolin project I get errors such as:

[ERROR] Private method com.example.CSVRecord.component1() is never called [com.example.CSVRecord] In CSVRecord.kt UPM_UNCALLED_PRIVATE_METHOD

on classes such as:

data class CSVRecord(private val columns: SortedSet<CSVColumn>) : Iterable<String> {

    override fun iterator(): Iterator<String> {
        return columns.map { it.value }.iterator()
    }
}

I'm not really clear where component1 came from!

Upvotes: 2

Views: 1980

Answers (2)

Vadzim
Vadzim

Reputation: 26180

There is a merged PR with Kotlin support in SpotBugs mentioned here: https://github.com/spotbugs/spotbugs/issues/573

Upvotes: 1

Leonardo Lima
Leonardo Lima

Reputation: 656

According to the Data Classes documentation:

The compiler automatically derives the following members from all properties declared in the primary constructor:

  • equals()/hashCode() pair;
  • toString() of the form "User(name=John, age=42)";
  • componentN() functions corresponding to the properties in their order of declaration;
  • copy() function (see below).

This is one the features of data classes. The auto-generated componentN functions allow you to use Destructuring Declarations on this type of classes:

data class Result(val result: Int, val status: Status)
fun function(...): Result {
    // computations

    return Result(result, status)
}

// Now, to use this function:
val (result, status) = function(...)

Upvotes: 2

Related Questions