use of org.springframework.core.type.MethodMetadata in project spring-integration by spring-projects.
the class IntegrationConverterInitializer method initialize.
@Override
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
for (String beanName : registry.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
if (beanDefinition instanceof AnnotatedBeanDefinition) {
AnnotationMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
boolean hasIntegrationConverter = metadata.hasAnnotation(IntegrationConverter.class.getName());
if (!hasIntegrationConverter && beanDefinition.getSource() instanceof MethodMetadata) {
MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
hasIntegrationConverter = beanMethod.isAnnotated(IntegrationConverter.class.getName());
}
if (hasIntegrationConverter) {
this.registerConverter(registry, new RuntimeBeanReference(beanName));
}
}
}
}
use of org.springframework.core.type.MethodMetadata in project BroadleafCommerce by BroadleafCommerce.
the class MergeAnnotationAwareBeanDefinitionRegistryPostProcessor method postProcessBeanDefinitionRegistry.
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
Map<String, BeanDefinition> clientAnnotatedBeanPostProcessors = new LinkedHashMap<>();
Map<String, BeanDefinition> clientBeanPostProcessors = new LinkedHashMap<>();
for (String name : registry.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = registry.getBeanDefinition(name);
if (beanDefinition instanceof AnnotatedBeanDefinition) {
MethodMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata();
if (metadata != null) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(Merge.class.getName());
if (!MapUtils.isEmpty(attributes)) {
boolean isEarly = MapUtils.getBooleanValue(attributes, "early");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(isEarly ? EarlyStageMergeBeanPostProcessor.class : LateStageMergeBeanPostProcessor.class).setScope(BeanDefinition.SCOPE_SINGLETON).addPropertyValue("sourceRef", name).addPropertyValue("targetRef", attributes.get("targetRef")).addPropertyValue("placement", attributes.get("placement")).addPropertyValue("position", attributes.get("position"));
Class<MergeBeanStatusProvider> clazz = (Class<MergeBeanStatusProvider>) attributes.get("statusProvider");
if (MergeBeanStatusProvider.class != clazz) {
try {
builder.addPropertyValue("statusProvider", clazz.newInstance());
} catch (InstantiationException e) {
throw ExceptionHelper.refineException(e);
} catch (IllegalAccessException e) {
throw ExceptionHelper.refineException(e);
}
}
BeanDefinition definition = builder.getBeanDefinition();
String beanName = name + "_" + attributes.get("targetRef") + (isEarly ? "Early" : "Late") + ANNOTATED_POST_PROCESSOR_SUFFIX;
if (isBroadleafAnnotationBean(metadata)) {
registry.registerBeanDefinition(beanName, definition);
} else {
clientAnnotatedBeanPostProcessors.put(beanName, definition);
}
}
}
}
/*
If this is a client bean post processor, then remove it and store it away until we register all
framework post processors.
*/
if (beanDefinition.getBeanClassName() != null && (beanDefinition.getBeanClassName().equals(EarlyStageMergeBeanPostProcessor.class.getName()) || beanDefinition.getBeanClassName().equals(LateStageMergeBeanPostProcessor.class.getName()))) {
if (!isBroadleafBean(beanDefinition)) {
registry.removeBeanDefinition(name);
clientBeanPostProcessors.put(name, beanDefinition);
}
}
}
if (org.apache.commons.collections4.MapUtils.isNotEmpty(clientBeanPostProcessors)) {
for (Map.Entry<String, BeanDefinition> entry : clientBeanPostProcessors.entrySet()) {
registry.registerBeanDefinition(entry.getKey(), entry.getValue());
}
}
if (org.apache.commons.collections4.MapUtils.isNotEmpty(clientAnnotatedBeanPostProcessors)) {
for (Map.Entry<String, BeanDefinition> entry : clientAnnotatedBeanPostProcessors.entrySet()) {
registry.registerBeanDefinition(entry.getKey(), entry.getValue());
}
}
}
use of org.springframework.core.type.MethodMetadata in project sofa-boot by alipay.
the class ServiceBeanFactoryPostProcessor method generateSofaServiceDefinitionOnMethod.
private void generateSofaServiceDefinitionOnMethod(String beanId, AnnotatedBeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
Class<?> returnType;
Class<?> declaringClass;
List<Method> candidateMethods = new ArrayList<>();
MethodMetadata methodMetadata = beanDefinition.getFactoryMethodMetadata();
try {
returnType = ClassUtils.forName(methodMetadata.getReturnTypeName(), null);
declaringClass = ClassUtils.forName(methodMetadata.getDeclaringClassName(), null);
} catch (Throwable throwable) {
// it's impossible to catch throwable here
SofaLogger.error(ErrorCode.convert("01-02001", beanId), throwable);
return;
}
if (methodMetadata instanceof StandardMethodMetadata) {
candidateMethods.add(((StandardMethodMetadata) methodMetadata).getIntrospectedMethod());
} else {
for (Method m : declaringClass.getDeclaredMethods()) {
// check methodName and return type
if (!m.getName().equals(methodMetadata.getMethodName()) || !m.getReturnType().getTypeName().equals(methodMetadata.getReturnTypeName())) {
continue;
}
// check bean method
if (!AnnotatedElementUtils.hasAnnotation(m, Bean.class)) {
continue;
}
Bean bean = m.getAnnotation(Bean.class);
Set<String> beanNames = new HashSet<>();
beanNames.add(m.getName());
if (bean != null) {
beanNames.addAll(Arrays.asList(bean.name()));
beanNames.addAll(Arrays.asList(bean.value()));
}
// check bean name
if (!beanNames.contains(beanId)) {
continue;
}
candidateMethods.add(m);
}
}
if (candidateMethods.size() == 1) {
SofaService sofaServiceAnnotation = candidateMethods.get(0).getAnnotation(SofaService.class);
if (sofaServiceAnnotation == null) {
sofaServiceAnnotation = returnType.getAnnotation(SofaService.class);
}
generateSofaServiceDefinition(beanId, sofaServiceAnnotation, returnType, beanDefinition, beanFactory);
generateSofaReferenceDefinition(beanId, candidateMethods.get(0), beanFactory);
} else if (candidateMethods.size() > 1) {
for (Method m : candidateMethods) {
if (AnnotatedElementUtils.hasAnnotation(m, SofaService.class) || AnnotatedElementUtils.hasAnnotation(returnType, SofaService.class)) {
throw new FatalBeanException(ErrorCode.convert("01-02002", declaringClass.getCanonicalName()));
}
if (Stream.of(m.getParameterAnnotations()).flatMap(Stream::of).anyMatch(annotation -> annotation instanceof SofaReference)) {
throw new FatalBeanException(ErrorCode.convert("01-02003", declaringClass.getCanonicalName()));
}
}
}
}
use of org.springframework.core.type.MethodMetadata in project spring-integration by spring-projects.
the class IdempotentReceiverAutoProxyCreatorInitializer method annotated.
private void annotated(ConfigurableListableBeanFactory beanFactory, List<Map<String, String>> idempotentEndpointsMapping, String beanName, BeanDefinition beanDefinition) throws LinkageError {
if (beanDefinition.getSource() instanceof MethodMetadata) {
MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
String annotationType = IdempotentReceiver.class.getName();
if (beanMethod.isAnnotated(annotationType)) {
// NOSONAR never null
// NOSONAR
Object value = beanMethod.getAnnotationAttributes(annotationType).get("value");
if (value != null) {
Class<?> returnType;
if (beanMethod instanceof StandardMethodMetadata) {
returnType = ((StandardMethodMetadata) beanMethod).getIntrospectedMethod().getReturnType();
} else {
try {
returnType = ClassUtils.forName(beanMethod.getReturnTypeName(), beanFactory.getBeanClassLoader());
} catch (ClassNotFoundException e) {
throw new CannotLoadBeanClassException(beanDefinition.getDescription(), beanName, beanMethod.getReturnTypeName(), e);
}
}
String endpoint = beanName;
if (!MessageHandler.class.isAssignableFrom(returnType)) {
/*
MessageHandler beans, populated from @Bean methods, have a complex id,
including @Configuration bean name, method name and the Messaging annotation name.
The following pattern matches the bean name, regardless of the annotation name.
*/
endpoint = beanDefinition.getFactoryBeanName() + "." + beanName + ".*" + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX;
}
String[] interceptors = (String[]) value;
for (String interceptor : interceptors) {
Map<String, String> idempotentEndpoint = new ManagedMap<>();
idempotentEndpoint.put(interceptor, endpoint);
idempotentEndpointsMapping.add(idempotentEndpoint);
}
}
}
}
}
use of org.springframework.core.type.MethodMetadata in project resilience4j by resilience4j.
the class RefreshScopedAutoConfigurationTest method testRefreshScoped.
private static void testRefreshScoped(AssertableApplicationContext context, String beanName) {
BeanDefinition beanDefinition = context.getBeanFactory().getBeanDefinition(beanName);
MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
assertTrue(beanMethod.isAnnotated(RefreshScope.class.getName()));
}
Aggregations