0

I read a code generated by khipster and in one dataclass I found such fragment:

import javax.validation.constraints.NotNull

data class MyDTO(
    var id: Long? = null,
    @get: NotNull
    var name: String? = null,

What does @get:NotNull annotation mean? As far as I understand @get means that I want to annotate the getter of name property and NotNull is a validation annotation which mean that the thing can't be set to null. But how the two work together? It doesn't make any sense to annotate getter with annotation which means this can't be set to null, because getter can't be set. It would make more sens to use NotNull annotation on setter.

user983447
  • 1,598
  • 4
  • 19
  • 36

2 Answers2

0

@NotNull on a method means it can't return null. So in particular annotating a setter with it makes no sense; annotating the setter's parameter does.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
0

If you use the decompile feature of IntelliJ ( please check this answer )

Kotlin Code:

@get: NotNull
var uid: String? = null

Decompiled Code:

import javax.validation.constraints.NotNull;
import org.jetbrains.annotations.Nullable;

@Nullable
private String uid;

@NotNull
@Nullable
public final String getUid() {
   return this.uid;
}

public final void setUid(@Nullable String var1) {
   this.uid = var1;
}

What does @get:NotNull annotation mean?

A quick answer: Please put @NotNull annotation on the top of the getter function

!! Please be aware that there is also @Nullable annotation added to the getter function because of the "?" at the end of the variable definition As you notice from import it is added by IntelliJ

As detailed answer: I could redirect you to "Use-Site Target Declarations"

Finally, I would like to express my experience on that, I had both @Nullable and @NotNull annotation on uid field (you could see on decompiled code), I could set that field to null

Birkan
  • 21
  • 4