List of usage examples for org.apache.ibatis.reflection TypeParameterResolver resolveReturnType
public static Type resolveReturnType(Method method, Type srcType)
From source file:com.baomidou.mybatisplus.MybatisMapperAnnotationBuilder.java
License:Apache License
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(); }//from w w w . j a va 2 s . c o m // 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; }
From source file:com.mybatisX.core.MybatisMapperAnnotationBuilder.java
License:Apache License
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(); }/*ww w . ja v a 2 s.c om*/ // 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; }