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

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

Introduction

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

Prototype

default boolean isAnnotated(String annotationName) 

Source Link

Document

Determine whether the underlying element has an annotation or meta-annotation of the given type defined.

Usage

From source file:lodsve.core.condition.OnWebApplicationCondition.java

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    boolean webApplicationRequired = metadata.isAnnotated(ConditionalOnWebApplication.class.getName());
    ConditionOutcome webApplication = isWebApplication(context, metadata);

    if (webApplicationRequired && !webApplication.isMatch()) {
        return ConditionOutcome.noMatch(webApplication.getMessage());
    }/*from  ww w  .  j a  va2 s  . c  o  m*/

    if (!webApplicationRequired && webApplication.isMatch()) {
        return ConditionOutcome.noMatch(webApplication.getMessage());
    }

    return ConditionOutcome.match(webApplication.getMessage());
}

From source file:org.juiser.spring.boot.config.JuiserSpringSecurityCondition.java

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {

    ConditionMessage matchMessage = ConditionMessage.empty();

    boolean enabled = isSpringSecurityEnabled(context);

    if (metadata.isAnnotated(ConditionalOnJuiserSpringSecurityEnabled.class.getName())) {
        if (!enabled) {
            return ConditionOutcome
                    .noMatch(ConditionMessage.forCondition(ConditionalOnJuiserSpringSecurityEnabled.class)
                            .didNotFind("spring security enabled").atAll());
        }// w w w .ja v  a  2s.c  o m
        matchMessage = matchMessage.andCondition(ConditionalOnJuiserSpringSecurityEnabled.class)
                .foundExactly("spring security enabled");
    }

    if (metadata.isAnnotated(ConditionalOnJuiserSpringSecurityDisabled.class.getName())) {
        if (enabled) {
            return ConditionOutcome
                    .noMatch(ConditionMessage.forCondition(ConditionalOnJuiserSpringSecurityDisabled.class)
                            .didNotFind("spring security disabled").atAll());
        }
        matchMessage = matchMessage.andCondition(ConditionalOnJuiserSpringSecurityDisabled.class)
                .didNotFind("spring security disabled").atAll();
    }

    return ConditionOutcome.match(matchMessage);
}

From source file:lodsve.core.condition.OnBeanCondition.java

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    StringBuilder matchMessage = new StringBuilder();
    if (metadata.isAnnotated(ConditionalOnBean.class.getName())) {
        BeanSearchSpec spec = new BeanSearchSpec(context, metadata, ConditionalOnBean.class);
        List<String> matching = getMatchingBeans(context, spec);
        if (matching.isEmpty()) {
            return ConditionOutcome.noMatch("@ConditionalOnBean " + spec + " found no beans");
        }//from  www. j  a  v a  2 s .co m
        matchMessage.append("@ConditionalOnBean ").append(spec).append(" found the following ")
                .append(matching);
    }
    if (metadata.isAnnotated(ConditionalOnMissingBean.class.getName())) {
        BeanSearchSpec spec = new BeanSearchSpec(context, metadata, ConditionalOnMissingBean.class);
        List<String> matching = getMatchingBeans(context, spec);
        if (!matching.isEmpty()) {
            return ConditionOutcome
                    .noMatch("@ConditionalOnMissingBean " + spec + " found the following " + matching);
        }
        matchMessage.append(matchMessage.length() == 0 ? "" : " ");
        matchMessage.append("@ConditionalOnMissingBean ").append(spec).append(" found no beans");
    }
    return ConditionOutcome.match(matchMessage.toString());
}

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

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationClass().getName(),
            true);/*from www . j a  va  2 s  . com*/
    final List<String> beanClasses = collect(attributes, "value");
    final List<String> beanNames = collect(attributes, "name");

    if (beanClasses.size() == 0) {
        if (metadata instanceof MethodMetadata && metadata.isAnnotated(Bean.class.getName())) {
            try {
                final MethodMetadata methodMetadata = (MethodMetadata) metadata;
                // We should be safe to load at this point since we are in the
                // REGISTER_BEAN phase
                Class<?> configClass = ClassUtils.forName(methodMetadata.getDeclaringClassName(),
                        context.getClassLoader());
                ReflectionUtils.doWithMethods(configClass, new MethodCallback() {
                    @Override
                    public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                        if (methodMetadata.getMethodName().equals(method.getName())) {
                            beanClasses.add(method.getReturnType().getName());
                        }
                    }
                });
            } catch (Exception ex) {
                // swallow exception and continue
            }
        }
    }

    Assert.isTrue(beanClasses.size() > 0 || beanNames.size() > 0,
            "@" + ClassUtils.getShortName(annotationClass()) + " annotations must specify at least one bean");

    return matches(context, metadata, beanClasses, beanNames);
}

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

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    StringBuffer matchMessage = new StringBuffer();
    if (metadata.isAnnotated(ConditionalOnBean.class.getName())) {
        BeanSearchSpec spec = new BeanSearchSpec(context, metadata, ConditionalOnBean.class);
        List<String> matching = getMatchingBeans(context, spec);
        if (matching.isEmpty()) {
            return ConditionOutcome.noMatch("@ConditionalOnBean " + spec + " found no beans");
        }//from w  w  w . ja  va2s  . co m
        matchMessage.append("@ConditionalOnBean " + spec + " found the following " + matching);
    }
    if (metadata.isAnnotated(ConditionalOnSingleCandidate.class.getName())) {
        BeanSearchSpec spec = new SingleCandidateBeanSearchSpec(context, metadata,
                ConditionalOnSingleCandidate.class);
        List<String> matching = getMatchingBeans(context, spec);
        if (matching.isEmpty()) {
            return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec + " found no beans");
        } else if (!hasSingleAutowireCandidate(context.getBeanFactory(), matching)) {
            return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec
                    + " found no primary candidate amongst the" + " following " + matching);
        }
        matchMessage.append("@ConditionalOnSingleCandidate " + spec + " found "
                + "a primary candidate amongst the following " + matching);
    }
    if (metadata.isAnnotated(ConditionalOnMissingBean.class.getName())) {
        BeanSearchSpec spec = new BeanSearchSpec(context, metadata, ConditionalOnMissingBean.class);
        List<String> matching = getMatchingBeans(context, spec);
        if (!matching.isEmpty()) {
            return ConditionOutcome
                    .noMatch("@ConditionalOnMissingBean " + spec + " found the following " + matching);
        }
        matchMessage.append(matchMessage.length() == 0 ? "" : " ");
        matchMessage.append("@ConditionalOnMissingBean " + spec + " found no beans");
    }
    return ConditionOutcome.match(matchMessage.toString());
}