Record can be converted to class
Reports record classes and suggests converting them to ordinary classes.
This inspection makes it possible to move a Java record to a codebase using an earlier Java version by applying the quick-fix to this record.
Note that the resulting class is not completely equivalent to the original record:
The resulting class no longer extends
java.lang.Record
, soinstanceof Record
returnsfalse
.Reflection methods like
Class.isRecord()
andClass.getRecordComponents()
produce different results.The generated
hashCode()
implementation may produce a different result because the formula to calculate recordhashCode
is deliberately not specified.Record serialization mechanism differs from that of an ordinary class. Refer to Java Object Serialization Specification for details.
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
RecordCanBeClass
Example:
record Point(int x, int y) {}
After the quick-fix is applied:
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 + ']';
}
}
This inspection depends on the Java feature 'Records', which is available since Java 16.
New in 2020.3
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Java, 243.23126 |
Thanks for your feedback!