Keep getEffectiveAnnotatedParameter for user code compiled with javac 8
This commit is contained in:
parent
ac5dc698e2
commit
3ad796e913
|
|
@ -18,6 +18,7 @@ package org.springframework.beans.factory.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Executable;
|
import java.lang.reflect.Executable;
|
||||||
import java.lang.reflect.Parameter;
|
import java.lang.reflect.Parameter;
|
||||||
|
|
||||||
|
|
@ -29,6 +30,7 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public delegate for resolving autowirable parameters on externally managed
|
* Public delegate for resolving autowirable parameters on externally managed
|
||||||
|
|
@ -79,9 +81,10 @@ public final class ParameterResolutionDelegate {
|
||||||
*/
|
*/
|
||||||
public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
|
public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
|
||||||
Assert.notNull(parameter, "Parameter must not be null");
|
Assert.notNull(parameter, "Parameter must not be null");
|
||||||
return (AnnotatedElementUtils.hasAnnotation(parameter, Autowired.class) ||
|
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
|
||||||
AnnotatedElementUtils.hasAnnotation(parameter, Qualifier.class) ||
|
return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
|
||||||
AnnotatedElementUtils.hasAnnotation(parameter, Value.class));
|
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
|
||||||
|
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -122,7 +125,8 @@ public final class ParameterResolutionDelegate {
|
||||||
Assert.notNull(containingClass, "Containing class must not be null");
|
Assert.notNull(containingClass, "Containing class must not be null");
|
||||||
Assert.notNull(beanFactory, "AutowireCapableBeanFactory must not be null");
|
Assert.notNull(beanFactory, "AutowireCapableBeanFactory must not be null");
|
||||||
|
|
||||||
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(parameter, Autowired.class);
|
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
|
||||||
|
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
|
||||||
boolean required = (autowired == null || autowired.required());
|
boolean required = (autowired == null || autowired.required());
|
||||||
|
|
||||||
MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(
|
MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(
|
||||||
|
|
@ -132,4 +136,38 @@ public final class ParameterResolutionDelegate {
|
||||||
return beanFactory.resolveDependency(descriptor, null);
|
return beanFactory.resolveDependency(descriptor, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
* <p>Note: Since Spring 6 may still encounter user code compiled with
|
||||||
|
* {@code javac 8}, this workaround is kept in place for the time being.
|
||||||
|
* <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()) &&
|
||||||
|
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 parameter;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue