Search in sources :

Example 26 with MethodMetadata

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));
            }
        }
    }
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) MethodMetadata(org.springframework.core.type.MethodMetadata) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 27 with MethodMetadata

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());
        }
    }
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) LinkedHashMap(java.util.LinkedHashMap) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) MethodMetadata(org.springframework.core.type.MethodMetadata) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 28 with MethodMetadata

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()));
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) SofaBeanNameGenerator(com.alipay.sofa.runtime.spring.bean.SofaBeanNameGenerator) SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) SofaLogger(com.alipay.sofa.runtime.log.SofaLogger) Method(java.lang.reflect.Method) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) ClassUtils(org.springframework.util.ClassUtils) MethodMetadata(org.springframework.core.type.MethodMetadata) AbstractContractDefinitionParser(com.alipay.sofa.runtime.spring.parser.AbstractContractDefinitionParser) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) Environment(org.springframework.core.env.Environment) Annotation(java.lang.annotation.Annotation) Binding(com.alipay.sofa.runtime.spi.binding.Binding) BindingConverterFactory(com.alipay.sofa.runtime.spi.service.BindingConverterFactory) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AnnotatedElementUtils(org.springframework.core.annotation.AnnotatedElementUtils) PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) Ordered(org.springframework.core.Ordered) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) BeanDefinitionUtil(com.alipay.sofa.boot.util.BeanDefinitionUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ServiceDefinitionParser(com.alipay.sofa.runtime.spring.parser.ServiceDefinitionParser) SofaRuntimeContext(com.alipay.sofa.runtime.spi.component.SofaRuntimeContext) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ErrorCode(com.alipay.sofa.boot.error.ErrorCode) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Order(org.springframework.core.annotation.Order) ScannedGenericBeanDefinition(org.springframework.context.annotation.ScannedGenericBeanDefinition) FatalBeanException(org.springframework.beans.FatalBeanException) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ObjectUtils(org.springframework.util.ObjectUtils) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) BeansException(org.springframework.beans.BeansException) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ApplicationContext(org.springframework.context.ApplicationContext) EnvironmentAware(org.springframework.context.EnvironmentAware) Bean(org.springframework.context.annotation.Bean) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) AnnotationWrapperBuilder(com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder) Assert(org.springframework.util.Assert) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) Bean(org.springframework.context.annotation.Bean) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) FatalBeanException(org.springframework.beans.FatalBeanException) MethodMetadata(org.springframework.core.type.MethodMetadata) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) Stream(java.util.stream.Stream) HashSet(java.util.HashSet)

Example 29 with MethodMetadata

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);
                }
            }
        }
    }
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) CannotLoadBeanClassException(org.springframework.beans.factory.CannotLoadBeanClassException) MethodMetadata(org.springframework.core.type.MethodMetadata) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 30 with MethodMetadata

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()));
}
Also used : MethodMetadata(org.springframework.core.type.MethodMetadata) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

MethodMetadata (org.springframework.core.type.MethodMetadata)73 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)33 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)32 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)20 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)15 StandardMethodMetadata (org.springframework.core.type.StandardMethodMetadata)14 ArrayList (java.util.ArrayList)12 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)12 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)12 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)11 Map (java.util.Map)10 StandardAnnotationMetadata (org.springframework.core.type.StandardAnnotationMetadata)10 Method (java.lang.reflect.Method)9 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)9 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)8 Bean (org.springframework.context.annotation.Bean)8 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)6 NestedIOException (org.springframework.core.NestedIOException)6