diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java index 429d61c926..94d0d98863 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java @@ -23,6 +23,7 @@ import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -84,6 +85,19 @@ public class BeanMethodQualificationTests { assertThat(pojo.testBean.getName(), equalTo("interesting")); } + @Test + public void testCustomWithAsm() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.registerBeanDefinition("customConfig", new RootBeanDefinition(CustomConfig.class.getName())); + RootBeanDefinition customPojo = new RootBeanDefinition(CustomPojo.class.getName()); + customPojo.setLazyInit(true); + ctx.registerBeanDefinition("customPojo", customPojo); + ctx.refresh(); + assertFalse(ctx.getBeanFactory().containsSingleton("testBean1")); + CustomPojo pojo = ctx.getBean(CustomPojo.class); + assertThat(pojo.testBean.getName(), equalTo("interesting")); + } + @Test public void testCustomWithAttributeOverride() { AnnotationConfigApplicationContext ctx = diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java index c12b83d160..c4eb54bf45 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java @@ -29,7 +29,6 @@ import org.springframework.asm.Type; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.MethodMetadata; -import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -48,13 +47,13 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito protected final ClassLoader classLoader; - protected final Set annotationSet = new LinkedHashSet(); + protected final Set annotationSet = new LinkedHashSet(4); protected final Map> metaAnnotationMap = new LinkedHashMap>(4); protected final MultiValueMap attributeMap = new LinkedMultiValueMap(4); - protected final MultiValueMap methodMetadataMap = new LinkedMultiValueMap(); + protected final Set methodMetadataSet = new LinkedHashSet(4); public AnnotationMetadataReadingVisitor(ClassLoader classLoader) { @@ -64,7 +63,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { - return new MethodMetadataReadingVisitor(name, access, this.getClassName(), this.classLoader, this.methodMetadataMap); + return new MethodMetadataReadingVisitor(name, access, getClassName(), this.classLoader, this.methodMetadataSet); } @Override @@ -141,17 +140,22 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito @Override public boolean hasAnnotatedMethods(String annotationType) { - return this.methodMetadataMap.containsKey(annotationType); + for (MethodMetadata methodMetadata : this.methodMetadataSet) { + if (methodMetadata.isAnnotated(annotationType)) { + return true; + } + } + return false; } @Override public Set getAnnotatedMethods(String annotationType) { - List list = this.methodMetadataMap.get(annotationType); - if (CollectionUtils.isEmpty(list)) { - return new LinkedHashSet(0); + Set annotatedMethods = new LinkedHashSet(4); + for (MethodMetadata methodMetadata : this.methodMetadataSet) { + if (methodMetadata.isAnnotated(annotationType)) { + annotatedMethods.add(methodMetadata); + } } - Set annotatedMethods = new LinkedHashSet(list.size()); - annotatedMethods.addAll(list); return annotatedMethods; } diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java index 721c10f5f7..f88b57b0d0 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java @@ -18,6 +18,7 @@ package org.springframework.core.type.classreading; import java.util.List; import java.util.Map; +import java.util.Set; import org.springframework.asm.AnnotationVisitor; import org.springframework.asm.MethodVisitor; @@ -51,27 +52,28 @@ public class MethodMetadataReadingVisitor extends MethodVisitor implements Metho protected final ClassLoader classLoader; - protected final MultiValueMap methodMetadataMap; + protected final Set methodMetadataSet; - protected final MultiValueMap attributeMap = new LinkedMultiValueMap(2); + protected final MultiValueMap attributeMap = + new LinkedMultiValueMap(4); - public MethodMetadataReadingVisitor(String name, int access, String declaringClassName, ClassLoader classLoader, - MultiValueMap methodMetadataMap) { + public MethodMetadataReadingVisitor(String name, int access, String declaringClassName, + ClassLoader classLoader, Set methodMetadataSet) { super(SpringAsmInfo.ASM_VERSION); this.name = name; this.access = access; this.declaringClassName = declaringClassName; this.classLoader = classLoader; - this.methodMetadataMap = methodMetadataMap; + this.methodMetadataSet = methodMetadataSet; } @Override public AnnotationVisitor visitAnnotation(final String desc, boolean visible) { String className = Type.getType(desc).getClassName(); - this.methodMetadataMap.add(className, this); + this.methodMetadataSet.add(this); return new AnnotationAttributesReadingVisitor(className, this.attributeMap, null, this.classLoader); } @@ -106,8 +108,7 @@ public class MethodMetadataReadingVisitor extends MethodVisitor implements Metho } @Override - public Map getAnnotationAttributes(String annotationType, - boolean classValuesAsString) { + public Map getAnnotationAttributes(String annotationType, boolean classValuesAsString) { List attributes = this.attributeMap.get(annotationType); return (attributes == null ? null : AnnotationReadingVisitorUtils.convertClassValues( this.classLoader, attributes.get(0), classValuesAsString)); @@ -119,9 +120,8 @@ public class MethodMetadataReadingVisitor extends MethodVisitor implements Metho } @Override - public MultiValueMap getAllAnnotationAttributes( - String annotationType, boolean classValuesAsString) { - if(!this.attributeMap.containsKey(annotationType)) { + public MultiValueMap getAllAnnotationAttributes(String annotationType, boolean classValuesAsString) { + if (!this.attributeMap.containsKey(annotationType)) { return null; } MultiValueMap allAttributes = new LinkedMultiValueMap();