Kotlin compiler inserts @Nullable
and @NotNull
from org.jetbrains.annotations
, is it possible to insert also javax.validation.constraints.NotNull
?
Asked
Active
Viewed 1,800 times
5

Romper
- 2,009
- 3
- 24
- 43
-
Ofcourse you can. You just need to write a Kotlin compiler plugin. You can take a look at a sample here: https://github.com/Takhion/sample-kotlin-compiler-plugin – Strelok Aug 21 '17 at 14:07
-
@Strelok Thanks. Maybe there is already a plugin? – Romper Aug 21 '17 at 14:17
-
3@Strelok Kotlin compiler plugins are undocumented, unsupported, not intended to be used by anyone outside JetBrains and guaranteed to break in the future, so please don't answer any Kotlin questions with "just need to write a compiler plugin". – yole Aug 21 '17 at 17:55
-
@Romper did you find a solution to this? I'm after the same thing :) – Russell Briggs Jan 24 '19 at 02:02
-
One year later, any progress? – Mike Holler Jun 08 '20 at 17:42
3 Answers
0
No, there is no such compiler plugin available at the moment. This is the list of available compiler plugins: https://kotlinlang.org/docs/reference/compiler-plugins.html

tango24
- 464
- 2
- 11
0
Yes, sure it is even if it’s not obvious.
@field:NotNull val valName: String?
But I prefer to use:
@field:NotEmpty val valName: String
Since I don’t think it’s a good idea to destroy the magic of null safe provided by Kotlin and have to manage later optional values.

Curtes Malteser
- 53
- 5
0
You can add dependency of hibernate validator just like below
dependencies {
...
implementation("org.hibernate.validator:hibernate-validator")
...
}
After that, you can import javax.validation.constraints.NotNull

sangho
- 11
- 3