Refactor to consistent use of AnnotationAttributes

Uses of AnnotationMetadata#getAnnotationAttributes throughout the
framework have been updated to use the new AnnotationAttributes API in
order to take advantage of the more concise, expressive and type-safe
methods there.

All changes are binary compatible to the 3.1.0 public API, save
the exception below.

A minor binary compatibility issue has been introduced in
AbstractCachingConfiguration, AbstractAsyncConfiguration and
AbstractTransactionManagementConfiguration when updating their
protected Map<String, Object> fields representing annotation attributes
to use the new AnnotationAttributes API. This is a negligible breakage,
however, as the likelilhood of users subclassing these types is very
low, the classes have only been in existence for a short time (further
reducing the likelihood), and it is a source-compatible change given
that AnnotationAttributes is assignable to Map<String, Object>.
This commit is contained in:
Chris Beams 2012-02-07 19:45:46 +01:00
parent d9f7fdd120
commit bf541db5b0
20 changed files with 151 additions and 169 deletions

View File

@ -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<String, Object> enableCaching;
protected AnnotationAttributes enableCaching;
protected CacheManager cacheManager;
protected KeyGenerator keyGenerator;
@ -52,8 +51,8 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
private Collection<CachingConfigurer> 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());

View File

@ -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;
}

View File

@ -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<A extends Annotation> 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<String, Object> 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;

View File

@ -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;
}
}

View File

@ -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<String> types = amd.getAnnotationTypes();
String beanName = null;
for (String type : types) {
Map<String, Object> 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)) {

View File

@ -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"));
}
}
}

View File

@ -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<String, Object> 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;
}

View File

@ -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<String, Object> 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);
}
}

View File

@ -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<String> annoTypes = importingClassMetadata.getAnnotationTypes();
for (String annoType : annoTypes) {
Map<String, Object> candidate = importingClassMetadata.getAnnotationAttributes(annoType);
AnnotationAttributes candidate = attributesFor(importingClassMetadata, annoType);
Object mode = candidate.get("mode");
Object proxyTargetClass = candidate.get("proxyTargetClass");
if (mode != null && proxyTargetClass != null

View File

@ -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;

View File

@ -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<String, Class<?>> importedResources = new LinkedHashMap<String, Class<?>>();
private final Map<String, Class<? extends BeanDefinitionReader>> importedResources =
new LinkedHashMap<String, Class<? extends BeanDefinitionReader>>();
private final Set<BeanMethod> beanMethods = new LinkedHashSet<BeanMethod>();
@ -154,11 +156,12 @@ final class ConfigurationClass {
return this.beanMethods;
}
public void addImportedResource(String importedResource, Class<?> readerClass) {
public void addImportedResource(
String importedResource, Class<? extends BeanDefinitionReader> readerClass) {
this.importedResources.put(importedResource, readerClass);
}
public Map<String, Class<?>> getImportedResources() {
public Map<String, Class<? extends BeanDefinitionReader>> getImportedResources() {
return this.importedResources;
}

View File

@ -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<String, Object> 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<String, Object> beanAttributes = metadata.getAnnotationAttributes(Bean.class.getName());
List<String> names = new ArrayList<String>(Arrays.asList((String[]) beanAttributes.get("name")));
AnnotationAttributes bean = attributesFor(metadata, Bean.class);
List<String> names = new ArrayList<String>(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<String, Object> 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<String, Class<?>> importedResources) {
private void loadBeanDefinitionsFromImportedResources(
Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<Class<?>, BeanDefinitionReader>();
for (Map.Entry<String, Class<?>> entry : importedResources.entrySet()) {
for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
String resource = entry.getKey();
Class<?> readerClass = entry.getValue();
Class<? extends BeanDefinitionReader> 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

View File

@ -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<String, Object> 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<BeanDefinitionHolder> 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<Map<String, Object>> allImportAttribs =
List<AnnotationAttributes> imports =
findAllAnnotationAttributes(Import.class, metadata.getClassName(), true);
for (Map<String, Object> 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<? extends BeanDefinitionReader> 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<Map<String, Object>> findAllAnnotationAttributes(
private List<AnnotationAttributes> findAllAnnotationAttributes(
Class<? extends Annotation> targetAnnotation, String annotatedClassName,
boolean classValuesAsString) throws IOException {
List<Map<String, Object>> allAttribs = new ArrayList<Map<String, Object>>();
List<AnnotationAttributes> allAttribs = new ArrayList<AnnotationAttributes>();
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<String, Object> 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<String, Object> localAttribs =
metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString);
AnnotationAttributes localAttribs =
AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString));
if (localAttribs != null) {
allAttribs.add(localAttribs);
}

View File

@ -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<String, Object> 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;

View File

@ -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");
}
}

View File

@ -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<String, Object> 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());
}

View File

@ -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<? extends Annotation> customAsyncAnnotation =
(Class<? extends Annotation>) enableAsync.get("annotation");
Class<? extends Annotation> 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;
}

View File

@ -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<String, Object> 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<String, Object> 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()");
}
}
}

View File

@ -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<String, Object> 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());

View File

@ -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;
}