Browse Source

Nullability refinements and related polishing

pull/23980/head
Juergen Hoeller 6 years ago
parent
commit
4612544505
  1. 54
      spring-core/src/main/java/org/springframework/core/MethodParameter.java
  2. 20
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java

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

@ -300,6 +300,16 @@ public class MethodParameter { @@ -300,6 +300,16 @@ public class MethodParameter {
return this.nestingLevel;
}
/**
* Return a variant of this {@code MethodParameter} with the type
* for the current level set to the specified value.
* @param typeIndex the new type index
* @since 5.2
*/
public MethodParameter withTypeIndex(int typeIndex) {
return nested(this.nestingLevel, typeIndex);
}
/**
* Set the type index for the current nesting level.
* @param typeIndex the corresponding type index
@ -312,15 +322,6 @@ public class MethodParameter { @@ -312,15 +322,6 @@ public class MethodParameter {
getTypeIndexesPerLevel().put(this.nestingLevel, typeIndex);
}
/**
* Return a variant of this {@code MethodParameter} with the type for the current
* level set to the specified value.
* @param typeIndex the new type index
*/
public MethodParameter withTypeIndex(int typeIndex) {
return nested(this.nestingLevel, typeIndex);
}
/**
* Return the type index for the current nesting level.
* @return the corresponding type index, or {@code null}
@ -435,22 +436,39 @@ public class MethodParameter { @@ -435,22 +436,39 @@ public class MethodParameter {
return (getParameterType() == Optional.class ? nested() : this);
}
public Class<?> getContainingClass() {
Class<?> containingClass = this.containingClass;
return (containingClass != null ? containingClass : getDeclaringClass());
/**
* Return a variant of this {@code MethodParameter} which refers to the
* given containing class.
* @param containingClass a specific containing class (potentially a
* subclass of the declaring class, e.g. substituting a type variable)
* @since 5.2
* @see #getParameterType()
*/
public MethodParameter withContainingClass(@Nullable Class<?> containingClass) {
MethodParameter result = clone();
result.containingClass = containingClass;
result.parameterType = null;
return result;
}
/**
* Set a containing class to resolve the parameter type against.
*/
@Deprecated
void setContainingClass(Class<?> containingClass) {
this.containingClass = containingClass;
this.parameterType = null;
}
public MethodParameter withContainingClass(@Nullable Class<?> containingClass) {
MethodParameter result = clone();
result.containingClass = containingClass;
result.parameterType = null;
return result;
/**
* Return the containing class for this method parameter.
* @return a specific containing class (potentially a subclass of the
* declaring class), or otherwise simply the declaring class itself
* @see #getDeclaringClass()
*/
public Class<?> getContainingClass() {
Class<?> containingClass = this.containingClass;
return (containingClass != null ? containingClass : getDeclaringClass());
}
/**
@ -470,7 +488,7 @@ public class MethodParameter { @@ -470,7 +488,7 @@ public class MethodParameter {
if (paramType != null) {
return paramType;
}
if (getContainingClass() != null) {
if (this.containingClass != null) {
paramType = ResolvableType.forMethodParameter(this, null, 1, null).resolve();
}
if (paramType == null) {

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

@ -189,20 +189,18 @@ abstract class AnnotationsScanner { @@ -189,20 +189,18 @@ abstract class AnnotationsScanner {
@Nullable
private static <C, R> R processClassHierarchy(C context, Class<?> source,
AnnotationsProcessor<C, R> processor,
@Nullable BiPredicate<C, Class<?>> classFilter, boolean includeInterfaces,
boolean includeEnclosing) {
AnnotationsProcessor<C, R> processor, @Nullable BiPredicate<C, Class<?>> classFilter,
boolean includeInterfaces, boolean includeEnclosing) {
int[] aggregateIndex = new int[] { 0 };
int[] aggregateIndex = new int[] {0};
return processClassHierarchy(context, aggregateIndex, source, processor,
classFilter, includeInterfaces, includeEnclosing);
}
@Nullable
private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex,
Class<?> source, AnnotationsProcessor<C, R> processor,
@Nullable BiPredicate<C, Class<?>> classFilter, boolean includeInterfaces,
boolean includeEnclosing) {
private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, Class<?> source,
AnnotationsProcessor<C, R> processor, @Nullable BiPredicate<C, Class<?>> classFilter,
boolean includeInterfaces, boolean includeEnclosing) {
R result = processor.doWithAggregate(context, aggregateIndex[0]);
if (result != null) {
@ -229,8 +227,7 @@ abstract class AnnotationsScanner { @@ -229,8 +227,7 @@ abstract class AnnotationsScanner {
Class<?> superclass = source.getSuperclass();
if (superclass != Object.class && superclass != null) {
R superclassResult = processClassHierarchy(context, aggregateIndex,
superclass, processor, classFilter, includeInterfaces,
includeEnclosing);
superclass, processor, classFilter, includeInterfaces, includeEnclosing);
if (superclassResult != null) {
return superclassResult;
}
@ -238,8 +235,7 @@ abstract class AnnotationsScanner { @@ -238,8 +235,7 @@ abstract class AnnotationsScanner {
Class<?> enclosingClass = source.getEnclosingClass();
if (includeEnclosing && enclosingClass != null) {
R enclosingResult = processClassHierarchy(context, aggregateIndex,
enclosingClass, processor, classFilter, includeInterfaces,
includeEnclosing);
enclosingClass, processor, classFilter, includeInterfaces, true);
if (enclosingResult != null) {
return enclosingResult;
}

Loading…
Cancel
Save