Skip to content

Commit b0e4b7e

Browse files
committed
Re-calculate SimpleKey's hashCode field on deserialization
Closes gh-24320
1 parent 08e9372 commit b0e4b7e

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/SimpleKey.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cache.interceptor;
1818

19+
import java.io.IOException;
20+
import java.io.ObjectInputStream;
1921
import java.io.Serializable;
2022
import java.util.Arrays;
2123

@@ -27,19 +29,23 @@
2729
* A simple key as returned from the {@link SimpleKeyGenerator}.
2830
*
2931
* @author Phillip Webb
32+
* @author Juergen Hoeller
3033
* @since 4.0
3134
* @see SimpleKeyGenerator
3235
*/
3336
@SuppressWarnings("serial")
3437
public class SimpleKey implements Serializable {
3538

36-
/** An empty key. */
39+
/**
40+
* An empty key.
41+
*/
3742
public static final SimpleKey EMPTY = new SimpleKey();
3843

3944

4045
private final Object[] params;
4146

42-
private final int hashCode;
47+
// Effectively final, just re-calculated on deserialization
48+
private transient int hashCode;
4349

4450

4551
/**
@@ -49,6 +55,7 @@ public class SimpleKey implements Serializable {
4955
public SimpleKey(Object... elements) {
5056
Assert.notNull(elements, "Elements must not be null");
5157
this.params = elements.clone();
58+
// Pre-calculate hashCode field
5259
this.hashCode = Arrays.deepHashCode(this.params);
5360
}
5461

@@ -61,6 +68,7 @@ public boolean equals(@Nullable Object other) {
6168

6269
@Override
6370
public final int hashCode() {
71+
// Expose pre-calculated hashCode field
6472
return this.hashCode;
6573
}
6674

@@ -69,4 +77,10 @@ public String toString() {
6977
return getClass().getSimpleName() + " [" + StringUtils.arrayToCommaDelimitedString(this.params) + "]";
7078
}
7179

80+
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
81+
ois.defaultReadObject();
82+
// Re-calculate hashCode field on deserialization
83+
this.hashCode = Arrays.deepHashCode(this.params);
84+
}
85+
7286
}

spring-context/src/test/java/org/springframework/cache/interceptor/SimpleKeyGeneratorTests.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,17 +18,16 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
import static org.assertj.core.api.Assertions.assertThat;
22-
23-
24-
21+
import org.springframework.core.testfixture.io.SerializationTestUtils;
2522

23+
import static org.assertj.core.api.Assertions.assertThat;
2624

2725
/**
2826
* Tests for {@link SimpleKeyGenerator} and {@link SimpleKey}.
2927
*
3028
* @author Phillip Webb
3129
* @author Stephane Nicoll
30+
* @author Juergen Hoeller
3231
*/
3332
public class SimpleKeyGeneratorTests {
3433

@@ -47,7 +46,7 @@ public void noValues() {
4746
}
4847

4948
@Test
50-
public void singleValue(){
49+
public void singleValue() {
5150
Object k1 = generateKey(new Object[] { "a" });
5251
Object k2 = generateKey(new Object[] { "a" });
5352
Object k3 = generateKey(new Object[] { "different" });
@@ -59,7 +58,7 @@ public void singleValue(){
5958
}
6059

6160
@Test
62-
public void multipleValues() {
61+
public void multipleValues() {
6362
Object k1 = generateKey(new Object[] { "a", 1, "b" });
6463
Object k2 = generateKey(new Object[] { "a", 1, "b" });
6564
Object k3 = generateKey(new Object[] { "b", 1, "a" });
@@ -114,6 +113,17 @@ public void arrayWithExtraParameter() {
114113
assertThat(k1).isNotEqualTo(k3);
115114
}
116115

116+
@Test
117+
public void serializedKeys() throws Exception {
118+
Object k1 = SerializationTestUtils.serializeAndDeserialize(generateKey(new Object[] { "a", 1, "b" }));
119+
Object k2 = SerializationTestUtils.serializeAndDeserialize(generateKey(new Object[] { "a", 1, "b" }));
120+
Object k3 = SerializationTestUtils.serializeAndDeserialize(generateKey(new Object[] { "b", 1, "a" }));
121+
assertThat(k1.hashCode()).isEqualTo(k2.hashCode());
122+
assertThat(k1.hashCode()).isNotEqualTo(k3.hashCode());
123+
assertThat(k1).isEqualTo(k2);
124+
assertThat(k1).isNotEqualTo(k3);
125+
}
126+
117127

118128
private Object generateKey(Object[] arguments) {
119129
return this.generator.generate(null, null, arguments);

0 commit comments

Comments
 (0)