Java Code Examples for org.springframework.core.type.StandardMethodMetadata#getAnnotationAttributes()

The following examples show how to use org.springframework.core.type.StandardMethodMetadata#getAnnotationAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseAutoConfiguration.java    From graphql-spqr-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
private <T> T findQualifiedBeanByType(Class<? extends T> type, String qualifierValue, Class<? extends Annotation> qualifierType) {
    final NoSuchBeanDefinitionException noSuchBeanDefinitionException = new NoSuchBeanDefinitionException(qualifierValue, "No matching " + type.getSimpleName() +
            " bean found for qualifier " + qualifierValue + " of type " + qualifierType.getSimpleName() + " !");
    try {

        if (StringUtils.isEmpty(qualifierValue)) {
            if (qualifierType.equals(Qualifier.class)) {
                return Optional.of(
                        context.getBean(type))
                        .orElseThrow(() -> noSuchBeanDefinitionException);
            }
            return context.getBean(
                    Arrays.stream(context.getBeanNamesForAnnotation(qualifierType))
                            .filter(beanName -> type.isInstance(context.getBean(beanName)))
                            .findFirst()
                            .orElseThrow(() -> noSuchBeanDefinitionException),
                    type);
        }

        return BeanFactoryAnnotationUtils.qualifiedBeanOfType(context.getBeanFactory(), type, qualifierValue);
    } catch (NoSuchBeanDefinitionException noBeanException) {
        ConfigurableListableBeanFactory factory = context.getBeanFactory();

        for (String name : factory.getBeanDefinitionNames()) {
            BeanDefinition bd = factory.getBeanDefinition(name);

            if (bd.getSource() instanceof StandardMethodMetadata) {
                StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource();

                if (metadata.getReturnTypeName().equals(type.getName())) {
                    Map<String, Object> attributes = metadata.getAnnotationAttributes(qualifierType.getName());
                    if (null != attributes) {
                        if (qualifierType.equals(Qualifier.class)) {
                            if (qualifierValue.equals(attributes.get("value"))) {
                                return context.getBean(name, type);
                            }
                        }
                        return context.getBean(name, type);
                    }
                }
            }
        }

        throw noSuchBeanDefinitionException;
    }
}
 
Example 2
Source File: BaseAutoConfiguration.java    From graphql-spqr-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
private List<SpqrBean> findGraphQLApiBeans() {
    ConfigurableListableBeanFactory factory = context.getBeanFactory();

    List<SpqrBean> spqrBeans = new ArrayList<>();

    for (String beanName : factory.getBeanDefinitionNames()) {
        BeanDefinition bd = factory.getBeanDefinition(beanName);

        if (bd.getSource() instanceof StandardMethodMetadata) {
            StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource();

            Map<String, Object> attributes = metadata.getAnnotationAttributes(GraphQLApi.class.getName());
            if (null == attributes) {
                continue;
            }

            SpqrBean spqrBean = new SpqrBean(context, beanName, metadata.getIntrospectedMethod().getAnnotatedReturnType());

            Map<String, Object> withResolverBuildersAttributes = metadata.getAnnotationAttributes(WithResolverBuilders.class.getTypeName());
            if (withResolverBuildersAttributes != null) {
                AnnotationAttributes[] annotationAttributesArray = (AnnotationAttributes[]) withResolverBuildersAttributes.get("value");
                Arrays.stream(annotationAttributesArray)
                        .forEach(annotationAttributes ->
                                spqrBean.getResolverBuilders().add(
                                        new ResolverBuilderBeanIdentity(
                                                (Class<? extends ResolverBuilder>) annotationAttributes.get("value"),
                                                (String) annotationAttributes.get("qualifierValue"),
                                                (Class<? extends Annotation>) annotationAttributes.get("qualifierType"))
                                )
                        );
            } else {
                Map<String, Object> withResolverBuilderAttributes = metadata.getAnnotationAttributes(WithResolverBuilder.class.getTypeName());
                if (withResolverBuilderAttributes != null) {
                    spqrBean.getResolverBuilders().add(
                            new ResolverBuilderBeanIdentity(
                                    (Class<? extends ResolverBuilder>) withResolverBuilderAttributes.get("value"),
                                    (String) withResolverBuilderAttributes.get("qualifierValue"),
                                    (Class<? extends Annotation>) withResolverBuilderAttributes.get("qualifierType"))
                    );
                }
            }

            spqrBeans.add(spqrBean);
        }
    }

    return spqrBeans;
}