Skip to content

Commit cb2a26c

Browse files
committed
Add nullability annotations to core/spring-boot-docker-compose
See gh-46587
1 parent b6e4533 commit cb2a26c

27 files changed

+151
-86
lines changed

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/ConnectionPorts.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.List;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
/**
2224
* Provides access to the ports that can be used to connect to a {@link RunningService}.
2325
*
@@ -52,6 +54,6 @@ public interface ConnectionPorts {
5254
* all host ports
5355
* @return a list of all host ports using the given protocol
5456
*/
55-
List<Integer> getAll(String protocol);
57+
List<Integer> getAll(@Nullable String protocol);
5658

5759
}

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DefaultConnectionPorts.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25+
import org.jspecify.annotations.Nullable;
26+
2527
import org.springframework.boot.docker.compose.core.DockerCliInspectResponse.Config;
2628
import org.springframework.boot.docker.compose.core.DockerCliInspectResponse.HostConfig;
2729
import org.springframework.boot.docker.compose.core.DockerCliInspectResponse.HostPort;
@@ -57,7 +59,7 @@ private static boolean isHostNetworkMode(DockerCliInspectResponse inspectRespons
5759
return (config != null) && "host".equals(config.networkMode());
5860
}
5961

60-
private Map<ContainerPort, Integer> buildMappingsForNetworkSettings(NetworkSettings networkSettings) {
62+
private Map<ContainerPort, Integer> buildMappingsForNetworkSettings(@Nullable NetworkSettings networkSettings) {
6163
if (networkSettings == null || CollectionUtils.isEmpty(networkSettings.ports())) {
6264
return Collections.emptyMap();
6365
}
@@ -73,7 +75,7 @@ private Map<ContainerPort, Integer> buildMappingsForNetworkSettings(NetworkSetti
7375
return Collections.unmodifiableMap(mappings);
7476
}
7577

76-
private boolean isIpV4(HostPort hostPort) {
78+
private boolean isIpV4(@Nullable HostPort hostPort) {
7779
String ip = (hostPort != null) ? hostPort.hostIp() : null;
7880
return !StringUtils.hasLength(ip) || ip.contains(".");
7981
}
@@ -108,7 +110,7 @@ public List<Integer> getAll() {
108110
}
109111

110112
@Override
111-
public List<Integer> getAll(String protocol) {
113+
public List<Integer> getAll(@Nullable String protocol) {
112114
List<Integer> hostPorts = new ArrayList<>();
113115
this.mappings.forEach((containerPort, hostPort) -> {
114116
if (protocol == null || protocol.equalsIgnoreCase(containerPort.protocol())) {

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DefaultDockerCompose.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.function.Function;
2626
import java.util.stream.Collectors;
2727

28+
import org.jspecify.annotations.Nullable;
29+
2830
import org.springframework.boot.logging.LogLevel;
2931
import org.springframework.util.Assert;
3032

@@ -41,7 +43,7 @@ class DefaultDockerCompose implements DockerCompose {
4143

4244
private final DockerHost hostname;
4345

44-
DefaultDockerCompose(DockerCli cli, String host) {
46+
DefaultDockerCompose(DockerCli cli, @Nullable String host) {
4547
this.cli = cli;
4648
this.hostname = DockerHost.get(host, () -> cli.run(new DockerCliCommand.Context()));
4749
}
@@ -114,7 +116,8 @@ private Map<String, DockerCliInspectResponse> inspect(List<DockerCliComposePsRes
114116
return inspectResponses.stream().collect(Collectors.toMap(DockerCliInspectResponse::id, Function.identity()));
115117
}
116118

117-
private DockerCliInspectResponse inspectContainer(String id, Map<String, DockerCliInspectResponse> inspected) {
119+
private @Nullable DockerCliInspectResponse inspectContainer(String id,
120+
Map<String, DockerCliInspectResponse> inspected) {
118121
DockerCliInspectResponse inspect = inspected.get(id);
119122
if (inspect != null) {
120123
return inspect;

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DefaultRunningService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.boot.origin.Origin;
2325
import org.springframework.boot.origin.OriginProvider;
2426

@@ -45,10 +47,10 @@ class DefaultRunningService implements RunningService, OriginProvider {
4547

4648
private final DockerEnv env;
4749

48-
private final DockerComposeFile composeFile;
50+
private final @Nullable DockerComposeFile composeFile;
4951

50-
DefaultRunningService(DockerHost host, DockerComposeFile composeFile, DockerCliComposePsResponse composePsResponse,
51-
DockerCliInspectResponse inspectResponse) {
52+
DefaultRunningService(DockerHost host, @Nullable DockerComposeFile composeFile,
53+
DockerCliComposePsResponse composePsResponse, DockerCliInspectResponse inspectResponse) {
5254
this.origin = new DockerComposeOrigin(composeFile, composePsResponse.name());
5355
this.name = composePsResponse.name();
5456
this.image = ImageReference
@@ -101,7 +103,7 @@ public String toString() {
101103
}
102104

103105
@Override
104-
public DockerComposeFile composeFile() {
106+
public @Nullable DockerComposeFile composeFile() {
105107
return this.composeFile;
106108
}
107109

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCli.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
30+
import org.jspecify.annotations.Nullable;
3031

3132
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeVersion;
3233
import org.springframework.boot.docker.compose.core.DockerCliCommand.Type;
@@ -60,7 +61,7 @@ class DockerCli {
6061
* @param workingDirectory the working directory or {@code null}
6162
* @param dockerComposeOptions the Docker Compose options to use or {@code null}.
6263
*/
63-
DockerCli(File workingDirectory, DockerComposeOptions dockerComposeOptions) {
64+
DockerCli(@Nullable File workingDirectory, @Nullable DockerComposeOptions dockerComposeOptions) {
6465
this.processRunner = new ProcessRunner(workingDirectory);
6566
this.dockerCommands = dockerCommandsCache.computeIfAbsent(workingDirectory,
6667
(key) -> new DockerCommands(this.processRunner));
@@ -82,7 +83,7 @@ <R> R run(DockerCliCommand<R> dockerCommand) {
8283
return dockerCommand.deserialize(json);
8384
}
8485

85-
private Consumer<String> createOutputConsumer(LogLevel logLevel) {
86+
private @Nullable Consumer<String> createOutputConsumer(@Nullable LogLevel logLevel) {
8687
if (logLevel == null || logLevel == LogLevel.OFF) {
8788
return null;
8889
}
@@ -123,7 +124,7 @@ private List<String> createCommand(Type type) {
123124
* Return the {@link DockerComposeFile} being used by this CLI instance.
124125
* @return the Docker Compose file
125126
*/
126-
DockerComposeFile getDockerComposeFile() {
127+
@Nullable DockerComposeFile getDockerComposeFile() {
127128
return this.dockerComposeOptions.composeFile();
128129
}
129130

@@ -205,11 +206,14 @@ private record DockerCommand(String version, List<String> command) {
205206
* @param activeProfiles the profiles to activate
206207
* @param arguments the arguments to pass to Docker Compose
207208
*/
208-
record DockerComposeOptions(DockerComposeFile composeFile, Set<String> activeProfiles, List<String> arguments) {
209-
210-
DockerComposeOptions {
211-
activeProfiles = (activeProfiles != null) ? activeProfiles : Collections.emptySet();
212-
arguments = (arguments != null) ? arguments : Collections.emptyList();
209+
record DockerComposeOptions(@Nullable DockerComposeFile composeFile, Set<String> activeProfiles,
210+
List<String> arguments) {
211+
212+
DockerComposeOptions(@Nullable DockerComposeFile composeFile, @Nullable Set<String> activeProfiles,
213+
@Nullable List<String> arguments) {
214+
this.composeFile = composeFile;
215+
this.activeProfiles = (activeProfiles != null) ? activeProfiles : Collections.emptySet();
216+
this.arguments = (arguments != null) ? arguments : Collections.emptyList();
213217
}
214218

215219
static DockerComposeOptions none() {

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCliCommand.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ List<String> getCommand(ComposeVersion composeVersion) {
7878

7979
@SuppressWarnings("unchecked")
8080
R deserialize(String json) {
81-
if (this.responseType == Void.class) {
82-
return null;
81+
if (this.responseType == None.class) {
82+
return (R) None.INSTANCE;
8383
}
8484
return (R) ((!this.listResponse) ? DockerJson.deserialize(json, this.responseType)
8585
: DockerJson.deserializeToList(json, this.responseType));
@@ -175,10 +175,10 @@ private static List<String> getPsCommand(ComposeVersion composeVersion) {
175175
/**
176176
* The {@code docker compose up} command.
177177
*/
178-
static final class ComposeUp extends DockerCliCommand<Void> {
178+
static final class ComposeUp extends DockerCliCommand<None> {
179179

180180
ComposeUp(LogLevel logLevel, List<String> arguments) {
181-
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, getCommand(arguments));
181+
super(Type.DOCKER_COMPOSE, logLevel, None.class, false, getCommand(arguments));
182182
}
183183

184184
private static String[] getCommand(List<String> arguments) {
@@ -196,10 +196,10 @@ private static String[] getCommand(List<String> arguments) {
196196
/**
197197
* The {@code docker compose down} command.
198198
*/
199-
static final class ComposeDown extends DockerCliCommand<Void> {
199+
static final class ComposeDown extends DockerCliCommand<None> {
200200

201201
ComposeDown(Duration timeout, List<String> arguments) {
202-
super(Type.DOCKER_COMPOSE, Void.class, false, getCommand(timeout, arguments));
202+
super(Type.DOCKER_COMPOSE, None.class, false, getCommand(timeout, arguments));
203203
}
204204

205205
private static String[] getCommand(Duration timeout, List<String> arguments) {
@@ -216,10 +216,10 @@ private static String[] getCommand(Duration timeout, List<String> arguments) {
216216
/**
217217
* The {@code docker compose start} command.
218218
*/
219-
static final class ComposeStart extends DockerCliCommand<Void> {
219+
static final class ComposeStart extends DockerCliCommand<None> {
220220

221221
ComposeStart(LogLevel logLevel, List<String> arguments) {
222-
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, getCommand(arguments));
222+
super(Type.DOCKER_COMPOSE, logLevel, None.class, false, getCommand(arguments));
223223
}
224224

225225
private static String[] getCommand(List<String> arguments) {
@@ -234,10 +234,10 @@ private static String[] getCommand(List<String> arguments) {
234234
/**
235235
* The {@code docker compose stop} command.
236236
*/
237-
static final class ComposeStop extends DockerCliCommand<Void> {
237+
static final class ComposeStop extends DockerCliCommand<None> {
238238

239239
ComposeStop(Duration timeout, List<String> arguments) {
240-
super(Type.DOCKER_COMPOSE, Void.class, false, getCommand(timeout, arguments));
240+
super(Type.DOCKER_COMPOSE, None.class, false, getCommand(timeout, arguments));
241241
}
242242

243243
private static String[] getCommand(Duration timeout, List<String> arguments) {
@@ -268,6 +268,15 @@ enum Type {
268268

269269
}
270270

271+
static final class None {
272+
273+
public static final None INSTANCE = new None();
274+
275+
private None() {
276+
}
277+
278+
}
279+
271280
/**
272281
* Docker compose version.
273282
*

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCliComposePsResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.docker.compose.core;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
/**
2022
* Response from {@link DockerCliCommand.ComposePs docker compose ps}.
2123
*
@@ -27,6 +29,6 @@
2729
* @author Andy Wilkinson
2830
* @author Phillip Webb
2931
*/
30-
record DockerCliComposePsResponse(String id, String name, String image, String state) {
32+
record DockerCliComposePsResponse(String id, String name, @Nullable String image, String state) {
3133

3234
}

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCliInspectResponse.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.List;
2020
import java.util.Map;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
/**
2325
* Response from {@link DockerCliCommand.Inspect docker inspect}.
2426
*
@@ -31,7 +33,8 @@
3133
* @author Phillip Webb
3234
*/
3335
record DockerCliInspectResponse(String id, DockerCliInspectResponse.Config config,
34-
DockerCliInspectResponse.NetworkSettings networkSettings, DockerCliInspectResponse.HostConfig hostConfig) {
36+
DockerCliInspectResponse.NetworkSettings networkSettings,
37+
DockerCliInspectResponse.@Nullable HostConfig hostConfig) {
3538

3639
/**
3740
* Configuration for the container that is portable between hosts.

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCompose.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.List;
2222
import java.util.Set;
2323

24+
import org.jspecify.annotations.Nullable;
25+
2426
import org.springframework.boot.docker.compose.core.DockerCli.DockerComposeOptions;
2527
import org.springframework.boot.logging.LogLevel;
2628

@@ -126,7 +128,7 @@ public interface DockerCompose {
126128
* @param activeProfiles a set of the profiles that should be activated
127129
* @return a {@link DockerCompose} instance
128130
*/
129-
static DockerCompose get(DockerComposeFile file, String hostname, Set<String> activeProfiles) {
131+
static DockerCompose get(DockerComposeFile file, @Nullable String hostname, Set<String> activeProfiles) {
130132
return get(file, hostname, activeProfiles, Collections.emptyList());
131133
}
132134

@@ -140,7 +142,7 @@ static DockerCompose get(DockerComposeFile file, String hostname, Set<String> ac
140142
* @return a {@link DockerCompose} instance
141143
* @since 3.4.0
142144
*/
143-
static DockerCompose get(DockerComposeFile file, String hostname, Set<String> activeProfiles,
145+
static DockerCompose get(DockerComposeFile file, @Nullable String hostname, Set<String> activeProfiles,
144146
List<String> arguments) {
145147
DockerCli cli = new DockerCli(null, new DockerComposeOptions(file, activeProfiles, arguments));
146148
return new DefaultDockerCompose(cli, hostname);

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerComposeFile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.List;
2727
import java.util.stream.Collectors;
2828

29+
import org.jspecify.annotations.Nullable;
30+
2931
import org.springframework.util.Assert;
3032

3133
/**
@@ -107,7 +109,7 @@ public String toString() {
107109
* current directory
108110
* @return the located file or {@code null} if no Docker Compose file can be found
109111
*/
110-
public static DockerComposeFile find(File workingDirectory) {
112+
public static @Nullable DockerComposeFile find(@Nullable File workingDirectory) {
111113
File base = (workingDirectory != null) ? workingDirectory : new File(".");
112114
if (!base.exists()) {
113115
return null;

0 commit comments

Comments
 (0)