java.lang.reflect.Method.getGenericReturnType()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(208)

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

Method.getGenericReturnType介绍

[英]Returns a Type object that represents the formal return type of the method represented by this Method object.

If the return type is a parameterized type, the Type object returned must accurately reflect the actual type parameters used in the source code.

If the return type is a type variable or a parameterized type, it is created. Otherwise, it is resolved.
[中]

代码示例

代码示例来源:origin: google/guava

@Override
Type getGenericReturnType() {
 return method.getGenericReturnType();
}

代码示例来源:origin: square/retrofit

private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter(
  Retrofit retrofit, Method method) {
 Type returnType = method.getGenericReturnType();
 Annotation[] annotations = method.getAnnotations();
 try {
  //noinspection unchecked
  return (CallAdapter<ResponseT, ReturnT>) retrofit.callAdapter(returnType, annotations);
 } catch (RuntimeException e) { // Wide exception range because factories are user code.
  throw methodError(method, e, "Unable to create call adapter for %s", returnType);
 }
}

代码示例来源:origin: square/retrofit

@Override
 public T invoke(Object proxy, Method method, Object[] args) throws Throwable {
  Type returnType = method.getGenericReturnType();
  Annotation[] methodAnnotations = method.getAnnotations();
  CallAdapter<R, T> callAdapter =
    (CallAdapter<R, T>) retrofit.callAdapter(returnType, methodAnnotations);
  return callAdapter.adapt(behaviorCall);
 }
});

代码示例来源:origin: google/guava

private static Type genericReturnType(Class<?> cls, String methodName) {
 try {
  return cls.getMethod(methodName).getGenericReturnType();
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: google/guava

static void verifyConsitentRawType() {
  for (Method method : RawTypeConsistencyTester.class.getDeclaredMethods()) {
   assertEquals(
     method.getReturnType(), TypeToken.of(method.getGenericReturnType()).getRawType());
   for (int i = 0; i < method.getParameterTypes().length; i++) {
    assertEquals(
      method.getParameterTypes()[i],
      TypeToken.of(method.getGenericParameterTypes()[i]).getRawType());
   }
  }
 }
}

代码示例来源:origin: google/guava

@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) {
 Invokable<?, ?> invokable = interfaceType.method(method);
 ImmutableList<Parameter> params = invokable.getParameters();
 for (int i = 0; i < args.length; i++) {
  Parameter param = params.get(i);
  if (!isNullable(param)) {
   checkNotNull(args[i]);
  }
 }
 return dummyReturnValue(interfaceType.resolveType(method.getGenericReturnType()));
}

代码示例来源:origin: google/guava

public void testToGenericType_staticMemberClass() throws Exception {
 Method getStaticAnonymousClassMethod =
   TypeTokenTest.class.getDeclaredMethod("getStaticAnonymousClass", Object.class);
 ParameterizedType javacReturnType =
   (ParameterizedType) getStaticAnonymousClassMethod.getGenericReturnType();
 ParameterizedType parameterizedType =
   (ParameterizedType) TypeToken.toGenericType(GenericClass.class).getType();
 assertThat(parameterizedType.getOwnerType()).isEqualTo(javacReturnType.getOwnerType());
}

代码示例来源:origin: square/retrofit

static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {
 RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method);
 Type returnType = method.getGenericReturnType();
 if (Utils.hasUnresolvableType(returnType)) {
  throw methodError(method,
    "Method return type must not include a type variable or wildcard: %s", returnType);
 }
 if (returnType == void.class) {
  throw methodError(method, "Service methods cannot return void.");
 }
 return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
}

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

@Override
  public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
    Type returnType = method.getGenericReturnType();
    assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(9) + ">",
        marshaller.supports(returnType));
    try {
      // make sure the marshalling does not result in errors
      Object returnValue = method.invoke(primitives);
      marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream()));
    }
    catch (InvocationTargetException e) {
      fail(e.getMessage());
    }
  }
}, new ReflectionUtils.MethodFilter() {

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

@Override
  public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
    Type returnType = method.getGenericReturnType();
    assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(13) + ">",
        marshaller.supports(returnType));
    try {
      // make sure the marshalling does not result in errors
      Object returnValue = method.invoke(standardClasses);
      marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream()));
    }
    catch (InvocationTargetException e) {
      fail(e.getMessage());
    }
  }
}, new ReflectionUtils.MethodFilter() {

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

@Test
public void listTypeReference() throws Exception {
  Type listType = getClass().getMethod("listMethod").getGenericReturnType();
  ParameterizedTypeReference<List<String>> typeReference = new ParameterizedTypeReference<List<String>>() {};
  assertEquals(listType, typeReference.getType());
}

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

@Test
public void mapTypeReference() throws Exception {
  Type mapType = getClass().getMethod("mapMethod").getGenericReturnType();
  ParameterizedTypeReference<Map<Object,String>> typeReference = new ParameterizedTypeReference<Map<Object,String>>() {};
  assertEquals(mapType, typeReference.getType());
}

代码示例来源:origin: google/guava

public void testResolveType() throws Exception {
 Method getFromList = List.class.getMethod("get", int.class);
 TypeToken<?> returnType =
   new TypeToken<List<String>>() {}.resolveType(getFromList.getGenericReturnType());
 assertEquals(String.class, returnType.getType());
}

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

@Test
public void forMethodReturn() throws Exception {
  Method method = Methods.class.getMethod("charSequenceReturn");
  ResolvableType type = ResolvableType.forMethodReturnType(method);
  assertThat(type.getType(), equalTo(method.getGenericReturnType()));
}

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

@Test
public void reflectiveTypeReferenceWithSpecificDeclaration() throws Exception{
  Type listType = getClass().getMethod("listMethod").getGenericReturnType();
  ParameterizedTypeReference<List<String>> typeReference = ParameterizedTypeReference.forType(listType);
  assertEquals(listType, typeReference.getType());
}

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

@Test
public void reflectiveTypeReferenceWithGenericDeclaration() throws Exception{
  Type listType = getClass().getMethod("listMethod").getGenericReturnType();
  ParameterizedTypeReference<?> typeReference = ParameterizedTypeReference.forType(listType);
  assertEquals(listType, typeReference.getType());
}

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

@Test
public void resolveTypeVariableFromType() throws Exception {
  Type sourceType = Methods.class.getMethod("typedReturn").getGenericReturnType();
  ResolvableType type = ResolvableType.forType(sourceType);
  assertThat(type.resolve(), nullValue());
  assertThat(type.getType().toString(), equalTo("T"));
}

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

@Test
public void resolveTypeVariableFromReflectiveParameterizedTypeReference() throws Exception {
  Type sourceType = Methods.class.getMethod("typedReturn").getGenericReturnType();
  ResolvableType type = ResolvableType.forType(ParameterizedTypeReference.forType(sourceType));
  assertThat(type.resolve(), nullValue());
  assertThat(type.getType().toString(), equalTo("T"));
}

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

@Test
public void resolveTypeVariableFromDeclaredParameterizedTypeReference() throws Exception {
  Type sourceType = Methods.class.getMethod("charSequenceReturn").getGenericReturnType();
  ResolvableType reflectiveType = ResolvableType.forType(sourceType);
  ResolvableType declaredType = ResolvableType.forType(new ParameterizedTypeReference<List<CharSequence>>() {});
  assertEquals(reflectiveType, declaredType);
}

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

@Test
public void resolveTypeVariableFromTypeWithVariableResolver() throws Exception {
  Type sourceType = Methods.class.getMethod("typedReturn").getGenericReturnType();
  ResolvableType type = ResolvableType.forType(
      sourceType, ResolvableType.forClass(TypedMethods.class).as(Methods.class).asVariableResolver());
  assertThat(type.resolve(), equalTo((Class) String.class));
  assertThat(type.getType().toString(), equalTo("T"));
}

相关文章

微信公众号

最新文章

更多