Skip to content

Commit dc1f921

Browse files
committed
Split between basic MethodParameter and SynthesizingMethodParameter
This split avoids a package tangle (between core and core.annotation) and also allows for selective use of raw annotation exposure versus synthesized annotations, with the latter primarily applicable to web and message handler processing at this point. Issue: SPR-13153
1 parent 26acb48 commit dc1f921

File tree

24 files changed

+286
-216
lines changed

24 files changed

+286
-216
lines changed

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
import java.util.HashMap;
2727
import java.util.Map;
2828

29-
import org.springframework.core.annotation.AnnotationUtils;
3029
import org.springframework.util.Assert;
3130

3231
/**
3332
* Helper class that encapsulates the specification of a method parameter, i.e.
3433
* a Method or Constructor plus a parameter index and a nested type index for
3534
* a declared generic type. Useful as a specification object to pass along.
3635
*
36+
* <p>As of 4.2, there is a {@link org.springframework.core.annotation.SynthesizingMethodParameter}
37+
* subclass available which synthesizes annotations based on overridden annotation attributes.
38+
* That subclass is being used for web and message endpoint processing, in particular.
39+
*
3740
* @author Juergen Hoeller
3841
* @author Rob Harrop
3942
* @author Andy Clement
@@ -388,17 +391,16 @@ public Type getNestedGenericParameterType() {
388391
* Return the annotations associated with the target method/constructor itself.
389392
*/
390393
public Annotation[] getMethodAnnotations() {
391-
return AnnotationUtils.synthesizeAnnotationArray(getAnnotatedElement().getAnnotations(), getAnnotatedElement());
394+
return adaptAnnotationArray(getAnnotatedElement().getAnnotations());
392395
}
393396

394397
/**
395398
* Return the method/constructor annotation of the given type, if available.
396399
* @param annotationType the annotation type to look for
397400
* @return the annotation object, or {@code null} if not found
398401
*/
399-
public <T extends Annotation> T getMethodAnnotation(Class<T> annotationType) {
400-
AnnotatedElement element = getAnnotatedElement();
401-
return AnnotationUtils.synthesizeAnnotation(element.getAnnotation(annotationType), element);
402+
public <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) {
403+
return adaptAnnotation(getAnnotatedElement().getAnnotation(annotationType));
402404
}
403405

404406
/**
@@ -409,8 +411,7 @@ public Annotation[] getParameterAnnotations() {
409411
Annotation[][] annotationArray = (this.method != null ?
410412
this.method.getParameterAnnotations() : this.constructor.getParameterAnnotations());
411413
if (this.parameterIndex >= 0 && this.parameterIndex < annotationArray.length) {
412-
this.parameterAnnotations = AnnotationUtils.synthesizeAnnotationArray(
413-
annotationArray[this.parameterIndex], getAnnotatedElement());
414+
this.parameterAnnotations = adaptAnnotationArray(annotationArray[this.parameterIndex]);
414415
}
415416
else {
416417
this.parameterAnnotations = new Annotation[0];
@@ -480,6 +481,31 @@ public String getParameterName() {
480481
}
481482

482483

484+
/**
485+
* A template method to post-process a given annotation instance before
486+
* returning it to the caller.
487+
* <p>The default implementation simply returns the given annotation as-is.
488+
* @param annotation the annotation about to be returned
489+
* @return the post-processed annotation (or simply the original one)
490+
* @since 4.2
491+
*/
492+
protected <A extends Annotation> A adaptAnnotation(A annotation) {
493+
return annotation;
494+
}
495+
496+
/**
497+
* A template method to post-process a given annotation array before
498+
* returning it to the caller.
499+
* <p>The default implementation simply returns the given annotation array as-is.
500+
* @param annotations the annotation array about to be returned
501+
* @return the post-processed annotation array (or simply the original one)
502+
* @since 4.2
503+
*/
504+
protected Annotation[] adaptAnnotationArray(Annotation[] annotations) {
505+
return annotations;
506+
}
507+
508+
483509
@Override
484510
public boolean equals(Object other) {
485511
if (this == other) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ public static ResolvableType forConstructorParameter(Constructor<?> constructor,
11221122
*/
11231123
public static ResolvableType forMethodReturnType(Method method) {
11241124
Assert.notNull(method, "Method must not be null");
1125-
return forMethodParameter(MethodParameter.forMethodOrConstructor(method, -1));
1125+
return forMethodParameter(new MethodParameter(method, -1));
11261126
}
11271127

11281128
/**
@@ -1136,7 +1136,7 @@ public static ResolvableType forMethodReturnType(Method method) {
11361136
*/
11371137
public static ResolvableType forMethodReturnType(Method method, Class<?> implementationClass) {
11381138
Assert.notNull(method, "Method must not be null");
1139-
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(method, -1);
1139+
MethodParameter methodParameter = new MethodParameter(method, -1);
11401140
methodParameter.setContainingClass(implementationClass);
11411141
return forMethodParameter(methodParameter);
11421142
}

0 commit comments

Comments
 (0)