Skip to content
New issue

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

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

Already on GitHub? # to your account

MethodInfo hashCode issue #42

Closed
BladeWise opened this issue Feb 5, 2022 · 1 comment
Closed

MethodInfo hashCode issue #42

BladeWise opened this issue Feb 5, 2022 · 1 comment

Comments

@BladeWise
Copy link

The current implementation of MethodInfo.hashCode returns the same value for overloaded methods.

As an example, below code

var abs1 = Type.of(Math.class).getMethod("abs", Types.Integer);
var abs2 = Type.of(Math.class).getMethod("abs", Types.Long);
String.format("%s vs %s:\nAre equal? %s\nHave same hashCode? %s", abs1, abs2, abs1.equals(abs2), abs1.hashCode() == abs2.hashCode());

will print

public static int abs(int) vs public static long abs(long):
Are equal? false
Have same hashCode? true

This means that all hash-based collections will never store overloaded methods together, only the last added one.

I noticed that some MemberInfo-derived classes override isEquivalentTo, without touching hashCode.
I suppose that all classes overriding IsEquivalentTo, should override hashCode accordingly.

@mstrobel
Copy link
Owner

mstrobel commented Feb 8, 2022

Thanks for the bug report!

for (MemberInfo m : Type.of(Math.class).findMembers(MemberType.methodsOnly(), BindingFlags.AllStatic, Type.FilterName, "abs")) {
    System.out.printf("%s = %d%n", m.getBriefDescription(), m.hashCode());
}

now yields

int abs(int) = -2086594591
double abs(double) = -2086575465
float abs(float) = -2086573285
long abs(long) = -2086594525

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants