Skip to content
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

SimpleKey contains invalid hashcode on deserialization when parameters include an enum #24320

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -39,7 +39,21 @@ public class SimpleKey implements Serializable {

private final Object[] params;

private final int hashCode;
/**
* The basic Object.hashCode functionality is to derive the hash code from
* a runtime-specific object identity. This makes SimpleKey hash codes unsafe
* to serialize and share with other JVM runtimes, since if the SimpleKey has
* any parameters of an object type that uses an identity hash code, a hash
* code received from another runtime will not match the hash code the
* receiving runtime would produce for an equal SimpleKey instance.
*
* For most object types this is not likely to cause a problem because an
* identity-based hash code implies an identity-based equals method, and objects
* like that are unlikely to make good cache keys in the first place. But
* enums are a big exception - they are singletons with special serialization
* rules and are fine cache key parameters, but it is important not to
* share their identity-based hash codes. */
private transient int hashCode;


/**
Expand All @@ -61,6 +75,10 @@ public boolean equals(@Nullable Object other) {

@Override
public final int hashCode() {
// If this instance was deserialized instead of constructed its hashCode may not be initialized.
if (this.hashCode == 0) {
this.hashCode = Arrays.deepHashCode(this.params);
}
return this.hashCode;
}

Expand Down