Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record.equals must accept null without an error #10016

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,30 @@ private void implementEquals(JRecordType type, JMethod method, SourceInfo info)
JMethodBody body = new JMethodBody(info);
JParameter otherParam = method.getParams().get(0);

// Equals is built from first == check between this and other, as a fast path for the same
// object and also to ensure that other isn't null. Then MyRecord.class == other.getClass(),
// and now we know they're the same type and can cast safely to access fields for the rest.
// if (this == other) return true;
JBinaryOperation eq =
new JBinaryOperation(info, JPrimitiveType.BOOLEAN, JBinaryOperator.EQ,
new JThisRef(info, type),
otherParam.createRef(info));
body.getBlock().addStmt(new JIfStatement(info, eq,
JBooleanLiteral.TRUE.makeReturnStatement(), null));

// other == null
JBinaryOperation nonNullCheck =
new JBinaryOperation(info, JPrimitiveType.BOOLEAN, JBinaryOperator.EQ,
otherParam.createRef(info), JNullLiteral.INSTANCE);
// MyRecordType.class != other.getClass()
JBinaryOperation sameTypeCheck =
new JBinaryOperation(info, JPrimitiveType.BOOLEAN, JBinaryOperator.NEQ,
new JClassLiteral(info, type),
new JMethodCall(info, otherParam.createRef(info), getClassMethod));
body.getBlock().addStmt(new JIfStatement(info, sameTypeCheck,
// other == null || MyRecordType.class != other.getClass()
JBinaryOperation nullAndTypeCheck =
new JBinaryOperation(info, JPrimitiveType.BOOLEAN, JBinaryOperator.OR,
nonNullCheck, sameTypeCheck);

// if (other == null || MyRecordType.class != other.getClass()) return false;
body.getBlock().addStmt(new JIfStatement(info, nullAndTypeCheck,
JBooleanLiteral.FALSE.makeReturnStatement(), null));

// Create a local to assign to and compare each component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public String toString() {
assertFalse(0 == new TopLevelRecord("Pear", 0).hashCode());

assertFalse(new InnerRecord().equals(new LocalRecord()));
assertFalse(new InnerRecord().equals(null));
assertFalse(new LocalRecord().equals(null));

RecordWithReferenceType sameA = new RecordWithReferenceType(new TopLevelRecord("a", 1));
RecordWithReferenceType sameB = new RecordWithReferenceType(new TopLevelRecord("a", 1));
Expand All @@ -184,6 +186,8 @@ public String toString() {

assertFalse(sameA.equals(different));
assertFalse(sameA.hashCode() == different.hashCode());

assertFalse(sameA.equals(null));
}

/**
Expand Down