Skip to content

Commit 01f7173

Browse files
committed
Introduce ObjectUtils#nullSafeHash(Object... element)
This commit deprecates the various nullSafeHashCode methods taking array types as they are superseded by Arrays.hashCode now. This means that the now only remaining nullSafeHashCode method does not trigger a warning only if the target type is not an array. At the same time, there are multiple use of this method on several elements, handling the accumulation of hash codes. For that reason, this commit also introduces a nullSafeHash that takes an array of elements. The only difference between Objects.hash is that this method handles arrays. The codebase has been reviewed to use any of those two methods when it is possible. Closes gh-29051
1 parent f2e898d commit 01f7173

File tree

38 files changed

+267
-313
lines changed

38 files changed

+267
-313
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,8 @@ public boolean equals(@Nullable Object other) {
525525

526526
@Override
527527
public int hashCode() {
528-
int hashCode = ObjectUtils.nullSafeHashCode(getExpression());
529-
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.pointcutDeclarationScope);
530-
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.pointcutParameterNames);
531-
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.pointcutParameterTypes);
532-
return hashCode;
528+
return ObjectUtils.nullSafeHash(getExpression(), this.pointcutDeclarationScope,
529+
this.pointcutParameterNames, this.pointcutParameterTypes);
533530
}
534531

535532
@Override

spring-aop/src/main/java/org/springframework/aop/aspectj/TypePatternClassFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.aop.aspectj;
1818

19+
import java.util.Objects;
20+
1921
import org.aspectj.weaver.tools.PointcutParser;
2022
import org.aspectj.weaver.tools.TypePatternMatcher;
2123

@@ -124,7 +126,7 @@ public boolean equals(@Nullable Object other) {
124126

125127
@Override
126128
public int hashCode() {
127-
return ObjectUtils.nullSafeHashCode(this.typePattern);
129+
return Objects.hashCode(this.typePattern);
128130
}
129131

130132
@Override

spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public boolean equals(@Nullable Object other) {
129129

130130
@Override
131131
public int hashCode() {
132-
return ObjectUtils.nullSafeHashCode(this.filters);
132+
return Arrays.hashCode(this.filters);
133133
}
134134

135135
@Override
@@ -170,7 +170,7 @@ public boolean equals(@Nullable Object other) {
170170

171171
@Override
172172
public int hashCode() {
173-
return ObjectUtils.nullSafeHashCode(this.filters);
173+
return Arrays.hashCode(this.filters);
174174
}
175175

176176
@Override

spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.aop.target;
1818

1919
import java.io.Serializable;
20+
import java.util.Objects;
2021

2122
import org.apache.commons.logging.Log;
2223
import org.apache.commons.logging.LogFactory;
@@ -190,7 +191,7 @@ public boolean equals(@Nullable Object other) {
190191

191192
@Override
192193
public int hashCode() {
193-
return getClass().hashCode() * 13 + ObjectUtils.nullSafeHashCode(this.targetBeanName);
194+
return Objects.hash(getClass(), this.targetBeanName);
194195
}
195196

196197
@Override

spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.aop.target;
1818

1919
import java.io.Serializable;
20+
import java.util.Objects;
2021

2122
import org.springframework.aop.TargetSource;
2223
import org.springframework.lang.Nullable;
@@ -140,7 +141,7 @@ public boolean equals(@Nullable Object other) {
140141

141142
@Override
142143
public int hashCode() {
143-
return EmptyTargetSource.class.hashCode() * 13 + ObjectUtils.nullSafeHashCode(this.targetClass);
144+
return Objects.hash(getClass(), this.targetClass);
144145
}
145146

146147
@Override

spring-aspects/src/test/java/org/springframework/cache/config/TestEntity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cache.config;
1818

19+
import java.util.Objects;
20+
1921
import org.springframework.lang.Nullable;
2022
import org.springframework.util.ObjectUtils;
2123

@@ -42,7 +44,7 @@ public void setId(Long id) {
4244

4345
@Override
4446
public int hashCode() {
45-
return ObjectUtils.nullSafeHashCode(this.id);
47+
return Objects.hashCode(this.id);
4648
}
4749

4850
@Override

spring-beans/src/main/java/org/springframework/beans/BeanMetadataAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public boolean equals(@Nullable Object other) {
9090

9191
@Override
9292
public int hashCode() {
93-
return this.name.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.value);
93+
return ObjectUtils.nullSafeHash(this.name, this.value);
9494
}
9595

9696
@Override

spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.ArrayList;
3131
import java.util.Comparator;
3232
import java.util.List;
33+
import java.util.Objects;
3334
import java.util.Set;
3435
import java.util.TreeSet;
3536

@@ -345,7 +346,7 @@ public boolean equals(@Nullable Object other) {
345346

346347
@Override
347348
public int hashCode() {
348-
return (ObjectUtils.nullSafeHashCode(getReadMethod()) * 29 + ObjectUtils.nullSafeHashCode(getWriteMethod()));
349+
return Objects.hash(getReadMethod(), getWriteMethod());
349350
}
350351

351352
@Override
@@ -500,11 +501,8 @@ public boolean equals(@Nullable Object other) {
500501

501502
@Override
502503
public int hashCode() {
503-
int hashCode = ObjectUtils.nullSafeHashCode(getReadMethod());
504-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getWriteMethod());
505-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getIndexedReadMethod());
506-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getIndexedWriteMethod());
507-
return hashCode;
504+
return Objects.hash(getReadMethod(), getWriteMethod(),
505+
getIndexedReadMethod(), getIndexedWriteMethod());
508506
}
509507

510508
@Override

spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.beans.PropertyDescriptor;
2121
import java.lang.reflect.Method;
2222
import java.util.HashSet;
23+
import java.util.Objects;
2324
import java.util.Set;
2425

2526
import org.apache.commons.logging.LogFactory;
@@ -30,7 +31,6 @@
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.ClassUtils;
33-
import org.springframework.util.ObjectUtils;
3434
import org.springframework.util.StringUtils;
3535

3636
/**
@@ -172,10 +172,7 @@ public boolean equals(@Nullable Object other) {
172172

173173
@Override
174174
public int hashCode() {
175-
int hashCode = getBeanClass().hashCode();
176-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getReadMethod());
177-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getWriteMethod());
178-
return hashCode;
175+
return Objects.hash(getBeanClass(), getReadMethod(), getWriteMethod());
179176
}
180177

181178
}

spring-beans/src/main/java/org/springframework/beans/PropertyValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public boolean equals(@Nullable Object other) {
197197

198198
@Override
199199
public int hashCode() {
200-
return this.name.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.value);
200+
return ObjectUtils.nullSafeHash(this.name, this.value);
201201
}
202202

203203
@Override

0 commit comments

Comments
 (0)