diff --git a/org.springframework.context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java b/org.springframework.context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java index 7e511b01aa..4937eeb5e3 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package org.springframework.cache.annotation; import java.util.Collection; -import java.util.Map; import javax.annotation.PostConstruct; @@ -26,6 +25,7 @@ import org.springframework.cache.CacheManager; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportAware; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -41,8 +41,7 @@ import org.springframework.util.CollectionUtils; @Configuration public abstract class AbstractCachingConfiguration implements ImportAware { - /** Parsed annotation metadata for {@code @EnableCaching} on the importing class. */ - protected Map enableCaching; + protected AnnotationAttributes enableCaching; protected CacheManager cacheManager; protected KeyGenerator keyGenerator; @@ -52,8 +51,8 @@ public abstract class AbstractCachingConfiguration implements ImportAware { private Collection cachingConfigurers; public void setImportMetadata(AnnotationMetadata importMetadata) { - this.enableCaching = importMetadata.getAnnotationAttributes( - EnableCaching.class.getName(), false); + this.enableCaching = AnnotationAttributes.fromMap( + importMetadata.getAnnotationAttributes(EnableCaching.class.getName(), false)); Assert.notNull(this.enableCaching, "@EnableCaching is not present on importing class " + importMetadata.getClassName()); diff --git a/org.springframework.context/src/main/java/org/springframework/cache/annotation/ProxyCachingConfiguration.java b/org.springframework.context/src/main/java/org/springframework/cache/annotation/ProxyCachingConfiguration.java index 476590beac..aa95cd7089 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/annotation/ProxyCachingConfiguration.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/annotation/ProxyCachingConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,7 +44,7 @@ public class ProxyCachingConfiguration extends AbstractCachingConfiguration { new BeanFactoryCacheOperationSourceAdvisor(); advisor.setCacheOperationSource(cacheOperationSource()); advisor.setAdvice(cacheInterceptor()); - advisor.setOrder(((Integer)this.enableCaching.get("order"))); + advisor.setOrder(this.enableCaching.getInt("order")); return advisor; } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/AdviceModeImportSelector.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/AdviceModeImportSelector.java index ecfa675a75..f7d49a4736 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/AdviceModeImportSelector.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/AdviceModeImportSelector.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,12 @@ package org.springframework.context.annotation; +import static org.springframework.context.annotation.MetadataUtils.attributesFor; + import java.lang.annotation.Annotation; -import java.util.Map; import org.springframework.core.GenericTypeResolver; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.Assert; @@ -64,26 +66,17 @@ public abstract class AdviceModeImportSelector implements * returns {@code null} */ public final String[] selectImports(AnnotationMetadata importingClassMetadata) { - Class annoType = GenericTypeResolver.resolveTypeArgument(getClass(), AdviceModeImportSelector.class); + Class annoType = GenericTypeResolver.resolveTypeArgument(this.getClass(), AdviceModeImportSelector.class); - Map attributes = importingClassMetadata.getAnnotationAttributes(annoType.getName()); + AnnotationAttributes attributes = attributesFor(importingClassMetadata, annoType); Assert.notNull(attributes, String.format( "@%s is not present on importing class '%s' as expected", annoType.getSimpleName(), importingClassMetadata.getClassName())); - String modeAttrName = getAdviceModeAttributeName(); - Assert.hasText(modeAttrName); + AdviceMode adviceMode = + attributes.getEnum(this.getAdviceModeAttributeName(), AdviceMode.class); - Object adviceMode = attributes.get(modeAttrName); - Assert.notNull(adviceMode, String.format( - "Advice mode attribute @%s#%s() does not exist", - annoType.getSimpleName(), modeAttrName)); - - Assert.isInstanceOf(AdviceMode.class, adviceMode, String.format( - "Incorrect type for advice mode attribute '@%s#%s()': ", - annoType.getSimpleName(), modeAttrName)); - - String[] imports = selectImports((AdviceMode) adviceMode); + String[] imports = selectImports(adviceMode); Assert.notNull(imports, String.format("Unknown AdviceMode: '%s'", adviceMode)); return imports; diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java index 2d3ee5ab8e..82cea7c49c 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import org.springframework.beans.factory.support.AutowireCandidateQualifier; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.Environment; import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.env.StandardEnvironment; @@ -136,8 +137,9 @@ public class AnnotatedBeanDefinitionReader { AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass); AnnotationMetadata metadata = abd.getMetadata(); - if (ProfileHelper.isProfileAnnotationPresent(metadata)) { - if (!this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata))) { + if (metadata.isAnnotated(Profile.class.getName())) { + AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class); + if (!this.environment.acceptsProfiles(profile.getStringArray("value"))) { return; } } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java index bb80b3157c..f0780a8fe2 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -85,7 +86,7 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator { Set types = amd.getAnnotationTypes(); String beanName = null; for (String type : types) { - Map attributes = amd.getAnnotationAttributes(type); + AnnotationAttributes attributes = MetadataUtils.attributesFor(amd, type); if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) { String value = (String) attributes.get("value"); if (StringUtils.hasLength(value)) { diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java index 42a053c2d3..753d3994b8 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java @@ -16,6 +16,8 @@ package org.springframework.context.annotation; +import static org.springframework.context.annotation.MetadataUtils.attributesFor; + import java.util.LinkedHashSet; import java.util.Set; @@ -27,6 +29,7 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.ClassUtils; /** @@ -217,21 +220,20 @@ public class AnnotationConfigUtils { } static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd) { - if (abd.getMetadata().isAnnotated(Primary.class.getName())) { + AnnotationMetadata metadata = abd.getMetadata(); + if (metadata.isAnnotated(Primary.class.getName())) { abd.setPrimary(true); } - if (abd.getMetadata().isAnnotated(Lazy.class.getName())) { - Boolean value = (Boolean) abd.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get("value"); - abd.setLazyInit(value); + if (metadata.isAnnotated(Lazy.class.getName())) { + abd.setLazyInit(attributesFor(metadata, Lazy.class).getBoolean("value")); } - if (abd.getMetadata().isAnnotated(DependsOn.class.getName())) { - String[] value = (String[]) abd.getMetadata().getAnnotationAttributes(DependsOn.class.getName()).get("value"); - abd.setDependsOn(value); + if (metadata.isAnnotated(DependsOn.class.getName())) { + abd.setDependsOn(attributesFor(metadata, DependsOn.class).getStringArray("value")); } if (abd instanceof AbstractBeanDefinition) { - if (abd.getMetadata().isAnnotated(Role.class.getName())) { - int value = (Integer) abd.getMetadata().getAnnotationAttributes(Role.class.getName()).get("value"); - ((AbstractBeanDefinition)abd).setRole(value); + if (metadata.isAnnotated(Role.class.getName())) { + ((AbstractBeanDefinition)abd).setRole( + attributesFor(metadata, Role.class).getInt("value")); } } } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationScopeMetadataResolver.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationScopeMetadataResolver.java index 62f0773190..9f1b82e196 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationScopeMetadataResolver.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationScopeMetadataResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,13 @@ package org.springframework.context.annotation; +import static org.springframework.context.annotation.MetadataUtils.attributesFor; + import java.lang.annotation.Annotation; -import java.util.Map; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.util.Assert; /** @@ -75,11 +77,11 @@ public class AnnotationScopeMetadataResolver implements ScopeMetadataResolver { ScopeMetadata metadata = new ScopeMetadata(); if (definition instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition; - Map attributes = - annDef.getMetadata().getAnnotationAttributes(this.scopeAnnotationType.getName()); + AnnotationAttributes attributes = + attributesFor(annDef.getMetadata(), this.scopeAnnotationType); if (attributes != null) { - metadata.setScopeName((String) attributes.get("value")); - ScopedProxyMode proxyMode = (ScopedProxyMode) attributes.get("proxyMode"); + metadata.setScopeName(attributes.getString("value")); + ScopedProxyMode proxyMode = attributes.getEnum("proxyMode", ScopedProxyMode.class); if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = this.defaultProxyMode; } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/AspectJAutoProxyRegistrar.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/AspectJAutoProxyRegistrar.java index 3cb2f2448f..b41fa7da25 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/AspectJAutoProxyRegistrar.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/AspectJAutoProxyRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,11 @@ package org.springframework.context.annotation; -import java.util.Map; +import static org.springframework.context.annotation.MetadataUtils.attributesFor; import org.springframework.aop.config.AopConfigUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; /** @@ -41,12 +42,11 @@ class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar { public void registerBeanDefinitions( AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - Map enableAJAutoProxy = - importingClassMetadata.getAnnotationAttributes(EnableAspectJAutoProxy.class.getName()); - AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry); - if ((Boolean)enableAJAutoProxy.get("proxyTargetClass")) { + AnnotationAttributes enableAJAutoProxy = + attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class); + if (enableAJAutoProxy.getBoolean("proxyTargetClass")) { AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry); } } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java index 2e063a85fc..804dbca526 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java @@ -16,6 +16,8 @@ package org.springframework.context.annotation; +import static org.springframework.context.annotation.MetadataUtils.attributesFor; + import java.util.Map; import java.util.Set; @@ -23,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.aop.config.AopConfigUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; /** @@ -58,7 +61,7 @@ public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar { boolean candidateFound = false; Set annoTypes = importingClassMetadata.getAnnotationTypes(); for (String annoType : annoTypes) { - Map candidate = importingClassMetadata.getAnnotationAttributes(annoType); + AnnotationAttributes candidate = attributesFor(importingClassMetadata, annoType); Object mode = candidate.get("mode"); Object proxyTargetClass = candidate.get("proxyTargetClass"); if (mode != null && proxyTargetClass != null diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java index 877ba1bdda..04a249566d 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.ResourceLoaderAware; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.EnvironmentCapable; @@ -302,10 +303,11 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC for (TypeFilter tf : this.includeFilters) { if (tf.match(metadataReader, this.metadataReaderFactory)) { AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); - if (!ProfileHelper.isProfileAnnotationPresent(metadata)) { + if (!metadata.isAnnotated(Profile.class.getName())) { return true; } - return this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata)); + AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class); + return this.environment.acceptsProfiles(profile.getStringArray("value")); } } return false; diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java index 79467f9fcb..5268e633aa 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java @@ -25,6 +25,7 @@ import java.util.Set; import org.springframework.beans.factory.parsing.Location; import org.springframework.beans.factory.parsing.Problem; import org.springframework.beans.factory.parsing.ProblemReporter; +import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.core.io.DescriptiveResource; import org.springframework.core.io.Resource; import org.springframework.core.type.AnnotationMetadata; @@ -50,7 +51,8 @@ final class ConfigurationClass { private final Resource resource; - private final Map> importedResources = new LinkedHashMap>(); + private final Map> importedResources = + new LinkedHashMap>(); private final Set beanMethods = new LinkedHashSet(); @@ -154,11 +156,12 @@ final class ConfigurationClass { return this.beanMethods; } - public void addImportedResource(String importedResource, Class readerClass) { + public void addImportedResource( + String importedResource, Class readerClass) { this.importedResources.put(importedResource, readerClass); } - public Map> getImportedResources() { + public Map> getImportedResources() { return this.importedResources; } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java index f2e437bf05..7a333eb068 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java @@ -16,6 +16,8 @@ package org.springframework.context.annotation; +import static org.springframework.context.annotation.MetadataUtils.attributesFor; + import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; @@ -43,6 +45,7 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; @@ -187,15 +190,14 @@ class ConfigurationClassBeanDefinitionReader { beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE); // consider role - Map roleAttributes = metadata.getAnnotationAttributes(Role.class.getName()); - if (roleAttributes != null) { - int role = (Integer) roleAttributes.get("value"); - beanDef.setRole(role); + AnnotationAttributes role = attributesFor(metadata, Role.class); + if (role != null) { + beanDef.setRole(role.getInt("value")); } // consider name and any aliases - Map beanAttributes = metadata.getAnnotationAttributes(Bean.class.getName()); - List names = new ArrayList(Arrays.asList((String[]) beanAttributes.get("name"))); + AnnotationAttributes bean = attributesFor(metadata, Bean.class); + List names = new ArrayList(Arrays.asList(bean.getStringArray("name"))); String beanName = (names.size() > 0 ? names.remove(0) : beanMethod.getMetadata().getMethodName()); for (String alias : names) { this.registry.registerAlias(beanName, alias); @@ -222,40 +224,43 @@ class ConfigurationClassBeanDefinitionReader { // is this bean to be instantiated lazily? if (metadata.isAnnotated(Lazy.class.getName())) { - beanDef.setLazyInit((Boolean) metadata.getAnnotationAttributes(Lazy.class.getName()).get("value")); + AnnotationAttributes lazy = attributesFor(metadata, Lazy.class); + beanDef.setLazyInit(lazy.getBoolean("value")); } else if (configClass.getMetadata().isAnnotated(Lazy.class.getName())){ - beanDef.setLazyInit((Boolean) configClass.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get("value")); + AnnotationAttributes lazy = attributesFor(configClass.getMetadata(), Lazy.class); + beanDef.setLazyInit(lazy.getBoolean("value")); } if (metadata.isAnnotated(DependsOn.class.getName())) { - String[] dependsOn = (String[]) metadata.getAnnotationAttributes(DependsOn.class.getName()).get("value"); - if (dependsOn.length > 0) { - beanDef.setDependsOn(dependsOn); + AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class); + String[] otherBeans = dependsOn.getStringArray("value"); + if (otherBeans.length > 0) { + beanDef.setDependsOn(otherBeans); } } - Autowire autowire = (Autowire) beanAttributes.get("autowire"); + Autowire autowire = bean.getEnum("autowire", Autowire.class); if (autowire.isAutowire()) { beanDef.setAutowireMode(autowire.value()); } - String initMethodName = (String) beanAttributes.get("initMethod"); + String initMethodName = bean.getString("initMethod"); if (StringUtils.hasText(initMethodName)) { beanDef.setInitMethodName(initMethodName); } - String destroyMethodName = (String) beanAttributes.get("destroyMethod"); + String destroyMethodName = bean.getString("destroyMethod"); if (StringUtils.hasText(destroyMethodName)) { beanDef.setDestroyMethodName(destroyMethodName); } // consider scoping ScopedProxyMode proxyMode = ScopedProxyMode.NO; - Map scopeAttributes = metadata.getAnnotationAttributes(Scope.class.getName()); - if (scopeAttributes != null) { - beanDef.setScope((String) scopeAttributes.get("value")); - proxyMode = (ScopedProxyMode) scopeAttributes.get("proxyMode"); + AnnotationAttributes scope = attributesFor(metadata, Scope.class); + if (scope != null) { + beanDef.setScope(scope.getString("value")); + proxyMode = scope.getEnum("proxyMode", ScopedProxyMode.class); if (proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = ScopedProxyMode.NO; } @@ -277,15 +282,17 @@ class ConfigurationClassBeanDefinitionReader { } - private void loadBeanDefinitionsFromImportedResources(Map> importedResources) { + private void loadBeanDefinitionsFromImportedResources( + Map> importedResources) { + Map, BeanDefinitionReader> readerInstanceCache = new HashMap, BeanDefinitionReader>(); - for (Map.Entry> entry : importedResources.entrySet()) { + for (Map.Entry> entry : importedResources.entrySet()) { String resource = entry.getKey(); - Class readerClass = entry.getValue(); + Class readerClass = entry.getValue(); if (!readerInstanceCache.containsKey(readerClass)) { try { // Instantiate the specified BeanDefinitionReader - BeanDefinitionReader readerInstance = (BeanDefinitionReader) + BeanDefinitionReader readerInstance = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry); // Delegate the current ResourceLoader to it if possible diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index e3ec57e236..38b6679bbd 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -16,6 +16,8 @@ package org.springframework.context.annotation; +import static org.springframework.context.annotation.MetadataUtils.attributesFor; + import java.io.IOException; import java.lang.annotation.Annotation; import java.util.ArrayList; @@ -34,6 +36,7 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.parsing.Location; import org.springframework.beans.factory.parsing.Problem; import org.springframework.beans.factory.parsing.ProblemReporter; +import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.Environment; @@ -128,8 +131,9 @@ class ConfigurationClassParser { protected void processConfigurationClass(ConfigurationClass configClass) throws IOException { AnnotationMetadata metadata = configClass.getMetadata(); - if (this.environment != null && ProfileHelper.isProfileAnnotationPresent(metadata)) { - if (!this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata))) { + if (this.environment != null && metadata.isAnnotated(Profile.class.getName())) { + AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class); + if (!this.environment.acceptsProfiles(profile.getStringArray("value"))) { return; } } @@ -172,11 +176,11 @@ class ConfigurationClassParser { } // process any @PropertySource annotations - Map propertySourceAttributes = - metadata.getAnnotationAttributes(org.springframework.context.annotation.PropertySource.class.getName()); - if (propertySourceAttributes != null) { - String name = (String) propertySourceAttributes.get("name"); - String[] locations = (String[]) propertySourceAttributes.get("value"); + AnnotationAttributes propertySource = + attributesFor(metadata, org.springframework.context.annotation.PropertySource.class); + if (propertySource != null) { + String name = propertySource.getString("name"); + String[] locations = propertySource.getStringArray("value"); ClassLoader classLoader = this.resourceLoader.getClassLoader(); for (String location : locations) { location = this.environment.resolveRequiredPlaceholders(location); @@ -188,34 +192,32 @@ class ConfigurationClassParser { } // process any @ComponentScan annotions - AnnotationAttributes componentScan = MetadataUtils.attributesFor(metadata, ComponentScan.class); + AnnotationAttributes componentScan = attributesFor(metadata, ComponentScan.class); if (componentScan != null) { // the config class is annotated with @ComponentScan -> perform the scan immediately Set scannedBeanDefinitions = this.componentScanParser.parse(componentScan); // check the set of scanned definitions for any further config classes and parse recursively if necessary for (BeanDefinitionHolder holder : scannedBeanDefinitions) { - if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), metadataReaderFactory)) { + if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), this.metadataReaderFactory)) { this.parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName()); } } } // process any @Import annotations - List> allImportAttribs = + List imports = findAllAnnotationAttributes(Import.class, metadata.getClassName(), true); - for (Map importAttribs : allImportAttribs) { - processImport(configClass, (String[]) importAttribs.get("value"), true); + for (AnnotationAttributes importAnno : imports) { + processImport(configClass, importAnno.getStringArray("value"), true); } // process any @ImportResource annotations if (metadata.isAnnotated(ImportResource.class.getName())) { - String[] resources = (String[]) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("value"); - Class readerClass = (Class) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("reader"); - if (readerClass == null) { - throw new IllegalStateException("No reader class associated with imported resources: " + - StringUtils.arrayToCommaDelimitedString(resources)); - } + AnnotationAttributes importResource = attributesFor(metadata, ImportResource.class); + String[] resources = importResource.getStringArray("value"); + Class readerClass = + importResource.getClass("reader", BeanDefinitionReader.class); for (String resource : resources) { configClass.addImportedResource(resource, readerClass); } @@ -239,11 +241,11 @@ class ConfigurationClassParser { * @param annotatedClassName the class to inspect * @param classValuesAsString whether class attributes should be returned as strings */ - private List> findAllAnnotationAttributes( + private List findAllAnnotationAttributes( Class targetAnnotation, String annotatedClassName, boolean classValuesAsString) throws IOException { - List> allAttribs = new ArrayList>(); + List allAttribs = new ArrayList(); MetadataReader reader = this.metadataReaderFactory.getMetadataReader(annotatedClassName); AnnotationMetadata metadata = reader.getAnnotationMetadata(); @@ -253,16 +255,17 @@ class ConfigurationClassParser { if (annotationType.equals(targetAnnotationType)) { continue; } - MetadataReader metaReader = this.metadataReaderFactory.getMetadataReader(annotationType); - Map targetAttribs = - metaReader.getAnnotationMetadata().getAnnotationAttributes(targetAnnotationType, classValuesAsString); + AnnotationMetadata metaAnnotations = + this.metadataReaderFactory.getMetadataReader(annotationType).getAnnotationMetadata(); + AnnotationAttributes targetAttribs = + AnnotationAttributes.fromMap(metaAnnotations.getAnnotationAttributes(targetAnnotationType, classValuesAsString)); if (targetAttribs != null) { allAttribs.add(targetAttribs); } } - Map localAttribs = - metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString); + AnnotationAttributes localAttribs = + AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString)); if (localAttribs != null) { allAttribs.add(localAttribs); } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java index 7acf3abbf0..1d460fa773 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java @@ -27,6 +27,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving; import org.springframework.context.weaving.AspectJWeavingEnabler; import org.springframework.context.weaving.DefaultContextLoadTimeWeaver; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.instrument.classloading.LoadTimeWeaver; import org.springframework.util.Assert; @@ -46,7 +47,7 @@ import org.springframework.util.Assert; @Configuration public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoaderAware { - private Map enableLTW; + private AnnotationAttributes enableLTW; @Autowired(required=false) private LoadTimeWeavingConfigurer ltwConfigurer; @@ -54,7 +55,7 @@ public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoade private ClassLoader beanClassLoader; public void setImportMetadata(AnnotationMetadata importMetadata) { - this.enableLTW = importMetadata.getAnnotationAttributes(EnableLoadTimeWeaving.class.getName(), false); + this.enableLTW = MetadataUtils.attributesFor(importMetadata, EnableLoadTimeWeaving.class); Assert.notNull(this.enableLTW, "@EnableLoadTimeWeaving is not present on importing class " + importMetadata.getClassName()); @@ -79,7 +80,7 @@ public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoade loadTimeWeaver = new DefaultContextLoadTimeWeaver(this.beanClassLoader); } - switch ((AspectJWeaving) this.enableLTW.get("aspectjWeaving")) { + switch (this.enableLTW.getEnum("aspectjWeaving", AspectJWeaving.class)) { case DISABLED: // AJ weaving is disabled -> do nothing break; diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ProfileHelper.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ProfileHelper.java deleted file mode 100644 index 309aa877f1..0000000000 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ProfileHelper.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.context.annotation; - -import org.springframework.core.type.AnnotationMetadata; - -class ProfileHelper { - /** - * Return whether the given metadata includes Profile information, whether directly or - * through meta-annotation. - */ - static boolean isProfileAnnotationPresent(AnnotationMetadata metadata) { - return metadata.isAnnotated(Profile.class.getName()); - } - - /** - * Return the String[] of candidate profiles from {@link Profile#value()}. - */ - static String[] getCandidateProfiles(AnnotationMetadata metadata) { - return (String[])metadata.getAnnotationAttributes(Profile.class.getName()).get("value"); - } -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java b/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java index d0bbe8899b..57fd7b6945 100644 --- a/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,12 @@ package org.springframework.scheduling.annotation; import java.util.Collection; -import java.util.Map; import java.util.concurrent.Executor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportAware; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.Assert; @@ -37,12 +37,13 @@ import org.springframework.util.Assert; @Configuration public abstract class AbstractAsyncConfiguration implements ImportAware { - protected Map enableAsync; + protected AnnotationAttributes enableAsync; protected Executor executor; public void setImportMetadata(AnnotationMetadata importMetadata) { - enableAsync = importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false); - Assert.notNull(enableAsync, + this.enableAsync = AnnotationAttributes.fromMap( + importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false)); + Assert.notNull(this.enableAsync, "@EnableAsync is not present on importing class " + importMetadata.getClassName()); } diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java b/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java index 93589de785..e6ab3a37c8 100644 --- a/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/ProxyAsyncConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,13 +42,11 @@ public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration { @Bean(name=AnnotationConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public AsyncAnnotationBeanPostProcessor asyncAdvisor() { - Assert.notNull(enableAsync, "@EnableAsync annotation metadata was not injected"); + Assert.notNull(this.enableAsync, "@EnableAsync annotation metadata was not injected"); AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor(); - @SuppressWarnings("unchecked") - Class customAsyncAnnotation = - (Class) enableAsync.get("annotation"); + Class customAsyncAnnotation = enableAsync.getClass("annotation", Annotation.class); if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) { bpp.setAsyncAnnotationType(customAsyncAnnotation); } @@ -57,9 +55,9 @@ public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration { bpp.setExecutor(this.executor); } - bpp.setProxyTargetClass((Boolean) enableAsync.get("proxyTargetClass")); + bpp.setProxyTargetClass(this.enableAsync.getBoolean("proxyTargetClass")); - bpp.setOrder(((Integer) enableAsync.get("order"))); + bpp.setOrder(this.enableAsync.getInt("order")); return bpp; } diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java index 6d38318b21..180176452b 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,18 +19,19 @@ package org.springframework.context.annotation; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; +import static org.springframework.context.annotation.MetadataUtils.attributesFor; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import java.util.Map; import org.junit.Test; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor; @@ -55,8 +56,8 @@ public class ImportAwareTests { AnnotationMetadata importMetadata = importAwareConfig.importMetadata; assertThat("import metadata was not injected", importMetadata, notNullValue()); assertThat(importMetadata.getClassName(), is(ImportingConfig.class.getName())); - Map importAttribs = importMetadata.getAnnotationAttributes(Import.class.getName()); - Class[] importedClasses = (Class[])importAttribs.get("value"); + AnnotationAttributes importAttribs = attributesFor(importMetadata, Import.class); + Class[] importedClasses = importAttribs.getClassArray("value"); assertThat(importedClasses[0].getName(), is(ImportedConfig.class.getName())); } @@ -72,8 +73,8 @@ public class ImportAwareTests { AnnotationMetadata importMetadata = importAwareConfig.importMetadata; assertThat("import metadata was not injected", importMetadata, notNullValue()); assertThat(importMetadata.getClassName(), is(IndirectlyImportingConfig.class.getName())); - Map enableAttribs = importMetadata.getAnnotationAttributes(EnableImportedConfig.class.getName()); - String foo = (String)enableAttribs.get("foo"); + AnnotationAttributes enableAttribs = attributesFor(importMetadata, EnableImportedConfig.class); + String foo = enableAttribs.getString("foo"); assertThat(foo, is("xyz")); } @@ -129,7 +130,6 @@ public class ImportAwareTests { } public void setBeanFactory(BeanFactory beanFactory) throws BeansException { - System.out.println("ImportAwareTests.BPP.setBeanFactory()"); } } } diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java index 9ef8ad23f2..d5ea077155 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,11 +17,11 @@ package org.springframework.transaction.annotation; import java.util.Collection; -import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportAware; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.util.Assert; @@ -37,11 +37,12 @@ import org.springframework.util.Assert; @Configuration public abstract class AbstractTransactionManagementConfiguration implements ImportAware { - protected Map enableTx; + protected AnnotationAttributes enableTx; protected PlatformTransactionManager txManager; public void setImportMetadata(AnnotationMetadata importMetadata) { - this.enableTx = importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false); + this.enableTx = AnnotationAttributes.fromMap( + importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false)); Assert.notNull(this.enableTx, "@EnableTransactionManagement is not present on importing class " + importMetadata.getClassName()); diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java index 48a6436ace..c24d9e16f0 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,7 +44,7 @@ public class ProxyTransactionManagementConfiguration extends AbstractTransaction new BeanFactoryTransactionAttributeSourceAdvisor(); advisor.setTransactionAttributeSource(transactionAttributeSource()); advisor.setAdvice(transactionInterceptor()); - advisor.setOrder(((Integer)this.enableTx.get("order"))); + advisor.setOrder(this.enableTx.getInt("order")); return advisor; }