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

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

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

TypeDescriptor.getMapValueTypeDescriptor介绍

[英]If this type is a Map and its value type is parameterized, returns the map's value type.

If the Map's value type is not parameterized, returns nullindicating the value type is not declared.
[中]如果此类型是映射且其值类型已参数化,则返回映射的值类型。
如果映射的值类型未参数化,则返回null,指示未声明值类型。

代码示例

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

private boolean canConvertValue(TypeDescriptor sourceType, TypeDescriptor targetType) {
  return ConversionUtils.canConvertElements(sourceType.getMapValueTypeDescriptor(),
      targetType.getMapValueTypeDescriptor(), this.conversionService);
}

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

@Nullable
private Object convertValue(Object sourceValue, TypeDescriptor sourceType, @Nullable TypeDescriptor targetType) {
  if (targetType == null) {
    return sourceValue;
  }
  return this.conversionService.convert(sourceValue, sourceType.getMapValueTypeDescriptor(sourceValue), targetType);
}

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

@Override
public void setValue(@Nullable Object newValue) {
  if (this.mapEntryDescriptor.getMapValueTypeDescriptor() != null) {
    newValue = this.typeConverter.convertValue(newValue, TypeDescriptor.forObject(newValue),
        this.mapEntryDescriptor.getMapValueTypeDescriptor());
  }
  this.map.put(this.key, newValue);
}

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

/**
 * If this type is a {@link Map}, creates a mapValue {@link TypeDescriptor}
 * from the provided map value.
 * <p>Narrows the {@link #getMapValueTypeDescriptor() mapValueType} property
 * to the class of the provided map value. For example, if this describes a
 * {@code java.util.Map&lt;java.lang.String, java.lang.Number&lt;} and the value
 * argument is a {@code java.lang.Integer}, the returned TypeDescriptor will be
 * {@code java.lang.Integer}. If this describes a {@code java.util.Map&lt;?, ?&gt;}
 * and the value argument is a {@code java.lang.Integer}, the returned
 * TypeDescriptor will be {@code java.lang.Integer} as well.
 * <p>Annotation and nested type context will be preserved in the narrowed
 * TypeDescriptor that is returned.
 * @param mapValue the map value
 * @return the map value type descriptor
 * @throws IllegalStateException if this type is not a {@code java.util.Map}
 * @see #narrow(Object)
 */
@Nullable
public TypeDescriptor getMapValueTypeDescriptor(Object mapValue) {
  return narrow(mapValue, getMapValueTypeDescriptor());
}

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

@Override
public TypedValue getValue() {
  Object value = this.map.get(this.key);
  exitTypeDescriptor = CodeFlow.toDescriptor(Object.class);
  return new TypedValue(value, this.mapEntryDescriptor.getMapValueTypeDescriptor(value));
}

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

@Override
public boolean equals(Object other) {
  if (this == other) {
    return true;
  }
  if (!(other instanceof TypeDescriptor)) {
    return false;
  }
  TypeDescriptor otherDesc = (TypeDescriptor) other;
  if (getType() != otherDesc.getType()) {
    return false;
  }
  if (!annotationsMatch(otherDesc)) {
    return false;
  }
  if (isCollection() || isArray()) {
    return ObjectUtils.nullSafeEquals(getElementTypeDescriptor(), otherDesc.getElementTypeDescriptor());
  }
  else if (isMap()) {
    return (ObjectUtils.nullSafeEquals(getMapKeyTypeDescriptor(), otherDesc.getMapKeyTypeDescriptor()) &&
        ObjectUtils.nullSafeEquals(getMapValueTypeDescriptor(), otherDesc.getMapValueTypeDescriptor()));
  }
  else {
    return true;
  }
}

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

@Test
public void mapValueType() {
  TypeDescriptor desc = TypeDescriptor.valueOf(Map.class);
  Integer value = Integer.valueOf(3);
  desc = desc.getMapValueTypeDescriptor(value);
  assertEquals(Integer.class, desc.getType());
}

代码示例来源: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 mapValueTypePreserveContext() throws Exception {
  TypeDescriptor desc = new TypeDescriptor(getClass().getField("mapPreserveContext"));
  assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
  List<Integer> value = new ArrayList<>(3);
  desc = desc.getMapValueTypeDescriptor(value);
  assertEquals(Integer.class, desc.getElementTypeDescriptor().getType());
  assertNotNull(desc.getAnnotation(FieldAnnotation.class));
}

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

@Test
public void propertyComplex() throws Exception {
  Property property = new Property(getClass(), getClass().getMethod("getComplexProperty"),
      getClass().getMethod("setComplexProperty", Map.class));
  TypeDescriptor desc = new TypeDescriptor(property);
  assertEquals(String.class, desc.getMapKeyTypeDescriptor().getType());
  assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getElementTypeDescriptor().getType());
}

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

@Test
public void multiValueMap() throws Exception {
  TypeDescriptor td = new TypeDescriptor(getClass().getField("multiValueMap"));
  assertTrue(td.isMap());
  assertEquals(String.class, td.getMapKeyTypeDescriptor().getType());
  assertEquals(List.class, td.getMapValueTypeDescriptor().getType());
  assertEquals(Integer.class,
      td.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
}

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

isNestedAssignable(getMapValueTypeDescriptor(), typeDescriptor.getMapValueTypeDescriptor());

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

@Test
public void fieldComplexTypeDescriptor2() throws Exception {
  TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("nestedMapField"));
  assertTrue(typeDescriptor.isMap());
  assertEquals(String.class,typeDescriptor.getMapKeyTypeDescriptor().getType());
  assertEquals(List.class, typeDescriptor.getMapValueTypeDescriptor().getType());
  assertEquals(Integer.class, typeDescriptor.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
  assertEquals("java.util.Map<java.lang.String, java.util.List<java.lang.Integer>>", typeDescriptor.toString());
}

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

@Test
public void property() throws Exception {
  Property property = new Property(
      getClass(), getClass().getMethod("getProperty"), getClass().getMethod("setProperty", Map.class));
  TypeDescriptor desc = new TypeDescriptor(property);
  assertEquals(Map.class, desc.getType());
  assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType());
  assertEquals(Long.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
  assertNotNull(desc.getAnnotation(MethodAnnotation1.class));
  assertNotNull(desc.getAnnotation(MethodAnnotation2.class));
  assertNotNull(desc.getAnnotation(MethodAnnotation3.class));
}

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

@Test
public void elementTypeForMapSubclass() throws Exception {
  @SuppressWarnings("serial")
  class CustomMap extends HashMap<String, Integer> {
  }
  assertEquals(TypeDescriptor.valueOf(CustomMap.class).getMapKeyTypeDescriptor(), TypeDescriptor.valueOf(String.class));
  assertEquals(TypeDescriptor.valueOf(CustomMap.class).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
  assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapKeyTypeDescriptor(), TypeDescriptor.valueOf(String.class));
  assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
}

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

@Test
public void fieldMap() throws Exception {
  TypeDescriptor desc = new TypeDescriptor(TypeDescriptorTests.class.getField("fieldMap"));
  assertTrue(desc.isMap());
  assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType());
  assertEquals(Long.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
}

代码示例来源: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());
}

代码示例来源: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 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 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());
}

相关文章

微信公众号

最新文章

更多