Example usage for org.springframework.core.annotation AnnotationUtils getDefaultValue

List of usage examples for org.springframework.core.annotation AnnotationUtils getDefaultValue

Introduction

In this page you can find the example usage for org.springframework.core.annotation AnnotationUtils getDefaultValue.

Prototype

@Nullable
public static Object getDefaultValue(Class<? extends Annotation> annotationType) 

Source Link

Document

Retrieve the default value of the value attribute of a single-element Annotation, given the Class annotation type .

Usage

From source file:org.springframework.integration.config.PublisherRegistrar.java

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
        BeanDefinitionRegistry registry) {
    Map<String, Object> annotationAttributes = importingClassMetadata
            .getAnnotationAttributes(EnablePublisher.class.getName());

    String value = (annotationAttributes == null
            ? (String) AnnotationUtils.getDefaultValue(EnablePublisher.class)
            : (String) annotationAttributes.get("value"));
    if (!registry.containsBeanDefinition(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME)) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder
                .genericBeanDefinition(PublisherAnnotationBeanPostProcessor.class)
                .setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

        if (StringUtils.hasText(value)) {
            builder.addPropertyValue("defaultChannelName", value);
            if (logger.isInfoEnabled()) {
                logger.info("Setting '@Publisher' default-output-channel to '" + value + "'.");
            }/*ww  w . j ava2  s  .co m*/
        }

        registry.registerBeanDefinition(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME,
                builder.getBeanDefinition());
    } else {
        BeanDefinition beanDefinition = registry
                .getBeanDefinition(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME);
        MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
        PropertyValue defaultChannelPropertyValue = propertyValues.getPropertyValue("defaultChannelName");
        if (StringUtils.hasText(value)) {
            if (defaultChannelPropertyValue == null) {
                propertyValues.addPropertyValue("defaultChannelName", value);
                if (logger.isInfoEnabled()) {
                    logger.info("Setting '@Publisher' default-output-channel to '" + value + "'.");
                }
            } else if (!value.equals(defaultChannelPropertyValue.getValue())) {
                throw new BeanDefinitionStoreException("When more than one enable publisher definition "
                        + "(@EnablePublisher or <annotation-config>)"
                        + " is found in the context, they all must have the same 'default-publisher-channel' value.");
            }
        }
    }
}

From source file:org.springframework.test.annotation.ProfileValueUtils.java

/**
 * Retrieves the {@link ProfileValueSource} type for the specified
 * {@link Class test class} as configured via the
 * {@link ProfileValueSourceConfiguration
 * &#064;ProfileValueSourceConfiguration} annotation and instantiates a new
 * instance of that type.//from   w ww  . j  av a  2  s . c  o m
 * <p>If {@link ProfileValueSourceConfiguration
 * &#064;ProfileValueSourceConfiguration} is not present on the specified
 * class or if a custom {@link ProfileValueSource} is not declared, the
 * default {@link SystemProfileValueSource} will be returned instead.
 * @param testClass The test class for which the ProfileValueSource should
 * be retrieved
 * @return the configured (or default) ProfileValueSource for the specified
 * class
 * @see SystemProfileValueSource
 */
@SuppressWarnings("unchecked")
public static ProfileValueSource retrieveProfileValueSource(Class<?> testClass) {
    Assert.notNull(testClass, "testClass must not be null");

    Class<ProfileValueSourceConfiguration> annotationType = ProfileValueSourceConfiguration.class;
    ProfileValueSourceConfiguration config = AnnotatedElementUtils.findMergedAnnotation(testClass,
            annotationType);
    if (logger.isDebugEnabled()) {
        logger.debug("Retrieved @ProfileValueSourceConfiguration [" + config + "] for test class ["
                + testClass.getName() + "]");
    }

    Class<? extends ProfileValueSource> profileValueSourceType;
    if (config != null) {
        profileValueSourceType = config.value();
    } else {
        profileValueSourceType = (Class<? extends ProfileValueSource>) AnnotationUtils
                .getDefaultValue(annotationType);
        Assert.state(profileValueSourceType != null, "No default ProfileValueSource class");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Retrieved ProfileValueSource type [" + profileValueSourceType + "] for class ["
                + testClass.getName() + "]");
    }

    ProfileValueSource profileValueSource;
    if (SystemProfileValueSource.class == profileValueSourceType) {
        profileValueSource = SystemProfileValueSource.getInstance();
    } else {
        try {
            profileValueSource = ReflectionUtils.accessibleConstructor(profileValueSourceType).newInstance();
        } catch (Exception ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Could not instantiate a ProfileValueSource of type [" + profileValueSourceType
                        + "] for class [" + testClass.getName() + "]: using default.", ex);
            }
            profileValueSource = SystemProfileValueSource.getInstance();
        }
    }

    return profileValueSource;
}