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