Skip to content

Commit c9f6a12

Browse files
dreis2211snicoll
authored andcommitted
Calculate hashCode only once in ConfigurationPropertyName
See gh-21394
1 parent df25603 commit c9f6a12

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ public int hashCode() {
444444
}
445445
hashCode = 31 * hashCode + elementHashCode;
446446
}
447+
this.hashCode = hashCode;
447448
}
448449
return hashCode;
449450
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.Form;
27+
import org.springframework.test.util.ReflectionTestUtils;
2728

2829
import static org.assertj.core.api.Assertions.assertThat;
2930
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -619,4 +620,12 @@ void isValidWhenNotValidShouldReturnFalse() {
619620
assertThat(ConfigurationPropertyName.isValid("foo!bar")).isFalse();
620621
}
621622

623+
@Test
624+
void hashCodeIsOnlyCalculatedOnce() {
625+
ConfigurationPropertyName name = ConfigurationPropertyName.of("hash.code");
626+
int hashCode = name.hashCode();
627+
int hashCodeField = (int) ReflectionTestUtils.getField(name, "hashCode");
628+
assertThat(hashCodeField).isEqualTo(hashCode);
629+
}
630+
622631
}

0 commit comments

Comments
 (0)