From f01fb193183b35302e366b625fcc08077e134460 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 16 Jul 2023 12:13:33 +0200 Subject: [PATCH] Stop using Constants utility in CustomizableTraceInterceptor See gh-30851 --- .../CustomizableTraceInterceptor.java | 12 +++++-- .../CustomizableTraceInterceptorTests.java | 36 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java b/spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java index bc7b48f4c9f..46c879ff1d5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java +++ b/spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java @@ -23,7 +23,6 @@ import java.util.regex.Pattern; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; -import org.springframework.core.Constants; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -153,8 +152,15 @@ public class CustomizableTraceInterceptor extends AbstractTraceInterceptor { /** * The {@code Set} of allowed placeholders. */ - private static final Set ALLOWED_PLACEHOLDERS = - new Constants(CustomizableTraceInterceptor.class).getValues("PLACEHOLDER_"); + static final Set ALLOWED_PLACEHOLDERS = Set.of( + PLACEHOLDER_METHOD_NAME, + PLACEHOLDER_TARGET_CLASS_NAME, + PLACEHOLDER_TARGET_CLASS_SHORT_NAME, + PLACEHOLDER_RETURN_VALUE, + PLACEHOLDER_ARGUMENT_TYPES, + PLACEHOLDER_ARGUMENTS, + PLACEHOLDER_EXCEPTION, + PLACEHOLDER_INVOCATION_TIME); /** diff --git a/spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java b/spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java index e989a0753d9..a95d98d9db4 100644 --- a/spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java @@ -16,10 +16,17 @@ package org.springframework.aop.interceptor; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.List; + import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.junit.jupiter.api.Test; +import org.springframework.util.ReflectionUtils; + +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -27,6 +34,7 @@ import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.springframework.aop.interceptor.CustomizableTraceInterceptor.ALLOWED_PLACEHOLDERS; import static org.springframework.aop.interceptor.CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS; import static org.springframework.aop.interceptor.CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENT_TYPES; import static org.springframework.aop.interceptor.CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION; @@ -164,6 +172,34 @@ class CustomizableTraceInterceptorTests { verify(log, times(2)).trace(anyString()); } + /** + * This test effectively verifies that the internal ALLOWED_PLACEHOLDERS set + * is properly configured in {@link CustomizableTraceInterceptor}. + */ + @Test + @SuppressWarnings("deprecation") + void supportedPlaceholderValues() { + assertThat(ALLOWED_PLACEHOLDERS).containsAll(getPlaceholderConstantValues()); + } + + private List getPlaceholderConstantValues() { + return Arrays.stream(CustomizableTraceInterceptor.class.getFields()) + .filter(ReflectionUtils::isPublicStaticFinal) + .filter(field -> field.getName().startsWith("PLACEHOLDER_")) + .map(this::getFieldValue) + .map(String.class::cast) + .toList(); + } + + private Object getFieldValue(Field field) { + try { + return field.get(null); + } + catch (Exception ex) { + throw new RuntimeException(ex); + } + } + @SuppressWarnings("serial") private static class StubCustomizableTraceInterceptor extends CustomizableTraceInterceptor {