-
Notifications
You must be signed in to change notification settings - Fork 343
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
Override hashCode where equals is overridden #86
Conversation
src/tests/TypeNameTest.java
Outdated
@@ -45,6 +45,11 @@ public boolean equals(Object other) { | |||
return ((B) other).data == data; | |||
} | |||
|
|||
@Override | |||
public int hashCode() { | |||
return data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
return Integer.hashCode(value);
Directly using the data does not hash it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I returned data
as it is a int
. And Integer.hashCode(value) always return the passed value
. Anyway it will be better to use Integer.hashCode(value)
@@ -56,6 +56,10 @@ public boolean equals(Object o) { | |||
return this.b == other.b; | |||
} | |||
|
|||
@Override | |||
public int hashCode() { | |||
return b ? 1009 : 2003; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is functionally correct but Boolean.hashCode(b)
is clearer and hides this implementation detail.
|
||
@Override | ||
public int hashCode() { | ||
return b ? 1511 : 2503; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I'd prefer Boolean.hashCode(b)
unless there is a need to provide our own hash function.
Thank you for the suggestions. I made the suggested changes. BTW is it okay to have the same hashCode for an instance of this class and its Integer counterpart. So for instance |
If two objects are equal according to the equals(Object), then calling the hashCode method on each of the two objects must produce the same integer result. However we have classes in src/tests that overrides equals(Object) without overriding the hashCode method. This overrides the missing hashCode methods. Fixes: javapathfinder#85
If it is not required to have a different hash functions than the built-in
ones, it is best to use the built-in hash functions. This makes it easier
to understand the code and keeps us in sync with the Java base libraries.
I'm actually a bit surprised the Integer.hashCode is this simple; this
probably works well for hash tables but is quite a far cry from a
cryptographically secure hash function, or even one that you could use to
balance data across different servers.
Gayan Weerakutti wrote:
… ***@***.**** commented on this pull request.
---------------------------------------------------------------------------
In src/tests/TypeNameTest.java
<#86 (comment)>:
> @@ -45,6 +45,11 @@ public boolean equals(Object other) {
return ((B) other).data == data;
}
+ @OverRide
+ public int hashCode() {
+ return data;
I returned |data| as it is a |int|. And Integer.hashCode(value) always
return the passed |value|. Anyway it will be better to use
|Integer.hashCode(value)|
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#86 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AG9eR3nVu-MmtGeCmU4eapEOI5GUz0HMks5t84usgaJpZM4UnyYf>.
--
Regards,
Cyrille Artho - http://artho.com/
I love deadlines. I like the whooshing sound they make as they fly by.
-- Douglas Adams
|
I think it's fine if there is a small int i which is not distinguished from a given boolean b by its hash value alone. A correct implementation of |
JVMClassInfo.Initializer#setBootstrapMethod assumes to have a bootstrap method with a structure similar to the following (with 3 bootstrap method arguments) BootstrapMethods: 0: javapathfinder#83 REF_invokeStatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; Method arguments: javapathfinder#84 (Ljava/lang/Object;)Ljava/lang/Object; javapathfinder#85 REF_invokeInterface java/lang/annotation/Annotation.annotationType:()Ljava/lang/Class; javapathfinder#86 (Ljava/lang/annotation/Annotation;)Ljava/lang/Class; But if JPF encounters an bootstrap method with arbitary numer of arguments such as the StringConcatFactory.makeConcatWithConstants which have only one bootstrap method argument it results in an ArrayIndexOutOfBoundsException exception: [junit] java.lang.ArrayIndexOutOfBoundsException: 1 [junit] at gov.nasa.jpf.jvm.JVMClassInfo$Initializer.setBootstrapMethod(JVMClassInfo.java:93) [junit] at gov.nasa.jpf.jvm.ClassFile.setBootstrapMethod(ClassFile.java:659) [junit] at gov.nasa.jpf.jvm.ClassFile.parseBootstrapMethodAttr(ClassFile.java:1422) [junit] at gov.nasa.jpf.jvm.JVMClassInfo$Initializer.setClassAttribute(JVMClassInfo.java:80) [junit] at gov.nasa.jpf.jvm.ClassFile.setClassAttribute(ClassFile.java:636) [junit] at gov.nasa.jpf.jvm.ClassFile.parseClassAttributes(ClassFile.java:1306) [junit] at gov.nasa.jpf.jvm.ClassFile.parse(ClassFile.java:875) [junit] at gov.nasa.jpf.jvm.JVMClassInfo$Initializer.<init>(JVMClassInfo.java:48) This prevents the above exception from being thrown, by adding a new constructor BootstrapMethodInfo#BootstrapMethodInfo(enclosingClass, cpArgs) for handling bootstrap methods with arbitrary number of bootstrap method arguments, without evaluating cpArgs in JVMClassInfo.Initializer#setBootstrapMethod.
JVMClassInfo.Initializer#setBootstrapMethod assumes to have a bootstrap method with a structure similar to the following (with 3 bootstrap method arguments) BootstrapMethods: 0: javapathfinder#83 REF_invokeStatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; Method arguments: javapathfinder#84 (Ljava/lang/Object;)Ljava/lang/Object; javapathfinder#85 REF_invokeInterface java/lang/annotation/Annotation.annotationType:()Ljava/lang/Class; javapathfinder#86 (Ljava/lang/annotation/Annotation;)Ljava/lang/Class; In case JPF encounters an bootstrap method with arbitrary number of arguments such as the StringConcatFactory.makeConcatWithConstants which have only one bootstrap method argument JVMClassInfo.Initializer#setBootstrapMethod throws an ArrayIndexOutOfBoundsException exception: [junit] java.lang.ArrayIndexOutOfBoundsException: 1 [junit] at gov.nasa.jpf.jvm.JVMClassInfo$Initializer.setBootstrapMethod(JVMClassInfo.java:93) [junit] at gov.nasa.jpf.jvm.ClassFile.setBootstrapMethod(ClassFile.java:659) [junit] at gov.nasa.jpf.jvm.ClassFile.parseBootstrapMethodAttr(ClassFile.java:1422) [junit] at gov.nasa.jpf.jvm.JVMClassInfo$Initializer.setClassAttribute(JVMClassInfo.java:80) [junit] at gov.nasa.jpf.jvm.ClassFile.setClassAttribute(ClassFile.java:636) [junit] at gov.nasa.jpf.jvm.ClassFile.parseClassAttributes(ClassFile.java:1306) [junit] at gov.nasa.jpf.jvm.ClassFile.parse(ClassFile.java:875) [junit] at gov.nasa.jpf.jvm.JVMClassInfo$Initializer.<init>(JVMClassInfo.java:48) This prevents the above exception from being thrown, by adding a new constructor BootstrapMethodInfo#BootstrapMethodInfo(enclosingClass, cpArgs) for handling bootstrap methods with arbitrary number of bootstrap method arguments, without evaluating cpArgs within JVMClassInfo.Initializer#setBootstrapMethod.
If two objects are equal according to the equals(Object), then calling
the hashCode method on each of the two objects must produce the same
integer result.
However we have classes in src/tests that overrides equals(Object)
without overriding the hashCode method. This overrides the missing
hashCode methods.
Fixes: #85