このインスペクションは、Java レコードにクイックフィックスを適用し、そのレコードを旧バージョンの Java を使用しているコードベースに移動することを可能にします。
ただし、結果的に作成されるクラスは、元のレコードと完全に同等のものにはなりません。ご注意ください。
java.lang.Record を拡張しなくなるため、instanceof Record は false を返します。Class.isRecord() や Class.getRecordComponents() のようなリフレクションのメソッドは、異なる結果を出力します。hashCode を計算する数式が意図的に指定されていないため、生成される hashCode() の実装は異なる結果を返す可能性があります。例:
record Point(int x, int y) {}
クイックフィックス適用後:
final class Point {
private final int x;
private final int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public int x() { return x; }
public int y() { return y; }
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (Point)obj;
return this.x == that.x &&
this.y == that.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public String toString() {
return "Point[" +
"x=" + x + ", " +
"y=" + y + ']';
}
}
2020.3 の新機能です