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
This commit is contained in:
Stephane Nicoll
2023-08-25 15:20:24 +02:00
parent f2e898d35d
commit 01f717375b
38 changed files with 276 additions and 322 deletions

View File

@@ -21,6 +21,7 @@ import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
@@ -404,7 +405,7 @@ public abstract class AbstractApplicationEventMulticaster
@Override
public int hashCode() {
return this.eventType.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.sourceType);
return Objects.hash(this.eventType, this.sourceType);
}
@Override

View File

@@ -17,6 +17,7 @@
package org.springframework.context.support;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
@@ -28,7 +29,6 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
/**
* {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener}
@@ -120,7 +120,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.applicationContext);
return Objects.hashCode(this.applicationContext);
}
}

View File

@@ -179,10 +179,7 @@ public class DefaultMessageSourceResolvable implements MessageSourceResolvable,
@Override
public int hashCode() {
int hashCode = ObjectUtils.nullSafeHashCode(getCodes());
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getArguments());
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getDefaultMessage());
return hashCode;
return ObjectUtils.nullSafeHash(getCode(), getArguments(), getDefaultMessage());
}
}

View File

@@ -167,11 +167,8 @@ public class NotificationListenerHolder {
@Override
public int hashCode() {
int hashCode = ObjectUtils.nullSafeHashCode(this.notificationListener);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.notificationFilter);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.handback);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.mappedObjectNames);
return hashCode;
return ObjectUtils.nullSafeHash(this.notificationListener, this.notificationFilter,
this.handback, this.mappedObjectNames);
}
}