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);
}
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());
}
}
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);
}
}
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;
}
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();
}
Aggregations