Example usage for org.springframework.core.type AnnotatedTypeMetadata getAnnotationAttributes

List of usage examples for org.springframework.core.type AnnotatedTypeMetadata getAnnotationAttributes

Introduction

In this page you can find the example usage for org.springframework.core.type AnnotatedTypeMetadata getAnnotationAttributes.

Prototype

@Nullable
default Map<String, Object> getAnnotationAttributes(String annotationName) 

Source Link

Document

Retrieve the attributes of the annotation of the given type, if any (i.e.

Usage

From source file:org.apache.atlas.utils.OnAtlasPropertyCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    boolean matches = false;
    String propertyName = (String) metadata.getAnnotationAttributes(ConditionalOnAtlasProperty.class.getName())
            .get("property");
    boolean isDefault = (Boolean) metadata.getAnnotationAttributes(ConditionalOnAtlasProperty.class.getName())
            .get("isDefault");
    String className = ((AnnotationMetadataReadingVisitor) metadata).getClassName();

    try {/*  w  w  w  . j  a v a 2  s.com*/
        Configuration configuration = ApplicationProperties.get();
        String configuredProperty = configuration.getString(propertyName);
        if (StringUtils.isNotEmpty(configuredProperty)) {
            matches = configuredProperty.equals(className);
        } else if (isDefault)
            matches = true;
    } catch (AtlasException e) {
        LOG.error("Unable to load atlas properties. Dependent bean configuration may fail");
    }
    return matches;
}

From source file:org.springframework.boot.autoconfigure.condition.AbstractOnBeanCondition.java

protected boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata, List<String> beanClasses,
        List<String> beanNames) throws LinkageError {

    String checking = ConditionLogUtils.getPrefix(this.logger, metadata);

    SearchStrategy search = (SearchStrategy) metadata.getAnnotationAttributes(annotationClass().getName())
            .get("search");
    search = search == null ? SearchStrategy.ALL : search;

    boolean considerHierarchy = search == SearchStrategy.ALL;
    boolean parentOnly = search == SearchStrategy.PARENTS;

    List<String> beanClassesFound = new ArrayList<String>();
    List<String> beanNamesFound = new ArrayList<String>();

    // eagerInit set to false to prevent early instantiation (some
    // factory beans will not be able to determine their object type at this
    // stage, so those are not eligible for matching this condition)
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (parentOnly) {
        BeanFactory parent = beanFactory.getParentBeanFactory();
        if (!(parent instanceof ConfigurableListableBeanFactory)) {
            throw new IllegalStateException(
                    "Cannot use parentOnly if parent is not ConfigurableListableBeanFactory");
        }/*  w w w .  j av  a  2s .c o  m*/
        beanFactory = (ConfigurableListableBeanFactory) parent;
    }
    for (String beanClass : beanClasses) {
        try {
            Class<?> type = ClassUtils.forName(beanClass, context.getClassLoader());
            String[] beans = (considerHierarchy
                    ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, type, false, false)
                    : beanFactory.getBeanNamesForType(type, false, false));
            if (beans.length != 0) {
                beanClassesFound.add(beanClass);
            }
        } catch (ClassNotFoundException ex) {
            // swallow exception and continue
        }
    }
    for (String beanName : beanNames) {
        if (considerHierarchy ? beanFactory.containsBean(beanName) : beanFactory.containsLocalBean(beanName)) {
            beanNamesFound.add(beanName);
        }
    }

    boolean result = evaluate(beanClassesFound, beanNamesFound);
    if (this.logger.isDebugEnabled()) {
        logFoundResults(checking, "class", beanClasses, beanClassesFound);
        logFoundResults(checking, "name", beanNames, beanClassesFound);
        this.logger.debug(checking + "Match result is: " + result);
    }
    return result;
}

From source file:org.springframework.boot.autoconfigure.condition.OnExpressionCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

    String checking = ConditionLogUtils.getPrefix(logger, metadata);

    String value = (String) metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName())
            .get("value");
    if (!value.startsWith("#{")) {
        // For convenience allow user to provide bare expression with no #{} wrapper
        value = "#{" + value + "}";
    }//w  ww .  j a  v a 2 s  . co  m
    if (logger.isDebugEnabled()) {
        StringBuilder builder = new StringBuilder(checking).append("Evaluating expression");
        if (metadata instanceof ClassMetadata) {
            builder.append(" on " + ((ClassMetadata) metadata).getClassName());
        }
        builder.append(": " + value);
        logger.debug(builder.toString());
    }
    // Explicitly allow environment placeholders inside the expression
    value = context.getEnvironment().resolvePlaceholders(value);
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();
    BeanExpressionContext expressionContext = (beanFactory != null)
            ? new BeanExpressionContext(beanFactory, null)
            : null;
    if (resolver == null) {
        resolver = new StandardBeanExpressionResolver();
    }
    boolean result = (Boolean) resolver.evaluate(value, expressionContext);
    if (logger.isDebugEnabled()) {
        logger.debug(checking + "Finished matching and result is matches=" + result);
    }
    return result;
}