Calculate hashCode only once in ConfigurationPropertyName

See gh-21394
This commit is contained in:
dreis2211
2020-05-11 22:13:53 +02:00
committed by Stephane Nicoll
parent df25603bc9
commit c9f6a126b7
2 changed files with 10 additions and 0 deletions

View File

@@ -444,6 +444,7 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
}
hashCode = 31 * hashCode + elementHashCode;
}
this.hashCode = hashCode;
}
return hashCode;
}

View File

@@ -24,6 +24,7 @@ import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.Form;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -619,4 +620,12 @@ class ConfigurationPropertyNameTests {
assertThat(ConfigurationPropertyName.isValid("foo!bar")).isFalse();
}
@Test
void hashCodeIsOnlyCalculatedOnce() {
ConfigurationPropertyName name = ConfigurationPropertyName.of("hash.code");
int hashCode = name.hashCode();
int hashCodeField = (int) ReflectionTestUtils.getField(name, "hashCode");
assertThat(hashCodeField).isEqualTo(hashCode);
}
}