From c24e98e51b9941bd71a1242dac788e52435268ca Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 28 Aug 2022 17:43:45 +0200 Subject: [PATCH] Avoid reflection for instantiating MockServerContainerContextCustomizer Since MockServerContainerContextCustomizerFactory already has a `webSocketPresent` check, there should be no need to instantiate MockServerContainerContextCustomizer via reflection. See gh-29028 Closes gh-29035 --- ...ServerContainerContextCustomizerFactory.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java b/spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java index 0cb74f154c..25462ef97b 100644 --- a/spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java +++ b/spring-test/src/main/java/org/springframework/test/context/web/socket/MockServerContainerContextCustomizerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package org.springframework.test.context.web.socket; import java.util.List; -import org.springframework.beans.BeanUtils; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.lang.Nullable; import org.springframework.test.context.ContextConfigurationAttributes; @@ -39,9 +38,6 @@ class MockServerContainerContextCustomizerFactory implements ContextCustomizerFa private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME = "org.springframework.test.context.web.WebAppConfiguration"; - private static final String MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME = - "org.springframework.test.context.web.socket.MockServerContainerContextCustomizer"; - private static final boolean webSocketPresent = ClassUtils.isPresent("jakarta.websocket.server.ServerContainer", MockServerContainerContextCustomizerFactory.class.getClassLoader()); @@ -52,17 +48,8 @@ class MockServerContainerContextCustomizerFactory implements ContextCustomizerFa List configAttributes) { if (webSocketPresent && isAnnotatedWithWebAppConfiguration(testClass)) { - try { - Class clazz = ClassUtils.forName(MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME, - getClass().getClassLoader()); - return (ContextCustomizer) BeanUtils.instantiateClass(clazz); - } - catch (Throwable ex) { - throw new IllegalStateException("Failed to enable WebSocket test support; could not load class: " + - MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME, ex); - } + return new MockServerContainerContextCustomizer(); } - // Else, nothing to customize return null; }