1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .lang .annotation .Annotation ;
20
20
import java .lang .reflect .AnnotatedElement ;
21
- import java .lang .reflect .Constructor ;
22
21
import java .lang .reflect .Executable ;
23
22
import java .lang .reflect .Parameter ;
24
23
30
29
import org .springframework .core .annotation .SynthesizingMethodParameter ;
31
30
import org .springframework .lang .Nullable ;
32
31
import org .springframework .util .Assert ;
33
- import org .springframework .util .ClassUtils ;
34
32
35
33
/**
36
34
* Public delegate for resolving autowirable parameters on externally managed
@@ -81,10 +79,9 @@ private ParameterResolutionDelegate() {
81
79
*/
82
80
public static boolean isAutowirable (Parameter parameter , int parameterIndex ) {
83
81
Assert .notNull (parameter , "Parameter must not be null" );
84
- AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter (parameter , parameterIndex );
85
- return (AnnotatedElementUtils .hasAnnotation (annotatedParameter , Autowired .class ) ||
86
- AnnotatedElementUtils .hasAnnotation (annotatedParameter , Qualifier .class ) ||
87
- AnnotatedElementUtils .hasAnnotation (annotatedParameter , Value .class ));
82
+ return (AnnotatedElementUtils .hasAnnotation (parameter , Autowired .class ) ||
83
+ AnnotatedElementUtils .hasAnnotation (parameter , Qualifier .class ) ||
84
+ AnnotatedElementUtils .hasAnnotation (parameter , Value .class ));
88
85
}
89
86
90
87
/**
@@ -125,8 +122,7 @@ public static Object resolveDependency(
125
122
Assert .notNull (containingClass , "Containing class must not be null" );
126
123
Assert .notNull (beanFactory , "AutowireCapableBeanFactory must not be null" );
127
124
128
- AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter (parameter , parameterIndex );
129
- Autowired autowired = AnnotatedElementUtils .findMergedAnnotation (annotatedParameter , Autowired .class );
125
+ Autowired autowired = AnnotatedElementUtils .findMergedAnnotation (parameter , Autowired .class );
130
126
boolean required = (autowired == null || autowired .required ());
131
127
132
128
MethodParameter methodParameter = SynthesizingMethodParameter .forExecutable (
@@ -136,36 +132,4 @@ public static Object resolveDependency(
136
132
return beanFactory .resolveDependency (descriptor , null );
137
133
}
138
134
139
- /**
140
- * Due to a bug in {@code javac} on JDK versions prior to JDK 9, looking up
141
- * annotations directly on a {@link Parameter} will fail for inner class
142
- * constructors.
143
- * <h4>Bug in javac in JDK < 9</h4>
144
- * <p>The parameter annotations array in the compiled byte code excludes an entry
145
- * for the implicit <em>enclosing instance</em> parameter for an inner class
146
- * constructor.
147
- * <h4>Workaround</h4>
148
- * <p>This method provides a workaround for this off-by-one error by allowing the
149
- * caller to access annotations on the preceding {@link Parameter} object (i.e.,
150
- * {@code index - 1}). If the supplied {@code index} is zero, this method returns
151
- * an empty {@code AnnotatedElement}.
152
- * <h4>WARNING</h4>
153
- * <p>The {@code AnnotatedElement} returned by this method should never be cast and
154
- * treated as a {@code Parameter} since the metadata (e.g., {@link Parameter#getName()},
155
- * {@link Parameter#getType()}, etc.) will not match those for the declared parameter
156
- * at the given index in an inner class constructor.
157
- * @return the supplied {@code parameter} or the <em>effective</em> {@code Parameter}
158
- * if the aforementioned bug is in effect
159
- */
160
- private static AnnotatedElement getEffectiveAnnotatedParameter (Parameter parameter , int index ) {
161
- Executable executable = parameter .getDeclaringExecutable ();
162
- if (executable instanceof Constructor && ClassUtils .isInnerClass (executable .getDeclaringClass ()) &&
163
- executable .getParameterAnnotations ().length == executable .getParameterCount () - 1 ) {
164
- // Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
165
- // for inner classes, so access it with the actual parameter index lowered by 1
166
- return (index == 0 ? EMPTY_ANNOTATED_ELEMENT : executable .getParameters ()[index - 1 ]);
167
- }
168
- return parameter ;
169
- }
170
-
171
135
}
0 commit comments