From a0dfc278c37a4ba86ed4a45c4f5e2223596099f1 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Thu, 10 May 2018 23:27:32 -0400 Subject: [PATCH] Implement toArrayMap for toType --- .../moparisthebest/jdbc/ResultSetMapper.java | 6 ++++-- .../moparisthebest/jdbc/TypeReference.java | 21 ++++++++++++++++++- .../jdbc/codegen/QueryMapperTypeQmDao.java | 4 +--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/querymapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java b/querymapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java index f0ea218..19e300d 100644 --- a/querymapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java +++ b/querymapper/src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java @@ -466,7 +466,7 @@ public class ResultSetMapper implements RowMapperProvider { // overloaded helper methods @SuppressWarnings("unchecked") - protected Object toType(final ResultSet rs, final Class returnType, final ParameterizedType type, final int arrayMaxLength, final Calendar cal) { + protected Object toType(final ResultSet rs, final Class returnType, final ParameterizedType type, final boolean genericArray, final int arrayMaxLength, final Calendar cal) { if (returnType.isArray()) { return toArray(rs, returnType.getComponentType(), arrayMaxLength, cal); } else if (Collection.class.isAssignableFrom(returnType)) { @@ -493,6 +493,8 @@ public class ResultSetMapper implements RowMapperProvider { Class collectionType = (Class) pt.getRawType(); if (Collection.class.isAssignableFrom(collectionType)) return toMapCollection(rs, returnType, (Class) types[0], collectionType, (Class) pt.getActualTypeArguments()[0], arrayMaxLength, cal); + } else if(genericArray && types[1] instanceof Class) { + return toArrayMap(rs, returnType, (Class) types[1], arrayMaxLength, cal); } return toMap(rs, com.moparisthebest.jdbc.ResultSetMapper.instantiateClass((Class)returnType, HashMap.class), (Class) types[0], (Class) types[1], arrayMaxLength, cal); } else if (Iterator.class.isAssignableFrom(returnType)) { @@ -516,7 +518,7 @@ public class ResultSetMapper implements RowMapperProvider { @SuppressWarnings("unchecked") public T toType(ResultSet rs, TypeReference typeReference, int arrayMaxLength, Calendar cal) { - return (T)this.toType(rs, typeReference.getRawType(), typeReference.getType(), arrayMaxLength, cal); + return (T)this.toType(rs, typeReference.getRawType(), typeReference.getType(), typeReference.isGenericArray(), arrayMaxLength, cal); } public T toObject(ResultSet rs, Class componentType, Calendar cal) { diff --git a/querymapper/src/main/java/com/moparisthebest/jdbc/TypeReference.java b/querymapper/src/main/java/com/moparisthebest/jdbc/TypeReference.java index 08e5abb..b4cfe7a 100644 --- a/querymapper/src/main/java/com/moparisthebest/jdbc/TypeReference.java +++ b/querymapper/src/main/java/com/moparisthebest/jdbc/TypeReference.java @@ -1,8 +1,8 @@ package com.moparisthebest.jdbc; +import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.Arrays; /** * This generic abstract class is used for obtaining full generics type information @@ -32,6 +32,7 @@ public abstract class TypeReference implements Comparable> { private final ParameterizedType type; private final Class rawType; + private final boolean genericArray; protected TypeReference() { final Type superClass = getClass().getGenericSuperclass(); @@ -49,9 +50,23 @@ public abstract class TypeReference implements Comparable> { if (type instanceof Class) { this.type = null; this.rawType = (Class) type; + this.genericArray = false; } else if (type instanceof ParameterizedType) { this.type = (ParameterizedType) type; this.rawType = (Class) this.type.getRawType(); + this.genericArray = false; + } else if (type instanceof GenericArrayType) { + final Type arrayComponentType = ((GenericArrayType)type).getGenericComponentType(); + if (arrayComponentType instanceof Class) { + this.type = null; + this.rawType = (Class) arrayComponentType; + } else if (arrayComponentType instanceof ParameterizedType) { + this.type = (ParameterizedType) arrayComponentType; + this.rawType = (Class) this.type.getRawType(); + } else { + throw new IllegalArgumentException("Internal error: TypeReference constructed with unknown type: '" + type + "' class: '" + type.getClass() + "'"); + } + this.genericArray = true; } else { throw new IllegalArgumentException("Internal error: TypeReference constructed with unknown type: '" + type + "' class: '" + type.getClass() + "'"); } @@ -65,6 +80,10 @@ public abstract class TypeReference implements Comparable> { return rawType; } + public boolean isGenericArray() { + return genericArray; + } + /** * The only reason we define this method (and require implementation * of Comparable) is to prevent constructing a diff --git a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java index fcbc060..83d9312 100644 --- a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperTypeQmDao.java @@ -173,9 +173,7 @@ public class QueryMapperTypeQmDao extends QueryMapperQmDao { @Override public Map[] getAllNamesArray() throws SQLException { - return super.getAllNamesArray(); - // todo: fix this - //return qm.toType(allNames, new TypeReference[]>() {}); + return qm.toType(allNames, new TypeReference[]>() {}); } @Override