Skip to content

Commit f01fb19

Browse files
committed
Stop using Constants utility in CustomizableTraceInterceptor
See gh-30851
1 parent 5e31856 commit f01fb19

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.aopalliance.intercept.MethodInvocation;
2424
import org.apache.commons.logging.Log;
2525

26-
import org.springframework.core.Constants;
2726
import org.springframework.lang.Nullable;
2827
import org.springframework.util.Assert;
2928
import org.springframework.util.ClassUtils;
@@ -153,8 +152,15 @@ public class CustomizableTraceInterceptor extends AbstractTraceInterceptor {
153152
/**
154153
* The {@code Set} of allowed placeholders.
155154
*/
156-
private static final Set<Object> ALLOWED_PLACEHOLDERS =
157-
new Constants(CustomizableTraceInterceptor.class).getValues("PLACEHOLDER_");
155+
static final Set<String> ALLOWED_PLACEHOLDERS = Set.of(
156+
PLACEHOLDER_METHOD_NAME,
157+
PLACEHOLDER_TARGET_CLASS_NAME,
158+
PLACEHOLDER_TARGET_CLASS_SHORT_NAME,
159+
PLACEHOLDER_RETURN_VALUE,
160+
PLACEHOLDER_ARGUMENT_TYPES,
161+
PLACEHOLDER_ARGUMENTS,
162+
PLACEHOLDER_EXCEPTION,
163+
PLACEHOLDER_INVOCATION_TIME);
158164

159165

160166
/**

spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@
1616

1717
package org.springframework.aop.interceptor;
1818

19+
import java.lang.reflect.Field;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
1923
import org.aopalliance.intercept.MethodInvocation;
2024
import org.apache.commons.logging.Log;
2125
import org.junit.jupiter.api.Test;
2226

27+
import org.springframework.util.ReflectionUtils;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
2330
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2431
import static org.mockito.ArgumentMatchers.anyString;
2532
import static org.mockito.ArgumentMatchers.eq;
2633
import static org.mockito.BDDMockito.given;
2734
import static org.mockito.Mockito.mock;
2835
import static org.mockito.Mockito.times;
2936
import static org.mockito.Mockito.verify;
37+
import static org.springframework.aop.interceptor.CustomizableTraceInterceptor.ALLOWED_PLACEHOLDERS;
3038
import static org.springframework.aop.interceptor.CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS;
3139
import static org.springframework.aop.interceptor.CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENT_TYPES;
3240
import static org.springframework.aop.interceptor.CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION;
@@ -164,6 +172,34 @@ void sunnyDayPathLogsCorrectlyWithPrettyMuchAllPlaceholdersMatching() throws Thr
164172
verify(log, times(2)).trace(anyString());
165173
}
166174

175+
/**
176+
* This test effectively verifies that the internal ALLOWED_PLACEHOLDERS set
177+
* is properly configured in {@link CustomizableTraceInterceptor}.
178+
*/
179+
@Test
180+
@SuppressWarnings("deprecation")
181+
void supportedPlaceholderValues() {
182+
assertThat(ALLOWED_PLACEHOLDERS).containsAll(getPlaceholderConstantValues());
183+
}
184+
185+
private List<String> getPlaceholderConstantValues() {
186+
return Arrays.stream(CustomizableTraceInterceptor.class.getFields())
187+
.filter(ReflectionUtils::isPublicStaticFinal)
188+
.filter(field -> field.getName().startsWith("PLACEHOLDER_"))
189+
.map(this::getFieldValue)
190+
.map(String.class::cast)
191+
.toList();
192+
}
193+
194+
private Object getFieldValue(Field field) {
195+
try {
196+
return field.get(null);
197+
}
198+
catch (Exception ex) {
199+
throw new RuntimeException(ex);
200+
}
201+
}
202+
167203

168204
@SuppressWarnings("serial")
169205
private static class StubCustomizableTraceInterceptor extends CustomizableTraceInterceptor {

0 commit comments

Comments
 (0)