1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Kotlin】「is~」で始まるメソッドでBoolean?を返す場合AssertTrue / AssertFalseでバリデーションできない【BeanValidation】

Posted at

記事内容はタイトル通りです。

こうなる原理は以下の記事をご参照ください。

概要

メソッドにAssertTrueを付けた場合、バリデーションできる/できないは以下のようになります。

// バリデーションできる
@AssertTrue
fun getHoge(): Boolean? = false

// バリデーションできない
@AssertTrue
fun isHoge(): Boolean? = false

// バリデーションできる
@AssertTrue
fun isFuga(): Boolean = false

原因

以下は上記コードのデコンパイル結果です。

KotlinBoolean?型をjava.lang.Booleanにコンパイルします。
そして、冒頭に貼り付けた記事の通り、java.lang.Booleanはオブジェクトなので、BeanValidationis~から始まるメソッドを無視してしまうため、バリデーションは機能しません。

@AssertTrue
@Nullable
public final Boolean getHoge() {
   return false;
}
@AssertTrue
@Nullable
public final Boolean isHoge() {
   return false;
}
@AssertTrue
public final boolean isFuga() {
   return false;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?