Skip to content

Commit d862ef0

Browse files
authored
Add additional null check to generated equals() method (#2686)
- prevent null check warnings for generated equals() method that uses super.equals() to test the input object - fixes #2683
1 parent 392a56b commit d862ef0

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/GenerateHashCodeEqualsOperation.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -829,13 +829,13 @@ private MethodDeclaration createEqualsMethod() throws CoreException {
829829
body.statements().add(
830830
createReturningIfStatement(fAst.newThisExpression(), fAst.newSimpleName(VARIABLE_NAME_EQUALS_PARAM), Operator.EQUALS, true));
831831

832-
if (needsNoSuperCall(fType, METHODNAME_EQUALS, new ITypeBinding[] {fAst.resolveWellKnownType(JAVA_LANG_OBJECT)})) {
833-
if (!fUseInstanceOf) {
834-
// if (obj == null) return false;
835-
body.statements().add(
836-
createReturningIfStatement(fAst.newSimpleName(VARIABLE_NAME_EQUALS_PARAM), fAst.newNullLiteral(), Operator.EQUALS, false));
837-
}
838-
} else {
832+
if (!fUseInstanceOf) {
833+
// if (obj == null) return false;
834+
body.statements().add(
835+
createReturningIfStatement(fAst.newSimpleName(VARIABLE_NAME_EQUALS_PARAM), fAst.newNullLiteral(), Operator.EQUALS, false));
836+
}
837+
838+
if (!needsNoSuperCall(fType, METHODNAME_EQUALS, new ITypeBinding[] {fAst.resolveWellKnownType(JAVA_LANG_OBJECT)})) {
839839
// if (!super.equals(obj)) return false;
840840
SuperMethodInvocation superEqualsCall= fAst.newSuperMethodInvocation();
841841
superEqualsCall.setName(fAst.newSimpleName(METHODNAME_EQUALS));

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/source/GenerateHashCodeEqualsTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -1886,6 +1886,8 @@ public void subTypeAndArraysIn14() throws Exception {
18861886
" public boolean equals(Object obj) {\r\n" +
18871887
" if (this == obj)\r\n" +
18881888
" return true;\r\n" +
1889+
" if (obj == null)\r\n" +
1890+
" return false;\r\n" +
18891891
" if (!super.equals(obj))\r\n" +
18901892
" return false;\r\n" +
18911893
" if (getClass() != obj.getClass())\r\n" +
@@ -1954,6 +1956,8 @@ public void subTypeIn17() throws Exception {
19541956
" public boolean equals(Object obj) {\r\n" +
19551957
" if (this == obj)\r\n" +
19561958
" return true;\r\n" +
1959+
" if (obj == null)\r\n" +
1960+
" return false;\r\n" +
19571961
" if (!super.equals(obj))\r\n" +
19581962
" return false;\r\n" +
19591963
" if (getClass() != obj.getClass())\r\n" +
@@ -2051,6 +2055,8 @@ public void subTypeNoFields() throws Exception {
20512055
" public boolean equals(Object obj) {\r\n" +
20522056
" if (this == obj)\r\n" +
20532057
" return true;\r\n" +
2058+
" if (obj == null)\r\n" +
2059+
" return false;\r\n" +
20542060
" if (!super.equals(obj))\r\n" +
20552061
" return false;\r\n" +
20562062
" if (getClass() != obj.getClass())\r\n" +
@@ -2336,6 +2342,8 @@ public int hashCode() {
23362342
public boolean equals(Object obj) {
23372343
if (this == obj)
23382344
return true;
2345+
if (obj == null)
2346+
return false;
23392347
if (!super.equals(obj))
23402348
return false;
23412349
if (getClass() != obj.getClass())

0 commit comments

Comments
 (0)