Implement toArrayMap for toType

This commit is contained in:
Travis Burtrum 2018-05-10 23:27:32 -04:00
parent e2c1f9a12d
commit a0dfc278c3
3 changed files with 25 additions and 6 deletions

View File

@ -466,7 +466,7 @@ public class ResultSetMapper implements RowMapperProvider {
// overloaded helper methods // overloaded helper methods
@SuppressWarnings("unchecked") @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()) { if (returnType.isArray()) {
return toArray(rs, returnType.getComponentType(), arrayMaxLength, cal); return toArray(rs, returnType.getComponentType(), arrayMaxLength, cal);
} else if (Collection.class.isAssignableFrom(returnType)) { } else if (Collection.class.isAssignableFrom(returnType)) {
@ -493,6 +493,8 @@ public class ResultSetMapper implements RowMapperProvider {
Class collectionType = (Class) pt.getRawType(); Class collectionType = (Class) pt.getRawType();
if (Collection.class.isAssignableFrom(collectionType)) if (Collection.class.isAssignableFrom(collectionType))
return toMapCollection(rs, returnType, (Class) types[0], collectionType, (Class) pt.getActualTypeArguments()[0], arrayMaxLength, cal); 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<Map>)returnType, HashMap.class), (Class) types[0], (Class) types[1], arrayMaxLength, cal); return toMap(rs, com.moparisthebest.jdbc.ResultSetMapper.instantiateClass((Class<Map>)returnType, HashMap.class), (Class) types[0], (Class) types[1], arrayMaxLength, cal);
} else if (Iterator.class.isAssignableFrom(returnType)) { } else if (Iterator.class.isAssignableFrom(returnType)) {
@ -516,7 +518,7 @@ public class ResultSetMapper implements RowMapperProvider {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T toType(ResultSet rs, TypeReference<T> typeReference, int arrayMaxLength, Calendar cal) { public <T> T toType(ResultSet rs, TypeReference<T> 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> T toObject(ResultSet rs, Class<T> componentType, Calendar cal) { public <T> T toObject(ResultSet rs, Class<T> componentType, Calendar cal) {

View File

@ -1,8 +1,8 @@
package com.moparisthebest.jdbc; package com.moparisthebest.jdbc;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Arrays;
/** /**
* This generic abstract class is used for obtaining full generics type information * This generic abstract class is used for obtaining full generics type information
@ -32,6 +32,7 @@ public abstract class TypeReference<T> implements Comparable<TypeReference<T>> {
private final ParameterizedType type; private final ParameterizedType type;
private final Class<?> rawType; private final Class<?> rawType;
private final boolean genericArray;
protected TypeReference() { protected TypeReference() {
final Type superClass = getClass().getGenericSuperclass(); final Type superClass = getClass().getGenericSuperclass();
@ -49,9 +50,23 @@ public abstract class TypeReference<T> implements Comparable<TypeReference<T>> {
if (type instanceof Class<?>) { if (type instanceof Class<?>) {
this.type = null; this.type = null;
this.rawType = (Class<?>) type; this.rawType = (Class<?>) type;
this.genericArray = false;
} else if (type instanceof ParameterizedType) { } else if (type instanceof ParameterizedType) {
this.type = (ParameterizedType) type; this.type = (ParameterizedType) type;
this.rawType = (Class<?>) this.type.getRawType(); 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 { } else {
throw new IllegalArgumentException("Internal error: TypeReference constructed with unknown type: '" + type + "' class: '" + type.getClass() + "'"); throw new IllegalArgumentException("Internal error: TypeReference constructed with unknown type: '" + type + "' class: '" + type.getClass() + "'");
} }
@ -65,6 +80,10 @@ public abstract class TypeReference<T> implements Comparable<TypeReference<T>> {
return rawType; return rawType;
} }
public boolean isGenericArray() {
return genericArray;
}
/** /**
* The only reason we define this method (and require implementation * The only reason we define this method (and require implementation
* of <code>Comparable</code>) is to prevent constructing a * of <code>Comparable</code>) is to prevent constructing a

View File

@ -173,9 +173,7 @@ public class QueryMapperTypeQmDao extends QueryMapperQmDao {
@Override @Override
public Map<String, String>[] getAllNamesArray() throws SQLException { public Map<String, String>[] getAllNamesArray() throws SQLException {
return super.getAllNamesArray(); return qm.toType(allNames, new TypeReference<Map<String, String>[]>() {});
// todo: fix this
//return qm.toType(allNames, new TypeReference<Map<String, String>[]>() {});
} }
@Override @Override