Skip to content

Commit

Permalink
Introduce ObjectUtils#nullSafeHash(Object... element)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Sep 13, 2023
1 parent f2e898d commit 01f7173
Show file tree
Hide file tree
Showing 38 changed files with 267 additions and 313 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,8 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
int hashCode = ObjectUtils.nullSafeHashCode(getExpression());
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.pointcutDeclarationScope);
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.pointcutParameterNames);
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.pointcutParameterTypes);
return hashCode;
return ObjectUtils.nullSafeHash(getExpression(), this.pointcutDeclarationScope,
this.pointcutParameterNames, this.pointcutParameterTypes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.aop.aspectj;

import java.util.Objects;

import org.aspectj.weaver.tools.PointcutParser;
import org.aspectj.weaver.tools.TypePatternMatcher;

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

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.typePattern);
return Objects.hashCode(this.typePattern);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.filters);
return Arrays.hashCode(this.filters);
}

@Override
Expand Down Expand Up @@ -170,7 +170,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.filters);
return Arrays.hashCode(this.filters);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.aop.target;

import java.io.Serializable;
import java.util.Objects;

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

@Override
public int hashCode() {
return getClass().hashCode() * 13 + ObjectUtils.nullSafeHashCode(this.targetBeanName);
return Objects.hash(getClass(), this.targetBeanName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.aop.target;

import java.io.Serializable;
import java.util.Objects;

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

@Override
public int hashCode() {
return EmptyTargetSource.class.hashCode() * 13 + ObjectUtils.nullSafeHashCode(this.targetClass);
return Objects.hash(getClass(), this.targetClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.cache.config;

import java.util.Objects;

import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

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

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.id);
return Objects.hashCode(this.id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return this.name.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.value);
return ObjectUtils.nullSafeHash(this.name, this.value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

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

@Override
public int hashCode() {
return (ObjectUtils.nullSafeHashCode(getReadMethod()) * 29 + ObjectUtils.nullSafeHashCode(getWriteMethod()));
return Objects.hash(getReadMethod(), getWriteMethod());
}

@Override
Expand Down Expand Up @@ -500,11 +501,8 @@ public boolean equals(@Nullable Object other) {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -30,7 +31,6 @@
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;

/**
Expand Down Expand Up @@ -172,10 +172,7 @@ public boolean equals(@Nullable Object other) {

@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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return this.name.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.value);
return ObjectUtils.nullSafeHash(this.name, this.value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -190,7 +191,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return (this.field != null ? this.field.hashCode() : ObjectUtils.nullSafeHashCode(this.methodParameter));
return Objects.hash(this.field, this.methodParameter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,8 @@ public boolean equals(@Nullable Object other) {

@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ private boolean contentEquals(ValueHolder other) {
* 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.value) * 29 + ObjectUtils.nullSafeHashCode(this.targetType);
return ObjectUtils.nullSafeHash(this.value, this.targetType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ public boolean equals(@Nullable Object other) {

@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,8 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
int hashCode = ObjectUtils.nullSafeHashCode(this.from);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.replyTo);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.to);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.cc);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.bcc);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.sentDate);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.subject);
return hashCode;
return ObjectUtils.nullSafeHash(this.from, this.replyTo, this.to, this.cc,
this.bcc, this.sentDate, this.subject);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -404,7 +405,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return this.eventType.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.sourceType);
return Objects.hash(this.eventType, this.sourceType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +29,6 @@
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}
Expand Down Expand Up @@ -120,7 +120,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.applicationContext);
return Objects.hashCode(this.applicationContext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,7 @@ public boolean equals(@Nullable Object other) {

@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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,8 @@ public boolean equals(@Nullable Object other) {

@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.context.testfixture.cache.beans;

import java.util.Objects;

import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

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

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.id);
return Objects.hashCode(this.id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.annotation.Repeatable;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;

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

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.parent);
return Objects.hashCode(this.parent);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

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

@Override
public int hashCode() {
return (ObjectUtils.nullSafeHashCode(this.objectType) * 31 + ObjectUtils.nullSafeHashCode(this.name));
return Objects.hash(this.objectType, this.name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.core.env;

import java.util.Objects;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -147,7 +149,7 @@ public boolean equals(@Nullable Object other) {
*/
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(getName());
return Objects.hashCode(getName());
}

/**
Expand Down
Loading

0 comments on commit 01f7173

Please sign in to comment.