Skip to content

Commit ef56296

Browse files
committed
Fix nullability errors introduced by previous commit.
Resolves #192 Signed-off-by: onobc <[email protected]>
1 parent 5df9cc4 commit ef56296

27 files changed

+170
-80
lines changed

spring-grpc-core/src/main/java/org/springframework/grpc/client/ClientInterceptorsConfigurer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ClientInterceptorsConfigurer implements InitializingBean {
3737

3838
private final ApplicationContext applicationContext;
3939

40-
private List<ClientInterceptor> globalInterceptors;
40+
private List<ClientInterceptor> globalInterceptors = new ArrayList<>();
4141

4242
public ClientInterceptorsConfigurer(ApplicationContext applicationContext) {
4343
this.applicationContext = applicationContext;

spring-grpc-core/src/main/java/org/springframework/grpc/client/DefaultGrpcChannelFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class DefaultGrpcChannelFactory<T extends ManagedChannelBuilder<T>>
5555

5656
private final ClientInterceptorsConfigurer interceptorsConfigurer;
5757

58-
private ClientInterceptorFilter interceptorFilter;
58+
private @Nullable ClientInterceptorFilter interceptorFilter;
5959

6060
private ChannelCredentialsProvider credentials = ChannelCredentialsProvider.INSECURE;
6161

spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactory.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import java.util.LinkedHashSet;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.Objects;
2728
import java.util.Set;
2829

30+
import org.jspecify.annotations.Nullable;
31+
2932
import org.springframework.beans.BeansException;
3033
import org.springframework.beans.factory.config.BeanDefinition;
3134
import org.springframework.beans.factory.support.AbstractBeanDefinition;
@@ -39,6 +42,7 @@
3942
import org.springframework.core.type.classreading.MetadataReaderFactory;
4043
import org.springframework.core.type.filter.TypeFilter;
4144
import org.springframework.grpc.internal.ClasspathScanner;
45+
import org.springframework.util.Assert;
4246
import org.springframework.util.ClassUtils;
4347
import org.springframework.util.ReflectionUtils;
4448
import org.springframework.util.StringUtils;
@@ -59,7 +63,7 @@ public class GrpcClientFactory implements ApplicationContextAware {
5963

6064
private Map<Class<?>, StubFactory<?>> factories = new LinkedHashMap<>();
6165

62-
private ApplicationContext context;
66+
private @Nullable ApplicationContext context;
6367

6468
static {
6569
DEFAULT_FACTORIES.add((Class<? extends StubFactory<?>>) BlockingStubFactory.class);
@@ -74,6 +78,10 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
7478
this.context = applicationContext;
7579
}
7680

81+
private ApplicationContext requireNonNullContext() {
82+
return Objects.requireNonNull(this.context, "ApplicationContext is required");
83+
}
84+
7785
public <T> T getClient(String target, Class<T> type, Class<?> factory) {
7886
@SuppressWarnings("unchecked")
7987
StubFactory<T> stubs = (StubFactory<T>) findFactory(factory, type);
@@ -84,7 +92,7 @@ public <T> T getClient(String target, Class<T> type, Class<?> factory) {
8492
private StubFactory<?> findFactory(Class<?> factoryType, Class<?> type) {
8593
if (this.factories.isEmpty()) {
8694
List<StubFactory<?>> factories = new ArrayList<>();
87-
for (StubFactory<?> factory : this.context.getBeansOfType(StubFactory.class).values()) {
95+
for (StubFactory<?> factory : this.requireNonNullContext().getBeansOfType(StubFactory.class).values()) {
8896
factories.add(factory);
8997
}
9098
AnnotationAwareOrderComparator.sort(factories);
@@ -96,7 +104,9 @@ private StubFactory<?> findFactory(Class<?> factoryType, Class<?> type) {
96104
continue;
97105
}
98106
this.factories.put(factory,
99-
(StubFactory<?>) this.context.getAutowireCapableBeanFactory().createBean(factory));
107+
(StubFactory<?>) this.requireNonNullContext()
108+
.getAutowireCapableBeanFactory()
109+
.createBean(factory));
100110
}
101111
}
102112
StubFactory<?> factory = findFactory(this.factories, factoryType, type);
@@ -107,7 +117,8 @@ private StubFactory<?> findFactory(Class<?> factoryType, Class<?> type) {
107117
return factory;
108118
}
109119

110-
private static Class<?> findDefaultFactory(BeanDefinitionRegistry registry, Class<?> factoryType, Class<?> type) {
120+
private static @Nullable Class<?> findDefaultFactory(BeanDefinitionRegistry registry,
121+
@Nullable Class<?> factoryType, Class<?> type) {
111122
if (factoryType != null && factoryType != UnspecifiedStubFactory.class) {
112123
return supports(factoryType, type) ? factoryType : null;
113124
}
@@ -131,6 +142,8 @@ private static Set<Class<?>> locateFactoryTypes(BeanDefinitionRegistry registry)
131142
beanDefinition = (AbstractBeanDefinition) registry.getBeanDefinition(FACTORIES_BEAN_DEFINITION_NAME);
132143
@SuppressWarnings("unchecked")
133144
Set<Class<?>> factories = (Set<Class<?>>) beanDefinition.getAttribute("factories");
145+
Assert.notEmpty(factories,
146+
"The %s bean requires a 'factories' attribute".formatted(FACTORIES_BEAN_DEFINITION_NAME));
134147
return factories;
135148

136149
}
@@ -152,7 +165,7 @@ public static HashSet<Class<?>> findStubFactoryTypes(BeanDefinitionRegistry regi
152165
return factories;
153166
}
154167

155-
private static Class<?> resolveBeanClass(BeanDefinition beanDefinition) {
168+
private static @Nullable Class<?> resolveBeanClass(BeanDefinition beanDefinition) {
156169
if (beanDefinition instanceof AbstractBeanDefinition rootBeanDefinition) {
157170
if (rootBeanDefinition.hasBeanClass()) {
158171
return rootBeanDefinition.getBeanClass();
@@ -161,6 +174,7 @@ private static Class<?> resolveBeanClass(BeanDefinition beanDefinition) {
161174
return null;
162175
}
163176

177+
@SuppressWarnings("NullAway")
164178
private static boolean supports(Class<?> factory, Class<?> type) {
165179
// To avoid needing to instantiate the factory we use reflection to check for a
166180
// static supports() method. If it exists we call it.
@@ -186,7 +200,7 @@ private static boolean supports(Class<?> factory, Class<?> type) {
186200
return supports;
187201
}
188202

189-
private static StubFactory<?> findFactory(Map<Class<?>, StubFactory<?>> values, Class<?> factoryType,
203+
private static @Nullable StubFactory<?> findFactory(Map<Class<?>, StubFactory<?>> values, Class<?> factoryType,
190204
Class<?> type) {
191205
StubFactory<?> factory = null;
192206
if (factoryType != null && factoryType != UnspecifiedStubFactory.class) {
@@ -204,7 +218,7 @@ private static StubFactory<?> findFactory(Map<Class<?>, StubFactory<?>> values,
204218
}
205219

206220
private GrpcChannelFactory channels() {
207-
return this.context.getBean(GrpcChannelFactory.class);
221+
return this.requireNonNullContext().getBean(GrpcChannelFactory.class);
208222
}
209223

210224
public static void register(BeanDefinitionRegistry registry, GrpcClientRegistrationSpec spec) {
@@ -230,8 +244,8 @@ public static void register(BeanDefinitionRegistry registry, GrpcClientRegistrat
230244
}
231245
}
232246

233-
public record GrpcClientRegistrationSpec(String prefix, Class<? extends StubFactory<?>> factory, String target,
234-
Class<?>[] types, String[] packages) {
247+
public record GrpcClientRegistrationSpec(String prefix, @Nullable Class<? extends StubFactory<?>> factory,
248+
String target, Class<?>[] types, String[] packages) {
235249

236250
private static ClasspathScanner SCANNER = new ClasspathScanner();
237251

@@ -276,7 +290,7 @@ public GrpcClientRegistrationSpec(String prefix, String target, Class<?>[] types
276290
this(prefix, UnspecifiedStubFactory.class, target, types, new String[0]);
277291
}
278292

279-
public GrpcClientRegistrationSpec factory(Class<? extends StubFactory<?>> factory) {
293+
public GrpcClientRegistrationSpec factory(@Nullable Class<? extends StubFactory<?>> factory) {
280294
return new GrpcClientRegistrationSpec(this.prefix, factory, this.target, this.types, this.packages);
281295
}
282296

spring-grpc-core/src/main/java/org/springframework/grpc/internal/ClasspathScanner.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.apache.commons.logging.Log;
2626
import org.apache.commons.logging.LogFactory;
27+
import org.jspecify.annotations.Nullable;
2728

2829
import org.springframework.beans.factory.BeanDefinitionStoreException;
2930
import org.springframework.context.ResourceLoaderAware;
@@ -49,9 +50,9 @@ public class ClasspathScanner implements ResourceLoaderAware {
4950

5051
private String resourcePattern = DEFAULT_RESOURCE_PATTERN;
5152

52-
private ResourcePatternResolver resourcePatternResolver;
53+
private @Nullable ResourcePatternResolver resourcePatternResolver;
5354

54-
private MetadataReaderFactory metadataReaderFactory;
55+
private @Nullable MetadataReaderFactory metadataReaderFactory;
5556

5657
@Override
5758
public void setResourceLoader(ResourceLoader resourceLoader) {

spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,24 @@ public class DefaultGrpcServerFactory<T extends ServerBuilder<T>> implements Grp
6666

6767
private final List<ServerBuilderCustomizer<T>> serverBuilderCustomizers;
6868

69-
private KeyManagerFactory keyManager;
69+
private @Nullable KeyManagerFactory keyManager;
7070

71-
private TrustManagerFactory trustManager;
71+
private @Nullable TrustManagerFactory trustManager;
7272

73-
private ClientAuth clientAuth;
73+
private @Nullable ClientAuth clientAuth;
7474

75-
private ServerServiceDefinitionFilter serviceFilter;
75+
private @Nullable ServerServiceDefinitionFilter serviceFilter;
7676

77-
private ServerInterceptorFilter interceptorFilter;
77+
private @Nullable ServerInterceptorFilter interceptorFilter;
7878

7979
public DefaultGrpcServerFactory(String address, List<ServerBuilderCustomizer<T>> serverBuilderCustomizers) {
8080
this.address = address;
8181
this.serverBuilderCustomizers = Objects.requireNonNull(serverBuilderCustomizers, "serverBuilderCustomizers");
8282
}
8383

8484
public DefaultGrpcServerFactory(String address, List<ServerBuilderCustomizer<T>> serverBuilderCustomizers,
85-
KeyManagerFactory keyManager, TrustManagerFactory trustManager, ClientAuth clientAuth) {
85+
@Nullable KeyManagerFactory keyManager, @Nullable TrustManagerFactory trustManager,
86+
@Nullable ClientAuth clientAuth) {
8687
this(address, serverBuilderCustomizers);
8788
this.keyManager = keyManager;
8889
this.trustManager = trustManager;

spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import javax.net.ssl.KeyManagerFactory;
2222
import javax.net.ssl.TrustManagerFactory;
2323

24+
import org.jspecify.annotations.Nullable;
25+
2426
import io.grpc.TlsServerCredentials.ClientAuth;
2527
import io.grpc.netty.NettyServerBuilder;
2628
import io.netty.channel.MultiThreadIoEventLoopGroup;
@@ -38,8 +40,9 @@
3840
public class NettyGrpcServerFactory extends DefaultGrpcServerFactory<NettyServerBuilder> {
3941

4042
public NettyGrpcServerFactory(String address,
41-
List<ServerBuilderCustomizer<NettyServerBuilder>> serverBuilderCustomizers, KeyManagerFactory keyManager,
42-
TrustManagerFactory trustManager, ClientAuth clientAuth) {
43+
List<ServerBuilderCustomizer<NettyServerBuilder>> serverBuilderCustomizers,
44+
@Nullable KeyManagerFactory keyManager, @Nullable TrustManagerFactory trustManager,
45+
@Nullable ClientAuth clientAuth) {
4346
super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth);
4447
}
4548

spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import javax.net.ssl.KeyManagerFactory;
2222
import javax.net.ssl.TrustManagerFactory;
2323

24+
import org.jspecify.annotations.Nullable;
25+
2426
import io.grpc.TlsServerCredentials.ClientAuth;
2527
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
2628
import io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoopGroup;
@@ -37,8 +39,9 @@
3739
public class ShadedNettyGrpcServerFactory extends DefaultGrpcServerFactory<NettyServerBuilder> {
3840

3941
public ShadedNettyGrpcServerFactory(String address,
40-
List<ServerBuilderCustomizer<NettyServerBuilder>> serverBuilderCustomizers, KeyManagerFactory keyManager,
41-
TrustManagerFactory trustManager, ClientAuth clientAuth) {
42+
List<ServerBuilderCustomizer<NettyServerBuilder>> serverBuilderCustomizers,
43+
@Nullable KeyManagerFactory keyManager, @Nullable TrustManagerFactory trustManager,
44+
@Nullable ClientAuth clientAuth) {
4245
super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth);
4346
}
4447

spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/CompositeGrpcExceptionHandler.java

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

1717
package org.springframework.grpc.server.exception;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import io.grpc.StatusException;
2022

2123
public class CompositeGrpcExceptionHandler implements GrpcExceptionHandler {
@@ -27,7 +29,7 @@ public CompositeGrpcExceptionHandler(GrpcExceptionHandler... exceptionHandlers)
2729
}
2830

2931
@Override
30-
public StatusException handleException(Throwable exception) {
32+
public @Nullable StatusException handleException(Throwable exception) {
3133
for (GrpcExceptionHandler exceptionHandler : this.exceptionHandlers) {
3234
StatusException status = exceptionHandler.handleException(exception);
3335
if (status != null) {

spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandledServerCall.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ protected GrpcExceptionHandledServerCall(ServerCall<ReqT, RespT> delegate, GrpcE
3535
@Override
3636
public void close(Status status, Metadata trailers) {
3737
if (status.getCode() == Status.Code.UNKNOWN && status.getCause() != null) {
38-
final Throwable cause = status.getCause();
39-
final StatusException statusException = this.exceptionHandler.handleException(cause);
40-
Metadata statusExceptionTrailers = statusException.getTrailers();
41-
if (statusExceptionTrailers != null) {
42-
trailers.merge(statusExceptionTrailers);
38+
StatusException statusException = this.exceptionHandler.handleException(status.getCause());
39+
if (statusException != null) {
40+
Metadata statusExceptionTrailers = statusException.getTrailers();
41+
if (statusExceptionTrailers != null) {
42+
trailers.merge(statusExceptionTrailers);
43+
}
44+
status = statusException.getStatus();
4345
}
44-
super.close(statusException.getStatus(), trailers);
46+
super.close(status, trailers);
4547
}
4648
else {
4749
super.close(status, trailers);

spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandler.java

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

1717
package org.springframework.grpc.server.exception;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import io.grpc.StatusException;
2022

2123
/**
@@ -33,6 +35,7 @@ public interface GrpcExceptionHandler {
3335
* @return the status to return to the client, or {@code null} if the exception cannot
3436
* be classified
3537
*/
38+
@Nullable
3639
StatusException handleException(Throwable exception);
3740

3841
}

0 commit comments

Comments
 (0)