org.springframework.core.type.StandardMethodMetadata Java Examples

The following examples show how to use org.springframework.core.type.StandardMethodMetadata. 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: SentinelBeanPostProcessor.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
		Class<?> beanType, String beanName) {
	if (checkSentinelProtect(beanDefinition, beanType, beanName)) {
		SentinelRestTemplate sentinelRestTemplate;
		if (beanDefinition.getSource() instanceof StandardMethodMetadata) {
			sentinelRestTemplate = ((StandardMethodMetadata) beanDefinition
					.getSource()).getIntrospectedMethod()
							.getAnnotation(SentinelRestTemplate.class);
		}
		else {
			sentinelRestTemplate = beanDefinition.getResolvedFactoryMethod()
					.getAnnotation(SentinelRestTemplate.class);
		}
		// check class and method validation
		checkSentinelRestTemplate(sentinelRestTemplate, beanName);
		cache.put(beanName, sentinelRestTemplate);
	}
}
 
Example #2
Source File: TestController.java    From saluki with Apache License 2.0 5 votes vote down vote up
private Collection<Object> getTypedBeansWithAnnotation(Class<? extends Annotation> annotationType)
    throws Exception {
  return Stream.of(applicationContext.getBeanNamesForAnnotation(annotationType)).filter(name -> {
    BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name);
    if (beanDefinition.getSource() instanceof StandardMethodMetadata) {
      StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource();
      return metadata.isAnnotated(annotationType.getName());
    }
    return null != applicationContext.getBeanFactory().findAnnotationOnBean(name, annotationType);
  }).map(name -> applicationContext.getBeanFactory().getBean(name)).collect(Collectors.toList());

}
 
Example #3
Source File: GrpcServiceRunner.java    From saluki with Apache License 2.0 5 votes vote down vote up
private Collection<Object> getTypedBeansWithAnnotation(Class<? extends Annotation> annotationType)
    throws Exception {
  return Stream.of(applicationContext.getBeanNamesForAnnotation(annotationType)).filter(name -> {
    BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name);
    if (beanDefinition.getSource() instanceof StandardMethodMetadata) {
      StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource();
      return metadata.isAnnotated(annotationType.getName());
    }
    return null != applicationContext.getBeanFactory().findAnnotationOnBean(name, annotationType);
  }).map(name -> applicationContext.getBeanFactory().getBean(name)).collect(Collectors.toList());

}
 
Example #4
Source File: RegionTemplateAutoConfiguration.java    From spring-boot-data-geode with Apache License 2.0 5 votes vote down vote up
private boolean isBeanWithGemfireTemplateDependency(@NonNull BeanFactory beanFactory,
		@NonNull BeanDefinition beanDefinition) {

	Predicate<Object> isGemfireTemplate = value -> value instanceof RuntimeBeanReference
		? beanFactory.isTypeMatch(((RuntimeBeanReference) value).getBeanName(), GemfireOperations.class)
		: value instanceof GemfireOperations;

	boolean match = beanDefinition.getConstructorArgumentValues().getGenericArgumentValues().stream()
		.map(ConstructorArgumentValues.ValueHolder::getValue)
		.anyMatch(isGemfireTemplate);

	match |= match || beanDefinition.getPropertyValues().getPropertyValueList().stream()
		.map(PropertyValue::getValue)
		.anyMatch(isGemfireTemplate);

	match |= match || Optional.of(beanDefinition)
		.filter(AnnotatedBeanDefinition.class::isInstance)
		.map(AnnotatedBeanDefinition.class::cast)
		.map(AnnotatedBeanDefinition::getFactoryMethodMetadata)
		.filter(StandardMethodMetadata.class::isInstance)
		.map(StandardMethodMetadata.class::cast)
		.map(StandardMethodMetadata::getIntrospectedMethod)
		.map(method -> Arrays.stream(ArrayUtils.nullSafeArray(method.getParameterTypes(), Class.class))
			.filter(Objects::nonNull)
			.anyMatch(GemfireOperations.class::isAssignableFrom)
		).orElse(false);

	return match;
}
 
Example #5
Source File: BeanFactoryAwareFunctionRegistry.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
private String getQualifier(String key) {
	if (this.applicationContext != null && this.applicationContext.getBeanFactory().containsBeanDefinition(key)) {
		BeanDefinition beanDefinition = this.applicationContext.getBeanFactory().getBeanDefinition(key);
		Object source = beanDefinition.getSource();
		if (source instanceof StandardMethodMetadata) {
			StandardMethodMetadata metadata = (StandardMethodMetadata) source;
			Qualifier qualifier = AnnotatedElementUtils.findMergedAnnotation(metadata.getIntrospectedMethod(),
				Qualifier.class);
			if (qualifier != null && qualifier.value().length() > 0) {
				return qualifier.value();
			}
		}
	}
	return key;
}
 
Example #6
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 #7
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;
}