Search in sources :

Example 11 with MethodParameter

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

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithMultipleCandidatesDescriptor.

@Test
public void testAutowireCandidateWithMultipleCandidatesDescriptor() throws Exception {
    StandardBeanFactory lbf = new StandardBeanFactory();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(JUERGEN, person1);
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    person2.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(MARK, person2);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0), false);
    assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue();
    assertThat(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)).isTrue();
}
Also used : DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) MethodParameter(cn.taketoday.core.MethodParameter) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Test(org.junit.jupiter.api.Test)

Example 12 with MethodParameter

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

the class RequestResponseBodyMethodProcessor method resolveParameter.

/**
 * Throws MethodArgumentNotValidException if validation fails.
 *
 * @throws HttpMessageNotReadableException if {@link RequestBody#required()}
 * is {@code true} and there is no body content or if there is no suitable
 * converter to read the content with.
 */
@Nullable
@Override
public Object resolveParameter(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    MethodParameter parameter = resolvable.getParameter();
    parameter = parameter.nestedIfOptional();
    Object arg = readWithMessageConverters(context, parameter, parameter.getNestedGenericParameterType());
    return adaptArgumentIfNecessary(arg, parameter);
}
Also used : ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 13 with MethodParameter

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

the class MatrixParamMapParameterResolvingStrategy method resolveParameter.

@Nullable
@Override
public Object resolveParameter(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    Map<String, MultiValueMap<String, String>> matrixVariables = context.getMatchingMetadata().getMatrixVariables();
    if (CollectionUtils.isEmpty(matrixVariables)) {
        return Collections.emptyMap();
    }
    MultiValueMap<String, String> map = MultiValueMap.fromLinkedHashMap();
    MethodParameter parameter = resolvable.getParameter();
    MatrixParam ann = parameter.getParameterAnnotation(MatrixParam.class);
    Assert.state(ann != null, "No MatrixVariable annotation");
    String pathVariable = ann.pathVar();
    if (!pathVariable.equals(Constant.DEFAULT_NONE)) {
        MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
        if (mapForPathVariable == null) {
            return Collections.emptyMap();
        }
        map.putAll(mapForPathVariable);
    } else {
        for (MultiValueMap<String, String> vars : matrixVariables.values()) {
            for (Map.Entry<String, List<String>> entry : vars.entrySet()) {
                String name = entry.getKey();
                List<String> values = entry.getValue();
                for (String value : values) {
                    map.add(name, value);
                }
            }
        }
    }
    return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
Also used : MatrixParam(cn.taketoday.web.annotation.MatrixParam) List(java.util.List) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Example 14 with MethodParameter

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

the class AbstractNamedValueResolvingStrategy method resolveParameter.

@Nullable
@Override
public final Object resolveParameter(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    MethodParameter methodParameter = resolvable.getParameter();
    NamedValueInfo namedValueInfo = resolvable.getNamedValueInfo();
    MethodParameter nestedParameter = methodParameter.nestedIfOptional();
    Object arg;
    if (namedValueInfo.nameEmbedded) {
        Object resolvedName = resolveEmbeddedValuesAndExpressions(namedValueInfo.name);
        if (resolvedName == null) {
            throw new IllegalArgumentException("Specified name must not resolve to null: [" + namedValueInfo.name + "]");
        }
        arg = resolveName(resolvedName.toString(), resolvable, context);
    } else {
        arg = resolveName(namedValueInfo.name, resolvable, context);
    }
    if (arg == null) {
        if (namedValueInfo.defaultValue != null) {
            arg = resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue);
        } else if (namedValueInfo.required && !nestedParameter.isOptional()) {
            handleMissingValue(namedValueInfo.name, nestedParameter, context);
        }
        arg = handleNullValue(namedValueInfo.name, arg, nestedParameter.getNestedParameterType());
    } else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
        arg = resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue);
    }
    if (configurableBeanFactory != null) {
        ConversionService conversionService = configurableBeanFactory.getConversionService();
        if (conversionService != null) {
            try {
                arg = conversionService.convert(arg, resolvable.getTypeDescriptor());
            } catch (ConversionException e) {
                throw new ParameterConversionException(methodParameter, arg, e);
            }
            // Check for null value after conversion of incoming argument value
            if (arg == null && namedValueInfo.defaultValue == null && namedValueInfo.required && !nestedParameter.isOptional()) {
                handleMissingValueAfterConversion(namedValueInfo.name, nestedParameter, context);
            }
        }
    }
    handleResolvedValue(arg, namedValueInfo.name, resolvable, context);
    return arg;
}
Also used : ConversionException(cn.taketoday.core.conversion.ConversionException) NamedValueInfo(cn.taketoday.web.handler.method.NamedValueInfo) ConversionService(cn.taketoday.core.conversion.ConversionService) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 15 with MethodParameter

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

the class HttpEntityMethodProcessorTests method jacksonTypeInfoList.

// SPR-12811
@Test
public void jacksonTypeInfoList() throws Exception {
    Method method = JacksonController.class.getMethod("handleList");
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodReturnType = handlerMethod.getReturnType();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null);
    Object returnValue = new JacksonController().handleList();
    processor.handleReturnValue(webRequest, handlerMethod, returnValue);
    String content = this.servletResponse.getContentAsString();
    assertThat(content.contains("\"type\":\"foo\"")).isTrue();
    assertThat(content.contains("\"type\":\"bar\"")).isTrue();
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

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