Skip to content

Commit

Permalink
Perform NullAway build-time checks in spring-webflux
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Mar 22, 2024
1 parent 637aa9f commit 0a50854
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion gradle/spring-module.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ tasks.withType(JavaCompile).configureEach {
options.errorprone {
disableAllChecks = true
option("NullAway:CustomContractAnnotations", "org.springframework.lang.Contract")
option("NullAway:AnnotatedPackages", "org.springframework.core,org.springframework.expression")
option("NullAway:AnnotatedPackages", "org.springframework.core,org.springframework.expression," +
"org.springframework.web.reactive")
option("NullAway:UnannotatedSubPackages", "org.springframework.instrument,org.springframework.context.index," +
"org.springframework.asm,org.springframework.cglib,org.springframework.objenesis," +
"org.springframework.javapoet,org.springframework.aot.nativex.substitution")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ private Mono<Void> handleResultMono(ServerWebExchange exchange, Mono<HandlerResu
}
return resultMono.flatMap(result -> {
Mono<Void> voidMono = handleResult(exchange, result, "Handler " + result.getHandler());
if (result.getExceptionHandler() != null) {
DispatchExceptionHandler exceptionHandler = result.getExceptionHandler();
if (exceptionHandler != null) {
voidMono = voidMono.onErrorResume(ex ->
result.getExceptionHandler().handleError(exchange, ex).flatMap(result2 ->
exceptionHandler.handleError(exchange, ex).flatMap(result2 ->
handleResult(exchange, result2, "Exception handler " +
result2.getHandler() + ", error=\"" + ex.getMessage() + "\"")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public ResourceChainRegistration(boolean cacheResources) {
this(cacheResources, cacheResources ? new ConcurrentMapCache(DEFAULT_CACHE_NAME) : null);
}

@SuppressWarnings("NullAway")
public ResourceChainRegistration(boolean cacheResources, @Nullable Cache cache) {
Assert.isTrue(!cacheResources || cache != null, "'cache' is required when cacheResources=true");
if (cacheResources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public ClientResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
return this;
}

@SuppressWarnings("ConstantConditions")
@SuppressWarnings({"ConstantConditions", "NullAway"})
private HttpHeaders getHeaders() {
if (this.headers == null) {
this.headers = new HttpHeaders(this.originalResponse.headers().asHttpHeaders());
Expand All @@ -159,7 +159,7 @@ public ClientResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCoo
return this;
}

@SuppressWarnings("ConstantConditions")
@SuppressWarnings({"ConstantConditions", "NullAway"})
private MultiValueMap<String, ResponseCookie> getCookies() {
if (this.cookies == null) {
this.cookies = new LinkedMultiValueMap<>(this.originalResponse.cookies());
Expand Down Expand Up @@ -256,13 +256,13 @@ public HttpStatusCode getStatusCode() {
}

@Override
@SuppressWarnings("ConstantConditions")
@SuppressWarnings({"ConstantConditions", "NullAway"})
public HttpHeaders getHeaders() {
return (this.headers != null ? this.headers : this.originalResponse.headers().asHttpHeaders());
}

@Override
@SuppressWarnings("ConstantConditions")
@SuppressWarnings({"ConstantConditions", "NullAway"})
public MultiValueMap<String, ResponseCookie> getCookies() {
return (this.cookies != null ? this.cookies : this.originalResponse.cookies());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ public boolean isOptimizeLocations() {
* @param mediaTypes media type mappings
* @since 5.3.2
*/
@SuppressWarnings("NullAway")
public void setMediaTypes(Map<String, MediaType> mediaTypes) {
if (this.mediaTypes == null) {
this.mediaTypes = new HashMap<>(mediaTypes.size());
Expand Down Expand Up @@ -483,6 +484,7 @@ public Mono<Void> handle(ServerWebExchange exchange) {
});
}

@SuppressWarnings("NullAway")
protected Mono<Resource> getResource(ServerWebExchange exchange) {
String rawPath = getResourcePath(exchange);
String path = processPath(rawPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ protected HandlerMethod lookupHandlerMethod(ServerWebExchange exchange) throws E
}
}

@SuppressWarnings("NullAway")
private void addMatchingMappings(Collection<T> mappings, List<Match> matches, ServerWebExchange exchange) {
for (T mapping : mappings) {
T match = getMatchingMapping(mapping, exchange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Contract;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -161,7 +162,7 @@ public void setMethodValidator(@Nullable MethodValidator methodValidator) {
* @param providedArgs optional list of argument values to match by type
* @return a Mono with a {@link HandlerResult}
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "NullAway"})
public Mono<HandlerResult> invoke(
ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) {

Expand Down Expand Up @@ -261,6 +262,7 @@ private void logArgumentErrorIfNecessary(ServerWebExchange exchange, MethodParam
}
}

@Contract("_, null -> false")
private static boolean isAsyncVoidReturnType(MethodParameter returnType, @Nullable ReactiveAdapter adapter) {
if (adapter != null && adapter.supportsEmpty()) {
if (adapter.isNoValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
* @return indicates completion or error
* @since 5.0.2
*/
@SuppressWarnings({"rawtypes", "unchecked", "ConstantConditions"})
@SuppressWarnings({"rawtypes", "unchecked", "ConstantConditions", "NullAway"})
protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParameter,
@Nullable MethodParameter actualParam, ServerWebExchange exchange) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ private InvocableHandlerMethod createAttributeMethod(Object bean, Method method)
* if {@code null}, check only {@code @ControllerAdvice} classes.
*/
@Nullable
@SuppressWarnings("NullAway")
public InvocableHandlerMethod getExceptionHandlerMethod(Throwable ex, @Nullable HandlerMethod handlerMethod) {

Class<?> handlerType = (handlerMethod != null ? handlerMethod.getBeanType() : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private boolean isSupportedType(@Nullable Class<?> type) {


@Override
@SuppressWarnings("ConstantConditions")
@SuppressWarnings({"ConstantConditions", "NullAway"})
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {

Mono<?> returnValueMono;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.Netty5DataBufferFactory;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.reactive.socket.HandshakeInfo;
import org.springframework.web.reactive.socket.WebSocketMessage;
Expand Down Expand Up @@ -76,7 +77,9 @@ public Netty5DataBufferFactory bufferFactory() {

protected WebSocketMessage toMessage(WebSocketFrame frame) {
DataBuffer payload = bufferFactory().wrap(frame.binaryData());
return new WebSocketMessage(messageTypes.get(frame.getClass()), payload, frame);
WebSocketMessage.Type messageType = messageTypes.get(frame.getClass());
Assert.state(messageType != null, "Unexpected message type");
return new WebSocketMessage(messageType, payload, frame);
}

protected WebSocketFrame toFrame(WebSocketMessage message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.reactive.socket.HandshakeInfo;
import org.springframework.web.reactive.socket.WebSocketMessage;
Expand Down Expand Up @@ -74,7 +75,9 @@ public NettyDataBufferFactory bufferFactory() {

protected WebSocketMessage toMessage(WebSocketFrame frame) {
DataBuffer payload = bufferFactory().wrap(frame.content());
return new WebSocketMessage(messageTypes.get(frame.getClass()), payload, frame);
WebSocketMessage.Type messageType = messageTypes.get(frame.getClass());
Assert.state(messageType != null, "Unexpected message type");
return new WebSocketMessage(messageType, payload, frame);
}

protected WebSocketFrame toFrame(WebSocketMessage message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public StandardWebSocketHandlerAdapter(WebSocketHandler handler,


@Override
@SuppressWarnings("NullAway")
public void onOpen(Session session, EndpointConfig config) {
this.delegateSession = this.sessionFactory.apply(session);
Assert.state(this.delegateSession != null, "No delegate session");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ private String selectProtocol(HttpHeaders headers, WebSocketHandler handler) {
return null;
}

@SuppressWarnings("NullAway")
private Mono<Map<String, Object>> initAttributes(ServerWebExchange exchange) {
if (this.sessionAttributePredicate == null) {
return EMPTY_ATTRIBUTES;
Expand Down

0 comments on commit 0a50854

Please sign in to comment.