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

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

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

TypeDescriptor.map介绍

[英]Create a new type descriptor from a java.util.Map type.

Useful for converting to typed Maps.

For example, a Map<String, String> could be converted to a Map<Id, EmailAddress> by converting to a targetType built with this method: The method call to construct such a TypeDescriptor would look something like:

map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));

[中]从java创建一个新的类型描述符。util。地图类型。
用于转换为类型化地图。
例如,映射<String,String>可以通过转换为使用此方法生成的targetType而转换为映射<Id,EmailAddress>:构造此类TypeDescriptor的方法调用如下所示:

map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));

代码示例

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

@Test
public void map() {
  Map<String, String> strings = new HashMap<>();
  strings.put("3", "9");
  strings.put("6", "31");
  @SuppressWarnings("unchecked")
  Map<Integer, Integer> integers = (Map<Integer, Integer>) conversionService.convert(strings,
      TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(Integer.class)));
  assertEquals(Integer.valueOf(9), integers.get(3));
  assertEquals(Integer.valueOf(31), integers.get(6));
}

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

@Test
public void createMapWithNullElements() throws Exception {
  TypeDescriptor typeDescriptor = TypeDescriptor.map(LinkedHashMap.class, null, null);
  assertThat(typeDescriptor.getMapKeyTypeDescriptor(), nullValue());
  assertThat(typeDescriptor.getMapValueTypeDescriptor(), nullValue());
}

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

@Test
public void noDefaultConstructorCopyNotRequired() throws Exception {
  // SPR-9284
  NoDefaultConstructorMap<String, Integer> map = new NoDefaultConstructorMap<>(
      Collections.<String, Integer>singletonMap("1", 1));
  TypeDescriptor sourceType = TypeDescriptor.map(NoDefaultConstructorMap.class,
      TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
  TypeDescriptor targetType = TypeDescriptor.map(NoDefaultConstructorMap.class,
      TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
  assertTrue(conversionService.canConvert(sourceType, targetType));
  @SuppressWarnings("unchecked")
  Map<String, Integer> result = (Map<String, Integer>) conversionService.convert(map, sourceType, targetType);
  assertEquals(map, result);
  assertEquals(NoDefaultConstructorMap.class, result.getClass());
}

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

@Test
public void createMapArray() throws Exception {
  TypeDescriptor mapType = TypeDescriptor.map(
      LinkedHashMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
  TypeDescriptor arrayType = TypeDescriptor.array(mapType);
  assertEquals(arrayType.getType(), LinkedHashMap[].class);
  assertEquals(arrayType.getElementTypeDescriptor(), mapType);
}

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

@Test
public void mapNested() {
  TypeDescriptor desc = TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class),
      TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
  assertEquals(Map.class, desc.getType());
  assertEquals(Map.class, desc.getObjectType());
  assertEquals("java.util.Map", desc.getName());
  assertEquals("java.util.Map<java.lang.String, java.util.Map<java.lang.String, java.lang.Integer>>", desc.toString());
  assertTrue(!desc.isPrimitive());
  assertEquals(0, desc.getAnnotations().length);
  assertFalse(desc.isCollection());
  assertFalse(desc.isArray());
  assertTrue(desc.isMap());
  assertEquals(String.class, desc.getMapKeyTypeDescriptor().getType());
  assertEquals(String.class, desc.getMapValueTypeDescriptor().getMapKeyTypeDescriptor().getType());
  assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getMapValueTypeDescriptor().getType());
}

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

@Test
public void map() {
  TypeDescriptor desc = TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
  assertEquals(Map.class, desc.getType());
  assertEquals(Map.class, desc.getObjectType());
  assertEquals("java.util.Map", desc.getName());
  assertEquals("java.util.Map<java.lang.String, java.lang.Integer>", desc.toString());
  assertTrue(!desc.isPrimitive());
  assertEquals(0, desc.getAnnotations().length);
  assertFalse(desc.isCollection());
  assertFalse(desc.isArray());
  assertTrue(desc.isMap());
  assertEquals(String.class, desc.getMapKeyTypeDescriptor().getType());
  assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getType());
}

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

Map<String, Map<String, Set<Bar>>> bars;
TypeDescriptor sourceType = TypeDescriptor.map(Map.class, null, null);
TypeDescriptor targetType = TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class),
    TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class),
        TypeDescriptor.collection(Set.class, TypeDescriptor.valueOf(Bar.class))));

代码示例来源:origin: apache/servicemix-bundles

/**
   * Returns the type descriptor for the given {@link PropertyPath} and empty value for that path.
   *
   * @param path must not be {@literal null}.
   * @param emptyValue must not be {@literal null}.
   * @return
   */
  private TypeDescriptor getDescriptor(PropertyPath path, Object emptyValue) {
    Class<?> actualPropertyType = path.getType();
    TypeDescriptor valueDescriptor = conversionService.canConvert(String.class, actualPropertyType)
        ? TypeDescriptor.valueOf(String.class)
        : TypeDescriptor.valueOf(HashMap.class);
    return path.isCollection() ? TypeDescriptor.collection(emptyValue.getClass(), valueDescriptor)
        : TypeDescriptor.map(emptyValue.getClass(), TypeDescriptor.valueOf(String.class), valueDescriptor);
  }
}

相关文章

微信公众号

最新文章

更多