55import me .zort .sqllib .api .data .QueryRowsResult ;
66
77import java .lang .reflect .Method ;
8- import java .lang .reflect .TypeVariable ;
8+ import java .lang .reflect .ParameterizedType ;
9+ import java .lang .reflect .Type ;
910import java .util .List ;
1011import java .util .Optional ;
1112
1213public class DefaultResultAdapter implements StatementMappingResultAdapter {
1314 @ Override
1415 public Object adaptResult (Method method , QueryResult result ) {
1516 Class <?> returnType = method .getReturnType ();
16- if (isVoid (returnType ) || !(result instanceof QueryRowsResult ))
17+
18+ if (returnType .equals (QueryResult .class )) {
19+ return result ;
20+ } else if (isVoid (returnType ) || !(result instanceof QueryRowsResult )) {
1721 return null ;
22+ }
1823
1924 QueryRowsResult <?> rows = (QueryRowsResult <?>) result ;
2025
@@ -34,16 +39,17 @@ public Object adaptResult(Method method, QueryResult result) {
3439 public Class <?> retrieveResultType (Method method ) {
3540 Class <?> returnType = method .getReturnType ();
3641 if (returnType .equals (Optional .class ) || returnType .equals (List .class )) {
37- return getGenericArgument (returnType , true );
42+ return getGenericArgument (returnType , method . getGenericReturnType (), true );
3843 } else if (isVoid (returnType )) {
3944 return null ;
4045 } else {
4146 return returnType ;
4247 }
4348 }
4449
45- private static Class <?> getGenericArgument (Class <?> clazz , boolean throwNotFound ) {
46- TypeVariable <? extends Class <?>>[] typeParameters = clazz .getTypeParameters ();
50+ // List<T>
51+ private static Class <?> getGenericArgument (Class <?> clazz , Type genericType , boolean throwNotFound ) {
52+ Type [] typeParameters = ((ParameterizedType ) genericType ).getActualTypeArguments ();
4753 if (typeParameters .length < 1 ) {
4854 if (throwNotFound ) {
4955 throw new IllegalArgumentException ("The given class does not have a generic argument" );
@@ -52,7 +58,12 @@ private static Class<?> getGenericArgument(Class<?> clazz, boolean throwNotFound
5258 } // For simplicity, return itself to be mapped.
5359 }
5460
55- return typeParameters [0 ].getGenericDeclaration (); // TODO: Test
61+ try {
62+ return Class .forName (typeParameters [0 ].getTypeName ());
63+ } catch (ClassNotFoundException e ) {
64+ e .printStackTrace ();
65+ return clazz ;
66+ }
5667 }
5768
5869 private static boolean isVoid (Class <?> type ) {
0 commit comments