Java Reflection Method Return getMethodGenericReturnType(Method method, Class rawType, int index)

Here you can find the source of getMethodGenericReturnType(Method method, Class rawType, int index)

Description

get Method Generic Return Type

License

Open Source License

Declaration

public static Class<?> getMethodGenericReturnType(Method method, Class<?> rawType, int index) 

Method Source Code

//package com.java2s;
/**/*from w  w w .  j  av  a2  s  .  c o m*/
 * Project: ocean.client.java.basic
 * 
 * File Created at 2011-10-27
 * $Id: GenericsUtil.java 311300 2013-12-23 06:15:28Z yichun.wangyc $
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */

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

public class Main {
    public static Class<?> getMethodGenericReturnType(Method method, Class<?> rawType, int index) {
        Type returnType = method.getGenericReturnType();
        return getGenericType(returnType, rawType, index);
    }

    public static Class<?> getGenericType(Type type, Class<?> rawType, int index) {
        if (type instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedType) type;
            if (rawType.equals(ptype.getRawType())) {
                Type[] typeArguments = ptype.getActualTypeArguments();
                if (index >= typeArguments.length || index < 0) {
                    throw new RuntimeException(
                            "index " + (index < 0 ? " must large then 0" : "out of arguments count"));
                }
                return getRawType(typeArguments[index]);
            }
        }
        return null;
    }

    public static Class<?>[] getGenericType(Type type) {
        if (type instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedType) type;
            Type[] typeArguments = ptype.getActualTypeArguments();
            Class<?>[] types = new Class<?>[typeArguments.length];
            System.arraycopy(typeArguments, 0, types, 0, types.length);
            return types;
        }
        return null;
    }

    public static Class<?> getRawType(Type type) {
        if (type instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedType) type;
            return getRawType(ptype.getRawType());
        } else if (type instanceof Class) {
            return (Class<?>) type;
        }
        return null;
    }
}

Related

  1. getMethod(Class type, String name, Class returnType, Class paramType, boolean caseSensitive)
  2. getMethodByReturnType(final Class source, final Class type)
  3. getMethodByTypes(Class clazz, String name, Class returnType, Class... parameterTypes)
  4. getMethodDesc(Class returnType, Class... params)
  5. getMethode(Class clasS, Class Returntype, int modifier, Class... classes)
  6. getMethodGenericReturnType(Method method, int index)
  7. getMethodGenericReturnType(Method method, int index)
  8. getMethodInvoker(final Class returnType, final String methodName)
  9. getMethodNoArgs(final Class objClass, final String methodName, final Class... returnTypePreference)