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

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

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

TypeDescriptor.narrow介绍

[英]Narrows this TypeDescriptor by setting its type to the class of the provided value.

If the value is null, no narrowing is performed and this TypeDescriptor is returned unchanged.

Designed to be called by binding frameworks when they read property, field, or method return values. Allows such frameworks to narrow a TypeDescriptor built from a declared property, field, or method return value type. For example, a field declared as java.lang.Object would be narrowed to java.util.HashMapif it was set to a java.util.HashMap value. The narrowed TypeDescriptor can then be used to convert the HashMap to some other type. Annotation and nested type context is preserved by the narrowed copy.
[中]通过将其类型设置为所提供值的类来缩小此TypeDescriptor的范围。
如果该值为null,则不会执行缩小操作,并且该TypeDescriptor将原封不动地返回。
设计用于绑定框架在读取属性、字段或方法返回值时调用。允许这样的框架缩小从声明的属性、字段或方法返回值类型构建的TypeDescriptor的范围。例如,一个声明为java的字段。对象将缩小到java。util。HashMapif设置为java。util。HashMap值。然后,可以使用缩小的TypeDescriptor将HashMap转换为其他类型。注释和嵌套类型上下文由缩小的副本保留。

代码示例

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

@Nullable
private TypeDescriptor narrow(@Nullable Object value, @Nullable TypeDescriptor typeDescriptor) {
  if (typeDescriptor != null) {
    return typeDescriptor.narrow(value);
  }
  if (value != null) {
    return narrow(value);
  }
  return null;
}

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

@Nullable
private TypeDescriptor narrow(@Nullable Object value, @Nullable TypeDescriptor typeDescriptor) {
  if (typeDescriptor != null) {
    return typeDescriptor.narrow(value);
  }
  if (value != null) {
    return narrow(value);
  }
  return null;
}

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

/**
 * If this type is a {@link Map}, creates a mapKey {@link TypeDescriptor}
 * from the provided map key.
 * <p>Narrows the {@link #getMapKeyTypeDescriptor() mapKeyType} property
 * to the class of the provided map key. For example, if this describes a
 * {@code java.util.Map&lt;java.lang.Number, java.lang.String&lt;} and the key
 * 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 key 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 mapKey the map key
 * @return the map key type descriptor
 * @throws IllegalStateException if this type is not a {@code java.util.Map}
 * @see #narrow(Object)
 */
@Nullable
public TypeDescriptor getMapKeyTypeDescriptor(Object mapKey) {
  return narrow(mapKey, getMapKeyTypeDescriptor());
}

代码示例来源: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

/**
 * If this type is a {@link Collection} or an array, creates a element TypeDescriptor
 * from the provided collection or array element.
 * <p>Narrows the {@link #getElementTypeDescriptor() elementType} property to the class
 * of the provided collection or array element. For example, if this describes a
 * {@code java.util.List&lt;java.lang.Number&lt;} and the element argument is an
 * {@code java.lang.Integer}, the returned TypeDescriptor will be {@code java.lang.Integer}.
 * If this describes a {@code java.util.List&lt;?&gt;} and the element argument is an
 * {@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 element the collection or array element
 * @return a element type descriptor, narrowed to the type of the provided element
 * @throws IllegalStateException if this type is not a {@code java.util.Collection}
 * or array type
 * @see #narrow(Object)
 */
@Nullable
public TypeDescriptor elementTypeDescriptor(Object element) {
  return narrow(element, getElementTypeDescriptor());
}

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

@Override
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
  if (this.member instanceof Method) {
    Method method = (Method) this.member;
    try {
      ReflectionUtils.makeAccessible(method);
      Object value = method.invoke(target);
      return new TypedValue(value, this.typeDescriptor.narrow(value));
    }
    catch (Exception ex) {
      throw new AccessException("Unable to access property '" + name + "' through getter method", ex);
    }
  }
  else {
    Field field = (Field) this.member;
    try {
      ReflectionUtils.makeAccessible(field);
      Object value = field.get(target);
      return new TypedValue(value, this.typeDescriptor.narrow(value));
    }
    catch (Exception ex) {
      throw new AccessException("Unable to access field '" + name + "'", ex);
    }
  }
}

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

/**
 * 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: org.springframework/spring-core

/**
 * If this type is a {@link Map}, creates a mapKey {@link TypeDescriptor}
 * from the provided map key.
 * <p>Narrows the {@link #getMapKeyTypeDescriptor() mapKeyType} property
 * to the class of the provided map key. For example, if this describes a
 * {@code java.util.Map&lt;java.lang.Number, java.lang.String&lt;} and the key
 * 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 key 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 mapKey the map key
 * @return the map key type descriptor
 * @throws IllegalStateException if this type is not a {@code java.util.Map}
 * @see #narrow(Object)
 */
@Nullable
public TypeDescriptor getMapKeyTypeDescriptor(Object mapKey) {
  return narrow(mapKey, getMapKeyTypeDescriptor());
}

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

/**
 * If this type is a {@link Collection} or an array, creates a element TypeDescriptor
 * from the provided collection or array element.
 * <p>Narrows the {@link #getElementTypeDescriptor() elementType} property to the class
 * of the provided collection or array element. For example, if this describes a
 * {@code java.util.List&lt;java.lang.Number&lt;} and the element argument is an
 * {@code java.lang.Integer}, the returned TypeDescriptor will be {@code java.lang.Integer}.
 * If this describes a {@code java.util.List&lt;?&gt;} and the element argument is an
 * {@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 element the collection or array element
 * @return a element type descriptor, narrowed to the type of the provided element
 * @throws IllegalStateException if this type is not a {@code java.util.Collection}
 * or array type
 * @see #narrow(Object)
 */
@Nullable
public TypeDescriptor elementTypeDescriptor(Object element) {
  return narrow(element, getElementTypeDescriptor());
}

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

@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
  try {
    this.argumentConversionOccurred = ReflectionHelper.convertArguments(
        context.getTypeConverter(), arguments, this.originalMethod, this.varargsPosition);
    if (this.originalMethod.isVarArgs()) {
      arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(
          this.originalMethod.getParameterTypes(), arguments);
    }
    ReflectionUtils.makeAccessible(this.methodToInvoke);
    Object value = this.methodToInvoke.invoke(target, arguments);
    return new TypedValue(value, new TypeDescriptor(new MethodParameter(this.originalMethod, -1)).narrow(value));
  }
  catch (Exception ex) {
    throw new AccessException("Problem invoking method: " + this.methodToInvoke, ex);
  }
}

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

@Override
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
  if (this.member instanceof Method) {
    Method method = (Method) this.member;
    try {
      ReflectionUtils.makeAccessible(method);
      Object value = method.invoke(target);
      return new TypedValue(value, this.typeDescriptor.narrow(value));
    }
    catch (Exception ex) {
      throw new AccessException("Unable to access property '" + name + "' through getter method", ex);
    }
  }
  else {
    Field field = (Field) this.member;
    try {
      ReflectionUtils.makeAccessible(field);
      Object value = field.get(target);
      return new TypedValue(value, this.typeDescriptor.narrow(value));
    }
    catch (Exception ex) {
      throw new AccessException("Unable to access field '" + name + "'", ex);
    }
  }
}

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

ReflectionUtils.makeAccessible(method);
Object value = method.invoke(target);
return new TypedValue(value, invoker.typeDescriptor.narrow(value));
ReflectionUtils.makeAccessible(field);
Object value = field.get(target);
return new TypedValue(value, invoker.typeDescriptor.narrow(value));

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

Object result = method.invoke(method.getClass(), functionArgs);
compilable = !argumentConversionOccurred;
return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));

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

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

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

@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
  try {
    this.argumentConversionOccurred = ReflectionHelper.convertArguments(
        context.getTypeConverter(), arguments, this.originalMethod, this.varargsPosition);
    if (this.originalMethod.isVarArgs()) {
      arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(
          this.originalMethod.getParameterTypes(), arguments);
    }
    ReflectionUtils.makeAccessible(this.methodToInvoke);
    Object value = this.methodToInvoke.invoke(target, arguments);
    return new TypedValue(value, new TypeDescriptor(new MethodParameter(this.originalMethod, -1)).narrow(value));
  }
  catch (Exception ex) {
    throw new AccessException("Problem invoking method: " + this.methodToInvoke, ex);
  }
}

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

ReflectionUtils.makeAccessible(method);
Object value = method.invoke(target);
return new TypedValue(value, invoker.typeDescriptor.narrow(value));
ReflectionUtils.makeAccessible(field);
Object value = field.get(target);
return new TypedValue(value, invoker.typeDescriptor.narrow(value));

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

Object result = method.invoke(method.getClass(), functionArgs);
compilable = !argumentConversionOccurred;
return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));

代码示例来源:origin: camunda/camunda-bpm-platform

private TypeDescriptor narrow(Object value, TypeDescriptor typeDescriptor) {
  if (typeDescriptor != null) {
    return typeDescriptor.narrow(value);
  }
  else {
    return (value != null ? new TypeDescriptor(value.getClass(), null, null, null, this.annotations) : null);
  }        
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * If this type is a {@link Map}, creates a mapValue {@link TypeDescriptor} from the provided map value.
 * Narrows the {@link #getMapValueTypeDescriptor() mapValueType} property to the class of the provided map value.
 * For example, if this describes a java.util.Map&lt;java.lang.String, java.lang.Number&lt; and the value argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer.
 * If this describes a java.util.Map&lt;?, ?&gt; and the value argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
 * 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 java.util.Map. 
 */
public TypeDescriptor getMapValueTypeDescriptor(Object mapValue) {
  return narrow(mapValue, getMapValueTypeDescriptor());		
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * If this type is a {@link Collection} or an Array, creates a element TypeDescriptor from the provided collection or array element.
 * Narrows the {@link #getElementTypeDescriptor() elementType} property to the class of the provided collection or array element.
 * For example, if this describes a java.util.List&lt;java.lang.Number&lt; and the element argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer.
 * If this describes a java.util.List&lt;?&gt; and the element argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
 * Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned. 
 * @param element the collection or array element
 * @return a element type descriptor, narrowed to the type of the provided element
 * @throws IllegalStateException if this type is not a java.util.Collection or Array type
 * @see #narrow(Object)
 */
public TypeDescriptor elementTypeDescriptor(Object element) {
  return narrow(element, getElementTypeDescriptor());
}

相关文章

微信公众号

最新文章

更多