Skip to content

Commit 48e9c44

Browse files
committed
fixes #555 suport primitive array resultType
1 parent 384fb2d commit 48e9c44

File tree

3 files changed

+127
-7
lines changed

3 files changed

+127
-7
lines changed

src/main/java/org/apache/ibatis/binding/MapperMethod.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.ibatis.session.ResultHandler;
2828
import org.apache.ibatis.session.RowBounds;
2929
import org.apache.ibatis.session.SqlSession;
30+
import org.apache.ibatis.type.ArraysUtils;
3031

3132
import java.lang.reflect.Array;
3233
import java.lang.reflect.Method;
@@ -167,10 +168,14 @@ private <E> Object convertToDeclaredCollection(Configuration config, List<E> lis
167168
}
168169

169170
@SuppressWarnings("unchecked")
170-
private <E> E[] convertToArray(List<E> list) {
171-
E[] array = (E[]) Array.newInstance(method.getReturnType().getComponentType(), list.size());
172-
array = list.toArray(array);
173-
return array;
171+
private <E> Object convertToArray(List<E> list) {
172+
173+
Class<?> componentType = method.getReturnType().getComponentType();
174+
if (componentType.isPrimitive()) {
175+
return ArraysUtils.toPrimitiveArray(list.toArray(), componentType);
176+
}
177+
178+
return list.toArray((E[])Array.newInstance(componentType, list.size()));
174179
}
175180

176181
private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.type;
17+
18+
/**
19+
* @author wuwen.55@gmail.com
20+
*/
21+
public class ArraysUtils {
22+
23+
private ArraysUtils() {
24+
// Prevent Instantiation
25+
}
26+
27+
public static Object toPrimitiveArray(Object[] obj, Class<?> classes) {
28+
if (classes == int.class) {
29+
return toPrimitiveInt(obj);
30+
} else if (classes == boolean.class) {
31+
return toPrimitiveBoolean(obj);
32+
} else if (classes == byte.class) {
33+
return toPrimitiveByte(obj);
34+
} else if (classes == long.class) {
35+
return toPrimitiveLong(obj);
36+
} else if (classes == float.class) {
37+
return toPrimitiveFloat(obj);
38+
} else if (classes == double.class) {
39+
return toPrimitiveDouble(obj);
40+
} else if (classes == short.class) {
41+
return toPrimitiveShort(obj);
42+
} else if (classes == char.class) {
43+
return toPrimitiveChar(obj);
44+
}
45+
46+
return null;
47+
}
48+
49+
50+
public static int[] toPrimitiveInt(Object[] array) {
51+
final int[] result = new int[array.length];
52+
for (int i = 0; i < array.length; i++) {
53+
result[i] = (Integer) array[i];
54+
}
55+
return result;
56+
}
57+
58+
public static long[] toPrimitiveLong(Object[] array) {
59+
final long[] result = new long[array.length];
60+
for (int i = 0; i < array.length; i++) {
61+
result[i] = (Long) array[i];
62+
}
63+
return result;
64+
}
65+
66+
public static short[] toPrimitiveShort(Object[] array) {
67+
final short[] result = new short[array.length];
68+
for (int i = 0; i < array.length; i++) {
69+
result[i] = (Short) array[i];
70+
}
71+
return result;
72+
}
73+
74+
public static byte[] toPrimitiveByte(Object[] array) {
75+
final byte[] result = new byte[array.length];
76+
for (int i = 0; i < array.length; i++) {
77+
result[i] = (Byte) array[i];
78+
}
79+
return result;
80+
}
81+
82+
public static double[] toPrimitiveDouble(Object[] array) {
83+
final double[] result = new double[array.length];
84+
for (int i = 0; i < array.length; i++) {
85+
result[i] = (Double) array[i];
86+
}
87+
return result;
88+
}
89+
90+
public static float[] toPrimitiveFloat(Object[] array) {
91+
92+
final float[] result = new float[array.length];
93+
for (int i = 0; i < array.length; i++) {
94+
result[i] = (Float) array[i];
95+
}
96+
return result;
97+
}
98+
99+
public static boolean[] toPrimitiveBoolean(Object[] array) {
100+
101+
final boolean[] result = new boolean[array.length];
102+
for (int i = 0; i < array.length; i++) {
103+
result[i] = (Boolean) array[i];
104+
}
105+
return result;
106+
}
107+
108+
public static char[] toPrimitiveChar(Object[] array) {
109+
110+
final char[] result = new char[array.length];
111+
for (int i = 0; i < array.length; i++) {
112+
result[i] = (Character) array[i];
113+
}
114+
return result;
115+
}
116+
117+
}

src/test/java/org/apache/ibatis/submitted/array_result_type/ArrayResultTypeTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,10 @@ public void shouldGetSimpleTypeArray() {
8888
}
8989
}
9090

91-
@Test(expected = ClassCastException.class)
91+
@Test
9292
public void shouldGetPrimitiveArray() {
9393
SqlSession sqlSession = sqlSessionFactory.openSession();
9494
try {
95-
// Throwing an exception is the expected behavior
96-
// until #555 is fixed
9795
Mapper mapper = sqlSession.getMapper(Mapper.class);
9896
int[] ids = mapper.getUserIdsPrimitive();
9997
assertEquals(1, ids[0]);

0 commit comments

Comments
 (0)