Skip to content

Irrelevant NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR for Kotlin data classes #3094

@gtoison

Description

@gtoison

Initially reported here: spotbugs/sonar-findbugs#1036

For Kotlin data classes a zero-arg constructor seems to be generated by the compiler and assigns null values to non-null fields. Considering that this is a generated constructor it is not very relevant to report it to the user.

For instance this Kotlin source:

@Entity
@Table(name = "\"case\"") // 'case' is protected SQL keyword, so we have to put it in quotes here
data class CaseBE(

    @Id
    override val id: CASE_ID = UUID.randomUUID(),

    override var createdAt: OffsetDateTime,

    override var updatedAt: OffsetDateTime,

    var breakdownFormattedAddress: String, // <--- this is perfectly legal, value has to be specified when calling constructor

    ) : AbstractAuditedBaseBE(id, createdAt, updatedAt)

Is compiled to a .class containing this constructor:

  // Method descriptor #104 ()V
  // Stack: 6, Locals: 1
  public DummyBE();
     0  aload_0 [this]
     1  aconst_null
     2  aconst_null
     3  aconst_null
     4  bipush 7
     6  aconst_null
     7  invokespecial com.bmw.cc.csvbe.persistence.AbstractAuditedBaseBE(java.util.UUID, java.time.OffsetDateTime, java.time.OffsetDateTime, int, kotlin.jvm.internal.DefaultConstructorMarker) [107]
    10  return
      Local variable table:
        [pc: 0, pc: 11] local: this index: 0 type: com.xxx.cases.entity.DummyBE

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions