Browse Source

Refine null-safety in the spring-core module

Closes gh-34150
pull/34171/head
Sébastien Deleuze 12 months ago
parent
commit
cf90beec0a
  1. 3
      spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java
  2. 2
      spring-core/src/main/java/org/springframework/util/ClassUtils.java
  3. 11
      spring-core/src/main/java/org/springframework/util/InvalidMimeTypeException.java
  4. 1
      spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java
  5. 2
      spring-core/src/main/java/org/springframework/util/NumberUtils.java

3
spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java

@ -173,7 +173,6 @@ enum InstrumentedMethod { @@ -173,7 +173,6 @@ enum InstrumentedMethod {
/**
* {@link Class#getField(String)}.
*/
@SuppressWarnings("NullAway")
CLASS_GETFIELD(Class.class, "getField", HintType.REFLECTION,
invocation -> {
Field field = invocation.getReturnValue();
@ -181,7 +180,7 @@ enum InstrumentedMethod { @@ -181,7 +180,7 @@ enum InstrumentedMethod {
return runtimeHints -> false;
}
return reflection().onType(field.getDeclaringClass())
.or(reflection().onField(invocation.getReturnValue()));
.or(reflection().onField(field));
}),
/**

2
spring-core/src/main/java/org/springframework/util/ClassUtils.java

@ -558,7 +558,7 @@ public abstract class ClassUtils { @@ -558,7 +558,7 @@ public abstract class ClassUtils {
* @param clazz the class to check
* @return the original class, or a primitive wrapper for the original primitive type
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null");
return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz);

11
spring-core/src/main/java/org/springframework/util/InvalidMimeTypeException.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,12 +16,15 @@ @@ -16,12 +16,15 @@
package org.springframework.util;
import org.jspecify.annotations.Nullable;
/**
* Exception thrown from {@link MimeTypeUtils#parseMimeType(String)} in case of
* encountering an invalid content type specification String.
*
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @since 4.0
*/
@SuppressWarnings("serial")
@ -35,8 +38,10 @@ public class InvalidMimeTypeException extends IllegalArgumentException { @@ -35,8 +38,10 @@ public class InvalidMimeTypeException extends IllegalArgumentException {
* @param mimeType the offending media type
* @param message a detail message indicating the invalid part
*/
public InvalidMimeTypeException(String mimeType, String message) {
super("Invalid mime type \"" + mimeType + "\": " + message);
public InvalidMimeTypeException(String mimeType, @Nullable String message) {
super(message == null ?
"Invalid mime type \"" + mimeType + "\"" :
"Invalid mime type \"" + mimeType + "\": " + message);
this.mimeType = mimeType;
}

1
spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

@ -203,7 +203,6 @@ public abstract class MimeTypeUtils { @@ -203,7 +203,6 @@ public abstract class MimeTypeUtils {
return cachedMimeTypes.get(mimeType);
}
@SuppressWarnings("NullAway")
private static MimeType parseMimeTypeInternal(String mimeType) {
int index = mimeType.indexOf(';');
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim();

2
spring-core/src/main/java/org/springframework/util/NumberUtils.java

@ -236,7 +236,7 @@ public abstract class NumberUtils { @@ -236,7 +236,7 @@ public abstract class NumberUtils {
* @see #convertNumberToTargetClass
* @see #parseNumber(String, Class)
*/
@SuppressWarnings("NullAway")
@SuppressWarnings("NullAway") // Dataflow analysis limitation
public static <T extends Number> T parseNumber(
String text, Class<T> targetClass, @Nullable NumberFormat numberFormat) {

Loading…
Cancel
Save