Skip to content

Commit d5e5dcb

Browse files
committed
Consider non-initialized holders as equal to empty holders
Closes gh-26433
1 parent defc246 commit d5e5dcb

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -1194,8 +1194,8 @@ public boolean equals(@Nullable Object other) {
11941194
this.primary == that.primary &&
11951195
this.nonPublicAccessAllowed == that.nonPublicAccessAllowed &&
11961196
this.lenientConstructorResolution == that.lenientConstructorResolution &&
1197-
ObjectUtils.nullSafeEquals(this.constructorArgumentValues, that.constructorArgumentValues) &&
1198-
ObjectUtils.nullSafeEquals(this.propertyValues, that.propertyValues) &&
1197+
equalsConstructorArgumentValues(that) &&
1198+
equalsPropertyValues(that) &&
11991199
ObjectUtils.nullSafeEquals(this.methodOverrides, that.methodOverrides) &&
12001200
ObjectUtils.nullSafeEquals(this.factoryBeanName, that.factoryBeanName) &&
12011201
ObjectUtils.nullSafeEquals(this.factoryMethodName, that.factoryMethodName) &&
@@ -1208,12 +1208,30 @@ public boolean equals(@Nullable Object other) {
12081208
super.equals(other));
12091209
}
12101210

1211+
private boolean equalsConstructorArgumentValues(AbstractBeanDefinition other) {
1212+
if (!hasConstructorArgumentValues()) {
1213+
return !other.hasConstructorArgumentValues();
1214+
}
1215+
return ObjectUtils.nullSafeEquals(this.constructorArgumentValues, other.constructorArgumentValues);
1216+
}
1217+
1218+
private boolean equalsPropertyValues(AbstractBeanDefinition other) {
1219+
if (!hasPropertyValues()) {
1220+
return !other.hasPropertyValues();
1221+
}
1222+
return ObjectUtils.nullSafeEquals(this.propertyValues, other.propertyValues);
1223+
}
1224+
12111225
@Override
12121226
public int hashCode() {
12131227
int hashCode = ObjectUtils.nullSafeHashCode(getBeanClassName());
12141228
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.scope);
1215-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.constructorArgumentValues);
1216-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.propertyValues);
1229+
if (hasConstructorArgumentValues()) {
1230+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.constructorArgumentValues);
1231+
}
1232+
if (hasPropertyValues()) {
1233+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.propertyValues);
1234+
}
12171235
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryBeanName);
12181236
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryMethodName);
12191237
hashCode = 29 * hashCode + super.hashCode();

spring-beans/src/test/java/org/springframework/beans/factory/support/BeanDefinitionTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -133,6 +133,16 @@ public void genericBeanDefinitionEquality() {
133133
assertThat(bd.equals(otherBd)).isTrue();
134134
assertThat(otherBd.equals(bd)).isTrue();
135135
assertThat(bd.hashCode() == otherBd.hashCode()).isTrue();
136+
137+
bd.getPropertyValues();
138+
assertThat(bd.equals(otherBd)).isTrue();
139+
assertThat(otherBd.equals(bd)).isTrue();
140+
assertThat(bd.hashCode() == otherBd.hashCode()).isTrue();
141+
142+
bd.getConstructorArgumentValues();
143+
assertThat(bd.equals(otherBd)).isTrue();
144+
assertThat(otherBd.equals(bd)).isTrue();
145+
assertThat(bd.hashCode() == otherBd.hashCode()).isTrue();
136146
}
137147

138148
@Test

0 commit comments

Comments
 (0)