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

spring-beans/src/main/java/org/springframework/beans/factory/InjectionPoint.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.AnnotatedElement;
2121
import java.lang.reflect.Field;
2222
import java.lang.reflect.Member;
23+
import java.util.Objects;
2324

2425
import org.springframework.core.MethodParameter;
2526
import org.springframework.lang.Nullable;
@@ -190,7 +191,7 @@ public boolean equals(@Nullable Object other) {
190191

191192
@Override
192193
public int hashCode() {
193-
return (this.field != null ? this.field.hashCode() : ObjectUtils.nullSafeHashCode(this.methodParameter));
194+
return Objects.hash(this.field, this.methodParameter);
194195
}
195196

196197
@Override

spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionHolder.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,8 @@ public boolean equals(@Nullable Object other) {
173173

174174
@Override
175175
public int hashCode() {
176-
int hashCode = this.beanDefinition.hashCode();
177-
hashCode = 29 * hashCode + this.beanName.hashCode();
178-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.aliases);
179-
return hashCode;
176+
return ObjectUtils.nullSafeHash(this.beanDefinition, this.beanName,
177+
this.aliases);
180178
}
181179

182180
}

spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ private boolean contentEquals(ValueHolder other) {
606606
* same content to reside in the same Set.
607607
*/
608608
private int contentHashCode() {
609-
return ObjectUtils.nullSafeHashCode(this.value) * 29 + ObjectUtils.nullSafeHashCode(this.type);
609+
return ObjectUtils.nullSafeHash(this.value, this.type);
610610
}
611611

612612
/**

spring-beans/src/main/java/org/springframework/beans/factory/config/TypedStringValue.java

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

224224
@Override
225225
public int hashCode() {
226-
return ObjectUtils.nullSafeHashCode(this.value) * 29 + ObjectUtils.nullSafeHashCode(this.targetType);
226+
return ObjectUtils.nullSafeHash(this.value, this.targetType);
227227
}
228228

229229
@Override

spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverride.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ public boolean equals(@Nullable Object other) {
113113

114114
@Override
115115
public int hashCode() {
116-
int hashCode = ObjectUtils.nullSafeHashCode(this.methodName);
117-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.source);
118-
return hashCode;
116+
return ObjectUtils.nullSafeHash(this.methodName, this.source);
119117
}
120118

121119
}

spring-context-support/src/main/java/org/springframework/mail/SimpleMailMessage.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,8 @@ public boolean equals(@Nullable Object other) {
236236

237237
@Override
238238
public int hashCode() {
239-
int hashCode = ObjectUtils.nullSafeHashCode(this.from);
240-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.replyTo);
241-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.to);
242-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.cc);
243-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.bcc);
244-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.sentDate);
245-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.subject);
246-
return hashCode;
239+
return ObjectUtils.nullSafeHash(this.from, this.replyTo, this.to, this.cc,
240+
this.bcc, this.sentDate, this.subject);
247241
}
248242

249243
@Override

spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.LinkedHashSet;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.Objects;
2425
import java.util.Set;
2526
import java.util.concurrent.ConcurrentHashMap;
2627
import java.util.function.Predicate;
@@ -404,7 +405,7 @@ public boolean equals(@Nullable Object other) {
404405

405406
@Override
406407
public int hashCode() {
407-
return this.eventType.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.sourceType);
408+
return Objects.hash(this.eventType, this.sourceType);
408409
}
409410

410411
@Override

spring-context/src/main/java/org/springframework/context/support/ApplicationListenerDetector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.context.support;
1818

1919
import java.util.Map;
20+
import java.util.Objects;
2021
import java.util.concurrent.ConcurrentHashMap;
2122

2223
import org.apache.commons.logging.Log;
@@ -28,7 +29,6 @@
2829
import org.springframework.context.ApplicationListener;
2930
import org.springframework.context.event.ApplicationEventMulticaster;
3031
import org.springframework.lang.Nullable;
31-
import org.springframework.util.ObjectUtils;
3232

3333
/**
3434
* {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener}
@@ -120,7 +120,7 @@ public boolean equals(@Nullable Object other) {
120120

121121
@Override
122122
public int hashCode() {
123-
return ObjectUtils.nullSafeHashCode(this.applicationContext);
123+
return Objects.hashCode(this.applicationContext);
124124
}
125125

126126
}

spring-context/src/main/java/org/springframework/context/support/DefaultMessageSourceResolvable.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ public boolean equals(@Nullable Object other) {
179179

180180
@Override
181181
public int hashCode() {
182-
int hashCode = ObjectUtils.nullSafeHashCode(getCodes());
183-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getArguments());
184-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getDefaultMessage());
185-
return hashCode;
182+
return ObjectUtils.nullSafeHash(getCode(), getArguments(), getDefaultMessage());
186183
}
187184

188185
}

spring-context/src/main/java/org/springframework/jmx/support/NotificationListenerHolder.java

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

168168
@Override
169169
public int hashCode() {
170-
int hashCode = ObjectUtils.nullSafeHashCode(this.notificationListener);
171-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.notificationFilter);
172-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.handback);
173-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.mappedObjectNames);
174-
return hashCode;
170+
return ObjectUtils.nullSafeHash(this.notificationListener, this.notificationFilter,
171+
this.handback, this.mappedObjectNames);
175172
}
176173

177174
}

spring-context/src/testFixtures/java/org/springframework/context/testfixture/cache/beans/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.context.testfixture.cache.beans;
1818

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

@@ -38,7 +40,7 @@ public void setId(Long id) {
3840

3941
@Override
4042
public int hashCode() {
41-
return ObjectUtils.nullSafeHashCode(this.id);
43+
return Objects.hashCode(this.id);
4244
}
4345

4446
@Override

spring-core/src/main/java/org/springframework/core/annotation/RepeatableContainers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.annotation.Repeatable;
2121
import java.lang.reflect.Method;
2222
import java.util.Map;
23+
import java.util.Objects;
2324

2425
import org.springframework.lang.Nullable;
2526
import org.springframework.util.Assert;
@@ -91,7 +92,7 @@ public boolean equals(@Nullable Object other) {
9192

9293
@Override
9394
public int hashCode() {
94-
return ObjectUtils.nullSafeHashCode(this.parent);
95+
return Objects.hashCode(this.parent);
9596
}
9697

9798

spring-core/src/main/java/org/springframework/core/convert/Property.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.Method;
2323
import java.util.LinkedHashMap;
2424
import java.util.Map;
25+
import java.util.Objects;
2526

2627
import org.springframework.core.MethodParameter;
2728
import org.springframework.lang.Nullable;
@@ -269,7 +270,7 @@ public boolean equals(@Nullable Object other) {
269270

270271
@Override
271272
public int hashCode() {
272-
return (ObjectUtils.nullSafeHashCode(this.objectType) * 31 + ObjectUtils.nullSafeHashCode(this.name));
273+
return Objects.hash(this.objectType, this.name);
273274
}
274275

275276
}

spring-core/src/main/java/org/springframework/core/env/PropertySource.java

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

1717
package org.springframework.core.env;
1818

19+
import java.util.Objects;
20+
1921
import org.apache.commons.logging.Log;
2022
import org.apache.commons.logging.LogFactory;
2123

@@ -147,7 +149,7 @@ public boolean equals(@Nullable Object other) {
147149
*/
148150
@Override
149151
public int hashCode() {
150-
return ObjectUtils.nullSafeHashCode(getName());
152+
return Objects.hashCode(getName());
151153
}
152154

153155
/**

0 commit comments

Comments
 (0)