Skip to content

Commit 5e15338

Browse files
committed
Nullability refinements (no IntelliJ warnings, fewer null checks)
Includes consistent ignoring of all java.lang (meta-)annotations. Closes gh-22586
1 parent 75b5230 commit 5e15338

33 files changed

+439
-801
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AbstractMergedAnnotation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
* @since 5.2
3434
* @param <A> the annotation type
3535
*/
36-
abstract class AbstractMergedAnnotation<A extends Annotation>
37-
implements MergedAnnotation<A> {
36+
abstract class AbstractMergedAnnotation<A extends Annotation> implements MergedAnnotation<A> {
3837

3938
@Nullable
4039
private volatile A synthesizedAnnotation;

spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,13 +828,13 @@ private static <A extends Annotation> Comparator<MergedAnnotation<A>> highAggreg
828828
}
829829

830830
@Nullable
831-
private static AnnotationAttributes getAnnotationAttributes(
832-
MergedAnnotation<?> annotation, boolean classValuesAsString,
833-
boolean nestedAnnotationsAsMap) {
831+
private static AnnotationAttributes getAnnotationAttributes(MergedAnnotation<?> annotation,
832+
boolean classValuesAsString, boolean nestedAnnotationsAsMap) {
833+
834834
if (!annotation.isPresent()) {
835835
return null;
836836
}
837-
return annotation.asMap((mergedAnnotation) -> new AnnotationAttributes(),
837+
return annotation.asMap(mergedAnnotation -> new AnnotationAttributes(),
838838
MapValues.of(classValuesAsString, nestedAnnotationsAsMap));
839839
}
840840

spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,12 @@ protected Integer findOrder(Object obj) {
6868
return findOrderFromAnnotation(obj);
6969
}
7070

71+
@Nullable
7172
private Integer findOrderFromAnnotation(Object obj) {
72-
AnnotatedElement element = obj instanceof AnnotatedElement
73-
? (AnnotatedElement) obj
74-
: obj.getClass();
75-
MergedAnnotations annotations = MergedAnnotations.from(element,
76-
SearchStrategy.EXHAUSTIVE);
73+
AnnotatedElement element = (obj instanceof AnnotatedElement ? (AnnotatedElement) obj : obj.getClass());
74+
MergedAnnotations annotations = MergedAnnotations.from(element, SearchStrategy.EXHAUSTIVE);
7775
Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
78-
if (order == null && obj instanceof DecoratingProxy) {
76+
if (order == null && obj instanceof DecoratingProxy) {
7977
return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass());
8078
}
8179
return order;

spring-core/src/main/java/org/springframework/core/annotation/AnnotationFilter.java

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Arrays;
2121
import java.util.Collection;
2222

23-
import org.springframework.lang.Nullable;
2423
import org.springframework.util.Assert;
2524

2625
/**
@@ -37,31 +36,27 @@ public interface AnnotationFilter {
3736
* {@code java.lang.*} or in the
3837
* {@code org.springframework.lang.*} package.
3938
*/
40-
static final AnnotationFilter PLAIN = packages("java.lang",
41-
"org.springframework.lang");
39+
AnnotationFilter PLAIN = packages("java.lang", "org.springframework.lang");
4240

4341
/**
4442
* {@link AnnotationFilter} that matches annotations in the
4543
* {@code java.lang.*} package.
4644
*/
47-
static final AnnotationFilter JAVA = packages("java.lang");
45+
AnnotationFilter JAVA = packages("java.lang");
4846

4947
/**
5048
* {@link AnnotationFilter} that never matches and can be used when no
5149
* filtering is needed.
5250
*/
53-
static final AnnotationFilter NONE = new AnnotationFilter() {
54-
51+
AnnotationFilter NONE = new AnnotationFilter() {
5552
@Override
56-
public boolean matches(@Nullable String typeName) {
53+
public boolean matches(String typeName) {
5754
return false;
5855
}
59-
6056
@Override
6157
public String toString() {
6258
return "No annotation filtering";
6359
}
64-
6560
};
6661

6762

@@ -70,25 +65,26 @@ public String toString() {
7065
* @param annotation the annotation to test
7166
* @return {@code true} if the annotation matches
7267
*/
73-
default boolean matches(@Nullable Annotation annotation) {
74-
return matches(annotation != null ? annotation.annotationType() : null);
68+
default boolean matches(Annotation annotation) {
69+
return matches(annotation.annotationType());
7570
}
7671

7772
/**
7873
* Test if the given type matches the filter.
7974
* @param type the annotation type to test
8075
* @return {@code true} if the annotation matches
8176
*/
82-
default boolean matches(@Nullable Class<?> type) {
83-
return matches(type != null ? type.getName() : null);
77+
default boolean matches(Class<?> type) {
78+
return matches(type.getName());
8479
}
8580

8681
/**
8782
* Test if the given type name matches the filter.
8883
* @param typeName the annotation type to test
8984
* @return {@code true} if the annotation matches
9085
*/
91-
boolean matches(@Nullable String typeName);
86+
boolean matches(String typeName);
87+
9288

9389
/**
9490
* Return a new {@link AnnotationFilter} that matches annotations in the
@@ -107,8 +103,8 @@ static AnnotationFilter packages(String... packages) {
107103
* @param annotationType the annotation type to check
108104
* @return the most appropriate annotation filter
109105
*/
110-
static AnnotationFilter mostAppropriateFor(@Nullable Class<?> annotationType) {
111-
return PLAIN.matches(annotationType) ? NONE : PLAIN;
106+
static AnnotationFilter mostAppropriateFor(Class<?> annotationType) {
107+
return (PLAIN.matches(annotationType) ? NONE : PLAIN);
112108
}
113109

114110
/**
@@ -119,30 +115,6 @@ static AnnotationFilter mostAppropriateFor(@Nullable Class<?> annotationType) {
119115
* @return the most appropriate annotation filter
120116
*/
121117
static AnnotationFilter mostAppropriateFor(Class<?>... annotationTypes) {
122-
Assert.notNull(annotationTypes, "AnnotationTypes must not be null");
123-
return mostAppropriateFor(Arrays.asList(annotationTypes));
124-
}
125-
126-
/**
127-
* Return an {@link AnnotationFilter} that is the most appropriate for, and
128-
* will always match all the given annotation type. Whenever possible,
129-
* {@link AnnotationFilter#PLAIN} will be returned.
130-
* @param annotationType the annotation type to check
131-
* @return the most appropriate annotation filter
132-
*/
133-
static AnnotationFilter mostAppropriateFor(@Nullable String annotationType) {
134-
return PLAIN.matches(annotationType) ? NONE : PLAIN;
135-
}
136-
137-
/**
138-
* Return an {@link AnnotationFilter} that is the most appropriate for, and
139-
* will always match all the given annotation types. Whenever possible,
140-
* {@link AnnotationFilter#PLAIN} will be returned.
141-
* @param annotationTypes the annotation types to check
142-
* @return the most appropriate annotation filter
143-
*/
144-
static AnnotationFilter mostAppropriateFor(String... annotationTypes) {
145-
Assert.notNull(annotationTypes, "AnnotationTypes must not be null");
146118
return mostAppropriateFor(Arrays.asList(annotationTypes));
147119
}
148120

@@ -156,20 +128,16 @@ static AnnotationFilter mostAppropriateFor(String... annotationTypes) {
156128
*/
157129
@SuppressWarnings("unchecked")
158130
static AnnotationFilter mostAppropriateFor(Collection<?> annotationTypes) {
159-
Assert.notNull(annotationTypes, "AnnotationTypes must not be null");
160131
for (Object annotationType : annotationTypes) {
161132
if (annotationType == null) {
162133
continue;
163134
}
164-
Assert.isTrue(
165-
annotationType instanceof Class || annotationType instanceof String,
135+
Assert.isTrue(annotationType instanceof Class || annotationType instanceof String,
166136
"AnnotationType must be a Class or String");
167-
if (annotationType instanceof Class
168-
&& PLAIN.matches((Class<Annotation>) annotationType)) {
137+
if (annotationType instanceof Class && PLAIN.matches((Class<Annotation>) annotationType)) {
169138
return NONE;
170139
}
171-
if (annotationType instanceof String
172-
&& PLAIN.matches((String) annotationType)) {
140+
if (annotationType instanceof String && PLAIN.matches((String) annotationType)) {
173141
return NONE;
174142
}
175143
}

spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* @since 5.2
4646
* @see AnnotationTypeMappings
4747
*/
48-
class AnnotationTypeMapping {
48+
final class AnnotationTypeMapping {
4949

5050
@Nullable
5151
private final AnnotationTypeMapping parent;

spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMappings.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Map;
2525

2626
import org.springframework.lang.Nullable;
27-
import org.springframework.util.Assert;
2827
import org.springframework.util.ConcurrentReferenceHashMap;
2928

3029
/**
@@ -75,11 +74,9 @@ private void addAllMappings(Class<? extends Annotation> annotationType) {
7574
}
7675
}
7776

78-
private void addMetaAnnotationsToQueue(Deque<AnnotationTypeMapping> queue,
79-
AnnotationTypeMapping parent) {
80-
81-
Annotation[] metaAnnotations = AnnotationsScanner.getDeclaredAnnotations(
82-
parent.getAnnotationType(), false);
77+
private void addMetaAnnotationsToQueue(Deque<AnnotationTypeMapping> queue, AnnotationTypeMapping parent) {
78+
Annotation[] metaAnnotations =
79+
AnnotationsScanner.getDeclaredAnnotations(parent.getAnnotationType(), false);
8380
for (Annotation metaAnnotation : metaAnnotations) {
8481
if (!isMappable(parent, metaAnnotation)) {
8582
continue;
@@ -125,19 +122,17 @@ private void addIfPossible(Deque<AnnotationTypeMapping> queue,
125122
}
126123
}
127124

128-
private boolean isMappable(AnnotationTypeMapping parent, Annotation metaAnnotation) {
129-
return !this.filter.matches(metaAnnotation) &&
125+
private boolean isMappable(AnnotationTypeMapping parent, @Nullable Annotation metaAnnotation) {
126+
return (metaAnnotation != null && !this.filter.matches(metaAnnotation) &&
130127
!AnnotationFilter.PLAIN.matches(parent.getAnnotationType()) &&
131-
!isAlreadyMapped(parent, metaAnnotation);
128+
!isAlreadyMapped(parent, metaAnnotation));
132129
}
133130

134-
private boolean isAlreadyMapped(AnnotationTypeMapping parent,
135-
Annotation metaAnnotation) {
136-
131+
private boolean isAlreadyMapped(AnnotationTypeMapping parent, Annotation metaAnnotation) {
137132
Class<? extends Annotation> annotationType = metaAnnotation.annotationType();
138133
AnnotationTypeMapping mapping = parent;
139134
while (mapping != null) {
140-
if (mapping.getAnnotationType().equals(annotationType)) {
135+
if (mapping.getAnnotationType() == annotationType) {
141136
return true;
142137
}
143138
mapping = mapping.getParent();
@@ -171,11 +166,8 @@ AnnotationTypeMapping get(int index) {
171166
* @param annotationType the source annotation type
172167
* @return type mappings for the annotation type
173168
*/
174-
static AnnotationTypeMappings forAnnotationType(
175-
Class<? extends Annotation> annotationType) {
176-
177-
return forAnnotationType(annotationType,
178-
AnnotationFilter.mostAppropriateFor(annotationType));
169+
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType) {
170+
return forAnnotationType(annotationType, AnnotationFilter.mostAppropriateFor(annotationType));
179171
}
180172

181173
/**
@@ -186,11 +178,8 @@ static AnnotationTypeMappings forAnnotationType(
186178
* @return type mappings for the annotation type
187179
*/
188180
static AnnotationTypeMappings forAnnotationType(
189-
Class<? extends Annotation> annotationType,
190-
AnnotationFilter annotationFilter) {
181+
Class<? extends Annotation> annotationType, AnnotationFilter annotationFilter) {
191182

192-
Assert.notNull(annotationType, "AnnotationType must not be null");
193-
Assert.notNull(annotationFilter, "AnnotationFilter must not be null");
194183
return cache.computeIfAbsent(annotationFilter, Cache::new).get(annotationType);
195184
}
196185

0 commit comments

Comments
 (0)