Skip to content

Commit

Permalink
Consistent detection of meta-annotation attributes via ASM
Browse files Browse the repository at this point in the history
Issue: SPR-13394
  • Loading branch information
jhoeller committed Aug 25, 2015
1 parent 6aa9e0c commit 3430f76
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
Expand Up @@ -27,6 +27,7 @@

import org.springframework.aop.scope.ScopedObject;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.FactoryBean;
Expand Down Expand Up @@ -426,6 +427,28 @@ public void genericsBasedInjectionWithScopedProxy() {
RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
assertEquals("Repository<String>", bean.stringRepository.toString());
assertEquals("Repository<Integer>", bean.integerRepository.toString());
assertTrue(AopUtils.isCglibProxy(bean.stringRepository));
assertTrue(AopUtils.isCglibProxy(bean.integerRepository));
}

@Test
public void genericsBasedInjectionWithScopedProxyUsingAsm() {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(beanFactory);
beanFactory.addBeanPostProcessor(bpp);
RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class.getName());
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
beanFactory.registerBeanDefinition("annotatedBean", bd);
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class.getName()));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory);
beanFactory.freezeConfiguration();

RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
assertEquals("Repository<String>", bean.stringRepository.toString());
assertEquals("Repository<Integer>", bean.integerRepository.toString());
assertTrue(AopUtils.isCglibProxy(bean.stringRepository));
assertTrue(AopUtils.isCglibProxy(bean.integerRepository));
}

@Test
Expand Down Expand Up @@ -783,6 +806,13 @@ public String toString() {
}
}

@Retention(RetentionPolicy.RUNTIME)
@Scope(scopeName = "prototype")
public @interface PrototypeScoped {

ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;
}

@Configuration
public static class ScopedProxyRepositoryConfiguration {

Expand All @@ -798,7 +828,7 @@ public String toString() {
}

@Bean
@Scope(scopeName = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
@PrototypeScoped
public Repository<Integer> integerRepo() {
return new Repository<Integer>() {
@Override
Expand Down
Expand Up @@ -111,7 +111,7 @@ public Map<String, Object> getAnnotationAttributes(String annotationName) {
@Override
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return AnnotatedElementUtils.getMergedAnnotationAttributes(getIntrospectedClass(),
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
}

@Override
Expand All @@ -122,7 +122,7 @@ public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotatio
@Override
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return AnnotatedElementUtils.getAllAnnotationAttributes(getIntrospectedClass(),
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
}

@Override
Expand Down
Expand Up @@ -66,6 +66,7 @@ public StandardMethodMetadata(Method introspectedMethod, boolean nestedAnnotatio
this.nestedAnnotationsAsMap = nestedAnnotationsAsMap;
}


/**
* Return the underlying Method.
*/
Expand Down Expand Up @@ -121,7 +122,7 @@ public Map<String, Object> getAnnotationAttributes(String annotationName) {
@Override
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return AnnotatedElementUtils.getMergedAnnotationAttributes(this.introspectedMethod,
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
}

@Override
Expand All @@ -132,7 +133,7 @@ public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotatio
@Override
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return AnnotatedElementUtils.getAllAnnotationAttributes(this.introspectedMethod,
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
}

}
Expand Up @@ -59,7 +59,8 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
* to ensure that the hierarchical ordering of the entries is preserved.
* @see AnnotationReadingVisitorUtils#getMergedAnnotationAttributes
*/
protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap = new LinkedMultiValueMap<String, AnnotationAttributes>(4);
protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap =
new LinkedMultiValueMap<String, AnnotationAttributes>(4);

protected final Set<MethodMetadata> methodMetadataSet = new LinkedHashSet<MethodMetadata>(4);

Expand All @@ -84,7 +85,8 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
String className = Type.getType(desc).getClassName();
this.annotationSet.add(className);
return new AnnotationAttributesReadingVisitor(className, this.attributesMap, this.metaAnnotationMap, this.classLoader);
return new AnnotationAttributesReadingVisitor(
className, this.attributesMap, this.metaAnnotationMap, this.classLoader);
}


Expand Down Expand Up @@ -116,7 +118,8 @@ public boolean hasMetaAnnotation(String metaAnnotationType) {

@Override
public boolean isAnnotated(String annotationName) {
return (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationName) && this.attributesMap.containsKey(annotationName));
return (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationName) &&
this.attributesMap.containsKey(annotationName));
}

@Override
Expand All @@ -127,8 +130,7 @@ public AnnotationAttributes getAnnotationAttributes(String annotationName) {
@Override
public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes(
this.attributesMap,
this.metaAnnotationMap, annotationName);
this.attributesMap, this.metaAnnotationMap, annotationName);
return AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString);
}

Expand All @@ -145,8 +147,8 @@ public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotatio
return null;
}
for (AnnotationAttributes raw : attributes) {
for (Map.Entry<String, Object> entry :
AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString).entrySet()) {
for (Map.Entry<String, Object> entry : AnnotationReadingVisitorUtils.convertClassValues(
this.classLoader, raw, classValuesAsString).entrySet()) {
allAttributes.add(entry.getKey(), entry.getValue());
}
}
Expand Down
Expand Up @@ -16,7 +16,7 @@

package org.springframework.core.type.classreading;

import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -56,7 +56,9 @@ public class MethodMetadataReadingVisitor extends MethodVisitor implements Metho

protected final Set<MethodMetadata> methodMetadataSet;

protected final MultiValueMap<String, AnnotationAttributes> attributeMap =
protected final Map<String, Set<String>> metaAnnotationMap = new LinkedHashMap<String, Set<String>>(4);

protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap =
new LinkedMultiValueMap<String, AnnotationAttributes>(4);


Expand All @@ -77,7 +79,8 @@ public MethodMetadataReadingVisitor(String methodName, int access, String declar
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
String className = Type.getType(desc).getClassName();
this.methodMetadataSet.add(this);
return new AnnotationAttributesReadingVisitor(className, this.attributeMap, null, this.classLoader);
return new AnnotationAttributesReadingVisitor(
className, this.attributesMap, this.metaAnnotationMap, this.classLoader);
}

@Override
Expand Down Expand Up @@ -107,19 +110,19 @@ public boolean isOverridable() {

@Override
public boolean isAnnotated(String annotationName) {
return this.attributeMap.containsKey(annotationName);
return this.attributesMap.containsKey(annotationName);
}

@Override
public Map<String, Object> getAnnotationAttributes(String annotationName) {
public AnnotationAttributes getAnnotationAttributes(String annotationName) {
return getAnnotationAttributes(annotationName, false);
}

@Override
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
List<AnnotationAttributes> attributes = this.attributeMap.get(annotationName);
return (attributes == null ? null : AnnotationReadingVisitorUtils.convertClassValues(
this.classLoader, attributes.get(0), classValuesAsString));
public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes(
this.attributesMap, this.metaAnnotationMap, annotationName);
return (AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString));
}

@Override
Expand All @@ -129,11 +132,11 @@ public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotatio

@Override
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
if (!this.attributeMap.containsKey(annotationName)) {
if (!this.attributesMap.containsKey(annotationName)) {
return null;
}
MultiValueMap<String, Object> allAttributes = new LinkedMultiValueMap<String, Object>();
for (AnnotationAttributes annotationAttributes : this.attributeMap.get(annotationName)) {
for (AnnotationAttributes annotationAttributes : this.attributesMap.get(annotationName)) {
for (Map.Entry<String, Object> entry : AnnotationReadingVisitorUtils.convertClassValues(
this.classLoader, annotationAttributes, classValuesAsString).entrySet()) {
allAttributes.add(entry.getKey(), entry.getValue());
Expand Down

0 comments on commit 3430f76

Please sign in to comment.