Avoid synthesizable annotation creation for @Bean/@Scope processing
Includes consistent (non-)use of AnnotationUtils/AnnotatedElementUtils. Issue: SPR-16933
This commit is contained in:
parent
9b671f8408
commit
589b7048ec
|
@ -17,8 +17,11 @@
|
||||||
package org.springframework.context.annotation;
|
package org.springframework.context.annotation;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
|
import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
|
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utilities for processing {@link Bean}-annotated methods.
|
* Utilities for processing {@link Bean}-annotated methods.
|
||||||
|
@ -27,10 +30,11 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
*/
|
*/
|
||||||
final class BeanAnnotationHelper {
|
abstract class BeanAnnotationHelper {
|
||||||
|
|
||||||
private BeanAnnotationHelper() {
|
private static final Map<Method, String> beanNameCache = new ConcurrentReferenceHashMap<>();
|
||||||
}
|
|
||||||
|
private static final Map<Method, Boolean> scopedProxyCache = new ConcurrentReferenceHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
public static boolean isBeanAnnotated(Method method) {
|
public static boolean isBeanAnnotated(Method method) {
|
||||||
|
@ -38,16 +42,33 @@ final class BeanAnnotationHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String determineBeanNameFor(Method beanMethod) {
|
public static String determineBeanNameFor(Method beanMethod) {
|
||||||
// By default, the bean name is the name of the @Bean-annotated method
|
String beanName = beanNameCache.get(beanMethod);
|
||||||
String beanName = beanMethod.getName();
|
if (beanName == null) {
|
||||||
|
// By default, the bean name is the name of the @Bean-annotated method
|
||||||
// Check to see if the user has explicitly set a custom bean name...
|
beanName = beanMethod.getName();
|
||||||
Bean bean = AnnotatedElementUtils.findMergedAnnotation(beanMethod, Bean.class);
|
// Check to see if the user has explicitly set a custom bean name...
|
||||||
if (bean != null && bean.name().length > 0) {
|
AnnotationAttributes bean =
|
||||||
beanName = bean.name()[0];
|
AnnotatedElementUtils.findMergedAnnotationAttributes(beanMethod, Bean.class, false, false);
|
||||||
|
if (bean != null) {
|
||||||
|
String[] names = bean.getStringArray("name");
|
||||||
|
if (names.length > 0) {
|
||||||
|
beanName = names[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
beanNameCache.put(beanMethod, beanName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return beanName;
|
return beanName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isScopedProxy(Method beanMethod) {
|
||||||
|
Boolean scopedProxy = scopedProxyCache.get(beanMethod);
|
||||||
|
if (scopedProxy == null) {
|
||||||
|
AnnotationAttributes scope =
|
||||||
|
AnnotatedElementUtils.findMergedAnnotationAttributes(beanMethod, Scope.class, false, false);
|
||||||
|
scopedProxy = (scope != null && scope.getEnum("proxyMode") != ScopedProxyMode.NO);
|
||||||
|
scopedProxyCache.put(beanMethod, scopedProxy);
|
||||||
|
}
|
||||||
|
return scopedProxy;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,6 @@ import org.springframework.cglib.proxy.MethodProxy;
|
||||||
import org.springframework.cglib.proxy.NoOp;
|
import org.springframework.cglib.proxy.NoOp;
|
||||||
import org.springframework.cglib.transform.ClassEmitterTransformer;
|
import org.springframework.cglib.transform.ClassEmitterTransformer;
|
||||||
import org.springframework.cglib.transform.TransformingClassGenerator;
|
import org.springframework.cglib.transform.TransformingClassGenerator;
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.objenesis.ObjenesisException;
|
import org.springframework.objenesis.ObjenesisException;
|
||||||
import org.springframework.objenesis.SpringObjenesis;
|
import org.springframework.objenesis.SpringObjenesis;
|
||||||
|
@ -317,8 +316,7 @@ class ConfigurationClassEnhancer {
|
||||||
String beanName = BeanAnnotationHelper.determineBeanNameFor(beanMethod);
|
String beanName = BeanAnnotationHelper.determineBeanNameFor(beanMethod);
|
||||||
|
|
||||||
// Determine whether this bean is a scoped-proxy
|
// Determine whether this bean is a scoped-proxy
|
||||||
Scope scope = AnnotatedElementUtils.findMergedAnnotation(beanMethod, Scope.class);
|
if (BeanAnnotationHelper.isScopedProxy(beanMethod)) {
|
||||||
if (scope != null && scope.proxyMode() != ScopedProxyMode.NO) {
|
|
||||||
String scopedBeanName = ScopedProxyCreator.getTargetBeanName(beanName);
|
String scopedBeanName = ScopedProxyCreator.getTargetBeanName(beanName);
|
||||||
if (beanFactory.isCurrentlyInCreation(scopedBeanName)) {
|
if (beanFactory.isCurrentlyInCreation(scopedBeanName)) {
|
||||||
beanName = scopedBeanName;
|
beanName = scopedBeanName;
|
||||||
|
|
|
@ -585,22 +585,20 @@ public abstract class AnnotatedElementUtils {
|
||||||
* attributes of the same name from higher levels, and
|
* attributes of the same name from higher levels, and
|
||||||
* {@link AliasFor @AliasFor} semantics are fully supported, both
|
* {@link AliasFor @AliasFor} semantics are fully supported, both
|
||||||
* within a single annotation and within the annotation hierarchy.
|
* within a single annotation and within the annotation hierarchy.
|
||||||
* <p>In contrast to {@link #getAllAnnotationAttributes}, the search
|
* <p>In contrast to {@link #getAllAnnotationAttributes}, the search algorithm
|
||||||
* algorithm used by this method will stop searching the annotation
|
* used by this method will stop searching the annotation hierarchy once the
|
||||||
* hierarchy once the first annotation of the specified
|
* first annotation of the specified {@code annotationType} has been found.
|
||||||
* {@code annotationType} has been found. As a consequence, additional
|
* As a consequence, additional annotations of the specified
|
||||||
* annotations of the specified {@code annotationType} will be ignored.
|
* {@code annotationType} will be ignored.
|
||||||
* <p>This method follows <em>find semantics</em> as described in the
|
* <p>This method follows <em>find semantics</em> as described in the
|
||||||
* {@linkplain AnnotatedElementUtils class-level javadoc}.
|
* {@linkplain AnnotatedElementUtils class-level javadoc}.
|
||||||
* @param element the annotated element
|
* @param element the annotated element
|
||||||
* @param annotationType the annotation type to find
|
* @param annotationType the annotation type to find
|
||||||
* @param classValuesAsString whether to convert Class references into
|
* @param classValuesAsString whether to convert Class references into
|
||||||
* Strings or to preserve them as Class references
|
* Strings or to preserve them as Class references
|
||||||
* @param nestedAnnotationsAsMap whether to convert nested Annotation
|
* @param nestedAnnotationsAsMap whether to convert nested Annotation instances into
|
||||||
* instances into {@code AnnotationAttributes} maps or to preserve them
|
* {@code AnnotationAttributes} maps or to preserve them as Annotation instances
|
||||||
* as Annotation instances
|
* @return the merged {@code AnnotationAttributes}, or {@code null} if not found
|
||||||
* @return the merged {@code AnnotationAttributes}, or {@code null} if
|
|
||||||
* not found
|
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
* @see #findMergedAnnotation(AnnotatedElement, Class)
|
* @see #findMergedAnnotation(AnnotatedElement, Class)
|
||||||
* @see #getMergedAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
|
* @see #getMergedAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
|
||||||
|
|
|
@ -75,19 +75,26 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
|
||||||
}
|
}
|
||||||
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationClass.getName())) {
|
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationClass.getName())) {
|
||||||
Set<Annotation> visited = new LinkedHashSet<>();
|
Set<Annotation> visited = new LinkedHashSet<>();
|
||||||
Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass);
|
try {
|
||||||
if (!ObjectUtils.isEmpty(metaAnnotations)) {
|
Annotation[] metaAnnotations = annotationClass.getAnnotations();
|
||||||
for (Annotation metaAnnotation : metaAnnotations) {
|
if (!ObjectUtils.isEmpty(metaAnnotations)) {
|
||||||
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
|
for (Annotation metaAnnotation : metaAnnotations) {
|
||||||
recursivelyCollectMetaAnnotations(visited, metaAnnotation);
|
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
|
||||||
|
recursivelyCollectMetaAnnotations(visited, metaAnnotation);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Set<String> metaAnnotationTypeNames = new LinkedHashSet<>(visited.size());
|
||||||
|
for (Annotation ann : visited) {
|
||||||
|
metaAnnotationTypeNames.add(ann.annotationType().getName());
|
||||||
|
}
|
||||||
|
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
|
||||||
}
|
}
|
||||||
Set<String> metaAnnotationTypeNames = new LinkedHashSet<>(visited.size());
|
catch (Throwable ex) {
|
||||||
for (Annotation ann : visited) {
|
if (logger.isDebugEnabled()) {
|
||||||
metaAnnotationTypeNames.add(ann.annotationType().getName());
|
logger.debug("Failed to introspect meta-annotations on " + annotationClass + ": " + ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,7 +117,7 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
|
||||||
}
|
}
|
||||||
catch (Throwable ex) {
|
catch (Throwable ex) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Failed to introspect meta-annotations on [" + annotation + "]: " + ex);
|
logger.debug("Failed to introspect meta-annotations on " + annotation + ": " + ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -192,7 +192,6 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
|
||||||
}
|
}
|
||||||
|
|
||||||
private DestinationHelper getDestinationHelper(MessageHeaders headers, MethodParameter returnType) {
|
private DestinationHelper getDestinationHelper(MessageHeaders headers, MethodParameter returnType) {
|
||||||
|
|
||||||
SendToUser m1 = AnnotatedElementUtils.findMergedAnnotation(returnType.getExecutable(), SendToUser.class);
|
SendToUser m1 = AnnotatedElementUtils.findMergedAnnotation(returnType.getExecutable(), SendToUser.class);
|
||||||
SendTo m2 = AnnotatedElementUtils.findMergedAnnotation(returnType.getExecutable(), SendTo.class);
|
SendTo m2 = AnnotatedElementUtils.findMergedAnnotation(returnType.getExecutable(), SendTo.class);
|
||||||
if ((m1 != null && !ObjectUtils.isEmpty(m1.value())) || (m2 != null && !ObjectUtils.isEmpty(m2.value()))) {
|
if ((m1 != null && !ObjectUtils.isEmpty(m1.value())) || (m2 != null && !ObjectUtils.isEmpty(m2.value()))) {
|
||||||
|
@ -205,8 +204,8 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
|
||||||
return new DestinationHelper(headers, c1, c2);
|
return new DestinationHelper(headers, c1, c2);
|
||||||
}
|
}
|
||||||
|
|
||||||
return m1 != null || m2 != null ?
|
return (m1 != null || m2 != null ?
|
||||||
new DestinationHelper(headers, m1, m2) : new DestinationHelper(headers, c1, c2);
|
new DestinationHelper(headers, m1, m2) : new DestinationHelper(headers, c1, c2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@ -291,9 +290,9 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
|
||||||
return this.sendToUser;
|
return this.sendToUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String expandTemplateVars(String destination) {
|
public String expandTemplateVars(String destination) {
|
||||||
return placeholderHelper.replacePlaceholders(destination, this.placeholderResolver);
|
return placeholderHelper.replacePlaceholders(destination, this.placeholderResolver);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -22,7 +22,7 @@ import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.core.ReactiveAdapterRegistry;
|
import org.springframework.core.ReactiveAdapterRegistry;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
import org.springframework.http.codec.HttpMessageWriter;
|
import org.springframework.http.codec.HttpMessageWriter;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.reactive.HandlerResult;
|
import org.springframework.web.reactive.HandlerResult;
|
||||||
|
@ -53,9 +53,7 @@ public class ResponseBodyResultHandler extends AbstractMessageWriterResultHandle
|
||||||
* @param writers writers for serializing to the response body
|
* @param writers writers for serializing to the response body
|
||||||
* @param resolver to determine the requested content type
|
* @param resolver to determine the requested content type
|
||||||
*/
|
*/
|
||||||
public ResponseBodyResultHandler(List<HttpMessageWriter<?>> writers,
|
public ResponseBodyResultHandler(List<HttpMessageWriter<?>> writers, RequestedContentTypeResolver resolver) {
|
||||||
RequestedContentTypeResolver resolver) {
|
|
||||||
|
|
||||||
this(writers, resolver, ReactiveAdapterRegistry.getSharedInstance());
|
this(writers, resolver, ReactiveAdapterRegistry.getSharedInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,10 +73,10 @@ public class ResponseBodyResultHandler extends AbstractMessageWriterResultHandle
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supports(HandlerResult result) {
|
public boolean supports(HandlerResult result) {
|
||||||
MethodParameter parameter = result.getReturnTypeSource();
|
MethodParameter returnType = result.getReturnTypeSource();
|
||||||
Class<?> containingClass = parameter.getContainingClass();
|
Class<?> containingClass = returnType.getContainingClass();
|
||||||
return (AnnotationUtils.findAnnotation(containingClass, ResponseBody.class) != null ||
|
return (AnnotatedElementUtils.hasAnnotation(containingClass, ResponseBody.class) ||
|
||||||
parameter.getMethodAnnotation(ResponseBody.class) != null);
|
returnType.hasMethodAnnotation(ResponseBody.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue