Skip to content

Commit c5b3848

Browse files
committed
Nullability refinements and related polishing
Includes fix for typo in visitor class names. See gh-22909
1 parent 7031964 commit c5b3848

16 files changed

+126
-109
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ private static Map<String, DefaultValueHolder> computeDefaultValues(
898898
for (int i = 0; i < methods.size(); i++) {
899899
Method method = methods.get(i);
900900
Object defaultValue = method.getDefaultValue();
901-
if(defaultValue != null) {
901+
if (defaultValue != null) {
902902
result.put(method.getName(), new DefaultValueHolder(defaultValue));
903903
}
904904
}

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ final class MergedAnnotationsCollection implements MergedAnnotations {
4343

4444
private final AnnotationTypeMappings[] mappings;
4545

46+
4647
private MergedAnnotationsCollection(Collection<MergedAnnotation<?>> annotations) {
4748
Assert.notNull(annotations, "Annotations must not be null");
4849
this.annotations = annotations.toArray(new MergedAnnotation<?>[0]);
@@ -52,11 +53,11 @@ private MergedAnnotationsCollection(Collection<MergedAnnotation<?>> annotations)
5253
Assert.notNull(annotation, "Annotation must not be null");
5354
Assert.isTrue(annotation.isDirectlyPresent(), "Annotation must be directly present");
5455
Assert.isTrue(annotation.getAggregateIndex() == 0, "Annotation must have aggregate index of zero");
55-
this.mappings[i] = AnnotationTypeMappings.forAnnotationType(
56-
annotation.getType());
56+
this.mappings[i] = AnnotationTypeMappings.forAnnotationType(annotation.getType());
5757
}
5858
}
5959

60+
6061
@Override
6162
public Iterator<MergedAnnotation<Annotation>> iterator() {
6263
return Spliterators.iterator(spliterator());
@@ -67,8 +68,7 @@ public Spliterator<MergedAnnotation<Annotation>> spliterator() {
6768
return spliterator(null);
6869
}
6970

70-
private <A extends Annotation> Spliterator<MergedAnnotation<A>> spliterator(
71-
@Nullable Object annotationType) {
71+
private <A extends Annotation> Spliterator<MergedAnnotation<A>> spliterator(@Nullable Object annotationType) {
7272
return new AnnotationsSpliterator<>(annotationType);
7373
}
7474

@@ -128,6 +128,7 @@ public <A extends Annotation> MergedAnnotation<A> get(Class<A> annotationType,
128128
public <A extends Annotation> MergedAnnotation<A> get(Class<A> annotationType,
129129
@Nullable Predicate<? super MergedAnnotation<A>> predicate,
130130
@Nullable MergedAnnotationSelector<A> selector) {
131+
131132
MergedAnnotation<A> result = find(annotationType, predicate, selector);
132133
return (result != null ? result : MergedAnnotation.missing());
133134
}
@@ -154,12 +155,15 @@ public <A extends Annotation> MergedAnnotation<A> get(String annotationType,
154155
}
155156

156157
@SuppressWarnings("unchecked")
158+
@Nullable
157159
private <A extends Annotation> MergedAnnotation<A> find(Object requiredType,
158-
Predicate<? super MergedAnnotation<A>> predicate,
159-
MergedAnnotationSelector<A> selector) {
160+
@Nullable Predicate<? super MergedAnnotation<A>> predicate,
161+
@Nullable MergedAnnotationSelector<A> selector) {
162+
160163
if (selector == null) {
161164
selector = MergedAnnotationSelectors.nearest();
162165
}
166+
163167
MergedAnnotation<A> result = null;
164168
for (int i = 0; i < this.annotations.length; i++) {
165169
MergedAnnotation<?> root = this.annotations[i];
@@ -208,7 +212,7 @@ private static boolean isMappingForType(AnnotationTypeMapping mapping, @Nullable
208212

209213
static MergedAnnotations of(Collection<MergedAnnotation<?>> annotations) {
210214
Assert.notNull(annotations, "Annotations must not be null");
211-
if(annotations.isEmpty()) {
215+
if (annotations.isEmpty()) {
212216
return TypeMappedAnnotations.NONE;
213217
}
214218
return new MergedAnnotationsCollection(annotations);
@@ -242,7 +246,8 @@ public boolean tryAdvance(Consumer<? super MergedAnnotation<A>> action) {
242246
}
243247
}
244248
if (annotationResult != -1) {
245-
MergedAnnotation<A> mergedAnnotation = createMergedAnnotationIfPossible(annotationResult, this.mappingCursors[annotationResult]);
249+
MergedAnnotation<A> mergedAnnotation = createMergedAnnotationIfPossible(
250+
annotationResult, this.mappingCursors[annotationResult]);
246251
this.mappingCursors[annotationResult]++;
247252
if (mergedAnnotation == null) {
248253
return tryAdvance(action);
@@ -275,20 +280,19 @@ private AnnotationTypeMapping getMapping(int annotationIndex, int mappingIndex)
275280

276281
@Nullable
277282
@SuppressWarnings("unchecked")
278-
private MergedAnnotation<A> createMergedAnnotationIfPossible(
279-
int annotationIndex, int mappingIndex) {
283+
private MergedAnnotation<A> createMergedAnnotationIfPossible(int annotationIndex, int mappingIndex) {
280284
MergedAnnotation<?> root = annotations[annotationIndex];
281-
if(mappingIndex == 0) {
285+
if (mappingIndex == 0) {
282286
return (MergedAnnotation<A>) root;
283287
}
284-
IntrospectionFailureLogger logger = (this.requiredType != null
285-
? IntrospectionFailureLogger.INFO
286-
: IntrospectionFailureLogger.DEBUG);
288+
IntrospectionFailureLogger logger = (this.requiredType != null ?
289+
IntrospectionFailureLogger.INFO : IntrospectionFailureLogger.DEBUG);
287290
return TypeMappedAnnotation.createIfPossible(
288291
mappings[annotationIndex].get(mappingIndex), root, logger);
289292
}
290293

291294
@Override
295+
@Nullable
292296
public Spliterator<MergedAnnotation<A>> trySplit() {
293297
return null;
294298
}
@@ -309,7 +313,6 @@ public long estimateSize() {
309313
public int characteristics() {
310314
return NONNULL | IMMUTABLE;
311315
}
312-
313316
}
314317

315318
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.reflect.Array;
2121
import java.lang.reflect.Member;
2222
import java.lang.reflect.Method;
23-
import java.util.Arrays;
2423
import java.util.Collections;
2524
import java.util.HashMap;
2625
import java.util.LinkedHashMap;
@@ -71,8 +70,6 @@
7170
*/
7271
final class TypeMappedAnnotation<A extends Annotation> extends AbstractMergedAnnotation<A> {
7372

74-
private static final Object[] EMPTY_OBJECT_ARRAY = {};
75-
7673
private static final Map<Class<?>, Object> EMPTY_ARRAYS;
7774
static {
7875
Map<Class<?>, Object> emptyArrays = new HashMap<>();
@@ -386,7 +383,7 @@ protected <T> T getAttributeValue(String attributeName, Class<T> type) {
386383
return (attributeIndex != -1 ? getValue(attributeIndex, type) : null);
387384
}
388385

389-
private final Object getRequiredValue(int attributeIndex, String attributeName) {
386+
private Object getRequiredValue(int attributeIndex, String attributeName) {
390387
Object value = getValue(attributeIndex, Object.class);
391388
if (value == null) {
392389
throw new NoSuchElementException("No element at attribute index "
@@ -527,7 +524,7 @@ private Object adaptForAttribute(Method attribute, Object value) {
527524
(attributeType == String[].class && value instanceof Class[])) {
528525
return value;
529526
}
530-
if(attributeType.isArray() && isEmptyObjectArray(value)) {
527+
if (attributeType.isArray() && isEmptyObjectArray(value)) {
531528
return emptyArray(attributeType.getComponentType());
532529
}
533530
if (!attributeType.isInstance(value)) {
@@ -540,7 +537,7 @@ private Object adaptForAttribute(Method attribute, Object value) {
540537
}
541538

542539
private boolean isEmptyObjectArray(Object value) {
543-
return value instanceof Object[] && Arrays.equals((Object[]) value, EMPTY_OBJECT_ARRAY);
540+
return (value instanceof Object[] && ((Object[]) value).length == 0);
544541
}
545542

546543
private Object emptyArray(Class<?> componentType) {
@@ -556,7 +553,8 @@ private MergedAnnotation<?> adaptToMergedAnnotation(Object value, Class<? extend
556553
return (MergedAnnotation<?>) value;
557554
}
558555
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(annotationType).get(0);
559-
return new TypeMappedAnnotation<>(mapping, null, this.source, value, getValueExtractor(value), this.aggregateIndex);
556+
return new TypeMappedAnnotation<>(
557+
mapping, null, this.source, value, getValueExtractor(value), this.aggregateIndex);
560558
}
561559

562560
private BiFunction<Method, Object, Object> getValueExtractor(Object value) {
@@ -659,8 +657,8 @@ static <A extends Annotation> TypeMappedAnnotation<A> createIfPossible(
659657
}
660658

661659
@Nullable
662-
static <A extends Annotation> TypeMappedAnnotation<A> createIfPossible(
663-
AnnotationTypeMapping mapping, @Nullable Object source, Object rootAttribute,
660+
private static <A extends Annotation> TypeMappedAnnotation<A> createIfPossible(
661+
AnnotationTypeMapping mapping, @Nullable Object source, @Nullable Object rootAttribute,
664662
BiFunction<Method, Object, Object> valueExtractor,
665663
int aggregateIndex, IntrospectionFailureLogger logger) {
666664

spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
5151

5252
private final boolean nestedAnnotationsAsMap;
5353

54+
@Nullable
5455
private Set<String> annotationTypes;
5556

57+
5658
/**
5759
* Create a new {@code StandardAnnotationMetadata} wrapper for the given Class.
5860
* @param introspectedClass the Class to introspect
@@ -109,22 +111,20 @@ public Set<String> getAnnotationTypes() {
109111
@Nullable
110112
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
111113
if (this.nestedAnnotationsAsMap) {
112-
return AnnotationMetadata.super.getAnnotationAttributes(annotationName,
113-
classValuesAsString);
114+
return AnnotationMetadata.super.getAnnotationAttributes(annotationName, classValuesAsString);
114115
}
115116
return AnnotatedElementUtils.getMergedAnnotationAttributes(
116-
getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
117+
getIntrospectedClass(), annotationName, classValuesAsString, false);
117118
}
118119

119120
@Override
120121
@Nullable
121122
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
122123
if (this.nestedAnnotationsAsMap) {
123-
return AnnotationMetadata.super.getAllAnnotationAttributes(annotationName,
124-
classValuesAsString);
124+
return AnnotationMetadata.super.getAllAnnotationAttributes(annotationName, classValuesAsString);
125125
}
126126
return AnnotatedElementUtils.getAllAnnotationAttributes(
127-
getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
127+
getIntrospectedClass(), annotationName, classValuesAsString, false);
128128
}
129129

130130
@Override

spring-core/src/main/java/org/springframework/core/type/StandardMethodMetadata.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public Map<String, Object> getAnnotationAttributes(String annotationName, boolea
138138
return MethodMetadata.super.getAnnotationAttributes(annotationName, classValuesAsString);
139139
}
140140
return AnnotatedElementUtils.getMergedAnnotationAttributes(this.introspectedMethod,
141-
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
141+
annotationName, classValuesAsString, false);
142142
}
143143

144144
@Override
@@ -148,7 +148,7 @@ public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotatio
148148
return MethodMetadata.super.getAllAnnotationAttributes(annotationName, classValuesAsString);
149149
}
150150
return AnnotatedElementUtils.getAllAnnotationAttributes(this.introspectedMethod,
151-
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
151+
annotationName, classValuesAsString, false);
152152
}
153153

154154
}

spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
9191
}
9292

9393
@Override
94+
@Nullable
9495
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
9596
if (!visible) {
9697
return null;

spring-core/src/main/java/org/springframework/core/type/classreading/ClassMetadataReadingVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public void visitSource(String source, String debug) {
118118
}
119119

120120
@Override
121+
@Nullable
121122
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
122123
// no-op
123124
return new EmptyAnnotationVisitor();

0 commit comments

Comments
 (0)