INRIA / INRIA/spoon

HashcodeVisitor and EqualsChecker do not take the declaring type into account

Open
#3,365 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

The current [`HashcodeVisitor`](https://github.com/INRIA/spoon/blob/3a8d04df7cb0319896ae98b08986a7006b208fa6/src/main/java/spoon/support/visitor/HashcodeVisitor.java) implementation does not take the [Declaring Type](https://github.com/INRIA/spoon/blob/3a8d04df7cb0319896ae98b08986a7006b208fa6/src/main/java/spoon/reflect/declaration/CtTypeMember.java#L24) of a [`CtTypeMember`](https://github.com/INRIA/spoon/blob/3a8d04df7cb0319896ae98b08986a7006b208fa6/src/main/java/spoon/reflect/declaration/CtTypeMember.java) into account. This leads to unexpected behaviour when using `HashSet`s containing `CtElement`s. Overriding the `scanCtTypeMember` in `HashcodeVisitor` and `EqualsChecker` would solve that problem.

## Example

Two `CtFieldImpl` instances representing two Java fields with **the same name** but **declared in different classes** are equal and generate the same hash code:

```java
public class Person {

private String name;

}

public class City {

private String name;

}

CtClass personCtClass = ...;
CtClass cityClass = ...;

CtField personNameField = personCtClass .getField("name");
CtField cityNameField = cityClass.getField("name");

personNameField.equals(cityNameField) // true
personNameField.hashCode() == cityNameField .hashCode() // true

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.