Java Reflection Generic Return Type getGenericReturnClass(Method method)

Here you can find the source of getGenericReturnClass(Method method)

Description

get Generic Return Class

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
public static Class getGenericReturnClass(Method method) 

Method Source Code


//package com.java2s;
/*//from ww w  .ja va2s.  c o m
 * $RCSfile: GenericsUtils,v $$
 * $Revision: 1.0  $
 * $Date: 2011  $
 *
 * Copyright (C) 2011 GyTech, Inc. All rights reserved.
 *
 * This software is the proprietary information of GyTech, Inc.
 * Use is subject to license terms.
 */

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {

    @SuppressWarnings("unchecked")
    public static Class getGenericReturnClass(Method method) {
        Class returnClazz = method.getReturnType();
        Type genType = method.getGenericReturnType();
        if (genType instanceof ParameterizedType) {
            Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

            if ((params != null) && (params.length >= 0)) {
                returnClazz = (Class) params[0];
            }
        }
        return returnClazz;
    }
}

Related

  1. getGenericClassFromMethodReturn(Method m)
  2. getGenericReturnType(Method m)
  3. getGenericReturnTypeMap(Method method, boolean isAllowNull)
  4. getGenericReturnTypeOfGenericInterfaceMethod( Class clazz, Method method)
  5. getGenericReturnTypeOfGenericInterfaceMethod(Class clazz, Method method)