Search in sources :

Example 6 with MethodParameter

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

the class RequestPartMethodArgumentResolver method resolveArgument.

@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    MethodParameter parameter = resolvable.getParameter();
    RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
    boolean isRequired = ((requestPart == null || requestPart.required()) && !parameter.isOptional());
    String name = getPartName(parameter, requestPart);
    parameter = parameter.nestedIfOptional();
    Object arg = null;
    Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, context);
    if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
        arg = mpArg;
    } else {
        try {
            var inputMessage = new RequestPartServletServerHttpRequest(context, name);
            arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
            BindingContext binderFactory = context.getBindingContext();
            if (binderFactory != null) {
                WebDataBinder binder = binderFactory.createBinder(context, arg, name);
                if (arg != null) {
                    validateIfApplicable(binder, parameter);
                    if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
                        throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
                    }
                }
                binderFactory.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
            }
        } catch (MissingRequestPartException | MultipartException ex) {
            if (isRequired) {
                throw ex;
            }
        }
    }
    if (arg == null && isRequired) {
        if (!context.isMultipart()) {
            throw new MultipartException("Current request is not a multipart request");
        } else {
            throw new MissingRequestPartException(name);
        }
    }
    return adaptArgumentIfNecessary(arg, parameter);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) WebDataBinder(cn.taketoday.web.bind.WebDataBinder) RequestPart(cn.taketoday.web.annotation.RequestPart) MultipartException(cn.taketoday.web.bind.MultipartException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 7 with MethodParameter

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

the class DataClassRowMapper method initialize.

@Override
protected void initialize(Class<T> mappedClass, BeanMetadata metadata) {
    super.initialize(mappedClass, metadata);
    this.mappedInstantiator = metadata.getInstantiator();
    Constructor<?> constructor = mappedInstantiator.getConstructor();
    if (constructor != null) {
        int paramCount = constructor.getParameterCount();
        if (paramCount > 0) {
            String[] constructorParameterNames = BeanUtils.getParameterNames(constructor);
            for (String name : constructorParameterNames) {
                suppressProperty(name);
            }
            TypeDescriptor[] constructorParameterTypes = new TypeDescriptor[paramCount];
            for (int i = 0; i < paramCount; i++) {
                constructorParameterTypes[i] = new TypeDescriptor(new MethodParameter(constructor, i));
            }
            this.constructorParameterTypes = constructorParameterTypes;
            this.constructorParameterNames = constructorParameterNames;
        }
    } else {
        LoggerFactory.getLogger(DataClassRowMapper.class).warn("Actual 'java.lang.reflect.Constructor' cannot determine in mappedClass: '{}'", mappedClass.getName());
    }
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) MethodParameter(cn.taketoday.core.MethodParameter)

Example 8 with MethodParameter

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

the class ResolvableParameterFactory method fillArray.

public void fillArray(InvocableHandlerMethod handlerMethod) {
    MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
    ResolvableMethodParameter[] resolvableParameters = handlerMethod.resolvableParameters;
    for (int i = 0; i < resolvableParameters.length; i++) {
        MethodParameter methodParameter = methodParameters[i];
        methodParameter.initParameterNameDiscovery(parameterNameDiscoverer);
        resolvableParameters[i] = createParameter(methodParameter);
    }
}
Also used : SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter)

Example 9 with MethodParameter

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

the class ResolvableParameterFactory method createArray.

@Nullable
public ResolvableMethodParameter[] createArray(HandlerMethod handlerMethod) {
    MethodParameter[] parameters = handlerMethod.getMethodParameters();
    if (ObjectUtils.isEmpty(parameters)) {
        return null;
    }
    int i = 0;
    ResolvableMethodParameter[] ret = new ResolvableMethodParameter[parameters.length];
    for (MethodParameter parameter : parameters) {
        parameter.initParameterNameDiscovery(parameterNameDiscoverer);
        ret[i] = createParameter(parameter);
        i++;
    }
    return ret;
}
Also used : SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 10 with MethodParameter

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

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithConstructorDescriptor.

@Disabled
@Test
public void testAutowireCandidateWithConstructorDescriptor() 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);
    lbf.registerBeanDefinition(MARK, person2);
    MethodParameter param = new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(param, false);
    param.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    assertThat(param.getParameterName()).isEqualTo("tpb");
    assertThat(lbf.isAutowireCandidate(JUERGEN, null)).isTrue();
    assertThat(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)).isTrue();
    assertThat(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)).isFalse();
}
Also used : LocalVariableTableParameterNameDiscoverer(cn.taketoday.core.LocalVariableTableParameterNameDiscoverer) 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) Disabled(org.junit.jupiter.api.Disabled)

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