Nullability refinements (based on IntelliJ IDEA 2018.1 introspection)
Issue: SPR-15756
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -209,6 +209,7 @@ public class SimpleNamingContext implements Context {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Object addToEnvironment(String propName, Object propVal) {
|
||||
return this.environment.put(propName, propVal);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Executable;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -41,23 +40,21 @@ import org.springframework.util.ClassUtils;
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 5.0
|
||||
* @see #isAutowirable(Parameter)
|
||||
* @see #resolveDependency(Parameter, Class, ApplicationContext)
|
||||
* @see #isAutowirable
|
||||
* @see #resolveDependency
|
||||
*/
|
||||
abstract class ParameterAutowireUtils {
|
||||
|
||||
private static final AnnotatedElement EMPTY_ANNOTATED_ELEMENT = new AnnotatedElement() {
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation[] getAnnotations() {
|
||||
return new Annotation[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation[] getDeclaredAnnotations() {
|
||||
return new Annotation[0];
|
||||
@@ -65,11 +62,6 @@ abstract class ParameterAutowireUtils {
|
||||
};
|
||||
|
||||
|
||||
|
||||
private ParameterAutowireUtils() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the supplied {@link Parameter} can potentially be
|
||||
* autowired from an {@link ApplicationContext}.
|
||||
@@ -79,16 +71,16 @@ abstract class ParameterAutowireUtils {
|
||||
* {@link Qualifier @Qualifier}, or {@link Value @Value}.
|
||||
* @param parameter the parameter whose dependency should be autowired
|
||||
* @param parameterIndex the index of the parameter
|
||||
* @see #resolveDependency(Parameter, Class, ApplicationContext)
|
||||
* @see #resolveDependency
|
||||
*/
|
||||
static boolean isAutowirable(Parameter parameter, int parameterIndex) {
|
||||
if (ApplicationContext.class.isAssignableFrom(parameter.getType())) {
|
||||
return true;
|
||||
}
|
||||
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
|
||||
return AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class)
|
||||
|| AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class)
|
||||
|| AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class);
|
||||
return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
|
||||
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
|
||||
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,62 +105,54 @@ abstract class ParameterAutowireUtils {
|
||||
* dependency
|
||||
* @return the resolved object, or {@code null} if none found
|
||||
* @throws BeansException if dependency resolution failed
|
||||
* @see #isAutowirable(Parameter)
|
||||
* @see #isAutowirable
|
||||
* @see Autowired#required
|
||||
* @see SynthesizingMethodParameter#forParameter(Parameter)
|
||||
* @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
|
||||
*/
|
||||
@Nullable
|
||||
static Object resolveDependency(Parameter parameter, int parameterIndex, Class<?> containingClass, ApplicationContext applicationContext) {
|
||||
static Object resolveDependency(
|
||||
Parameter parameter, int parameterIndex, Class<?> containingClass, ApplicationContext applicationContext) {
|
||||
|
||||
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
|
||||
boolean required = findMergedAnnotation(annotatedParameter, Autowired.class).map(Autowired::required).orElse(true);
|
||||
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
|
||||
boolean required = (autowired == null || autowired.required());
|
||||
|
||||
MethodParameter methodParameter = SynthesizingMethodParameter.forParameter(parameter);
|
||||
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
|
||||
descriptor.setContainingClass(containingClass);
|
||||
return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);
|
||||
}
|
||||
|
||||
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
|
||||
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
|
||||
}
|
||||
|
||||
/**
|
||||
* Due to a bug in {@code javac} on JDK versions prior to JDK 9, looking up
|
||||
* annotations directly on a {@link Parameter} will fail for inner class
|
||||
* constructors.
|
||||
*
|
||||
* <h4>Bug in javac in JDK < 9</h4>
|
||||
* <p>The parameter annotations array in the compiled byte code excludes an entry
|
||||
* for the implicit <em>enclosing instance</em> parameter for an inner class
|
||||
* constructor.
|
||||
*
|
||||
* <h4>Workaround</h4>
|
||||
* <p>This method provides a workaround for this off-by-one error by allowing the
|
||||
* caller to access annotations on the preceding {@link Parameter} object (i.e.,
|
||||
* {@code index - 1}). If the supplied {@code index} is zero, this method returns
|
||||
* an empty {@code AnnotatedElement}.
|
||||
*
|
||||
* <h4>WARNING</h4>
|
||||
* <p>The {@code AnnotatedElement} returned by this method should never be cast and
|
||||
* treated as a {@code Parameter} since the metadata (e.g., {@link Parameter#getName()},
|
||||
* {@link Parameter#getType()}, etc.) will not match those for the declared parameter
|
||||
* at the given index in an inner class constructor.
|
||||
*
|
||||
* @return the supplied {@code parameter} or the <em>effective</em> {@code Parameter}
|
||||
* if the aforementioned bug is in effect
|
||||
*/
|
||||
private static AnnotatedElement getEffectiveAnnotatedParameter(Parameter parameter, int index) {
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
|
||||
if (executable instanceof Constructor &&
|
||||
ClassUtils.isInnerClass(executable.getDeclaringClass()) &&
|
||||
if (executable instanceof Constructor && ClassUtils.isInnerClass(executable.getDeclaringClass()) &&
|
||||
executable.getParameterAnnotations().length == executable.getParameterCount() - 1) {
|
||||
|
||||
// Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
|
||||
// for inner classes, so access it with the actual parameter index lowered by 1
|
||||
return (index == 0) ? EMPTY_ANNOTATED_ELEMENT : executable.getParameters()[index - 1];
|
||||
return (index == 0 ? EMPTY_ANNOTATED_ELEMENT : executable.getParameters()[index - 1]);
|
||||
}
|
||||
|
||||
return parameter;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user