Skip to content

Commit

Permalink
Fix generate hashcode to add this expression when needed (#1636)
Browse files Browse the repository at this point in the history
- fix GenerateHashCodeEqualsOperation.createHashCodeMethod() to
  add this specifier for fields that match local variables generated
- add new test to GenerateHashCodeEqualsTest
- fixes #1560
  • Loading branch information
jjohnstn authored Sep 11, 2024
1 parent 56aa875 commit e82cd2c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -471,7 +471,7 @@ private MethodDeclaration createHashCodeMethod() throws CoreException {
if (field.getType().isArray()) {
body.statements().add(createAddArrayHashCode(field));
} else if (fUseJ7HashEquals) {
j7Invoc.arguments().add(fAst.newSimpleName(field.getName()));
j7Invoc.arguments().add(getThisAccessForHashCode(field.getName()));
} else if (field.getType().isPrimitive()) {
Statement[] sts= createAddSimpleHashCode(field.getType(), this::getThisAccessForHashCode, field.getName(), false);
body.statements().addAll(Arrays.asList(sts));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -2238,4 +2238,118 @@ public void arraysDeepEqualsIn15() throws Exception {
"";
compareSource(expected, a.getSource());
}

@Test
public void testIssue1560() throws Exception {
ICompilationUnit a= fPackageP.createCompilationUnit("A.java",
"""
package p;
import java.util.Objects;
public class A {
static class Parent {
private int fieldA;
private String fieldB;
@Override
public int hashCode() {
return Objects.hash(fieldA, fieldB);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Parent)) {
return false;
}
Parent other = (Parent) obj;
return fieldA == other.fieldA && Objects.equals(fieldB, other.fieldB);
}
}
static class Child extends Parent {
private String result;
}
}
""", true, null);

IType[] allTypes= a.getAllTypes();
IType childType= null;
for (IType type : allTypes) {
if (type.getElementName().equals("Child")) {
childType= type;
break;
}
}
IField[] fields= getFields(childType, new String[] { "result"} );
runJ7Operation(childType, fields, false);

String expected= """
package p;
import java.util.Objects;
public class A {
static class Parent {
private int fieldA;
private String fieldB;
@Override
public int hashCode() {
return Objects.hash(fieldA, fieldB);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Parent)) {
return false;
}
Parent other = (Parent) obj;
return fieldA == other.fieldA && Objects.equals(fieldB, other.fieldB);
}
}
static class Child extends Parent {
private String result;
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Objects.hash(this.result);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Child other = (Child) obj;
return Objects.equals(result, other.result);
}
}
}
""";

compareSource(expected, a.getSource());

}
}

0 comments on commit e82cd2c

Please sign in to comment.