Skip to content

Commit

Permalink
Perform NullAway build-time checks in spring-web
Browse files Browse the repository at this point in the history
Also in spring-websocket.

See spring-projectsgh-32475
  • Loading branch information
sdeleuze committed Mar 25, 2024
1 parent 2b1eb48 commit e52ee01
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle/spring-module.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ tasks.withType(JavaCompile).configureEach {
disableAllChecks = true
option("NullAway:CustomContractAnnotations", "org.springframework.lang.Contract")
option("NullAway:AnnotatedPackages", "org.springframework.core,org.springframework.expression," +
"org.springframework.web.reactive,org.springframework.web.servlet")
"org.springframework.web")
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 @@ -62,6 +62,7 @@ public abstract class CollectionUtils {
* @param collection the Collection to check
* @return whether the given Collection is empty
*/
@Contract("null -> true")
public static boolean isEmpty(@Nullable Collection<?> collection) {
return (collection == null || collection.isEmpty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static boolean isCompatibleWithThrowsClause(Throwable ex, @Nullable Class
* either an Object array or a primitive array.
* @param obj the object to check
*/
@Contract("null -> false")
public static boolean isArray(@Nullable Object obj) {
return (obj != null && obj.getClass().isArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ else if (statusCode.is5xxServerError()) {
* {@link RestClientResponseException#setBodyConvertFunction(Function)}.
* @since 6.0
*/
@SuppressWarnings("NullAway")
protected Function<ResolvableType, ?> initBodyConvertFunction(ClientHttpResponse response, byte[] body) {
Assert.state(!CollectionUtils.isEmpty(this.messageConverters), "Expected message converters");
return resolvableType -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ else if (CollectionUtils.isEmpty(defaultHeaders)) {
}
}

@SuppressWarnings("NullAway")
private ClientHttpRequest createRequest(URI uri) throws IOException {
ClientHttpRequestFactory factory;
if (DefaultRestClient.this.interceptors != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import io.micrometer.context.ThreadLocalAccessor;

import org.springframework.lang.Nullable;

/**
* Adapt {@link RequestContextHolder} to the {@link ThreadLocalAccessor} contract
* to assist the Micrometer Context Propagation library with
Expand All @@ -40,6 +42,7 @@ public Object key() {
}

@Override
@Nullable
public RequestAttributes getValue() {
return RequestContextHolder.getRequestAttributes();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public StandardServletAsyncWebRequest(HttpServletRequest request, HttpServletRes
* @param previousRequest the existing request from the last dispatch
* @since 5.3.33
*/
@SuppressWarnings("NullAway")
StandardServletAsyncWebRequest(HttpServletRequest request, HttpServletResponse response,
@Nullable StandardServletAsyncWebRequest previousRequest) {

Expand Down Expand Up @@ -243,6 +244,7 @@ public void setAsyncWebRequest(StandardServletAsyncWebRequest asyncWebRequest) {
}

@Override
@SuppressWarnings("NullAway")
public ServletOutputStream getOutputStream() throws IOException {
int level = obtainLockAndCheckState();
try {
Expand All @@ -262,6 +264,7 @@ public ServletOutputStream getOutputStream() throws IOException {
}

@Override
@SuppressWarnings("NullAway")
public PrintWriter getWriter() throws IOException {
int level = obtainLockAndCheckState();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ public void startCallableProcessing(Callable<?> callable, Object... processingCo
* via {@link #getConcurrentResultContext()}
* @throws Exception if concurrent processing failed to start
*/
@SuppressWarnings("NullAway")
public void startCallableProcessing(final WebAsyncTask<?> webAsyncTask, Object... processingContext)
throws Exception {

Expand Down Expand Up @@ -408,6 +409,7 @@ private void setConcurrentResultAndDispatch(@Nullable Object result) {
* @see #getConcurrentResult()
* @see #getConcurrentResultContext()
*/
@SuppressWarnings("NullAway")
public void startDeferredResultProcessing(
final DeferredResult<?> deferredResult, Object... processingContext) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource
* @see ServletContextResource
* @see jakarta.servlet.ServletContext#getResourcePaths
*/
@SuppressWarnings("NullAway")
protected void doRetrieveMatchingServletContextResources(
ServletContext servletContext, String fullPattern, String dir, Set<Resource> result)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public List<String> getAllowedOrigins() {
/**
* Variant of {@link #setAllowedOrigins} for adding one origin at a time.
*/
@SuppressWarnings("NullAway")
public void addAllowedOrigin(@Nullable String origin) {
if (origin == null) {
return;
Expand Down Expand Up @@ -244,6 +245,7 @@ public List<String> getAllowedOriginPatterns() {
* Variant of {@link #setAllowedOriginPatterns} for adding one origin at a time.
* @since 5.3
*/
@SuppressWarnings("NullAway")
public void addAllowedOriginPattern(@Nullable String originPattern) {
if (originPattern == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public String getShortLogMessage() {

@Override
public boolean equals(@Nullable Object other) {
return (this == other || (super.equals(other) && this.bean.equals(((HandlerMethod) other).bean)));
return (this == other || (super.equals(other) && other instanceof HandlerMethod otherMethod && this.bean.equals(otherMethod.bean)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ protected final void setMultipartFiles(MultiValueMap<String, MultipartFile> mult
* lazily initializing it if necessary.
* @see #initializeMultipart()
*/
@SuppressWarnings("NullAway")
protected MultiValueMap<String, MultipartFile> getMultipartFiles() {
if (this.multipartFiles == null) {
initializeMultipart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ protected void initializeMultipart() {
}

@Override
@SuppressWarnings("NullAway")
public Enumeration<String> getParameterNames() {
if (this.multipartParameterNames == null) {
initializeMultipart();
Expand All @@ -157,6 +158,7 @@ public Enumeration<String> getParameterNames() {
}

@Override
@SuppressWarnings("NullAway")
public Map<String, String[]> getParameterMap() {
if (this.multipartParameterNames == null) {
initializeMultipart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public void setCodecConfigurer(ServerCodecConfigurer codecConfigurer) {
/**
* Return the configured {@link ServerCodecConfigurer}.
*/
@SuppressWarnings("NullAway")
public ServerCodecConfigurer getCodecConfigurer() {
if (this.codecConfigurer == null) {
setCodecConfigurer(ServerCodecConfigurer.create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public InMemoryWebSession(Instant creationTime) {
}

@Override
@SuppressWarnings("NullAway")
public String getId() {
return this.id.get();
}
Expand Down Expand Up @@ -224,6 +225,7 @@ public void start() {
}

@Override
@SuppressWarnings("NullAway")
public boolean isStarted() {
return this.state.get().equals(State.STARTED) || !getAttributes().isEmpty();
}
Expand Down Expand Up @@ -252,6 +254,7 @@ public Mono<Void> invalidate() {
}

@Override
@SuppressWarnings("NullAway")
public Mono<Void> save() {

checkMaxSessionsLimit();
Expand Down Expand Up @@ -289,6 +292,7 @@ public boolean isExpired() {
return isExpired(clock.instant());
}

@SuppressWarnings("NullAway")
private boolean isExpired(Instant now) {
if (this.state.get().equals(State.EXPIRED)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ private static HttpMethod initHttpMethod(@Nullable HttpExchange typeAnnotation,
}

@Nullable
@SuppressWarnings("NullAway")
private static String initUrl(
@Nullable HttpExchange typeAnnotation, HttpExchange methodAnnotation,
@Nullable StringValueResolver embeddedValueResolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public HttpServiceProxyFactory build() {
this.exchangeAdapter, initArgumentResolvers(), this.embeddedValueResolver);
}

@SuppressWarnings("DataFlowIssue")
@SuppressWarnings({"DataFlowIssue", "NullAway"})
private List<HttpServiceArgumentResolver> initArgumentResolvers() {

// Custom
Expand Down

0 comments on commit e52ee01

Please sign in to comment.