Skip to content

Commit 52966a4

Browse files
authored
Enables jspecify (#1603)
spring-cloud-context spring-cloud-commons spring-cloud-loadbalancer
1 parent 74ae11a commit 52966a4

File tree

68 files changed

+659
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+659
-348
lines changed

.mvn/maven.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
-DaltSnapshotDeploymentRepository=repo.spring.io::default::https://repo.spring.io/libs-snapshot-local -P spring
1+
-Djspecify.enabled=true
2+
-P spring

spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.Map;
2222
import java.util.Objects;
2323

24+
import org.jspecify.annotations.Nullable;
25+
2426
/**
2527
* Default implementation of {@link ServiceInstance}.
2628
*
@@ -31,19 +33,19 @@
3133
*/
3234
public class DefaultServiceInstance implements ServiceInstance {
3335

34-
private String instanceId;
36+
private @Nullable String instanceId;
3537

36-
private String serviceId;
38+
private @Nullable String serviceId;
3739

38-
private String host;
40+
private @Nullable String host;
3941

4042
private int port;
4143

4244
private boolean secure;
4345

4446
private Map<String, String> metadata = new LinkedHashMap<>();
4547

46-
private URI uri;
48+
private @Nullable URI uri;
4749

4850
/**
4951
* @param instanceId the id of the instance.
@@ -53,14 +55,16 @@ public class DefaultServiceInstance implements ServiceInstance {
5355
* @param secure indicates whether or not the connection needs to be secure.
5456
* @param metadata a map containing metadata.
5557
*/
56-
public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
57-
Map<String, String> metadata) {
58+
public DefaultServiceInstance(@Nullable String instanceId, @Nullable String serviceId, @Nullable String host,
59+
int port, boolean secure, @Nullable Map<String, String> metadata) {
5860
this.instanceId = instanceId;
5961
this.serviceId = serviceId;
6062
this.host = host;
6163
this.port = port;
6264
this.secure = secure;
63-
this.metadata = metadata;
65+
if (metadata != null) {
66+
this.metadata = metadata;
67+
}
6468
}
6569

6670
/**
@@ -70,7 +74,8 @@ public DefaultServiceInstance(String instanceId, String serviceId, String host,
7074
* @param port the port on which the service is running.
7175
* @param secure indicates whether or not the connection needs to be secure.
7276
*/
73-
public DefaultServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure) {
77+
public DefaultServiceInstance(@Nullable String instanceId, @Nullable String serviceId, @Nullable String host,
78+
int port, boolean secure) {
7479
this(instanceId, serviceId, host, port, secure, new LinkedHashMap<>());
7580
}
7681

@@ -104,17 +109,17 @@ public Map<String, String> getMetadata() {
104109
}
105110

106111
@Override
107-
public String getInstanceId() {
112+
public @Nullable String getInstanceId() {
108113
return instanceId;
109114
}
110115

111116
@Override
112-
public String getServiceId() {
117+
public @Nullable String getServiceId() {
113118
return serviceId;
114119
}
115120

116121
@Override
117-
public String getHost() {
122+
public @Nullable String getHost() {
118123
return host;
119124
}
120125

spring-cloud-commons/src/main/java/org/springframework/cloud/client/ServiceInstance.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public interface ServiceInstance {
3939
/**
4040
* @return The service ID as registered.
4141
*/
42-
String getServiceId();
42+
@Nullable String getServiceId();
4343

4444
/**
4545
* @return The hostname of the registered service instance.
4646
*/
47-
String getHost();
47+
@Nullable String getHost();
4848

4949
/**
5050
* @return The port of the registered service instance.
@@ -64,7 +64,7 @@ public interface ServiceInstance {
6464
/**
6565
* @return The key / value pair metadata associated with the service instance.
6666
*/
67-
Map<String, String> getMetadata();
67+
@Nullable Map<String, String> getMetadata();
6868

6969
/**
7070
* @return The scheme of the service instance.

spring-cloud-commons/src/main/java/org/springframework/cloud/client/circuitbreaker/httpservice/CircuitBreakerConfigurerUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
*/
4949
final class CircuitBreakerConfigurerUtils {
5050

51+
/**
52+
* Default fallback key.
53+
*/
5154
public static final String DEFAULT_FALLBACK_KEY = "default";
5255

5356
private CircuitBreakerConfigurerUtils() {

spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public String[] selectImports(AnnotationMetadata metadata) {
4343
AnnotationAttributes attributes = AnnotationAttributes
4444
.fromMap(metadata.getAnnotationAttributes(getAnnotationClass().getName(), true));
4545

46-
boolean autoRegister = attributes.getBoolean("autoRegister");
46+
boolean autoRegister = true;
47+
if (attributes != null) {
48+
autoRegister = attributes.getBoolean("autoRegister");
49+
}
4750

4851
if (autoRegister) {
4952
List<String> importsList = new ArrayList<>(Arrays.asList(imports));

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/CompletionContext.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cloud.client.loadbalancer;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import org.springframework.core.style.ToStringCreator;
2022

2123
/**
@@ -29,11 +31,11 @@ public class CompletionContext<RES, T, C> {
2931

3032
private final Status status;
3133

32-
private final Throwable throwable;
34+
private final @Nullable Throwable throwable;
3335

34-
private final Response<T> loadBalancerResponse;
36+
private final @Nullable Response<T> loadBalancerResponse;
3537

36-
private final RES clientResponse;
38+
private final @Nullable RES clientResponse;
3739

3840
private final Request<C> loadBalancerRequest;
3941

@@ -55,8 +57,8 @@ public CompletionContext(Status status, Request<C> loadBalancerRequest, Response
5557
this(status, null, loadBalancerRequest, loadBalancerResponse, clientResponse);
5658
}
5759

58-
public CompletionContext(Status status, Throwable throwable, Request<C> loadBalancerRequest,
59-
Response<T> loadBalancerResponse, RES clientResponse) {
60+
public CompletionContext(Status status, @Nullable Throwable throwable, Request<C> loadBalancerRequest,
61+
@Nullable Response<T> loadBalancerResponse, @Nullable RES clientResponse) {
6062
this.status = status;
6163
this.throwable = throwable;
6264
this.loadBalancerRequest = loadBalancerRequest;
@@ -68,15 +70,15 @@ public Status status() {
6870
return this.status;
6971
}
7072

71-
public Throwable getThrowable() {
73+
public @Nullable Throwable getThrowable() {
7274
return this.throwable;
7375
}
7476

75-
public Response<T> getLoadBalancerResponse() {
77+
public @Nullable Response<T> getLoadBalancerResponse() {
7678
return loadBalancerResponse;
7779
}
7880

79-
public RES getClientResponse() {
81+
public @Nullable RES getClientResponse() {
8082
return clientResponse;
8183
}
8284

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public class DefaultRequest<T> implements Request<T> {
3030

3131
private T context;
3232

33+
@SuppressWarnings("unchecked")
3334
public DefaultRequest() {
34-
new DefaultRequestContext();
35+
this((T) new DefaultRequestContext());
3536
}
3637

3738
public DefaultRequest(T context) {

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequestContext.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.Objects;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.core.style.ToStringCreator;
2224

2325
/**
@@ -31,22 +33,22 @@ public class DefaultRequestContext extends HintRequestContext {
3133
* The request to be executed against the service instance selected by the
3234
* LoadBalancer.
3335
*/
34-
private final Object clientRequest;
36+
private final @Nullable Object clientRequest;
3537

3638
public DefaultRequestContext() {
3739
clientRequest = null;
3840
}
3941

40-
public DefaultRequestContext(Object clientRequest) {
42+
public DefaultRequestContext(@Nullable Object clientRequest) {
4143
this.clientRequest = clientRequest;
4244
}
4345

44-
public DefaultRequestContext(Object clientRequest, String hint) {
46+
public DefaultRequestContext(@Nullable Object clientRequest, String hint) {
4547
super(hint);
4648
this.clientRequest = clientRequest;
4749
}
4850

49-
public Object getClientRequest() {
51+
public @Nullable Object getClientRequest() {
5052
return clientRequest;
5153
}
5254

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.Objects;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.cloud.client.ServiceInstance;
2224
import org.springframework.core.style.ToStringCreator;
2325

@@ -27,7 +29,7 @@
2729
*/
2830
public class DefaultResponse implements Response<ServiceInstance> {
2931

30-
private final ServiceInstance serviceInstance;
32+
private final @Nullable ServiceInstance serviceInstance;
3133

3234
public DefaultResponse(ServiceInstance serviceInstance) {
3335
this.serviceInstance = serviceInstance;
@@ -39,7 +41,7 @@ public boolean hasServer() {
3941
}
4042

4143
@Override
42-
public ServiceInstance getServer() {
44+
public @Nullable ServiceInstance getServer() {
4345
return this.serviceInstance;
4446
}
4547

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DeferringLoadBalancerInterceptor.java

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

1919
import java.io.IOException;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.beans.factory.ObjectProvider;
2224
import org.springframework.http.HttpRequest;
2325
import org.springframework.http.client.ClientHttpRequestExecution;
@@ -37,14 +39,15 @@ public class DeferringLoadBalancerInterceptor implements ClientHttpRequestInterc
3739

3840
private final ObjectProvider<BlockingLoadBalancerInterceptor> loadBalancerInterceptorProvider;
3941

40-
private BlockingLoadBalancerInterceptor delegate;
42+
private @Nullable BlockingLoadBalancerInterceptor delegate;
4143

4244
public DeferringLoadBalancerInterceptor(
4345
ObjectProvider<BlockingLoadBalancerInterceptor> loadBalancerInterceptorProvider) {
4446
this.loadBalancerInterceptorProvider = loadBalancerInterceptorProvider;
4547
}
4648

4749
@Override
50+
@SuppressWarnings("NullAway") // nullability checked in tryResolveDelegate()
4851
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
4952
throws IOException {
5053
tryResolveDelegate();

0 commit comments

Comments
 (0)