Improve null-safety of loader/spring-boot-jarmode-tools

See gh-46926
This commit is contained in:
Moritz Halbritter 2025-08-18 14:36:10 +02:00
parent 43f5eb5f79
commit f5be18587c
6 changed files with 32 additions and 26 deletions

View File

@ -100,7 +100,7 @@ abstract class Command {
*/
final void run(PrintStream out, Deque<String> args) {
List<String> parameters = new ArrayList<>();
Map<Option, String> options = new HashMap<>();
Map<Option, @Nullable String> options = new HashMap<>();
while (!args.isEmpty()) {
String arg = args.removeFirst();
Option option = this.options.find(arg);
@ -120,7 +120,7 @@ abstract class Command {
* @param options any options extracted from the arguments
* @param parameters any parameters extracted from the arguments
*/
abstract void run(PrintStream out, Map<Option, String> options, List<String> parameters);
abstract void run(PrintStream out, Map<Option, @Nullable String> options, List<String> parameters);
/**
* Whether the command is deprecated.

View File

@ -101,7 +101,7 @@ class ExtractCommand extends Command {
}
@Override
void run(PrintStream out, Map<Option, String> options, List<String> parameters) {
void run(PrintStream out, Map<Option, @Nullable String> options, List<String> parameters) {
try {
checkJarCompatibility();
File destination = getDestination(options);
@ -122,7 +122,7 @@ class ExtractCommand extends Command {
}
}
private static void checkDirectoryIsEmpty(Map<Option, String> options, File destination) {
private static void checkDirectoryIsEmpty(Map<Option, @Nullable String> options, File destination) {
if (options.containsKey(FORCE_OPTION)) {
return;
}
@ -150,8 +150,8 @@ class ExtractCommand extends Command {
}
}
private void extractLibraries(FileResolver fileResolver, JarStructure jarStructure, Map<Option, String> options)
throws IOException {
private void extractLibraries(FileResolver fileResolver, JarStructure jarStructure,
Map<Option, @Nullable String> options) throws IOException {
String librariesDirectory = getLibrariesDirectory(options);
extractArchive(fileResolver, (jarEntry) -> {
Entry entry = jarStructure.resolve(jarEntry);
@ -162,18 +162,18 @@ class ExtractCommand extends Command {
});
}
private static String getLibrariesDirectory(Map<Option, String> options) {
if (options.containsKey(LIBRARIES_DIRECTORY_OPTION)) {
String value = options.get(LIBRARIES_DIRECTORY_OPTION);
if (value.endsWith("/")) {
return value;
private static String getLibrariesDirectory(Map<Option, @Nullable String> options) {
String libraryDirectory = options.get(LIBRARIES_DIRECTORY_OPTION);
if (libraryDirectory != null) {
if (libraryDirectory.endsWith("/")) {
return libraryDirectory;
}
return value + "/";
return libraryDirectory + "/";
}
return "lib/";
}
private FileResolver getFileResolver(File destination, Map<Option, String> options) {
private FileResolver getFileResolver(File destination, Map<Option, @Nullable String> options) {
String applicationFilename = getApplicationFilename(options);
if (!options.containsKey(LAYERS_OPTION)) {
return new NoLayersFileResolver(destination, applicationFilename);
@ -183,9 +183,10 @@ class ExtractCommand extends Command {
return new LayersFileResolver(destination, layers, layersToExtract, applicationFilename);
}
private File getDestination(Map<Option, String> options) {
if (options.containsKey(DESTINATION_OPTION)) {
File destination = new File(options.get(DESTINATION_OPTION));
private File getDestination(Map<Option, @Nullable String> options) {
String value = options.get(DESTINATION_OPTION);
if (value != null) {
File destination = new File(value);
if (destination.isAbsolute()) {
return destination;
}
@ -232,8 +233,8 @@ class ExtractCommand extends Command {
return (this.layers != null) ? this.layers : Layers.get(this.context);
}
private void createApplication(JarStructure jarStructure, FileResolver fileResolver, Map<Option, String> options)
throws IOException {
private void createApplication(JarStructure jarStructure, FileResolver fileResolver,
Map<Option, @Nullable String> options) throws IOException {
File file = fileResolver.resolveApplication();
if (file == null) {
return;
@ -264,9 +265,10 @@ class ExtractCommand extends Command {
}
}
private String getApplicationFilename(Map<Option, String> options) {
if (options.containsKey(APPLICATION_FILENAME_OPTION)) {
return options.get(APPLICATION_FILENAME_OPTION);
private String getApplicationFilename(Map<Option, @Nullable String> options) {
String value = options.get(APPLICATION_FILENAME_OPTION);
if (value != null) {
return value;
}
return this.context.getArchiveFile().getName();
}

View File

@ -58,8 +58,8 @@ class ExtractLayersCommand extends Command {
}
@Override
void run(PrintStream out, Map<Option, String> options, List<String> parameters) {
Map<Option, String> rewrittenOptions = new HashMap<>();
void run(PrintStream out, Map<Option, @Nullable String> options, List<String> parameters) {
Map<Option, @Nullable String> rewrittenOptions = new HashMap<>();
rewrittenOptions.put(ExtractCommand.DESTINATION_OPTION, options.getOrDefault(DESTINATION_OPTION, "."));
rewrittenOptions.put(ExtractCommand.LAYERS_OPTION, StringUtils.collectionToCommaDelimitedString(parameters));
rewrittenOptions.put(ExtractCommand.LAUNCHER_OPTION, null);

View File

@ -49,7 +49,7 @@ class HelpCommand extends Command {
}
@Override
void run(PrintStream out, Map<Option, String> options, List<String> parameters) {
void run(PrintStream out, Map<Option, @Nullable String> options, List<String> parameters) {
run(out, parameters);
}

View File

@ -20,6 +20,8 @@ import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import org.jspecify.annotations.Nullable;
/**
* The {@code 'list'} tools command.
*
@ -48,7 +50,7 @@ class ListCommand extends Command {
}
@Override
void run(PrintStream out, Map<Option, String> options, List<String> parameters) {
void run(PrintStream out, Map<Option, @Nullable String> options, List<String> parameters) {
this.delegate.run(out, options, parameters);
}

View File

@ -20,6 +20,8 @@ import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import org.jspecify.annotations.Nullable;
/**
* The {@code 'list-layers'} tools command.
*
@ -35,7 +37,7 @@ class ListLayersCommand extends Command {
}
@Override
void run(PrintStream out, Map<Option, String> options, List<String> parameters) {
void run(PrintStream out, Map<Option, @Nullable String> options, List<String> parameters) {
Layers layers = Layers.get(this.context);
printLayers(out, layers);
}