Re-calculate SimpleKey's hashCode field on deserialization
Closes gh-24320
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.cache.interceptor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -27,19 +29,23 @@ import org.springframework.util.StringUtils;
|
||||
* A simple key as returned from the {@link SimpleKeyGenerator}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.0
|
||||
* @see SimpleKeyGenerator
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class SimpleKey implements Serializable {
|
||||
|
||||
/** An empty key. */
|
||||
/**
|
||||
* An empty key.
|
||||
*/
|
||||
public static final SimpleKey EMPTY = new SimpleKey();
|
||||
|
||||
|
||||
private final Object[] params;
|
||||
|
||||
private final int hashCode;
|
||||
// Effectively final, just re-calculated on deserialization
|
||||
private transient int hashCode;
|
||||
|
||||
|
||||
/**
|
||||
@@ -49,6 +55,7 @@ public class SimpleKey implements Serializable {
|
||||
public SimpleKey(Object... elements) {
|
||||
Assert.notNull(elements, "Elements must not be null");
|
||||
this.params = elements.clone();
|
||||
// Pre-calculate hashCode field
|
||||
this.hashCode = Arrays.deepHashCode(this.params);
|
||||
}
|
||||
|
||||
@@ -61,6 +68,7 @@ public class SimpleKey implements Serializable {
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
// Expose pre-calculated hashCode field
|
||||
return this.hashCode;
|
||||
}
|
||||
|
||||
@@ -69,4 +77,10 @@ public class SimpleKey implements Serializable {
|
||||
return getClass().getSimpleName() + " [" + StringUtils.arrayToCommaDelimitedString(this.params) + "]";
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
|
||||
ois.defaultReadObject();
|
||||
// Re-calculate hashCode field on deserialization
|
||||
this.hashCode = Arrays.deepHashCode(this.params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user