Skip to content

Commit

Permalink
Specify generic type nullness in spring-core
Browse files Browse the repository at this point in the history
Also in spring-core-test.

See spring-projectsgh-34140
  • Loading branch information
sdeleuze committed Jan 14, 2025
1 parent 435cb0c commit 9d9383a
Show file tree
Hide file tree
Showing 31 changed files with 160 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -37,7 +37,7 @@ final class CompileWithForkedClassLoaderClassLoader extends ClassLoader {

private final ClassLoader testClassLoader;

private Function<String, byte[]> classResourceLookup = name -> null;
private Function<String, byte @Nullable []> classResourceLookup = name -> null;


public CompileWithForkedClassLoaderClassLoader(ClassLoader testClassLoader) {
Expand All @@ -48,7 +48,7 @@ public CompileWithForkedClassLoaderClassLoader(ClassLoader testClassLoader) {

// Invoked reflectively by DynamicClassLoader
@SuppressWarnings("unused")
void setClassResourceLookup(Function<String, byte[]> classResourceLookup) {
void setClassResourceLookup(Function<String, byte @Nullable []> classResourceLookup) {
this.classResourceLookup = classResourceLookup;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -71,7 +71,7 @@ public DynamicClassLoader(ClassLoader parent, ClassFiles classFiles, ResourceFil
"setClassResourceLookup", Function.class);
ReflectionUtils.makeAccessible(setClassResourceLookupMethod);
ReflectionUtils.invokeMethod(setClassResourceLookupMethod,
getParent(), (Function<String, byte[]>) this::findClassBytes);
getParent(), (Function<String, byte @Nullable []>) this::findClassBytes);
this.defineClassMethod = lookupMethod(parentClass,
"defineDynamicClass", String.class, byte[].class, int.class, int.class);
ReflectionUtils.makeAccessible(this.defineClassMethod);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -106,7 +106,7 @@ static ArgumentCodeGenerator of(Class<?> argumentType, String argumentCode) {
* @param function the resolver function
* @return a new {@link ArgumentCodeGenerator} instance backed by the function
*/
static ArgumentCodeGenerator from(Function<TypeName, CodeBlock> function) {
static ArgumentCodeGenerator from(Function<TypeName, @Nullable CodeBlock> function) {
return function::apply;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ public void initParameterNameDiscovery(@Nullable ParameterNameDiscoverer paramet
}
ParameterNameDiscoverer discoverer = this.parameterNameDiscoverer;
if (discoverer != null) {
String[] parameterNames = null;
@Nullable String[] parameterNames = null;
if (this.executable instanceof Method method) {
parameterNames = discoverer.getParameterNames(method);
}
Expand Down Expand Up @@ -865,7 +865,7 @@ private static int validateIndex(Executable executable, int parameterIndex) {
* @return the corresponding MethodParameter instance
* @since 6.1
*/
public static MethodParameter forFieldAwareConstructor(Constructor<?> ctor, int parameterIndex, String fieldName) {
public static MethodParameter forFieldAwareConstructor(Constructor<?> ctor, int parameterIndex, @Nullable String fieldName) {
return new FieldAwareConstructorParameter(ctor, parameterIndex, fieldName);
}

Expand All @@ -877,7 +877,7 @@ private static class FieldAwareConstructorParameter extends MethodParameter {

private volatile Annotation @Nullable [] combinedAnnotations;

public FieldAwareConstructorParameter(Constructor<?> constructor, int parameterIndex, String fieldName) {
public FieldAwareConstructorParameter(Constructor<?> constructor, int parameterIndex, @Nullable String fieldName) {
super(constructor, parameterIndex);
this.parameterName = fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand All @@ -19,12 +19,15 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Flow;
import java.util.function.Function;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import org.jspecify.annotations.Nullable;
import org.reactivestreams.FlowAdapters;
import org.reactivestreams.Publisher;
Expand Down Expand Up @@ -380,13 +383,13 @@ void registerAdapters(ReactiveAdapterRegistry registry) {
io.smallrye.mutiny.groups.MultiCreate.class, "publisher", Flow.Publisher.class);
registry.registerReactiveType(uniDesc,
uni -> FlowAdapters.toPublisher((Flow.Publisher<Object>)
ReflectionUtils.invokeMethod(uniToPublisher, ((io.smallrye.mutiny.Uni<?>) uni).convert())),
publisher -> ReflectionUtils.invokeMethod(uniPublisher, io.smallrye.mutiny.Uni.createFrom(),
FlowAdapters.toFlowPublisher(publisher)));
Objects.requireNonNull(ReflectionUtils.invokeMethod(uniToPublisher, ((Uni<?>) uni).convert()))),
publisher -> Objects.requireNonNull(ReflectionUtils.invokeMethod(uniPublisher, Uni.createFrom(),
FlowAdapters.toFlowPublisher(publisher))));
registry.registerReactiveType(multiDesc,
multi -> FlowAdapters.toPublisher((Flow.Publisher<Object>) multi),
publisher -> ReflectionUtils.invokeMethod(multiPublisher, io.smallrye.mutiny.Multi.createFrom(),
FlowAdapters.toFlowPublisher(publisher)));
publisher -> Objects.requireNonNull(ReflectionUtils.invokeMethod(multiPublisher, Multi.createFrom(),
FlowAdapters.toFlowPublisher(publisher))));
}
else {
// Mutiny 1 based on Reactive Streams
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -825,9 +825,9 @@ else if (this.type instanceof ParameterizedType parameterizedType) {
* @see #getGenerics()
* @see #resolve()
*/
public Class<?>[] resolveGenerics() {
public @Nullable Class<?>[] resolveGenerics() {
ResolvableType[] generics = getGenerics();
Class<?>[] resolvedGenerics = new Class<?>[generics.length];
@Nullable Class<?>[] resolvedGenerics = new Class<?>[generics.length];
for (int i = 0; i < generics.length; i++) {
resolvedGenerics[i] = generics[i].resolve();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -205,7 +205,7 @@ else if (Type[].class == method.getReturnType() && ObjectUtils.isEmpty(args)) {
if (returnValue == null) {
return null;
}
Type[] result = new Type[((Type[]) returnValue).length];
@Nullable Type[] result = new Type[((Type[]) returnValue).length];
for (int i = 0; i < result.length; i++) {
result[i] = forTypeProvider(new MethodInvokeTypeProvider(this.provider, method, i));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -478,7 +478,7 @@ public static <A extends Annotation> Set<A> getMergedRepeatableAnnotations(
* attributes from all annotations found, or {@code null} if not found
* @see #getAllAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
*/
public static @Nullable MultiValueMap<String, Object> getAllAnnotationAttributes(
public static MultiValueMap<String, @Nullable Object> getAllAnnotationAttributes(
AnnotatedElement element, String annotationName) {

return getAllAnnotationAttributes(element, annotationName, false, false);
Expand All @@ -502,7 +502,7 @@ public static <A extends Annotation> Set<A> getMergedRepeatableAnnotations(
* @return a {@link MultiValueMap} keyed by attribute name, containing the annotation
* attributes from all annotations found, or {@code null} if not found
*/
public static @Nullable MultiValueMap<String, Object> getAllAnnotationAttributes(AnnotatedElement element,
public static MultiValueMap<String, @Nullable Object> getAllAnnotationAttributes(AnnotatedElement element,
String annotationName, final boolean classValuesAsString, final boolean nestedAnnotationsAsMap) {

Adapt[] adaptations = Adapt.values(classValuesAsString, nestedAnnotationsAsMap);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -45,7 +45,7 @@
* @see AnnotatedElementUtils
*/
@SuppressWarnings("serial")
public class AnnotationAttributes extends LinkedHashMap<String, Object> {
public class AnnotationAttributes extends LinkedHashMap<String, @Nullable Object> {

private static final String UNKNOWN = "unknown";

Expand Down Expand Up @@ -83,7 +83,7 @@ public AnnotationAttributes(int initialCapacity) {
* @param map original source of annotation attribute <em>key-value</em> pairs
* @see #fromMap(Map)
*/
public AnnotationAttributes(Map<String, Object> map) {
public AnnotationAttributes(Map<String, @Nullable Object> map) {
super(map);
this.annotationType = null;
this.displayName = UNKNOWN;
Expand Down Expand Up @@ -376,10 +376,10 @@ private <T> T getRequiredAttribute(String attributeName, Class<T> expectedType)

@Override
public String toString() {
Iterator<Map.Entry<String, Object>> entries = entrySet().iterator();
Iterator<Map.Entry<String, @Nullable Object>> entries = entrySet().iterator();
StringBuilder sb = new StringBuilder("{");
while (entries.hasNext()) {
Map.Entry<String, Object> entry = entries.next();
Map.Entry<String, @Nullable Object> entry = entries.next();
sb.append(entry.getKey());
sb.append('=');
sb.append(valueToString(entry.getValue()));
Expand All @@ -391,7 +391,7 @@ public String toString() {
return sb.toString();
}

private String valueToString(Object value) {
private String valueToString(@Nullable Object value) {
if (value == this) {
return "(this Map)";
}
Expand All @@ -410,7 +410,7 @@ private String valueToString(Object value) {
* to the {@link #AnnotationAttributes(Map)} constructor.
* @param map original source of annotation attribute <em>key-value</em> pairs
*/
public static @Nullable AnnotationAttributes fromMap(@Nullable Map<String, Object> map) {
public static @Nullable AnnotationAttributes fromMap(@Nullable Map<String, @Nullable Object> map) {
if (map == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -27,6 +27,7 @@

import org.jspecify.annotations.Nullable;

import org.springframework.lang.Contract;
import org.springframework.util.ConcurrentReferenceHashMap;

/**
Expand Down Expand Up @@ -87,7 +88,7 @@ private void addAllMappings(Class<? extends Annotation> annotationType,
}

private void addMetaAnnotationsToQueue(Deque<AnnotationTypeMapping> queue, AnnotationTypeMapping source) {
Annotation[] metaAnnotations = AnnotationsScanner.getDeclaredAnnotations(source.getAnnotationType(), false);
@Nullable Annotation[] metaAnnotations = AnnotationsScanner.getDeclaredAnnotations(source.getAnnotationType(), false);
for (Annotation metaAnnotation : metaAnnotations) {
if (!isMappable(source, metaAnnotation)) {
continue;
Expand Down Expand Up @@ -127,6 +128,7 @@ private void addIfPossible(Deque<AnnotationTypeMapping> queue, @Nullable Annotat
}
}

@Contract("_, null -> false")
private boolean isMappable(AnnotationTypeMapping source, @Nullable Annotation metaAnnotation) {
return (metaAnnotation != null && !this.filter.matches(metaAnnotation) &&
!AnnotationFilter.PLAIN.matches(source.getAnnotationType()) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -773,7 +773,7 @@ public static void validateAnnotation(Annotation annotation) {
* @see #getAnnotationAttributes(Annotation, boolean, boolean)
* @see #getAnnotationAttributes(AnnotatedElement, Annotation, boolean, boolean)
*/
public static Map<String, Object> getAnnotationAttributes(Annotation annotation) {
public static Map<String, @Nullable Object> getAnnotationAttributes(Annotation annotation) {
return getAnnotationAttributes(null, annotation);
}

Expand All @@ -791,7 +791,7 @@ public static Map<String, Object> getAnnotationAttributes(Annotation annotation)
* corresponding attribute values as values (never {@code null})
* @see #getAnnotationAttributes(Annotation, boolean, boolean)
*/
public static Map<String, Object> getAnnotationAttributes(
public static Map<String, @Nullable Object> getAnnotationAttributes(
Annotation annotation, boolean classValuesAsString) {

return getAnnotationAttributes(annotation, classValuesAsString, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -54,7 +54,7 @@ interface AnnotationsProcessor<C, R> {
* {@code null} elements)
* @return a {@code non-null} result if no further processing is required
*/
@Nullable R doWithAnnotations(C context, int aggregateIndex, @Nullable Object source, Annotation[] annotations);
@Nullable R doWithAnnotations(C context, int aggregateIndex, @Nullable Object source, @Nullable Annotation[] annotations);

/**
* Get the final result to be returned. By default this method returns
Expand Down
Loading

0 comments on commit 9d9383a

Please sign in to comment.