From e2ecc61ca87664fdb933e0f7b2e2a3891cd3912e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 15 Sep 2025 13:57:55 -0700 Subject: [PATCH] Refactor CacheCondition to remove multiple casts Closes gh-47208 --- .../boot/autoconfigure/cache/CacheCondition.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheCondition.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheCondition.java index 26bc3bc0cba..9beef3315da 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheCondition.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheCondition.java @@ -26,7 +26,7 @@ import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotationMetadata; -import org.springframework.core.type.ClassMetadata; +import org.springframework.util.Assert; /** * General cache condition used with all cache configuration classes. @@ -39,10 +39,12 @@ class CacheCondition extends SpringBootCondition { @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { - String sourceClass = ""; - if (metadata instanceof ClassMetadata classMetadata) { - sourceClass = classMetadata.getClassName(); - } + Assert.isInstanceOf(AnnotationMetadata.class, metadata); + return extracted(context, (AnnotationMetadata) metadata); + } + + private ConditionOutcome extracted(ConditionContext context, AnnotationMetadata metadata) { + String sourceClass = metadata.getClassName(); ConditionMessage.Builder message = ConditionMessage.forCondition("Cache", sourceClass); Environment environment = context.getEnvironment(); try { @@ -50,7 +52,7 @@ class CacheCondition extends SpringBootCondition { if (!specified.isBound()) { return ConditionOutcome.match(message.because("automatic cache type")); } - CacheType required = CacheConfigurations.getType(((AnnotationMetadata) metadata).getClassName()); + CacheType required = CacheConfigurations.getType(metadata.getClassName()); if (specified.get() == required) { return ConditionOutcome.match(message.because(specified.get() + " cache type")); }