All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yixsoft.support.mybatis.utils.MapperMethodUtils Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package com.yixsoft.support.mybatis.utils;

import java.lang.reflect.*;
import java.util.Collection;

/**
 * get method return type utils
 * Created by yixian on 2015-09-03.
 */
public class MapperMethodUtils {
    public static Class getReturnType(Method method) {
        Class returnType = method.getReturnType();
        // issue #508
        if (Collection.class.isAssignableFrom(returnType)) {
            Type returnTypeParameter = method.getGenericReturnType();
            if (returnTypeParameter instanceof ParameterizedType) {
                Type[] actualTypeArguments = ((ParameterizedType) returnTypeParameter).getActualTypeArguments();
                if (actualTypeArguments != null && actualTypeArguments.length == 1) {
                    returnTypeParameter = actualTypeArguments[0];
                    if (returnTypeParameter instanceof Class) {
                        returnType = (Class) returnTypeParameter;
                    } else if (returnTypeParameter instanceof ParameterizedType) {
                        // (issue #443) actual type can be a also a parameterized type
                        returnType = (Class) ((ParameterizedType) returnTypeParameter).getRawType();
                    } else if (returnTypeParameter instanceof GenericArrayType) {
                        Class componentType = (Class) ((GenericArrayType) returnTypeParameter).getGenericComponentType();
                        // (issue #525) support List
                        returnType = Array.newInstance(componentType, 0).getClass();
                    }
                }
            }
        }

        return returnType;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy