Search in sources :

Example 11 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project blog_demos by zq2599.

the class ConstructorResolver method resolveConstructorArguments.

/**
 * Resolve the constructor arguments for this bean into the resolvedValues object.
 * This may involve looking up other beans.
 * This method is also used for handling invocations of static factory methods.
 */
private int resolveConstructorArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw, ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues) {
    TypeConverter converter = (this.beanFactory.getCustomTypeConverter() != null ? this.beanFactory.getCustomTypeConverter() : bw);
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
    int minNrOfArgs = cargs.getArgumentCount();
    for (Map.Entry<Integer, ValueHolder> entry : cargs.getIndexedArgumentValues().entrySet()) {
        int index = entry.getKey();
        if (index < 0) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid constructor argument index: " + index);
        }
        if (index > minNrOfArgs) {
            minNrOfArgs = index + 1;
        }
        ValueHolder valueHolder = entry.getValue();
        if (valueHolder.isConverted()) {
            resolvedValues.addIndexedArgumentValue(index, valueHolder);
        } else {
            Object resolvedValue = valueResolver.resolveValueIfNecessary("constructor argument", valueHolder.getValue());
            ValueHolder resolvedValueHolder = new ValueHolder(resolvedValue, valueHolder.getType(), valueHolder.getName());
            resolvedValueHolder.setSource(valueHolder);
            resolvedValues.addIndexedArgumentValue(index, resolvedValueHolder);
        }
    }
    for (ValueHolder valueHolder : cargs.getGenericArgumentValues()) {
        if (valueHolder.isConverted()) {
            resolvedValues.addGenericArgumentValue(valueHolder);
        } else {
            Object resolvedValue = valueResolver.resolveValueIfNecessary("constructor argument", valueHolder.getValue());
            ValueHolder resolvedValueHolder = new ValueHolder(resolvedValue, valueHolder.getType(), valueHolder.getName());
            resolvedValueHolder.setSource(valueHolder);
            resolvedValues.addGenericArgumentValue(resolvedValueHolder);
        }
    }
    return minNrOfArgs;
}
Also used : TypeConverter(org.springframework.beans.TypeConverter) BeanCreationException(org.springframework.beans.factory.BeanCreationException) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) Map(java.util.Map)

Example 12 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project blog_demos by zq2599.

the class SimpleConstructorNamespaceHandler method decorate.

@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
    if (node instanceof Attr) {
        Attr attr = (Attr) node;
        String argName = StringUtils.trimWhitespace(parserContext.getDelegate().getLocalName(attr));
        String argValue = StringUtils.trimWhitespace(attr.getValue());
        ConstructorArgumentValues cvs = definition.getBeanDefinition().getConstructorArgumentValues();
        boolean ref = false;
        // handle -ref arguments
        if (argName.endsWith(REF_SUFFIX)) {
            ref = true;
            argName = argName.substring(0, argName.length() - REF_SUFFIX.length());
        }
        ValueHolder valueHolder = new ValueHolder(ref ? new RuntimeBeanReference(argValue) : argValue);
        valueHolder.setSource(parserContext.getReaderContext().extractSource(attr));
        // handle "escaped"/"_" arguments
        if (argName.startsWith(DELIMITER_PREFIX)) {
            String arg = argName.substring(1).trim();
            // fast default check
            if (!StringUtils.hasText(arg)) {
                cvs.addGenericArgumentValue(valueHolder);
            } else // assume an index otherwise
            {
                int index = -1;
                try {
                    index = Integer.parseInt(arg);
                } catch (NumberFormatException ex) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' specifies an invalid integer", attr);
                }
                if (index < 0) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' specifies a negative index", attr);
                }
                if (cvs.hasIndexedArgumentValue(index)) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' with index " + index + " already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr);
                }
                cvs.addIndexedArgumentValue(index, valueHolder);
            }
        } else // no escaping -> ctr name
        {
            String name = Conventions.attributeNameToPropertyName(argName);
            if (containsArgWithName(name, cvs)) {
                parserContext.getReaderContext().error("Constructor argument '" + argName + "' already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr);
            }
            valueHolder.setName(Conventions.attributeNameToPropertyName(argName));
            cvs.addGenericArgumentValue(valueHolder);
        }
    }
    return definition;
}
Also used : ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Attr(org.w3c.dom.Attr) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 13 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-framework-debug by Joker-5.

the class SimpleConstructorNamespaceHandler method decorate.

@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
    if (node instanceof Attr) {
        Attr attr = (Attr) node;
        String argName = StringUtils.trimWhitespace(parserContext.getDelegate().getLocalName(attr));
        String argValue = StringUtils.trimWhitespace(attr.getValue());
        ConstructorArgumentValues cvs = definition.getBeanDefinition().getConstructorArgumentValues();
        boolean ref = false;
        // handle -ref arguments
        if (argName.endsWith(REF_SUFFIX)) {
            ref = true;
            argName = argName.substring(0, argName.length() - REF_SUFFIX.length());
        }
        ValueHolder valueHolder = new ValueHolder(ref ? new RuntimeBeanReference(argValue) : argValue);
        valueHolder.setSource(parserContext.getReaderContext().extractSource(attr));
        // handle "escaped"/"_" arguments
        if (argName.startsWith(DELIMITER_PREFIX)) {
            String arg = argName.substring(1).trim();
            // fast default check
            if (!StringUtils.hasText(arg)) {
                cvs.addGenericArgumentValue(valueHolder);
            } else // assume an index otherwise
            {
                int index = -1;
                try {
                    index = Integer.parseInt(arg);
                } catch (NumberFormatException ex) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' specifies an invalid integer", attr);
                }
                if (index < 0) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' specifies a negative index", attr);
                }
                if (cvs.hasIndexedArgumentValue(index)) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' with index " + index + " already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr);
                }
                cvs.addIndexedArgumentValue(index, valueHolder);
            }
        } else // no escaping -> ctr name
        {
            String name = Conventions.attributeNameToPropertyName(argName);
            if (containsArgWithName(name, cvs)) {
                parserContext.getReaderContext().error("Constructor argument '" + argName + "' already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr);
            }
            valueHolder.setName(Conventions.attributeNameToPropertyName(argName));
            cvs.addGenericArgumentValue(valueHolder);
        }
    }
    return definition;
}
Also used : ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Attr(org.w3c.dom.Attr) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 14 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class ConstructorResolver method resolveConstructorArguments.

/**
 * Resolve the constructor arguments for this bean into the resolvedValues object.
 * This may involve looking up other beans.
 * <p>This method is also used for handling invocations of static factory methods.
 */
private int resolveConstructorArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw, ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues) {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = (customConverter != null ? customConverter : bw);
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
    int minNrOfArgs = cargs.getArgumentCount();
    for (Map.Entry<Integer, ConstructorArgumentValues.ValueHolder> entry : cargs.getIndexedArgumentValues().entrySet()) {
        int index = entry.getKey();
        if (index < 0) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid constructor argument index: " + index);
        }
        if (index + 1 > minNrOfArgs) {
            minNrOfArgs = index + 1;
        }
        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);
        }
    }
    for (ConstructorArgumentValues.ValueHolder valueHolder : cargs.getGenericArgumentValues()) {
        if (valueHolder.isConverted()) {
            resolvedValues.addGenericArgumentValue(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.addGenericArgumentValue(resolvedValueHolder);
        }
    }
    return minNrOfArgs;
}
Also used : TypeConverter(org.springframework.beans.TypeConverter) BeanCreationException(org.springframework.beans.factory.BeanCreationException) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) 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)

Example 15 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project openolat by klemens.

the class ExtensionsAdminController method getBeanDefListFor.

private Map<String, GenericBeanDefinition> getBeanDefListFor(Class<?> clazz) {
    boolean debug = log.isDebug();
    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(CoreSpringFactory.servletContext);
    XmlWebApplicationContext context = (XmlWebApplicationContext) applicationContext;
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    Map<String, GenericBeanDefinition> beanDefinitionList = new HashMap<String, GenericBeanDefinition>();
    String[] beanNames = beanFactory.getBeanNamesForType(clazz);
    for (int i = 0; i < beanNames.length; i++) {
        try {
            if (debug)
                log.debug(">>> beanNames=" + beanNames[i]);
            GenericBeanDefinition beanDef = (GenericBeanDefinition) beanFactory.getBeanDefinition(beanNames[i]);
            ConstructorArgumentValues args = beanDef.getConstructorArgumentValues();
            List<ValueHolder> values = args.getGenericArgumentValues();
            for (Iterator<ValueHolder> iterator = values.iterator(); iterator.hasNext(); ) {
                ValueHolder valueHolder = iterator.next();
                if (debug) {
                    log.debug("valueHolder=" + valueHolder);
                    log.debug("valueHolder.getType()=" + valueHolder.getType());
                    log.debug("valueHolder.getName()=" + valueHolder.getName());
                    log.debug("valueHolder.getValue()=" + valueHolder.getValue());
                }
            }
            beanDefinitionList.put(beanNames[i], beanDef);
        } catch (NoSuchBeanDefinitionException e) {
            log.warn("Error while trying to analyze bean with name: " + beanNames[i] + " :" + e);
        } catch (Exception e) {
            log.warn("Error while trying to analyze bean with name: " + beanNames[i] + " :" + e);
        }
    }
    return beanDefinitionList;
}
Also used : HashMap(java.util.HashMap) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ApplicationContext(org.springframework.context.ApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

ValueHolder (org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)41 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)26 BeansException (org.springframework.beans.BeansException)11 LinkedHashSet (java.util.LinkedHashSet)10 InjectionPoint (org.springframework.beans.factory.InjectionPoint)10 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 TypeConverter (org.springframework.beans.TypeConverter)8 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)8 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 TypeMismatchException (org.springframework.beans.TypeMismatchException)6 BeanCreationException (org.springframework.beans.factory.BeanCreationException)6 MethodParameter (org.springframework.core.MethodParameter)6 HashSet (java.util.HashSet)5 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 Set (java.util.Set)4 Test (org.junit.jupiter.api.Test)4 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)4 ParameterNameDiscoverer (org.springframework.core.ParameterNameDiscoverer)4