Fix returning of primitive array types

This commit is contained in:
Travis Burtrum 2015-03-16 13:18:37 -04:00
parent bb40891e14
commit 79eac08efe
3 changed files with 14 additions and 3 deletions

View File

@ -120,7 +120,9 @@ public class RowToObjectMapper<T> extends RowMapper {
if (!constructor.isAccessible()) if (!constructor.isAccessible())
constructor.setAccessible(true); constructor.setAccessible(true);
} catch (Throwable e1) { } catch (Throwable e1) {
if(_columnCount > 2) // if column count is 2 or less, it might map directly to a type like a Long or something, or be a map which does // if column count is 2 or less, it might map directly to a type like a Long or something, or be a map which does
// or if componentType is non-null, then we want an array like Long[] or String[]
if(_columnCount > 2 && componentType == null)
throw new MapperException("Exception when trying to get constructor for : "+returnTypeClass.getName() + " Must have default no-arg constructor or one that takes a single ResultSet.", e1); throw new MapperException("Exception when trying to get constructor for : "+returnTypeClass.getName() + " Must have default no-arg constructor or one that takes a single ResultSet.", e1);
} }
} }

View File

@ -239,6 +239,12 @@ public class QueryMapperTest {
Assert.assertArrayEquals(expected, qm.toArray("SELECT person_no FROM person WHERE person_no = ?", Long.class, expected[0])); Assert.assertArrayEquals(expected, qm.toArray("SELECT person_no FROM person WHERE person_no = ?", Long.class, expected[0]));
} }
@Test
public void testSelectPrimitiveArray() throws Throwable {
final Long[] arr = {1L, 2L, 3L};
Assert.assertArrayEquals(arr, qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long[].class, fieldPerson1.getPersonNo()));
}
private List<Map<String, String>> getListMap() { private List<Map<String, String>> getListMap() {
final List<Map<String, String>> arrayMap = new ArrayList<Map<String, String>>(); final List<Map<String, String>> arrayMap = new ArrayList<Map<String, String>>();
for (final Person person : new Person[]{fieldPerson1, fieldBoss1, fieldBoss2}) { for (final Person person : new Person[]{fieldPerson1, fieldBoss1, fieldBoss2}) {

View File

@ -3,7 +3,6 @@ package com.moparisthebest.jdbc.dto;
import sun.misc.Unsafe; import sun.misc.Unsafe;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Objects;
/** /**
* This class is meant to test the java compiler's in-lining behavior for final variables * This class is meant to test the java compiler's in-lining behavior for final variables
@ -102,7 +101,7 @@ public class FinalDTO {
} }
private boolean directEqualsReflection(final Object object, final String fieldName) { private boolean directEqualsReflection(final Object object, final String fieldName) {
return Objects.equals(object, getField(fieldName)); return equals(object, getField(fieldName));
} }
public Object getField(final String fieldName) { public Object getField(final String fieldName) {
@ -114,6 +113,10 @@ public class FinalDTO {
return directEqualsReflection(); return directEqualsReflection();
} }
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
private static Object getField(final Object object, final String fieldName) { private static Object getField(final Object object, final String fieldName) {
try { try {
final Field field = object.getClass().getDeclaredField(fieldName); final Field field = object.getClass().getDeclaredField(fieldName);