From 2c5934dab27ecc1fa7e34032b8f0c3d85c5b22af Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Tue, 11 Jun 2024 11:56:31 -0700 Subject: [PATCH 1/4] Use 'switch' instead of 'if' See gh-40985 --- ...eckClasspathForProhibitedDependencies.java | 32 ++++++------------- ...ssPathExtensionForkParameterizedTests.java | 14 +++----- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForProhibitedDependencies.java b/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForProhibitedDependencies.java index 548344b731f..eb16949b5ac 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForProhibitedDependencies.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForProhibitedDependencies.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -70,27 +70,18 @@ public class CheckClasspathForProhibitedDependencies extends DefaultTask { private boolean prohibited(ModuleVersionIdentifier id) { String group = id.getGroup(); - if (group.equals("javax.batch")) { - return false; - } - if (group.equals("javax.cache")) { - return false; - } - if (group.equals("javax.money")) { - return false; - } - if (group.equals("org.codehaus.groovy")) { - return true; - } - if (group.equals("org.eclipse.jetty.toolchain")) { - return true; + switch (group) { + case "javax.batch", "javax.cache", "javax.money" -> { + return false; + } + case "commons-logging", "org.codehaus.groovy", "org.eclipse.jetty.toolchain", + "org.apache.geronimo.specs" -> { + return true; + } } if (group.startsWith("javax")) { return true; } - if (group.equals("commons-logging")) { - return true; - } if (group.equals("org.slf4j") && id.getName().equals("jcl-over-slf4j")) { return true; } @@ -100,10 +91,7 @@ public class CheckClasspathForProhibitedDependencies extends DefaultTask { if (group.equals("org.apache.geronimo.specs")) { return true; } - if (group.equals("com.sun.activation")) { - return true; - } - return false; + return group.equals("com.sun.activation"); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java index 2a1bc4f47e5..c2ddf1b7708 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -37,14 +37,10 @@ class ModifiedClassPathExtensionForkParameterizedTests { @ParameterizedTest @ValueSource(strings = { "one", "two", "three" }) void testIsInvokedOnceForEachArgument(String argument) { - if (argument.equals("one")) { - assertThat(arguments).isEmpty(); - } - else if (argument.equals("two")) { - assertThat(arguments).doesNotContain("two", "three"); - } - else if (argument.equals("three")) { - assertThat(arguments).doesNotContain("three"); + switch (argument) { + case "one" -> assertThat(arguments).isEmpty(); + case "two" -> assertThat(arguments).doesNotContain("two", "three"); + case "three" -> assertThat(arguments).doesNotContain("three"); } arguments.add(argument); } From a18cb37657a112d514d24d5c81a8df1bd1b2fdfa Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 11 Jun 2024 12:18:32 -0700 Subject: [PATCH 2/4] Polish "Use 'switch' instead of 'if'" See gh-40985 --- ...eckClasspathForProhibitedDependencies.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForProhibitedDependencies.java b/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForProhibitedDependencies.java index eb16949b5ac..2a3c36c19f7 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForProhibitedDependencies.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForProhibitedDependencies.java @@ -16,6 +16,7 @@ package org.springframework.boot.build.classpath; +import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; @@ -35,6 +36,11 @@ import org.gradle.api.tasks.TaskAction; */ public class CheckClasspathForProhibitedDependencies extends DefaultTask { + private static final Set PROHIBITED_GROUPS = Set.of("org.codehaus.groovy", "org.eclipse.jetty.toolchain", + "commons-logging", "org.apache.geronimo.specs", "com.sun.activation"); + + private static final Set PERMITTED_JAVAX_GROUPS = Set.of("javax.batch", "javax.cache", "javax.money"); + private Configuration classpath; public CheckClasspathForProhibitedDependencies() { @@ -69,29 +75,20 @@ public class CheckClasspathForProhibitedDependencies extends DefaultTask { } private boolean prohibited(ModuleVersionIdentifier id) { - String group = id.getGroup(); - switch (group) { - case "javax.batch", "javax.cache", "javax.money" -> { - return false; - } - case "commons-logging", "org.codehaus.groovy", "org.eclipse.jetty.toolchain", - "org.apache.geronimo.specs" -> { - return true; - } - } - if (group.startsWith("javax")) { - return true; - } - if (group.equals("org.slf4j") && id.getName().equals("jcl-over-slf4j")) { - return true; - } - if (group.startsWith("org.jboss.spec")) { - return true; - } - if (group.equals("org.apache.geronimo.specs")) { - return true; - } - return group.equals("com.sun.activation"); + return PROHIBITED_GROUPS.contains(id.getGroup()) || prohibitedJavax(id) || prohibitedSlf4j(id) + || prohibitedJbossSpec(id); + } + + private boolean prohibitedSlf4j(ModuleVersionIdentifier id) { + return id.getGroup().equals("org.slf4j") && id.getName().equals("jcl-over-slf4j"); + } + + private boolean prohibitedJbossSpec(ModuleVersionIdentifier id) { + return id.getGroup().startsWith("org.jboss.spec"); + } + + private boolean prohibitedJavax(ModuleVersionIdentifier id) { + return id.getGroup().startsWith("javax.") && !PERMITTED_JAVAX_GROUPS.contains(id.getGroup()); } } From 623c395c0c9471d15f342f31a385bc699d979e6c Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Tue, 11 Jun 2024 11:56:55 -0700 Subject: [PATCH 3/4] Remove redundant cast See gh-40985 --- .../springframework/boot/logging/LoggingSystemProperties.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java index 17b2e1a3aec..0c306f51a31 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -219,7 +219,7 @@ public class LoggingSystemProperties { if (this.environment instanceof ConfigurableEnvironment configurableEnvironment) { PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver( configurableEnvironment.getPropertySources()); - resolver.setConversionService(((ConfigurableEnvironment) this.environment).getConversionService()); + resolver.setConversionService(configurableEnvironment.getConversionService()); resolver.setIgnoreUnresolvableNestedPlaceholders(true); return resolver; } From 4f6509d5ff866028a01de0cc08d39112be99cc5a Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Tue, 11 Jun 2024 11:57:05 -0700 Subject: [PATCH 4/4] Prevent unnecessary unboxing See gh-40985 --- .../boot/web/embedded/tomcat/NestedJarResourceSet.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/NestedJarResourceSet.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/NestedJarResourceSet.java index 0f1be9560a9..dc4e3535aa7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/NestedJarResourceSet.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/NestedJarResourceSet.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -128,7 +128,7 @@ class NestedJarResourceSet extends AbstractSingleArchiveResourceSet { } } } - return this.multiRelease.booleanValue(); + return this.multiRelease; } @Override