org.springframework.core.ResolvableType.forMethodParameter()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(165)

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

ResolvableType.forMethodParameter介绍

[英]Return a ResolvableType for the specified Method parameter.
[中]返回指定方法参数的ResolvableType。

代码示例

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

/**
 * Return a {@link ResolvableType} for the specified {@link MethodParameter}.
 * @param methodParameter the source method parameter (must not be {@code null})
 * @return a {@link ResolvableType} for the specified method parameter
 * @see #forMethodParameter(Method, int)
 */
public static ResolvableType forMethodParameter(MethodParameter methodParameter) {
  return forMethodParameter(methodParameter, (Type) null);
}

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

/**
 * Return a {@link ResolvableType} for the specified {@link Method} return type.
 * @param method the source for the method return type
 * @return a {@link ResolvableType} for the specified method return
 * @see #forMethodReturnType(Method, Class)
 */
public static ResolvableType forMethodReturnType(Method method) {
  Assert.notNull(method, "Method must not be null");
  return forMethodParameter(new MethodParameter(method, -1));
}

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

/**
 * Return a {@link ResolvableType} for the specified {@link Constructor} parameter.
 * @param constructor the source constructor (must not be {@code null})
 * @param parameterIndex the parameter index
 * @return a {@link ResolvableType} for the specified constructor parameter
 * @see #forConstructorParameter(Constructor, int, Class)
 */
public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex) {
  Assert.notNull(constructor, "Constructor must not be null");
  return forMethodParameter(new MethodParameter(constructor, parameterIndex));
}

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

/**
 * Return a {@link ResolvableType} for the specified {@link Method} parameter.
 * @param method the source method (must not be {@code null})
 * @param parameterIndex the parameter index
 * @return a {@link ResolvableType} for the specified method parameter
 * @see #forMethodParameter(Method, int, Class)
 * @see #forMethodParameter(MethodParameter)
 */
public static ResolvableType forMethodParameter(Method method, int parameterIndex) {
  Assert.notNull(method, "Method must not be null");
  return forMethodParameter(new MethodParameter(method, parameterIndex));
}

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

private boolean isSingleValueMap(MethodParameter parameter) {
  if (!MultiValueMap.class.isAssignableFrom(parameter.getParameterType())) {
    ResolvableType[] genericTypes = ResolvableType.forMethodParameter(parameter).getGenerics();
    if (genericTypes.length == 2) {
      return !List.class.isAssignableFrom(genericTypes[1].toClass());
    }
  }
  return false;
}

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

private boolean isSingleValueMap(MethodParameter parameter) {
  if (!MultiValueMap.class.isAssignableFrom(parameter.getParameterType())) {
    ResolvableType[] genericTypes = ResolvableType.forMethodParameter(parameter).getGenerics();
    if (genericTypes.length == 2) {
      return !List.class.isAssignableFrom(genericTypes[1].toClass());
    }
  }
  return false;
}

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

@Override
protected Class<?> getReturnValueType(@Nullable Object returnValue, MethodParameter returnType) {
  if (returnValue != null) {
    return returnValue.getClass();
  }
  else {
    Type type = getHttpEntityType(returnType);
    type = (type != null ? type : Object.class);
    return ResolvableType.forMethodParameter(returnType, type).toClass();
  }
}

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

@Nullable
private static Class<?> getCollectionParameterType(MethodParameter methodParam) {
  Class<?> paramType = methodParam.getNestedParameterType();
  if (Collection.class == paramType || List.class.isAssignableFrom(paramType)){
    Class<?> valueType = ResolvableType.forMethodParameter(methodParam).asCollection().resolveGeneric();
    if (valueType != null) {
      return valueType;
    }
  }
  return null;
}

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

/**
 * Create a new type descriptor from a {@link Property}.
 * <p>Use this constructor when a source or target conversion point is a
 * property on a Java class.
 * @param property the property
 */
public TypeDescriptor(Property property) {
  Assert.notNull(property, "Property must not be null");
  this.resolvableType = ResolvableType.forMethodParameter(property.getMethodParameter());
  this.type = this.resolvableType.resolve(property.getType());
  this.annotatedElement = new AnnotatedElementAdapter(property.getAnnotations());
}

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

@Test
public void forMethodParameterByIndex() throws Exception {
  Method method = Methods.class.getMethod("charSequenceParameter", List.class);
  ResolvableType type = ResolvableType.forMethodParameter(method, 0);
  assertThat(type.getType(), equalTo(method.getGenericParameterTypes()[0]));
}

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

@Test
public void forMethodParameter() throws Exception {
  Method method = Methods.class.getMethod("charSequenceParameter", List.class);
  MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
  ResolvableType type = ResolvableType.forMethodParameter(methodParameter);
  assertThat(type.getType(), equalTo(method.getGenericParameterTypes()[0]));
}

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

@Test
public void resolveTypeVariableFromMethodParameter() throws Exception {
  Method method = Methods.class.getMethod("typedParameter", Object.class);
  ResolvableType type = ResolvableType.forMethodParameter(method, 0);
  assertThat(type.resolve(), nullValue());
  assertThat(type.getType().toString(), equalTo("T"));
}

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

@Test
public void resolveTypeVariableFromMethodParameterWithImplementsClass() throws Exception {
  Method method = Methods.class.getMethod("typedParameter", Object.class);
  ResolvableType type = ResolvableType.forMethodParameter(method, 0, TypedMethods.class);
  assertThat(type.resolve(), equalTo((Class) String.class));
  assertThat(type.getType().toString(), equalTo("T"));
}

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

/**
 * Create a new type descriptor from a {@link MethodParameter}.
 * <p>Use this constructor when a source or target conversion point is a
 * constructor parameter, method parameter, or method return value.
 * @param methodParameter the method parameter
 */
public TypeDescriptor(MethodParameter methodParameter) {
  this.resolvableType = ResolvableType.forMethodParameter(methodParameter);
  this.type = this.resolvableType.resolve(methodParameter.getNestedParameterType());
  this.annotatedElement = new AnnotatedElementAdapter(methodParameter.getParameterIndex() == -1 ?
      methodParameter.getMethodAnnotations() : methodParameter.getParameterAnnotations());
}

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

@Test
public void forMethodParameterByIndexMustNotBeNull() throws Exception {
  this.thrown.expect(IllegalArgumentException.class);
  this.thrown.expectMessage("Method must not be null");
  ResolvableType.forMethodParameter(null, 0);
}

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

@Test
public void resolveTypeVariableFromMethodParameterTypeWithImplementsClass() throws Exception {
  Method method = Methods.class.getMethod("typedParameter", Object.class);
  MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
  methodParameter.setContainingClass(TypedMethods.class);
  ResolvableType type = ResolvableType.forMethodParameter(methodParameter);
  assertThat(type.resolve(), equalTo((Class) String.class));
  assertThat(type.getType().toString(), equalTo("T"));
}

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

@Test
public void resolveTypeVariableFromMethodParameterTypeWithImplementsType() throws Exception {
  Method method = Methods.class.getMethod("typedParameter", Object.class);
  MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
  ResolvableType implementationType = ResolvableType.forClassWithGenerics(Methods.class, Integer.class);
  ResolvableType type = ResolvableType.forMethodParameter(methodParameter, implementationType);
  assertThat(type.resolve(), equalTo((Class) Integer.class));
  assertThat(type.getType().toString(), equalTo("T"));
}

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

@Test
public void forMethodParameterMustNotBeNull() throws Exception {
  this.thrown.expect(IllegalArgumentException.class);
  this.thrown.expectMessage("MethodParameter must not be null");
  ResolvableType.forMethodParameter(null);
}

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

@Test
public void forMethodParameterWithNesting() throws Exception {
  Method method = Methods.class.getMethod("nested", Map.class);
  MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
  methodParameter.increaseNestingLevel();
  ResolvableType type = ResolvableType.forMethodParameter(methodParameter);
  assertThat(type.resolve(), equalTo((Class) Map.class));
  assertThat(type.getGeneric(0).resolve(), equalTo((Class) Byte.class));
  assertThat(type.getGeneric(1).resolve(), equalTo((Class) Long.class));
}

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

@Test
public void serialize() throws Exception {
  testSerialization(ResolvableType.forClass(List.class));
  testSerialization(ResolvableType.forField(Fields.class.getField("charSequenceList")));
  testSerialization(ResolvableType.forMethodParameter(Methods.class.getMethod("charSequenceParameter", List.class), 0));
  testSerialization(ResolvableType.forMethodReturnType(Methods.class.getMethod("charSequenceReturn")));
  testSerialization(ResolvableType.forConstructorParameter(Constructors.class.getConstructor(List.class), 0));
  testSerialization(ResolvableType.forField(Fields.class.getField("charSequenceList")).getGeneric());
  ResolvableType deserializedNone = testSerialization(ResolvableType.NONE);
  assertThat(deserializedNone, sameInstance(ResolvableType.NONE));
}

相关文章

微信公众号

最新文章

更多