GH-1192 - Migrate code base to jSpecify for nullness verification.

This commit is contained in:
Oliver Drotbohm
2025-02-17 17:10:47 +01:00
parent a136ff920b
commit 9ea2b24b53
134 changed files with 548 additions and 211 deletions

View File

@@ -17,6 +17,11 @@
<dependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>

View File

@@ -25,6 +25,7 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.jspecify.annotations.Nullable;
import org.springframework.modulith.events.RoutingTarget.ParsedRoutingTarget;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -45,7 +46,7 @@ class AnnotationTargetLookup implements Supplier<Optional<ParsedRoutingTarget>>
private static Map<Class<?>, AnnotationTargetLookup> LOOKUPS = new ConcurrentReferenceHashMap<>(25);
private static final String JMOLECULES_EXTERNALIZED = "org.jmolecules.event.annotation.Externalized";
private static final Class<? extends Annotation> JMOLECULES_ANNOTATION = loadJMoleculesExternalizedIfPresent();
private static final @Nullable Class<? extends Annotation> JMOLECULES_ANNOTATION = loadJMoleculesExternalizedIfPresent();
private final Class<?> type;
private final Supplier<Optional<ParsedRoutingTarget>> lookup;
@@ -159,7 +160,7 @@ class AnnotationTargetLookup implements Supplier<Optional<ParsedRoutingTarget>>
}
@SuppressWarnings("unchecked")
private static Class<? extends Annotation> loadJMoleculesExternalizedIfPresent() {
private static @Nullable Class<? extends Annotation> loadJMoleculesExternalizedIfPresent() {
var classLoader = DefaultEventExternalizationConfiguration.class.getClassLoader();

View File

@@ -28,8 +28,8 @@ import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import org.jspecify.annotations.Nullable;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable;
import org.springframework.modulith.events.RoutingTarget.ParsedRoutingTarget;
import org.springframework.modulith.events.RoutingTarget.RoutingTargetBuilder;
import org.springframework.util.Assert;
@@ -359,7 +359,16 @@ public interface EventExternalizationConfiguration {
}
private static <T extends Annotation> T findAnnotation(Object event, Class<T> annotationType) {
return findMergedAnnotation(event.getClass(), annotationType);
var type = event.getClass();
var result = findMergedAnnotation(type, annotationType);
if (result == null) {
throw new IllegalStateException(
"Couldn't find annotation %s on type %s!".formatted(annotationType, type));
}
return result;
}
}

View File

@@ -17,9 +17,10 @@ package org.springframework.modulith.events;
import java.util.Objects;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -103,7 +104,7 @@ public class EventExternalized<S, T> implements ResolvableTypeProvider {
*
* @return can be {@literal null}.
*/
public T getBrokerResult() {
public @Nullable T getBrokerResult() {
return brokerResult;
}
@@ -112,7 +113,7 @@ public class EventExternalized<S, T> implements ResolvableTypeProvider {
* @see org.springframework.core.ResolvableTypeProvider#getResolvableType()
*/
@Override
public ResolvableType getResolvableType() {
public @NonNull ResolvableType getResolvableType() {
return type;
}

View File

@@ -17,7 +17,7 @@ package org.springframework.modulith.events;
import java.util.Objects;
import org.springframework.lang.Nullable;
import org.jspecify.annotations.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -238,14 +238,14 @@ public class RoutingTarget {
/**
* @return the target
*/
public String getTarget() {
public @Nullable String getTarget() {
return target;
}
/**
* @return the key
*/
public String getKey() {
public @Nullable String getKey() {
return key;
}

View File

@@ -1,5 +1,5 @@
/**
* API of the event publication registry abstraction.
*/
@org.springframework.lang.NonNullApi
@org.jspecify.annotations.NullMarked
package org.springframework.modulith.events;