Search in sources :

Example 31 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-integration by spring-projects.

the class MqttParserUtils method parseCommon.

static void parseCommon(Element element, BeanDefinitionBuilder builder, ParserContext parserContext) {
    ValueHolder holder;
    int n = 0;
    String url = element.getAttribute("url");
    if (StringUtils.hasText(url)) {
        builder.addConstructorArgValue(url);
        holder = builder.getRawBeanDefinition().getConstructorArgumentValues().getIndexedArgumentValues().get(n++);
        holder.setType("java.lang.String");
    }
    builder.addConstructorArgValue(element.getAttribute("client-id"));
    holder = builder.getRawBeanDefinition().getConstructorArgumentValues().getIndexedArgumentValues().get(n);
    holder.setType("java.lang.String");
    String clientFactory = element.getAttribute("client-factory");
    if (StringUtils.hasText(clientFactory)) {
        builder.addConstructorArgReference(clientFactory);
    } else {
        if (!StringUtils.hasText(url)) {
            parserContext.getReaderContext().error("If no 'url' attribute is provided, a 'client-factory' " + "(with serverURIs) is required", element);
        }
        builder.addConstructorArgValue(new RootBeanDefinition(DefaultMqttPahoClientFactory.class));
    }
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout");
}
Also used : DefaultMqttPahoClientFactory(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)

Example 32 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spock by spockframework.

the class SpockMockPostprocessor method register.

/**
 * Register the processor with a {@link BeanDefinitionRegistry}. Not required when
 * using the Spocks {@link spock.lang.Specification} as registration is automatic.
 *
 * @param registry the bean definition registry
 * @param postProcessor the post processor class to register
 * @param definitions the initial mock/spy definitions
 */
@SuppressWarnings("unchecked")
static void register(BeanDefinitionRegistry registry, Class<? extends SpockMockPostprocessor> postProcessor, Set<Definition> definitions) {
    SpyPostProcessor.register(registry);
    BeanDefinition definition = getOrAddBeanDefinition(registry, postProcessor);
    ValueHolder constructorArg = definition.getConstructorArgumentValues().getIndexedArgumentValue(0, Set.class);
    Set<Definition> existing = (Set<Definition>) constructorArg.getValue();
    if (definitions != null) {
        existing.addAll(definitions);
    }
}
Also used : ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)

Example 33 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-boot by spring-projects.

the class MockitoPostProcessor method register.

/**
 * Register the processor with a {@link BeanDefinitionRegistry}. Not required when
 * using the {@link SpringRunner} as registration is automatic.
 * @param registry the bean definition registry
 * @param postProcessor the post processor class to register
 * @param definitions the initial mock/spy definitions
 */
@SuppressWarnings("unchecked")
public static void register(BeanDefinitionRegistry registry, Class<? extends MockitoPostProcessor> postProcessor, Set<Definition> definitions) {
    SpyPostProcessor.register(registry);
    BeanDefinition definition = getOrAddBeanDefinition(registry, postProcessor);
    ValueHolder constructorArg = definition.getConstructorArgumentValues().getIndexedArgumentValue(0, Set.class);
    Set<Definition> existing = (Set<Definition>) constructorArg.getValue();
    if (definitions != null) {
        existing.addAll(definitions);
    }
}
Also used : TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)

Example 34 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-native by spring-projects-experimental.

the class InjectedConstructionResolver method resolve.

@Override
public InjectedElementAttributes resolve(DefaultListableBeanFactory beanFactory, boolean required) {
    int argumentCount = this.executable.getParameterCount();
    List<Object> arguments = new ArrayList<>();
    Set<String> autowiredBeans = new LinkedHashSet<>(argumentCount);
    TypeConverter typeConverter = beanFactory.getTypeConverter();
    ConstructorArgumentValues argumentValues = resolveArgumentValues(beanFactory);
    for (int i = 0; i < argumentCount; i++) {
        MethodParameter methodParam = createMethodParameter(i);
        ValueHolder valueHolder = argumentValues.getIndexedArgumentValue(i, null);
        if (valueHolder != null) {
            if (valueHolder.isConverted()) {
                arguments.add(valueHolder.getConvertedValue());
            } else {
                Object userValue = beanFactory.getTypeConverter().convertIfNecessary(valueHolder.getValue(), methodParam.getParameterType());
                arguments.add(userValue);
            }
        } else {
            DependencyDescriptor depDescriptor = new DependencyDescriptor(methodParam, true);
            depDescriptor.setContainingClass(this.targetType);
            try {
                Object arg = resolveDependency(() -> beanFactory.resolveDependency(depDescriptor, beanName, autowiredBeans, typeConverter), methodParam.getParameterType());
                arguments.add(arg);
            } catch (BeansException ex) {
                throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(methodParam), ex);
            }
        }
    }
    return new InjectedElementAttributes(arguments);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) InjectionPoint(org.springframework.beans.factory.InjectionPoint) ArrayList(java.util.ArrayList) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) InjectionPoint(org.springframework.beans.factory.InjectionPoint) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) TypeConverter(org.springframework.beans.TypeConverter) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) MethodParameter(org.springframework.core.MethodParameter) BeansException(org.springframework.beans.BeansException)

Example 35 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-native by spring-projects-experimental.

the class InjectedConstructionResolver method resolveArgumentValues.

private ConstructorArgumentValues resolveArgumentValues(DefaultListableBeanFactory beanFactory) {
    ConstructorArgumentValues resolvedValues = new ConstructorArgumentValues();
    BeanDefinition beanDefinition = this.beanDefinitionResolver.apply(beanFactory);
    if (beanDefinition == null || !beanDefinition.hasConstructorArgumentValues()) {
        return resolvedValues;
    }
    ConstructorArgumentValues argumentValues = beanDefinition.getConstructorArgumentValues();
    ValueResolver valueResolver = BeanDefinitionValueResolverAccessor.get(beanFactory, this.beanName, beanDefinition);
    for (Map.Entry<Integer, ConstructorArgumentValues.ValueHolder> entry : argumentValues.getIndexedArgumentValues().entrySet()) {
        int index = entry.getKey();
        ConstructorArgumentValues.ValueHolder valueHolder = entry.getValue();
        if (valueHolder.isConverted()) {
            resolvedValues.addIndexedArgumentValue(index, valueHolder);
        } else {
            Object resolvedValue = valueResolver.resolveValueIfNecessary("constructor argument", valueHolder.getValue());
            ConstructorArgumentValues.ValueHolder resolvedValueHolder = new ConstructorArgumentValues.ValueHolder(resolvedValue, valueHolder.getType(), valueHolder.getName());
            resolvedValueHolder.setSource(valueHolder);
            resolvedValues.addIndexedArgumentValue(index, resolvedValueHolder);
        }
    }
    return resolvedValues;
}
Also used : ValueResolver(org.springframework.beans.factory.support.BeanDefinitionValueResolverAccessor.ValueResolver) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) Map(java.util.Map) InjectionPoint(org.springframework.beans.factory.InjectionPoint) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Aggregations

ValueHolder (org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)50 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)34 BeansException (org.springframework.beans.BeansException)13 InjectionPoint (org.springframework.beans.factory.InjectionPoint)13 LinkedHashSet (java.util.LinkedHashSet)12 TypeConverter (org.springframework.beans.TypeConverter)10 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)10 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 TypeMismatchException (org.springframework.beans.TypeMismatchException)8 BeanCreationException (org.springframework.beans.factory.BeanCreationException)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)7 Map (java.util.Map)7 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)7 MethodParameter (org.springframework.core.MethodParameter)7 Attr (org.w3c.dom.Attr)6 Set (java.util.Set)5 Test (org.junit.jupiter.api.Test)5 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 ParameterNameDiscoverer (org.springframework.core.ParameterNameDiscoverer)5