@Override Type getGenericReturnType() { return method.getGenericReturnType(); }
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); } }
@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); } });
private static Type genericReturnType(Class<?> cls, String methodName) { try { return cls.getMethod(methodName).getGenericReturnType(); } catch (Exception e) { throw new RuntimeException(e); } }
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()); } } } }
@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())); }
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()); }
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); }
@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() {
@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() {
@Test public void listTypeReference() throws Exception { Type listType = getClass().getMethod("listMethod").getGenericReturnType(); ParameterizedTypeReference<List<String>> typeReference = new ParameterizedTypeReference<List<String>>() {}; assertEquals(listType, typeReference.getType()); }
@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()); }
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()); }
@Test public void forMethodReturn() throws Exception { Method method = Methods.class.getMethod("charSequenceReturn"); ResolvableType type = ResolvableType.forMethodReturnType(method); assertThat(type.getType(), equalTo(method.getGenericReturnType())); }
@Test public void reflectiveTypeReferenceWithSpecificDeclaration() throws Exception{ Type listType = getClass().getMethod("listMethod").getGenericReturnType(); ParameterizedTypeReference<List<String>> typeReference = ParameterizedTypeReference.forType(listType); assertEquals(listType, typeReference.getType()); }
@Test public void reflectiveTypeReferenceWithGenericDeclaration() throws Exception{ Type listType = getClass().getMethod("listMethod").getGenericReturnType(); ParameterizedTypeReference<?> typeReference = ParameterizedTypeReference.forType(listType); assertEquals(listType, typeReference.getType()); }
@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")); }
@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")); }
@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); }
@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")); }