Browse Source

Remove NullAway suppressions where possible

See gh-46926
pull/47162/head
Moritz Halbritter 3 months ago
parent
commit
2e8428b760
  1. 4
      build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoProperties.java
  2. 4
      build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/StartMojo.java
  3. 4
      core/spring-boot/src/main/java/org/springframework/boot/DefaultBootstrapContext.java
  4. 10
      core/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java
  5. 4
      core/spring-boot/src/main/java/org/springframework/boot/context/properties/source/MutuallyExclusiveConfigurationPropertiesException.java
  6. 33
      core/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java
  7. 1
      module/spring-boot-http-client/src/main/java/org/springframework/boot/http/client/autoconfigure/ClientHttpRequestFactories.java
  8. 1
      module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/metrics/DataSourcePoolMetrics.java
  9. 8
      module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/PropertiesMeterFilter.java
  10. 16
      module/spring-boot-r2dbc/src/main/java/org/springframework/boot/r2dbc/autoconfigure/R2dbcAutoConfiguration.java

4
build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoProperties.java

@ -142,9 +142,9 @@ public abstract class BuildInfoProperties implements Serializable { @@ -142,9 +142,9 @@ public abstract class BuildInfoProperties implements Serializable {
return coerceToStringValues(applyExclusions(getAdditional().getOrElse(Collections.emptyMap())));
}
@SuppressWarnings("NullAway") // Doesn't detect lambda with correct nullability
private <T> @Nullable T getIfNotExcluded(Property<T> property, String name) {
return getIfNotExcluded(property, name, () -> null);
Supplier<@Nullable T> supplier = () -> null;
return getIfNotExcluded(property, name, supplier);
}
private <T> @Nullable T getIfNotExcluded(Property<T> property, String name, Supplier<@Nullable T> defaultValue) {

4
build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/StartMojo.java

@ -156,12 +156,12 @@ public class StartMojo extends AbstractRunMojo { @@ -156,12 +156,12 @@ public class StartMojo extends AbstractRunMojo {
}
}
@SuppressWarnings("NullAway") // Lambda isn't detected with the correct nullability
private void doWaitForSpringApplication(MBeanServerConnection connection)
throws MojoExecutionException, MojoFailureException {
final SpringApplicationAdminClient client = new SpringApplicationAdminClient(connection, this.jmxName);
try {
execute(this.wait, this.maxAttempts, () -> (client.isReady() ? true : null));
Callable<@Nullable Boolean> isReady = () -> (client.isReady() ? true : null);
execute(this.wait, this.maxAttempts, isReady);
}
catch (ReflectionException ex) {
throw new MojoExecutionException("Unable to retrieve 'ready' attribute", ex.getCause());

4
core/spring-boot/src/main/java/org/springframework/boot/DefaultBootstrapContext.java

@ -91,9 +91,9 @@ public class DefaultBootstrapContext implements ConfigurableBootstrapContext { @@ -91,9 +91,9 @@ public class DefaultBootstrapContext implements ConfigurableBootstrapContext {
}
@Override
@SuppressWarnings("NullAway") // Doesn't detect lambda with correct nullability
public <T> @Nullable T getOrElse(Class<T> type, @Nullable T other) {
return getOrElseSupply(type, () -> other);
Supplier<@Nullable T> supplier = () -> other;
return getOrElseSupply(type, supplier);
}
@Override

10
core/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java

@ -459,7 +459,6 @@ public class Binder { @@ -459,7 +459,6 @@ public class Binder {
return null;
}
@SuppressWarnings("NullAway") // Doesn't detect lambda with correct nullability
private <T> @Nullable Object bindAggregate(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler,
Context context, AggregateBinder<?> aggregateBinder) {
AggregateElementBinder elementBinder = (itemName, itemTarget, source) -> {
@ -467,7 +466,8 @@ public class Binder { @@ -467,7 +466,8 @@ public class Binder {
Supplier<?> supplier = () -> bind(itemName, itemTarget, handler, context, allowRecursiveBinding, false);
return context.withSource(source, supplier);
};
return context.withIncreasedDepth(() -> aggregateBinder.bind(name, target, elementBinder));
Supplier<@Nullable Object> supplier = () -> aggregateBinder.bind(name, target, elementBinder);
return context.withIncreasedDepth(supplier);
}
private <T> @Nullable ConfigurationProperty findProperty(ConfigurationPropertyName name, Bindable<T> target,
@ -492,7 +492,6 @@ public class Binder { @@ -492,7 +492,6 @@ public class Binder {
return result;
}
@SuppressWarnings("NullAway") // Doesn't detect lambda with correct nullability
private @Nullable Object bindDataObject(ConfigurationPropertyName name, Bindable<?> target, BindHandler handler,
Context context, boolean allowRecursiveBinding) {
if (isUnbindableBean(name, target, context)) {
@ -505,8 +504,9 @@ public class Binder { @@ -505,8 +504,9 @@ public class Binder {
}
DataObjectPropertyBinder propertyBinder = (propertyName, propertyTarget) -> bind(name.append(propertyName),
propertyTarget, handler, context, false, false);
return context.withDataObject(type, () -> fromDataObjectBinders(bindMethod,
(dataObjectBinder) -> dataObjectBinder.bind(name, target, context, propertyBinder)));
Supplier<@Nullable Object> supplier = () -> fromDataObjectBinders(bindMethod,
(dataObjectBinder) -> dataObjectBinder.bind(name, target, context, propertyBinder));
return context.withDataObject(type, supplier);
}
private @Nullable Object fromDataObjectBinders(@Nullable BindMethod bindMethod,

4
core/spring-boot/src/main/java/org/springframework/boot/context/properties/source/MutuallyExclusiveConfigurationPropertiesException.java

@ -101,9 +101,9 @@ public class MutuallyExclusiveConfigurationPropertiesException extends RuntimeEx @@ -101,9 +101,9 @@ public class MutuallyExclusiveConfigurationPropertiesException extends RuntimeEx
* non-null values are defined in a set of entries.
* @param entries a consumer used to populate the entries to check
*/
@SuppressWarnings("NullAway") // Doesn't detect lambda with correct nullability
public static void throwIfMultipleNonNullValuesIn(Consumer<Map<String, @Nullable Object>> entries) {
throwIfMultipleMatchingValuesIn(entries, Objects::nonNull);
Predicate<@Nullable Object> isNonNull = Objects::nonNull;
throwIfMultipleMatchingValuesIn(entries, isNonNull);
}
/**

33
core/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java

@ -252,13 +252,12 @@ public final class LambdaSafe { @@ -252,13 +252,12 @@ public final class LambdaSafe {
* Invoke the callback instance where the callback method returns void.
* @param invoker the invoker used to invoke the callback
*/
// Lambda isn't detected with the correct nullability
@SuppressWarnings("NullAway")
public void invoke(Consumer<C> invoker) {
invoke(this.callbackInstance, () -> {
Supplier<@Nullable Void> supplier = () -> {
invoker.accept(this.callbackInstance);
return null;
});
};
invoke(this.callbackInstance, supplier);
}
/**
@ -268,10 +267,9 @@ public final class LambdaSafe { @@ -268,10 +267,9 @@ public final class LambdaSafe {
* @return the result of the invocation (may be {@link InvocationResult#noResult}
* if the callback was not invoked)
*/
// Lambda isn't detected with the correct nullability
@SuppressWarnings("NullAway")
public <R> InvocationResult<R> invokeAnd(Function<C, @Nullable R> invoker) {
return invoke(this.callbackInstance, () -> invoker.apply(this.callbackInstance));
Supplier<@Nullable R> supplier = () -> invoker.apply(this.callbackInstance);
return invoke(this.callbackInstance, supplier);
}
}
@ -296,13 +294,14 @@ public final class LambdaSafe { @@ -296,13 +294,14 @@ public final class LambdaSafe {
* Invoke the callback instances where the callback method returns void.
* @param invoker the invoker used to invoke the callback
*/
// Lambda isn't detected with the correct nullability
@SuppressWarnings("NullAway")
public void invoke(Consumer<C> invoker) {
this.callbackInstances.forEach((callbackInstance) -> invoke(callbackInstance, () -> {
invoker.accept(callbackInstance);
return null;
}));
this.callbackInstances.forEach((callbackInstance) -> {
Supplier<@Nullable Void> supplier = () -> {
invoker.accept(callbackInstance);
return null;
};
invoke(callbackInstance, supplier);
});
}
/**
@ -312,11 +311,11 @@ public final class LambdaSafe { @@ -312,11 +311,11 @@ public final class LambdaSafe {
* @return the results of the invocation (may be an empty stream if no callbacks
* could be called)
*/
// Lambda isn't detected with the correct nullability
@SuppressWarnings("NullAway")
public <R> Stream<R> invokeAnd(Function<C, @Nullable R> invoker) {
Function<C, InvocationResult<R>> mapper = (callbackInstance) -> invoke(callbackInstance,
() -> invoker.apply(callbackInstance));
Function<C, InvocationResult<R>> mapper = (callbackInstance) -> {
Supplier<@Nullable R> supplier = () -> invoker.apply(callbackInstance);
return invoke(callbackInstance, supplier);
};
return this.callbackInstances.stream()
.map(mapper)
.filter(InvocationResult::hasResult)

1
module/spring-boot-http-client/src/main/java/org/springframework/boot/http/client/autoconfigure/ClientHttpRequestFactories.java

@ -100,7 +100,6 @@ public final class ClientHttpRequestFactories { @@ -100,7 +100,6 @@ public final class ClientHttpRequestFactories {
return getProperty(accessor, Function.identity(), predicate, fallback, fallbackAccessor);
}
@SuppressWarnings("NullAway") // Lambda isn't detected with the correct nullability
private <P, T, F> @Nullable T getProperty(Function<AbstractHttpRequestFactoryProperties, @Nullable P> accessor,
Function<P, @Nullable T> extractor, Predicate<@Nullable T> predicate, @Nullable F fallback,
Function<F, @Nullable T> fallbackAccessor) {

1
module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/metrics/DataSourcePoolMetrics.java

@ -81,7 +81,6 @@ public class DataSourcePoolMetrics implements MeterBinder { @@ -81,7 +81,6 @@ public class DataSourcePoolMetrics implements MeterBinder {
}
}
@SuppressWarnings("NullAway") // Lambda isn't detected with the correct nullability
private <N extends Number> void bindPoolMetadata(MeterRegistry registry, String metricName, String description,
Function<DataSourcePoolMetadata, @Nullable N> function) {
bindDataSource(registry, metricName, description, this.metadataProvider.getValueFunction(function));

8
module/spring-boot-micrometer-metrics/src/main/java/org/springframework/boot/micrometer/metrics/autoconfigure/PropertiesMeterFilter.java

@ -117,21 +117,21 @@ public class PropertiesMeterFilter implements MeterFilter { @@ -117,21 +117,21 @@ public class PropertiesMeterFilter implements MeterFilter {
return (value != null) ? MeterValue.valueOf(value).getValue(meterType) : null;
}
@SuppressWarnings("NullAway") // Lambda isn't detected with the correct nullability
private <T> @Nullable T lookup(Map<String, T> values, Id id, @Nullable T defaultValue) {
if (values.isEmpty()) {
return defaultValue;
}
return doLookup(values, id, () -> defaultValue);
Supplier<@Nullable T> getDefaultValue = () -> defaultValue;
return doLookup(values, id, getDefaultValue);
}
@Contract("_, _, !null -> !null")
@SuppressWarnings("NullAway") // Lambda isn't detected with the correct nullability
private <T> @Nullable T lookupWithFallbackToAll(Map<String, T> values, Id id, @Nullable T defaultValue) {
if (values.isEmpty()) {
return defaultValue;
}
return doLookup(values, id, () -> values.getOrDefault("all", defaultValue));
Supplier<@Nullable T> getAllOrDefaultValue = () -> values.getOrDefault("all", defaultValue);
return doLookup(values, id, getAllOrDefaultValue);
}
private <T> @Nullable T doLookup(Map<String, T> values, Id id, Supplier<@Nullable T> defaultValue) {

16
module/spring-boot-r2dbc/src/main/java/org/springframework/boot/r2dbc/autoconfigure/R2dbcAutoConfiguration.java

@ -83,23 +83,19 @@ public final class R2dbcAutoConfiguration { @@ -83,23 +83,19 @@ public final class R2dbcAutoConfiguration {
return optionsBuilder.build();
}
// Lambda isn't detected with the correct nullability
@SuppressWarnings("NullAway")
private void configureDatabase(Builder optionsBuilder, ConnectionFactoryOptions urlOptions) {
configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.DATABASE,
() -> determineDatabaseName(this.properties));
Supplier<@Nullable String> getDatabaseName = () -> determineDatabaseName(this.properties);
configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.DATABASE, getDatabaseName);
}
// Lambda isn't detected with the correct nullability
@SuppressWarnings("NullAway")
private void configurePassword(Builder optionsBuilder, ConnectionFactoryOptions urlOptions) {
configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.PASSWORD, this.properties::getPassword);
Supplier<@Nullable CharSequence> getPassword = this.properties::getPassword;
configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.PASSWORD, getPassword);
}
// Lambda isn't detected with the correct nullability
@SuppressWarnings("NullAway")
private void configureUser(Builder optionsBuilder, ConnectionFactoryOptions urlOptions) {
configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.USER, this.properties::getUsername);
Supplier<@Nullable String> getUsername = this.properties::getUsername;
configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.USER, getUsername);
}
private <T extends CharSequence> void configureIf(Builder optionsBuilder,

Loading…
Cancel
Save