Example usage for org.springframework.core.annotation AnnotationAttributes getEnum

List of usage examples for org.springframework.core.annotation AnnotationAttributes getEnum

Introduction

In this page you can find the example usage for org.springframework.core.annotation AnnotationAttributes getEnum.

Prototype

@SuppressWarnings("unchecked")
public <E extends Enum<?>> E getEnum(String attributeName) 

Source Link

Document

Get the value stored under the specified attributeName as an enum.

Usage

From source file:org.spring.guice.annotation.GuiceModuleRegistrar.java

private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {

    List<TypeFilter> typeFilters = new ArrayList<TypeFilter>();
    FilterType filterType = filterAttributes.getEnum("type");

    for (Class<?> filterClass : filterAttributes.getClassArray("value")) {
        switch (filterType) {
        case ANNOTATION:
            Assert.isAssignable(Annotation.class, filterClass,
                    "An error occured when processing a @ComponentScan " + "ANNOTATION type filter: ");
            @SuppressWarnings("unchecked")
            Class<Annotation> annoClass = (Class<Annotation>) filterClass;
            typeFilters.add(new AnnotationTypeFilter(annoClass));
            break;
        case ASSIGNABLE_TYPE:
            typeFilters.add(new AssignableTypeFilter(filterClass));
            break;
        case CUSTOM:
            Assert.isAssignable(TypeFilter.class, filterClass,
                    "An error occured when processing a @ComponentScan " + "CUSTOM type filter: ");
            typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class));
            break;
        default:/*from www.j a  va2s . c  o m*/
            throw new IllegalArgumentException("Unknown filter type " + filterType);
        }
    }

    for (String expression : getPatterns(filterAttributes)) {

        String rawName = filterType.toString();

        if ("REGEX".equals(rawName)) {
            typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
        } else if ("ASPECTJ".equals(rawName)) {
            typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader()));
        } else {
            throw new IllegalArgumentException("Unknown filter type " + filterType);
        }
    }

    return typeFilters;
}

From source file:us.swcraft.springframework.cache.aerospike.config.annotation.AerospikeCacheConfiguration.java

public void setImportMetadata(AnnotationMetadata importMetadata) {
    Map<String, Object> enableAttrMap = importMetadata
            .getAnnotationAttributes(EnableAerospikeCacheManager.class.getName());
    AnnotationAttributes enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
    if (enableAttrs == null) {
        // search parent classes
        Class<?> currentClass = ClassUtils.resolveClassName(importMetadata.getClassName(), beanClassLoader);
        for (Class<?> classToInspect = currentClass; classToInspect != null; classToInspect = classToInspect
                .getSuperclass()) {//from   w w  w  .j  a  va2s . c o  m
            EnableAerospikeCacheManager enableWebSecurityAnnotation = AnnotationUtils
                    .findAnnotation(classToInspect, EnableAerospikeCacheManager.class);
            if (enableWebSecurityAnnotation == null) {
                continue;
            }
            enableAttrMap = AnnotationUtils.getAnnotationAttributes(enableWebSecurityAnnotation);
            enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
        }
    }
    defaultTimeToLiveInSeconds = enableAttrs.getNumber("defaultTimeToLiveInSeconds");
    defaultNamespace = enableAttrs.getString("defaultNamespace");
    defaultCacheName = enableAttrs.getString("defaultCacheName");
    compression = enableAttrs.getEnum("compression");
    serializerClass = enableAttrs.getClass("serializerClass");

    cachesConfiguration = enableAttrs.getAnnotationArray("caches");
}

From source file:org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.java

/**
 * Read the given {@link BeanMethod}, registering bean definitions
 * with the BeanDefinitionRegistry based on its contents.
 *//*from   ww w .  j ava  2  s .  c om*/
private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
    ConfigurationClass configClass = beanMethod.getConfigurationClass();
    MethodMetadata metadata = beanMethod.getMetadata();
    String methodName = metadata.getMethodName();

    // Do we need to mark the bean as skipped by its condition?
    if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
        configClass.skippedBeanMethods.add(methodName);
        return;
    }
    if (configClass.skippedBeanMethods.contains(methodName)) {
        return;
    }

    AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class);
    Assert.state(bean != null, "No @Bean annotation attributes");

    // Consider name and any aliases
    List<String> names = new ArrayList<>(Arrays.asList(bean.getStringArray("name")));
    String beanName = (!names.isEmpty() ? names.remove(0) : methodName);

    // Register aliases even when overridden
    for (String alias : names) {
        this.registry.registerAlias(beanName, alias);
    }

    // Has this effectively been overridden before (e.g. via XML)?
    if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
        if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) {
            throw new BeanDefinitionStoreException(
                    beanMethod.getConfigurationClass().getResource().getDescription(), beanName,
                    "Bean name derived from @Bean method '" + beanMethod.getMetadata().getMethodName()
                            + "' clashes with bean name for containing configuration class; please make those names unique!");
        }
        return;
    }

    ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata);
    beanDef.setResource(configClass.getResource());
    beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));

    if (metadata.isStatic()) {
        // static @Bean method
        beanDef.setBeanClassName(configClass.getMetadata().getClassName());
        beanDef.setFactoryMethodName(methodName);
    } else {
        // instance @Bean method
        beanDef.setFactoryBeanName(configClass.getBeanName());
        beanDef.setUniqueFactoryMethodName(methodName);
    }
    beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);

    AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata);

    Autowire autowire = bean.getEnum("autowire");
    if (autowire.isAutowire()) {
        beanDef.setAutowireMode(autowire.value());
    }

    String initMethodName = bean.getString("initMethod");
    if (StringUtils.hasText(initMethodName)) {
        beanDef.setInitMethodName(initMethodName);
    }

    String destroyMethodName = bean.getString("destroyMethod");
    beanDef.setDestroyMethodName(destroyMethodName);

    // Consider scoping
    ScopedProxyMode proxyMode = ScopedProxyMode.NO;
    AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, Scope.class);
    if (attributes != null) {
        beanDef.setScope(attributes.getString("value"));
        proxyMode = attributes.getEnum("proxyMode");
        if (proxyMode == ScopedProxyMode.DEFAULT) {
            proxyMode = ScopedProxyMode.NO;
        }
    }

    // Replace the original bean definition with the target one, if necessary
    BeanDefinition beanDefToRegister = beanDef;
    if (proxyMode != ScopedProxyMode.NO) {
        BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy(
                new BeanDefinitionHolder(beanDef, beanName), this.registry,
                proxyMode == ScopedProxyMode.TARGET_CLASS);
        beanDefToRegister = new ConfigurationClassBeanDefinition(
                (RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Registering bean definition for @Bean method %s.%s()",
                configClass.getMetadata().getClassName(), beanName));
    }

    this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}

From source file:org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration.java

@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
    Map<String, Object> attributeMap = importMetadata
            .getAnnotationAttributes(EnableRedisHttpSession.class.getName());
    AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
    this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
    String redisNamespaceValue = attributes.getString("redisNamespace");
    if (StringUtils.hasText(redisNamespaceValue)) {
        this.redisNamespace = this.embeddedValueResolver.resolveStringValue(redisNamespaceValue);
    }//ww w .  j a va2  s  . c om
    this.redisFlushMode = attributes.getEnum("redisFlushMode");
    String cleanupCron = attributes.getString("cleanupCron");
    if (StringUtils.hasText(cleanupCron)) {
        this.cleanupCron = cleanupCron;
    }
}

From source file:org.springframework.yarn.config.annotation.configuration.SpringYarnConfiguration.java

@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
    super.setImportMetadata(importMetadata);
    Map<String, Object> enableYarnMap = importMetadata.getAnnotationAttributes(EnableYarn.class.getName());
    AnnotationAttributes enableYarnAttrs = AnnotationAttributes.fromMap(enableYarnMap);
    log.info("Enabling " + enableYarnAttrs.getEnum("enable") + " for Yarn");
}

From source file:org.springframework.yarn.config.annotation.SpringYarnConfigurationImportSelector.java

@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {

    Map<String, Object> attrMap = importingClassMetadata.getAnnotationAttributes(EnableYarn.class.getName());
    AnnotationAttributes enableAttrs = AnnotationAttributes.fromMap(attrMap);
    Enable enable = enableAttrs.getEnum("enable");

    String[] configs = new String[2];

    if (enable == Enable.CLIENT) {
        configs[0] = SpringYarnClientConfiguration.class.getName();
    } else if (enable == Enable.APPMASTER) {
        configs[0] = SpringYarnAppmasterConfiguration.class.getName();
    } else if (enable == Enable.CONTAINER) {
        configs[0] = SpringYarnContainerConfiguration.class.getName();
    } else {//w ww. j  a  v  a2  s  .  c o  m
        configs[0] = SpringYarnConfiguration.class.getName();
    }

    configs[1] = ConfiguringBeanFactoryPostProcessorConfiguration.class.getName();

    if (log.isDebugEnabled()) {
        log.debug("Using configs " + configs[0] + "," + configs[1]);
    }

    return configs;
}