Skip to content

Commit 4612544

Browse files
committed
Nullability refinements and related polishing
1 parent 2230742 commit 4612544

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

spring-core/src/main/java/org/springframework/core/MethodParameter.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,16 @@ public int getNestingLevel() {
300300
return this.nestingLevel;
301301
}
302302

303+
/**
304+
* Return a variant of this {@code MethodParameter} with the type
305+
* for the current level set to the specified value.
306+
* @param typeIndex the new type index
307+
* @since 5.2
308+
*/
309+
public MethodParameter withTypeIndex(int typeIndex) {
310+
return nested(this.nestingLevel, typeIndex);
311+
}
312+
303313
/**
304314
* Set the type index for the current nesting level.
305315
* @param typeIndex the corresponding type index
@@ -312,15 +322,6 @@ public void setTypeIndexForCurrentLevel(int typeIndex) {
312322
getTypeIndexesPerLevel().put(this.nestingLevel, typeIndex);
313323
}
314324

315-
/**
316-
* Return a variant of this {@code MethodParameter} with the type for the current
317-
* level set to the specified value.
318-
* @param typeIndex the new type index
319-
*/
320-
public MethodParameter withTypeIndex(int typeIndex) {
321-
return nested(this.nestingLevel, typeIndex);
322-
}
323-
324325
/**
325326
* Return the type index for the current nesting level.
326327
* @return the corresponding type index, or {@code null}
@@ -435,22 +436,39 @@ public MethodParameter nestedIfOptional() {
435436
return (getParameterType() == Optional.class ? nested() : this);
436437
}
437438

438-
public Class<?> getContainingClass() {
439-
Class<?> containingClass = this.containingClass;
440-
return (containingClass != null ? containingClass : getDeclaringClass());
439+
/**
440+
* Return a variant of this {@code MethodParameter} which refers to the
441+
* given containing class.
442+
* @param containingClass a specific containing class (potentially a
443+
* subclass of the declaring class, e.g. substituting a type variable)
444+
* @since 5.2
445+
* @see #getParameterType()
446+
*/
447+
public MethodParameter withContainingClass(@Nullable Class<?> containingClass) {
448+
MethodParameter result = clone();
449+
result.containingClass = containingClass;
450+
result.parameterType = null;
451+
return result;
441452
}
442453

454+
/**
455+
* Set a containing class to resolve the parameter type against.
456+
*/
443457
@Deprecated
444458
void setContainingClass(Class<?> containingClass) {
445459
this.containingClass = containingClass;
446460
this.parameterType = null;
447461
}
448462

449-
public MethodParameter withContainingClass(@Nullable Class<?> containingClass) {
450-
MethodParameter result = clone();
451-
result.containingClass = containingClass;
452-
result.parameterType = null;
453-
return result;
463+
/**
464+
* Return the containing class for this method parameter.
465+
* @return a specific containing class (potentially a subclass of the
466+
* declaring class), or otherwise simply the declaring class itself
467+
* @see #getDeclaringClass()
468+
*/
469+
public Class<?> getContainingClass() {
470+
Class<?> containingClass = this.containingClass;
471+
return (containingClass != null ? containingClass : getDeclaringClass());
454472
}
455473

456474
/**
@@ -470,7 +488,7 @@ public Class<?> getParameterType() {
470488
if (paramType != null) {
471489
return paramType;
472490
}
473-
if (getContainingClass() != null) {
491+
if (this.containingClass != null) {
474492
paramType = ResolvableType.forMethodParameter(this, null, 1, null).resolve();
475493
}
476494
if (paramType == null) {

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,18 @@ private static <C, R> R processClassInheritedAnnotations(C context, Class<?> sou
189189

190190
@Nullable
191191
private static <C, R> R processClassHierarchy(C context, Class<?> source,
192-
AnnotationsProcessor<C, R> processor,
193-
@Nullable BiPredicate<C, Class<?>> classFilter, boolean includeInterfaces,
194-
boolean includeEnclosing) {
192+
AnnotationsProcessor<C, R> processor, @Nullable BiPredicate<C, Class<?>> classFilter,
193+
boolean includeInterfaces, boolean includeEnclosing) {
195194

196-
int[] aggregateIndex = new int[] { 0 };
195+
int[] aggregateIndex = new int[] {0};
197196
return processClassHierarchy(context, aggregateIndex, source, processor,
198197
classFilter, includeInterfaces, includeEnclosing);
199198
}
200199

201200
@Nullable
202-
private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex,
203-
Class<?> source, AnnotationsProcessor<C, R> processor,
204-
@Nullable BiPredicate<C, Class<?>> classFilter, boolean includeInterfaces,
205-
boolean includeEnclosing) {
201+
private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, Class<?> source,
202+
AnnotationsProcessor<C, R> processor, @Nullable BiPredicate<C, Class<?>> classFilter,
203+
boolean includeInterfaces, boolean includeEnclosing) {
206204

207205
R result = processor.doWithAggregate(context, aggregateIndex[0]);
208206
if (result != null) {
@@ -229,17 +227,15 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex,
229227
Class<?> superclass = source.getSuperclass();
230228
if (superclass != Object.class && superclass != null) {
231229
R superclassResult = processClassHierarchy(context, aggregateIndex,
232-
superclass, processor, classFilter, includeInterfaces,
233-
includeEnclosing);
230+
superclass, processor, classFilter, includeInterfaces, includeEnclosing);
234231
if (superclassResult != null) {
235232
return superclassResult;
236233
}
237234
}
238235
Class<?> enclosingClass = source.getEnclosingClass();
239236
if (includeEnclosing && enclosingClass != null) {
240237
R enclosingResult = processClassHierarchy(context, aggregateIndex,
241-
enclosingClass, processor, classFilter, includeInterfaces,
242-
includeEnclosing);
238+
enclosingClass, processor, classFilter, includeInterfaces, true);
243239
if (enclosingResult != null) {
244240
return enclosingResult;
245241
}

0 commit comments

Comments
 (0)