Search in sources :

Example 96 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-infrastructure by TAKETODAY.

the class ReactiveTypeHandlerTests method handleValue.

private ResponseBodyEmitter handleValue(Object returnValue, Class<?> asyncType, ResolvableType genericType) throws Exception {
    BindingContext mavContainer = new BindingContext();
    MethodParameter returnType = on(TestController.class).resolveReturnType(asyncType, genericType);
    webRequest.setBindingContext(mavContainer);
    return this.handler.handleValue(returnValue, returnType, this.webRequest);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) MethodParameter(cn.taketoday.core.MethodParameter)

Example 97 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.

the class ConstructorResolver method createArgumentArray.

/**
 * Create an array of arguments to invoke a constructor or factory method,
 * given the resolved constructor argument values.
 */
private ArgumentsHolder createArgumentArray(BeanDefinition definition, @Nullable ConstructorArgumentValues resolvedValues, Class<?>[] paramTypes, @Nullable String[] paramNames, Executable executable, BeanWrapper wrapper, boolean autowiring, boolean fallback) throws UnsatisfiedDependencyException {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = customConverter != null ? customConverter : wrapper;
    String beanName = definition.getBeanName();
    ArgumentsHolder args = new ArgumentsHolder(paramTypes.length);
    HashSet<ValueHolder> usedValueHolders = new HashSet<>(paramTypes.length);
    LinkedHashSet<String> autowiredBeanNames = new LinkedHashSet<>(4);
    for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
        Class<?> paramType = paramTypes[paramIndex];
        String paramName = paramNames != null ? paramNames[paramIndex] : "";
        // Try to find matching constructor argument value, either indexed or generic.
        ValueHolder valueHolder = null;
        if (resolvedValues != null) {
            valueHolder = resolvedValues.getArgumentValue(paramIndex, paramType, paramName, usedValueHolders);
            // it could match after type conversion (for example, String -> int).
            if (valueHolder == null && (!autowiring || paramTypes.length == resolvedValues.getArgumentCount())) {
                valueHolder = resolvedValues.getGenericArgumentValue(null, null, usedValueHolders);
            }
        }
        if (valueHolder != null) {
            // We found a potential match - let's give it a try.
            // Do not consider the same value definition multiple times!
            usedValueHolders.add(valueHolder);
            Object originalValue = valueHolder.getValue();
            Object convertedValue;
            if (valueHolder.isConverted()) {
                convertedValue = valueHolder.getConvertedValue();
                args.preparedArguments[paramIndex] = convertedValue;
            } else {
                MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
                try {
                    convertedValue = convertIfNecessary(originalValue, paramType, methodParam);
                } catch (TypeMismatchException ex) {
                    throw new UnsatisfiedDependencyException(definition.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(valueHolder.getValue()) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage());
                }
                Object sourceHolder = valueHolder.getSource();
                if (sourceHolder instanceof ValueHolder) {
                    Object sourceValue = ((ValueHolder) sourceHolder).getValue();
                    args.resolveNecessary = true;
                    args.preparedArguments[paramIndex] = sourceValue;
                }
            }
            args.arguments[paramIndex] = convertedValue;
            args.rawArguments[paramIndex] = originalValue;
        } else {
            MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
            // }
            try {
                Object autowiredArgument = resolveAutowiredArgument(methodParam, beanName, autowiredBeanNames, converter, fallback);
                args.resolveNecessary = true;
                args.arguments[paramIndex] = autowiredArgument;
                args.rawArguments[paramIndex] = autowiredArgument;
                args.preparedArguments[paramIndex] = autowiredArgumentMarker;
            } catch (UnsatisfiedDependencyException e) {
                throw e;
            } catch (BeansException ex) {
                throw new UnsatisfiedDependencyException(definition.getResourceDescription(), beanName, new InjectionPoint(methodParam), ex);
            }
        }
    }
    for (String autowiredBeanName : autowiredBeanNames) {
        beanFactory.registerDependentBean(autowiredBeanName, beanName);
        if (log.isDebugEnabled()) {
            log.debug("Autowiring by type from bean name '{}' via {} to bean named '{}'", beanName, (executable instanceof Constructor ? "constructor" : "factory method"), autowiredBeanName);
        }
    }
    return args;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) Constructor(java.lang.reflect.Constructor) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) ValueHolder(cn.taketoday.beans.factory.config.ConstructorArgumentValues.ValueHolder) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeConverter(cn.taketoday.beans.TypeConverter) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) MethodParameter(cn.taketoday.core.MethodParameter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BeansException(cn.taketoday.beans.BeansException)

Aggregations

MethodParameter (cn.taketoday.core.MethodParameter)97 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)43 Method (java.lang.reflect.Method)43 Test (org.junit.jupiter.api.Test)42 ArrayList (java.util.ArrayList)30 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)28 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)24 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)23 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)22 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)22 Nullable (cn.taketoday.lang.Nullable)21 TypeDescriptor (cn.taketoday.core.TypeDescriptor)18 AllEncompassingFormHttpMessageConverter (cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter)17 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)17 MappingJackson2XmlHttpMessageConverter (cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter)17 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)9 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)8 SynthesizingMethodParameter (cn.taketoday.core.annotation.SynthesizingMethodParameter)8