From e35028619660d5756101b40da3aa71cf0151995f Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 16 Oct 2025 12:23:02 +0200 Subject: [PATCH] Improve null-safety of loader/spring-boot-loader-tools See gh-47263 --- .../boot/loader/tools/DefaultLaunchScript.java | 2 +- .../loader/tools/DefaultLibraryCoordinates.java | 16 +++++++++------- .../boot/loader/tools/LibraryCoordinates.java | 8 ++++---- .../boot/loader/tools/MainClassFinder.java | 11 ++++++----- .../layer/IncludeExcludeContentSelector.java | 4 ++-- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java index 79c4198d556..340d367af07 100644 --- a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java +++ b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java @@ -58,7 +58,7 @@ public class DefaultLaunchScript implements LaunchScript { * @param properties an optional set of script properties used for variable expansion * @throws IOException if the script cannot be loaded */ - public DefaultLaunchScript(@Nullable File file, Map properties) throws IOException { + public DefaultLaunchScript(@Nullable File file, @Nullable Map properties) throws IOException { String content = loadContent(file); this.content = expandPlaceholders(content, properties); } diff --git a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLibraryCoordinates.java b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLibraryCoordinates.java index d13191a7578..ee662c26839 100644 --- a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLibraryCoordinates.java +++ b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLibraryCoordinates.java @@ -16,6 +16,8 @@ package org.springframework.boot.loader.tools; +import org.jspecify.annotations.Nullable; + /** * Encapsulates information about the artifact coordinates of a library. * @@ -23,11 +25,11 @@ package org.springframework.boot.loader.tools; */ class DefaultLibraryCoordinates implements LibraryCoordinates { - private final String groupId; + private final @Nullable String groupId; - private final String artifactId; + private final @Nullable String artifactId; - private final String version; + private final @Nullable String version; /** * Create a new instance from discrete elements. @@ -35,7 +37,7 @@ class DefaultLibraryCoordinates implements LibraryCoordinates { * @param artifactId the artifact ID * @param version the version */ - DefaultLibraryCoordinates(String groupId, String artifactId, String version) { + DefaultLibraryCoordinates(@Nullable String groupId, @Nullable String artifactId, @Nullable String version) { this.groupId = groupId; this.artifactId = artifactId; this.version = version; @@ -46,7 +48,7 @@ class DefaultLibraryCoordinates implements LibraryCoordinates { * @return the group ID */ @Override - public String getGroupId() { + public @Nullable String getGroupId() { return this.groupId; } @@ -55,7 +57,7 @@ class DefaultLibraryCoordinates implements LibraryCoordinates { * @return the artifact ID */ @Override - public String getArtifactId() { + public @Nullable String getArtifactId() { return this.artifactId; } @@ -64,7 +66,7 @@ class DefaultLibraryCoordinates implements LibraryCoordinates { * @return the version */ @Override - public String getVersion() { + public @Nullable String getVersion() { return this.version; } diff --git a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCoordinates.java b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCoordinates.java index 67cbd854d09..3112352b1a3 100644 --- a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCoordinates.java +++ b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCoordinates.java @@ -31,19 +31,19 @@ public interface LibraryCoordinates { * Return the group ID of the coordinates. * @return the group ID */ - String getGroupId(); + @Nullable String getGroupId(); /** * Return the artifact ID of the coordinates. * @return the artifact ID */ - String getArtifactId(); + @Nullable String getArtifactId(); /** * Return the version of the coordinates. * @return the version */ - String getVersion(); + @Nullable String getVersion(); /** * Factory method to create {@link LibraryCoordinates} with the specified values. @@ -52,7 +52,7 @@ public interface LibraryCoordinates { * @param version the version * @return a new {@link LibraryCoordinates} instance */ - static LibraryCoordinates of(String groupId, String artifactId, String version) { + static LibraryCoordinates of(@Nullable String groupId, @Nullable String artifactId, @Nullable String version) { return new DefaultLibraryCoordinates(groupId, artifactId, version); } diff --git a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/MainClassFinder.java b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/MainClassFinder.java index 681bd313b95..32b1e873481 100644 --- a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/MainClassFinder.java +++ b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/MainClassFinder.java @@ -173,7 +173,7 @@ public abstract class MainClassFinder { * @return the main class or {@code null} * @throws IOException if the jar file cannot be read */ - public static @Nullable String findMainClass(JarFile jarFile, String classesLocation) throws IOException { + public static @Nullable String findMainClass(JarFile jarFile, @Nullable String classesLocation) throws IOException { return doWithMainClasses(jarFile, classesLocation, MainClass::getName); } @@ -184,7 +184,8 @@ public abstract class MainClassFinder { * @return the main class or {@code null} * @throws IOException if the jar file cannot be read */ - public static @Nullable String findSingleMainClass(JarFile jarFile, String classesLocation) throws IOException { + public static @Nullable String findSingleMainClass(JarFile jarFile, @Nullable String classesLocation) + throws IOException { return findSingleMainClass(jarFile, classesLocation, null); } @@ -199,7 +200,7 @@ public abstract class MainClassFinder { * @return the main class or {@code null} * @throws IOException if the jar file cannot be read */ - public static @Nullable String findSingleMainClass(JarFile jarFile, String classesLocation, + public static @Nullable String findSingleMainClass(JarFile jarFile, @Nullable String classesLocation, @Nullable String annotationName) throws IOException { SingleMainClassCallback callback = new SingleMainClassCallback(annotationName); MainClassFinder.doWithMainClasses(jarFile, classesLocation, callback); @@ -215,8 +216,8 @@ public abstract class MainClassFinder { * @return the first callback result or {@code null} * @throws IOException in case of I/O errors */ - static @Nullable T doWithMainClasses(JarFile jarFile, String classesLocation, MainClassCallback callback) - throws IOException { + static @Nullable T doWithMainClasses(JarFile jarFile, @Nullable String classesLocation, + MainClassCallback callback) throws IOException { List classEntries = getClassEntries(jarFile, classesLocation); classEntries.sort(new ClassEntryComparator()); for (JarEntry entry : classEntries) { diff --git a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/layer/IncludeExcludeContentSelector.java b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/layer/IncludeExcludeContentSelector.java index 52c0a86389c..6763bf5a976 100644 --- a/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/layer/IncludeExcludeContentSelector.java +++ b/loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/layer/IncludeExcludeContentSelector.java @@ -42,8 +42,8 @@ public class IncludeExcludeContentSelector implements ContentSelector { private final List> excludes; - public IncludeExcludeContentSelector(Layer layer, List> includes, - List> excludes) { + public IncludeExcludeContentSelector(Layer layer, @Nullable List> includes, + @Nullable List> excludes) { this(layer, includes, excludes, Function.identity()); }