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

@@ -90,7 +90,7 @@ public class BeanMetadataAttribute implements BeanMetadataElement {
@Override
public int hashCode() {
return this.name.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.value);
return ObjectUtils.nullSafeHash(this.name, this.value);
}
@Override

View File

@@ -30,6 +30,7 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
@@ -345,7 +346,7 @@ class ExtendedBeanInfo implements BeanInfo {
@Override
public int hashCode() {
return (ObjectUtils.nullSafeHashCode(getReadMethod()) * 29 + ObjectUtils.nullSafeHashCode(getWriteMethod()));
return Objects.hash(getReadMethod(), getWriteMethod());
}
@Override
@@ -500,11 +501,8 @@ class ExtendedBeanInfo implements BeanInfo {
@Override
public int hashCode() {
int hashCode = ObjectUtils.nullSafeHashCode(getReadMethod());
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getWriteMethod());
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getIndexedReadMethod());
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getIndexedWriteMethod());
return hashCode;
return Objects.hash(getReadMethod(), getWriteMethod(),
getIndexedReadMethod(), getIndexedWriteMethod());
}
@Override

View File

@@ -20,6 +20,7 @@ import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.logging.LogFactory;
@@ -30,7 +31,6 @@ import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
@@ -172,10 +172,7 @@ final class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor {
@Override
public int hashCode() {
int hashCode = getBeanClass().hashCode();
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getReadMethod());
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getWriteMethod());
return hashCode;
return Objects.hash(getBeanClass(), getReadMethod(), getWriteMethod());
}
}

View File

@@ -197,7 +197,7 @@ public class PropertyValue extends BeanMetadataAttributeAccessor implements Seri
@Override
public int hashCode() {
return this.name.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.value);
return ObjectUtils.nullSafeHash(this.name, this.value);
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -20,6 +20,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.util.Objects;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
@@ -190,7 +191,7 @@ public class InjectionPoint {
@Override
public int hashCode() {
return (this.field != null ? this.field.hashCode() : ObjectUtils.nullSafeHashCode(this.methodParameter));
return Objects.hash(this.field, this.methodParameter);
}
@Override

View File

@@ -173,10 +173,8 @@ public class BeanDefinitionHolder implements BeanMetadataElement {
@Override
public int hashCode() {
int hashCode = this.beanDefinition.hashCode();
hashCode = 29 * hashCode + this.beanName.hashCode();
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.aliases);
return hashCode;
return ObjectUtils.nullSafeHash(this.beanDefinition, this.beanName,
this.aliases);
}
}

View File

@@ -606,7 +606,7 @@ public class ConstructorArgumentValues {
* same content to reside in the same Set.
*/
private int contentHashCode() {
return ObjectUtils.nullSafeHashCode(this.value) * 29 + ObjectUtils.nullSafeHashCode(this.type);
return ObjectUtils.nullSafeHash(this.value, this.type);
}
/**

View File

@@ -223,7 +223,7 @@ public class TypedStringValue implements BeanMetadataElement {
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.value) * 29 + ObjectUtils.nullSafeHashCode(this.targetType);
return ObjectUtils.nullSafeHash(this.value, this.targetType);
}
@Override

View File

@@ -113,9 +113,7 @@ public abstract class MethodOverride implements BeanMetadataElement {
@Override
public int hashCode() {
int hashCode = ObjectUtils.nullSafeHashCode(this.methodName);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.source);
return hashCode;
return ObjectUtils.nullSafeHash(this.methodName, this.source);
}
}