Search in sources :

Example 1 with ResultType

use of org.apache.ibatis.annotations.ResultType in project mybatis-3 by mybatis.

the class MapperAnnotationBuilder method getReturnType.

private Class<?> getReturnType(Method method) {
    Class<?> returnType = method.getReturnType();
    Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, type);
    if (resolvedReturnType instanceof Class) {
        returnType = (Class<?>) resolvedReturnType;
        if (returnType.isArray()) {
            returnType = returnType.getComponentType();
        }
        // gcode issue #508
        if (void.class.equals(returnType)) {
            ResultType rt = method.getAnnotation(ResultType.class);
            if (rt != null) {
                returnType = rt.value();
            }
        }
    } else if (resolvedReturnType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) resolvedReturnType;
        Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        if (Collection.class.isAssignableFrom(rawType) || Cursor.class.isAssignableFrom(rawType)) {
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            if (actualTypeArguments != null && actualTypeArguments.length == 1) {
                Type returnTypeParameter = actualTypeArguments[0];
                if (returnTypeParameter instanceof Class<?>) {
                    returnType = (Class<?>) returnTypeParameter;
                } else if (returnTypeParameter instanceof ParameterizedType) {
                    // (gcode 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();
                    // (gcode issue #525) support List<byte[]>
                    returnType = Array.newInstance(componentType, 0).getClass();
                }
            }
        } else if (method.isAnnotationPresent(MapKey.class) && Map.class.isAssignableFrom(rawType)) {
            // (gcode issue 504) Do not look into Maps if there is not MapKey annotation
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            if (actualTypeArguments != null && actualTypeArguments.length == 2) {
                Type returnTypeParameter = actualTypeArguments[1];
                if (returnTypeParameter instanceof Class<?>) {
                    returnType = (Class<?>) returnTypeParameter;
                } else if (returnTypeParameter instanceof ParameterizedType) {
                    // (gcode issue 443) actual type can be a also a parameterized type
                    returnType = (Class<?>) ((ParameterizedType) returnTypeParameter).getRawType();
                }
            }
        }
    }
    return returnType;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) SqlCommandType(org.apache.ibatis.mapping.SqlCommandType) FetchType(org.apache.ibatis.mapping.FetchType) Type(java.lang.reflect.Type) JdbcType(org.apache.ibatis.type.JdbcType) StatementType(org.apache.ibatis.mapping.StatementType) ResultType(org.apache.ibatis.annotations.ResultType) ResultSetType(org.apache.ibatis.mapping.ResultSetType) ParameterizedType(java.lang.reflect.ParameterizedType) MapKey(org.apache.ibatis.annotations.MapKey) ResultType(org.apache.ibatis.annotations.ResultType) GenericArrayType(java.lang.reflect.GenericArrayType) Map(java.util.Map) ParamMap(org.apache.ibatis.binding.MapperMethod.ParamMap) HashMap(java.util.HashMap) ResultMap(org.apache.ibatis.annotations.ResultMap)

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MapKey (org.apache.ibatis.annotations.MapKey)1 ResultMap (org.apache.ibatis.annotations.ResultMap)1 ResultType (org.apache.ibatis.annotations.ResultType)1 ParamMap (org.apache.ibatis.binding.MapperMethod.ParamMap)1 FetchType (org.apache.ibatis.mapping.FetchType)1 ResultSetType (org.apache.ibatis.mapping.ResultSetType)1 SqlCommandType (org.apache.ibatis.mapping.SqlCommandType)1 StatementType (org.apache.ibatis.mapping.StatementType)1 JdbcType (org.apache.ibatis.type.JdbcType)1