org.springframework.core.convert.TypeDescriptor.nested()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(100)

本文整理了Java中org.springframework.core.convert.TypeDescriptor.nested()方法的一些代码示例,展示了TypeDescriptor.nested()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TypeDescriptor.nested()方法的具体详情如下:
包路径:org.springframework.core.convert.TypeDescriptor
类名称:TypeDescriptor
方法名:nested

TypeDescriptor.nested介绍

[英]Create a type descriptor for a nested type declared within the field.

For example, if the field is a List and the nesting level is 1, the nested type descriptor will be String.class.

If the field is a List> and the nesting level is 2, the nested type descriptor will also be a String.class.

If the field is a Map and the nesting level is 1, the nested type descriptor will be String, derived from the map value.

If the field is a List> and the nesting level is 2, the nested type descriptor will be String, derived from the map value.

Returns null if a nested type cannot be obtained because it was not declared. For example, if the field is a List, the nested type descriptor returned will be null.
[中]为字段中声明的嵌套类型创建类型描述符。
例如,如果字段是列表,嵌套级别为1,则嵌套类型描述符将为String。班
如果字段是List>且嵌套级别为2,则嵌套类型描述符也将是字符串。班
如果字段是映射,嵌套级别为1,则嵌套类型描述符将是从映射值派生的字符串。
如果字段是列表>且嵌套级别为2,则嵌套类型描述符将是从映射值派生的字符串。
如果由于未声明嵌套类型而无法获取该类型,则返回null。例如,如果字段是列表,则返回的嵌套类型描述符将为null。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public TypeDescriptor nested(int level) {
  return TypeDescriptor.nested(property(this.pd), level);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public TypeDescriptor nested(int level) {
  return TypeDescriptor.nested(this.field, level);
}

代码示例来源:origin: org.springframework/spring-beans

@Override
@Nullable
public TypeDescriptor nested(int level) {
  return TypeDescriptor.nested(property(this.pd), level);
}

代码示例来源:origin: org.springframework/spring-beans

@Override
@Nullable
public TypeDescriptor nested(int level) {
  return TypeDescriptor.nested(this.field, level);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a type descriptor for a nested type declared within the field.
 * <p>For example, if the field is a {@code List<String>} and the nesting
 * level is 1, the nested type descriptor will be {@code String.class}.
 * <p>If the field is a {@code List<List<String>>} and the nesting level is
 * 2, the nested type descriptor will also be a {@code String.class}.
 * <p>If the field is a {@code Map<Integer, String>} and the nesting level
 * is 1, the nested type descriptor will be String, derived from the map value.
 * <p>If the field is a {@code List<Map<Integer, String>>} and the nesting
 * level is 2, the nested type descriptor will be String, derived from the map value.
 * <p>Returns {@code null} if a nested type cannot be obtained because it was not
 * declared. For example, if the field is a {@code List<?>}, the nested type
 * descriptor returned will be {@code null}.
 * @param field the field
 * @param nestingLevel the nesting level of the collection/array element or
 * map key/value declaration within the field
 * @return the nested type descriptor at the specified nesting level,
 * or {@code null} if it could not be obtained
 * @throws IllegalArgumentException if the types up to the specified nesting
 * level are not of collection, array, or map types
 */
@Nullable
public static TypeDescriptor nested(Field field, int nestingLevel) {
  return nested(new TypeDescriptor(field), nestingLevel);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a type descriptor for a nested type declared within the property.
 * <p>For example, if the property is a {@code List<String>} and the nesting
 * level is 1, the nested type descriptor will be {@code String.class}.
 * <p>If the property is a {@code List<List<String>>} and the nesting level
 * is 2, the nested type descriptor will also be a {@code String.class}.
 * <p>If the property is a {@code Map<Integer, String>} and the nesting level
 * is 1, the nested type descriptor will be String, derived from the map value.
 * <p>If the property is a {@code List<Map<Integer, String>>} and the nesting
 * level is 2, the nested type descriptor will be String, derived from the map value.
 * <p>Returns {@code null} if a nested type cannot be obtained because it was not
 * declared. For example, if the property is a {@code List<?>}, the nested type
 * descriptor returned will be {@code null}.
 * @param property the property
 * @param nestingLevel the nesting level of the collection/array element or
 * map key/value declaration within the property
 * @return the nested type descriptor at the specified nesting level, or
 * {@code null} if it could not be obtained
 * @throws IllegalArgumentException if the types up to the specified nesting
 * level are not of collection, array, or map types
 */
@Nullable
public static TypeDescriptor nested(Property property, int nestingLevel) {
  return nested(new TypeDescriptor(property), nestingLevel);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a type descriptor for a nested type declared within the method parameter.
 * <p>For example, if the methodParameter is a {@code List<String>} and the
 * nesting level is 1, the nested type descriptor will be String.class.
 * <p>If the methodParameter is a {@code List<List<String>>} and the nesting
 * level is 2, the nested type descriptor will also be a String.class.
 * <p>If the methodParameter is a {@code Map<Integer, String>} and the nesting
 * level is 1, the nested type descriptor will be String, derived from the map value.
 * <p>If the methodParameter is a {@code List<Map<Integer, String>>} and the
 * nesting level is 2, the nested type descriptor will be String, derived from the map value.
 * <p>Returns {@code null} if a nested type cannot be obtained because it was not declared.
 * For example, if the method parameter is a {@code List<?>}, the nested type
 * descriptor returned will be {@code null}.
 * @param methodParameter the method parameter with a nestingLevel of 1
 * @param nestingLevel the nesting level of the collection/array element or
 * map key/value declaration within the method parameter
 * @return the nested type descriptor at the specified nesting level,
 * or {@code null} if it could not be obtained
 * @throws IllegalArgumentException if the nesting level of the input
 * {@link MethodParameter} argument is not 1, or if the types up to the
 * specified nesting level are not of collection, array, or map types
 */
@Nullable
public static TypeDescriptor nested(MethodParameter methodParameter, int nestingLevel) {
  if (methodParameter.getNestingLevel() != 1) {
    throw new IllegalArgumentException("MethodParameter nesting level must be 1: " +
        "use the nestingLevel parameter to specify the desired nestingLevel for nested type traversal");
  }
  return nested(new TypeDescriptor(methodParameter), nestingLevel);
}

代码示例来源:origin: spring-projects/spring-framework

@Test(expected = IllegalArgumentException.class)
public void nestedMethodParameterNot1NestedLevel() throws Exception {
  TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test4", List.class), 0, 2), 2);
}

代码示例来源:origin: spring-projects/spring-framework

@Test(expected = IllegalArgumentException.class)
public void nestedMethodParameterTypeInvalidNestingLevel() throws Exception {
  TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test5", String.class), 0, 2), 2);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void nestedPropertyTypeMapTwoLevels() throws Exception {
  Property property = new Property(getClass(), getClass().getMethod("getTest4"), getClass().getMethod("setTest4", List.class));
  TypeDescriptor t1 = TypeDescriptor.nested(property, 2);
  assertEquals(String.class, t1.getType());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void nestedTooManyLevels() throws Exception {
  TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test4", List.class), 0), 3);
  assertNull(t1);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void nestedMethodParameterTypeNotNestable() throws Exception {
  TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test5", String.class), 0), 2);
  assertNull(t1);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void nestedFieldTypeMapTwoLevels() throws Exception {
  TypeDescriptor t1 = TypeDescriptor.nested(getClass().getField("test4"), 2);
  assertEquals(String.class, t1.getType());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void nestedMethodParameterType2Levels() throws Exception {
  TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test2", List.class), 0), 2);
  assertEquals(String.class, t1.getType());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void nestedMethodParameterTypeMap() throws Exception {
  TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test3", Map.class), 0), 1);
  assertEquals(String.class, t1.getType());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void nestedNotParameterized() throws Exception {
  TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test6", List.class), 0), 1);
  assertEquals(List.class,t1.getType());
  assertEquals("java.util.List<?>", t1.toString());
  TypeDescriptor t2 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test6", List.class), 0), 2);
  assertNull(t2);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void nestedMethodParameterTypeMapTwoLevels() throws Exception {
  TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test4", List.class), 0), 2);
  assertEquals(String.class, t1.getType());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void contributeMethodArgument(MethodParameter parameter, @Nullable Object value,
    UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
  Class<?> paramType = parameter.getNestedParameterType();
  if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType || Part.class == paramType) {
    return;
  }
  RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
  String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ?
      parameter.getParameterName() : requestParam.name());
  Assert.state(name != null, "Unresolvable parameter name");
  if (value == null) {
    if (requestParam != null &&
        (!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE))) {
      return;
    }
    builder.queryParam(name);
  }
  else if (value instanceof Collection) {
    for (Object element : (Collection<?>) value) {
      element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
      builder.queryParam(name, element);
    }
  }
  else {
    builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void parameterList() throws Exception {
  MethodParameter methodParameter = new MethodParameter(getClass().getMethod("testParameterList", List.class), 0);
  TypeDescriptor desc = new TypeDescriptor(methodParameter);
  assertEquals(List.class, desc.getType());
  assertEquals(List.class, desc.getObjectType());
  assertEquals("java.util.List", desc.getName());
  assertEquals("java.util.List<java.util.List<java.util.Map<java.lang.Integer, java.lang.Enum<?>>>>", desc.toString());
  assertTrue(!desc.isPrimitive());
  assertEquals(0, desc.getAnnotations().length);
  assertTrue(desc.isCollection());
  assertFalse(desc.isArray());
  assertEquals(List.class, desc.getElementTypeDescriptor().getType());
  assertEquals(TypeDescriptor.nested(methodParameter, 1), desc.getElementTypeDescriptor());
  assertEquals(TypeDescriptor.nested(methodParameter, 2), desc.getElementTypeDescriptor().getElementTypeDescriptor());
  assertEquals(TypeDescriptor.nested(methodParameter, 3), desc.getElementTypeDescriptor().getElementTypeDescriptor().getMapValueTypeDescriptor());
  assertEquals(Integer.class, desc.getElementTypeDescriptor().getElementTypeDescriptor().getMapKeyTypeDescriptor().getType());
  assertEquals(Enum.class, desc.getElementTypeDescriptor().getElementTypeDescriptor().getMapValueTypeDescriptor().getType());
  assertFalse(desc.isMap());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void parameterMap() throws Exception {
  MethodParameter methodParameter = new MethodParameter(getClass().getMethod("testParameterMap", Map.class), 0);
  TypeDescriptor desc = new TypeDescriptor(methodParameter);
  assertEquals(Map.class, desc.getType());
  assertEquals(Map.class, desc.getObjectType());
  assertEquals("java.util.Map", desc.getName());
  assertEquals("java.util.Map<java.lang.Integer, java.util.List<java.lang.String>>", desc.toString());
  assertTrue(!desc.isPrimitive());
  assertEquals(0, desc.getAnnotations().length);
  assertFalse(desc.isCollection());
  assertFalse(desc.isArray());
  assertTrue(desc.isMap());
  assertEquals(TypeDescriptor.nested(methodParameter, 1), desc.getMapValueTypeDescriptor());
  assertEquals(TypeDescriptor.nested(methodParameter, 2), desc.getMapValueTypeDescriptor().getElementTypeDescriptor());
  assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getType());
  assertEquals(List.class, desc.getMapValueTypeDescriptor().getType());
  assertEquals(String.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
}

相关文章

微信公众号

最新文章

更多