From a4ae6600efd2b3f437c1c4f34175f7c23a4460a2 Mon Sep 17 00:00:00 2001 From: BenchmarkingBuffalo <46448799+BenchmarkingBuffalo@users.noreply.github.com> Date: Thu, 11 Jan 2024 18:14:11 +0100 Subject: [PATCH 1/2] Remove unreachable throw code Improve `SpringApplication` by removing the unreachable throw statement in favor of returning an exception from `handleRunFailure`. This commit also removes the if statements in favor of dedicated catch blocks. See gh-39107 --- .../boot/SpringApplication.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index ed21921e80f..d760c90a35f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -95,7 +95,6 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; -import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import org.springframework.util.function.ThrowingConsumer; import org.springframework.util.function.ThrowingSupplier; @@ -172,6 +171,7 @@ import org.springframework.util.function.ThrowingSupplier; * @author Ethan Rubinson * @author Chris Bono * @author Tadaya Tsuyukubo + * @author Lasse Wulff * @since 1.0.0 * @see #run(Class, String[]) * @see #run(Class[], String[]) @@ -330,12 +330,11 @@ public class SpringApplication { listeners.started(context, timeTakenToStartup); callRunners(context, applicationArguments); } + catch (AbandonedRunException ex) { + throw ex; + } catch (Throwable ex) { - if (ex instanceof AbandonedRunException) { - throw ex; - } - handleRunFailure(context, ex, listeners); - throw new IllegalStateException(ex); + throw handleRunFailure(context, ex, listeners); } try { if (context.isRunning()) { @@ -343,12 +342,11 @@ public class SpringApplication { listeners.ready(context, timeTakenToReady); } } + catch (AbandonedRunException ex) { + throw ex; + } catch (Throwable ex) { - if (ex instanceof AbandonedRunException) { - throw ex; - } - handleRunFailure(context, ex, null); - throw new IllegalStateException(ex); + throw handleRunFailure(context, ex, null); } return context; } @@ -790,7 +788,7 @@ public class SpringApplication { .accept((R) runner); } - private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception, + private RuntimeException handleRunFailure(ConfigurableApplicationContext context, Throwable exception, SpringApplicationRunListeners listeners) { try { try { @@ -810,7 +808,10 @@ public class SpringApplication { catch (Exception ex) { logger.warn("Unable to close ApplicationContext", ex); } - ReflectionUtils.rethrowRuntimeException(exception); + if (exception instanceof RuntimeException runtimeException) { + return runtimeException; + } + return new IllegalStateException(exception); } private Collection getExceptionReporters(ConfigurableApplicationContext context) { From e23e431f1026ef056549d1b14563a0f8e2228afc Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 16 Jan 2024 11:35:13 -0800 Subject: [PATCH 2/2] Polish 'Remove unreachable throw code' See gh-39107 --- .../springframework/boot/SpringApplication.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index d760c90a35f..0fc3b65d862 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -330,9 +330,6 @@ public class SpringApplication { listeners.started(context, timeTakenToStartup); callRunners(context, applicationArguments); } - catch (AbandonedRunException ex) { - throw ex; - } catch (Throwable ex) { throw handleRunFailure(context, ex, listeners); } @@ -342,9 +339,6 @@ public class SpringApplication { listeners.ready(context, timeTakenToReady); } } - catch (AbandonedRunException ex) { - throw ex; - } catch (Throwable ex) { throw handleRunFailure(context, ex, null); } @@ -790,6 +784,9 @@ public class SpringApplication { private RuntimeException handleRunFailure(ConfigurableApplicationContext context, Throwable exception, SpringApplicationRunListeners listeners) { + if (exception instanceof AbandonedRunException abandonedRunException) { + return abandonedRunException; + } try { try { handleExitCode(context, exception); @@ -808,10 +805,8 @@ public class SpringApplication { catch (Exception ex) { logger.warn("Unable to close ApplicationContext", ex); } - if (exception instanceof RuntimeException runtimeException) { - return runtimeException; - } - return new IllegalStateException(exception); + return (exception instanceof RuntimeException runtimeException) ? runtimeException + : new IllegalStateException(exception); } private Collection getExceptionReporters(ConfigurableApplicationContext context) {